onEdit 触发器无法捕获当前用户 [英] onEdit trigger doesn't catch current user

查看:21
本文介绍了onEdit 触发器无法捕获当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为使用单元格注释的工作表构建一个简单的日志.不知何故,我无法包含触发 onEdit-event 的用户的电子邮件.

I'm trying to build a simple log for sheets that makes use of cell notes. Somehow i can't include the email of the user who triggers the onEdit-event.

function onEdit(e){
    var email = Session.getActiveUser().getEmail();
    var date = String(Utilities.formatDate(new Date(), 'Europe/Berlin', 'HH:mm dd/MM/yy'));
    var range = e.range;
    range.setNote(email + " @ " + date);
}

笔记出现在单元格上,但电子邮件是空的.这可能与缺少权限有关吗?我假设如果我的代码有问题,注释根本不会出现在工作表中编辑过的单元格上...

The note appears on the cell but the email is empty. Could that have to do with missing permissions? I assume if something would be wrong with my code the note wouldn't appear at all on the edited cell in sheets...

推荐答案

Google 文档对 Session 类的说明

Here's what Google documentation says about the Session class

Session 类提供对会话信息的访问,例如用户的电子邮件地址(在某些情况下)和语言设置.

The Session class provides access to session information, such as the user's email address (in some circumstances) and language setting.

关于getActiveUser()"方法,它声明

In regards to 'getActiveUser()' method, it states

如果安全策略不允许访问用户的身份,User.getEmail() 返回一个空字符串.这电子邮件地址可用的情况各不相同:对于例如,用户的电子邮件地址在任何上下文中都不可用允许脚本在没有该用户授权的情况下运行,例如简单的 onOpen(e) 或 onEdit(e) 触发器,Google 中的自定义函数Sheets 或部署为像我一样执行"(即授权由开发者而不是用户).但是,这些限制如果开发者和用户属于相同的 G Suite 域.

If security policies do not allow access to the user's identity, User.getEmail() returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). However, these restrictions generally do not apply if the developer and the user belong to the same G Suite domain.

因为您使用简单的触发器 - onEdit() - 除非用户明确授权,否则代码不会执行.对于可安装的触发器,这意味着从脚本编辑器手动执行函数.

Because you use the simple trigger - onEdit() - the code will not execute unless explicitly authorized by the user. For installable triggers, that would mean executing the function manually from the script editor.

您可以将项目发布为工作表插件.在这种情况下,系统会要求用户授予附加组件清单中声明的​​权限.

You can publish the project as a sheets add-on. In this case, users will be asked to grant permissions declared in the add-on manifest.

更多信息 https://developers.google.com/apps-script/参考/基础/会话

这篇关于onEdit 触发器无法捕获当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆