Apps脚本制作onEdit和onOpen触发器仅适用于当前用户

时间:2019-03-05 16:37:30

标签: google-apps-script triggers

可能的答案很简单,但是我很难解决这个问题。

我有许多用户使用的电子表格。我希望自定义onEdit和onOpen函数仅对随后触发的用户运行。例如,如果人B打开电子表格,则仅应运行其自定义的onOpen触发器,而不运行其他用户的自定义的onOpen触发器。同样,如果用户编辑单元格,则只能运行其自定义的onEdit。

目前,我的设置方式是每个人都有自己的触发器,但是每次有任何用户触发时,它们都运行相同的脚本,这意味着触发器无缘无故地运行4倍,这会拖慢电子表格的运行速度,而且为其他未打开或编辑工作表的用户浪费了脚本时间。

2 个答案:

答案 0 :(得分:0)

您可以为每个用户设置单独的功能,并让代码动态检查当前用户并仅运行当前用户的功能。

有关获得当前用户的信息,请参见此处。 Link

答案 1 :(得分:0)

Assuming the functions are the same but you just aren't wanting it to run for some users, you could use PropertiesService to store a user property to identify if it should run or not. You have to make some UI method to set the property and then you would check for the property at the beginning of each of your functions. Technically the functions would run but you could return out of them if you don't want to finish the logic.