gpt4 book ai didi

google-apps-script - onEdit 触发器无法捕获当前用户

转载 作者:行者123 更新时间:2023-12-02 20:31:27 30 4
gpt4 key购买 nike

我正在尝试为使用单元格注释的工作表构建一个简单的日志。不知何故,我无法包含触发 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);
}

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

最佳答案

这是 Google 文档中关于 Session 类的说明

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

关于“getActiveUser()”方法,它指出

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() - 除非用户明确授权,否则代码不会执行。对于可安装的触发器,这意味着从脚本编辑器手动执行该函数。

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

更多信息https://developers.google.com/apps-script/reference/base/session

关于google-apps-script - onEdit 触发器无法捕获当前用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610745/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com