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

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

问题描述

我有一个电子表格来跟踪数据,每个有链接的用户都可以编辑。虽然我明白,这是一种安全风险,我不介意,因为电子表格只是我知道的公会和我感兴趣的。



更进一步确定地区受到保护,只有文件的管理员才能访问它。



我想要做的是,让所有的(匿名)GAS响应onEdit用户。



最简单的例子是:


$ b Code.gs

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

我将它附加到表格上 - >在编辑触发器上,它会显示给我 - 因为我已登录或因为我是此文档的管理员。但是:其他(匿名)用户出于某种原因不会收到消息框。我已经通读了权限表,并指出,如果您想使用任何类型的脚本(除了那些以名称执行的时间驱动的脚本,添加它们的脚本以外),您显然需要登录)。



有没有解决方法?

非常感谢您的提前!实际上有两个onEdit触发器,简单和可安装。



>简单的onEdi t作为用户访问电子表格而不询问任何授权而运行,这意味着无法执行任何需要授权的事情,并且使用SpreadsheetApp方法需要授权(正如您注意到的那样当您第一次运行该脚本时)。


$ b $

可安装触发器(onEdit,onChange等...)以脚本的作者,为了更准确,他们以安装触发器的用户身份运行。



它们(触发的函数)具有与用户自己相同的权利,并可以做用户可以做的所有事情。



匿名(不是日志ged in)使用电子表格的用户会触发这个脚本,但他们不会对脚本做什么负责,所有事情都将以触发器installer的名义完成。



请随时尝试此电子表格我编写了这个脚本:

  function onEdit(e){
SpreadsheetApp.getActiveRange()。setValue('simple onEdit触发器以未经授权的方式作为表单用户运行);
}

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

在尝试并测试该副本而不安装之后,请制作一份副本任何特殊的触发。 然后比较结果:在我的工作表中,您将从第二个函数获得消息,而在您的工作表中,将是第一个消息。



当可安装的触发器处于活动状态并编辑工作表时,两个功能都将被触发,并且您可能会连续看到两个文本不要完全一样地运行。

如果您在工作表中安装一个调用 onEditInstallable 函数的触发器,匿名用户将始终看到第二个 onEdit (从脚本编辑器转到Ressources /当前项目触发器/创建新触发器并选择你需要的)

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


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.

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

The easiest example is this:

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.

Thank you very much in advance!

解决方案

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

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

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.

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天全站免登陆