onEdit触发器不捕捉当前用户 [英] onEdit trigger doesn't catch current user

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

问题描述

我正在尝试为使用单元格笔记的表单创建简单的日志。
不知何故,我不能包含触发 onEdit-event 的用户的电子邮件。



<$ p $ 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(电子邮件+@+日期);
}

单元格出现在单元格上,但电子邮件为空。这可能与丢失的权限有关吗?我假设,如果我的代码中出现错误,注释将不会出现在编辑过的单元格中...

解决方案

以下是Google文档中关于Session类的内容


$ b


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

关于'getActiveUser()'方法,它指出


如果安全策略不允许访问
用户的身份,User.getEmail()将返回一个空白字符串。电子邮件地址可用的
情况会有所不同:例如,对于
,用户的电子邮件地址在
允许脚本在没有用户授权的情况下运行的任何上下文中不可用,例如
简单的onOpen(e)或onEdit(e)触发器,Google
Sheets中的自定义函数,或部署为作为我执行的web应用程序(即开发人员授权
的用户)。但是,如果开发人员和用户属于
相同的G Suite域名,则这些限制
一般不适用。


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



您可以将项目发布为工作表加载项。在这种情况下,用户将被要求授予在附加清单中声明的​​权限。



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


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...

解决方案

Here's what Google documentation says about the Session class

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

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

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.

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.

More info https://developers.google.com/apps-script/reference/base/session

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

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