独立脚本可以为绑定到电子表格的另一个脚本创建触发器吗? [英] Can a standalone script create a trigger for another script which is bound to a spreadsheet?
问题描述
我正在尝试为电子表格绑定脚本创建可安装的onEdit触发器.我想使用一个单独的独立脚本以编程方式执行此操作.根据文档看来,这应该是可能的:
I am trying to create an installable onEdit trigger for a spreadsheet bound script. I would like to do this programmatically with a separate, standalone script. It looks like this should be possible according to documentation:
请注意,与简单的onOpen()触发器不同,不需要将可安装触发器的脚本绑定到电子表格.要从独立脚本创建此触发器,只需将SpreadsheetApp.getActive()替换为对SpreadsheetApp.openById(id)的调用即可. https://developers.google.com/apps-script/guides/triggers /installable#managing_triggers_programmatically
Note that, unlike for a simple onOpen() trigger, the script for the installable trigger does not need to be bound to the spreadsheet. To create this trigger from a standalone script, simply replace SpreadsheetApp.getActive() with a call to SpreadsheetApp.openById(id). https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically
但是,当我运行下面的代码时,触发器将添加到独立脚本项目中,而不是与电子表格绑定的目标脚本中.
However, when I run the code below, the trigger is added to the standalone script project instead of the target, spreadsheet-bound script.
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.openById('1vcAgQ6vPZiILFX0fB_jojyrSdGKr7goD_iCQcFsImEM');
ScriptApp.newTrigger('update')
.forSpreadsheet(ss)
.onEdit()
.create();
}
我想念什么?
推荐答案
嗯,这可能是可行的,但并非如此.我认为您对文档有些误解了.换句话说,它的意思是:创建可安装的触发器,不需要将脚本绑定到目标电子表格.但是,触发是像往常一样针对正在运行的脚本本身.没有为其他脚本安装触发器.
Well, this may be possible, but not like that. I think you misunderstood the documentation a bit. Putting in other words, what it says is: to create a installable trigger, a script does not need to be bounded to the target spreadsheet. But the trigger is for the running script itself, as always. There's no installing a trigger for another script.
脚本只能自行设置触发器,没有API可以为另一个脚本设置触发器.
Scripts can only set triggers for themselves, and there's no API to set a trigger for another script.
您可以将有界脚本发布为Web应用程序,然后远程独立脚本可以调用其URL,基本上是告诉"有界脚本是时候设置其可安装触发器了.
You could have the bounded script published as a web-app, then the remote standalone script could call its URL, basically "telling" the bounded script that it's time to set its installable trigger.
这篇关于独立脚本可以为绑定到电子表格的另一个脚本创建触发器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!