匿名编辑器不会触发 Google 电子表格 GAS 触发器 [英] Google Spreadsheet GAS Trigger not firing for anonymous editors

查看:26
本文介绍了匿名编辑器不会触发 Google 电子表格 GAS 触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子表格来跟踪数据,每个知道链接的用户都可以编辑.虽然我知道这是一个安全风险,但我不介意,因为只有我所在的公会知道电子表格并对其感兴趣.

I have a spreadsheet to track data, that every user with the link can edit. While I understand, that it is a security risk, I do not mind, because the spreadsheet is only know and of interest to the guild I am in.

更多特定区域受到保护,只有文档管理员才能访问.

Further more certain areas are protected and only administrators of the document have access to it.

我想做的是,让 GAS 响应onEdit"适用于所有(匿名)用户.

What I want to do, is, have a GAS respond to "onEdit" for all (anonymous) users.

最简单的例子是:

Code.gs

function onEdit(e) { Browser.msgBox('Editing...'); }

我把它贴在桌子上 ->在编辑触发器上,它确实为我显示 - 因为我已登录或因为我是该文档的管理员.但是:其他(匿名)用户由于某种原因没有收到消息框.我已经通读了权限表,它指出,如果您想使用任何类型的脚本,显然您必须登录(除了那些以添加它们的人的名义执行的时间驱动的脚本)).

I attached this to the table -> on edit trigger and it does display for me - as I am logged in OR because I am the administrator of this document. However: other (anonymous) users do not get the message box for some reason. I have read through the permissions sheet and it states, that apparently you HAVE to be logged in if you want to use any kind of scripts (except for those time-driven ones, that are executed under the name of the one, who added them).

没有解决方法吗?我的触发器只是将值从一个表复制到另一个表,对它们进行排序和格式化.

Is there no workaround? My triggers just copy values from one table to another, ordering and formatting them.

推荐答案

实际上有 2 个 onEdit 触发器,一个是简单"的,另一个是可安装的".

There are actually 2 onEdit triggers, the "simple" one and the "installable" one.

简单的 onEdit 在用户访问电子表格时运行而不要求任何授权,这意味着它无法执行任何需要授权的事情并使用 SpreadsheetApp 方法需要授权(正如您第一次自己运行该脚本时所​​注意到的那样).

The simple onEdit runs as the user accessing the spreadsheet without asking for any authorization, that means it is unable to execute anything that requires authorization and using the SpreadsheetApp methods requires authorization (as you noticed when you ran that script yourself for the first time).

可安装触发器(onEdit、onChange 等...)以脚本作者的身份运行,更准确地说,它们以安装的用户身份运行触发器.

The installable triggers (onEdit, onChange, etc...) run as the author of the script , to be more accurate, they run as the user who installed the triggers.

它们(被触发的函数)与用户本人拥有相同的权利",可以做用户可以做的一切.

They (the triggered functions) have the same "rights" as the user himself and can do everything the user can do.

使用电子表格的匿名(未登录)用户将触发此脚本,但他们不会对该脚本所做的事情负责,一切都将以触发器安装程序"的名义完成.

Anonymous (not logged in) users that use the spreadsheet will trigger this script but they won't be responsible for what the script does and everything will be done in the name of the trigger "installer".

随意尝试这个电子表格写了这个脚本:

Feel free to try this spreadsheet where I wrote this script :

function onEdit(e) {
  SpreadsheetApp.getActiveRange().setValue('simple onEdit Trigger running as sheet user without authorization');
}

function onEditInstallable(e) { 
  SpreadsheetApp.getActiveRange().setValue('Installable onEdit Trigger running as script author');
}

在您尝试并测试该副本后,请同时制作一份副本,而无需安装任何特殊触发器.

Please make also a copy of it after you tried and test that copy without installing any special trigger.

然后比较结果:在我的工作表中,您将收到来自第二个函数的消息,而在您的工作表中,它将是来自第一个函数的消息.

Then compare the results : in my sheet you'll get the message from the second function and in yours it will be the message from the first one.

当可安装触发器处于活动状态并且编辑工作表时,这两个功能都将被触发,并且您实际上有可能连续看到两个文本,因为它们通常不会花费完全相同的时间跑步.

When the installable trigger is active and you edit the sheet, both functions will be triggered and there is a chance that you actually see both texts successively since they generally don't take exactly the same time to run.

如果您在工作表中安装了一个触发器来调用 onEditInstallable 函数 onEdit(从脚本编辑器转到 Ressources/current project triggers/创建新触发器并选择您需要的触发器)

Anonymous user will always see the second one if you install a trigger in your sheet that calls the onEditInstallable function onEdit (from the script editor goto Ressources/current project triggers/create new trigger and select the one you need)

有关该主题的文档:简单触发器可安装的

Documentation on that subject : simple triggers and installable ones

这篇关于匿名编辑器不会触发 Google 电子表格 GAS 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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