通过触发器名称以编程方式删除Google Apps Script基于时间的触发器? [英] Programmatically delete a Google Apps Script time-based trigger by trigger name?
问题描述
如何通过触发函数名称以编程方式删除Google Apps Script基于时间的触发器,而不是使用triggerarray ID?
How can I programmatically delete a Google Apps Script time-based trigger by the triggered function's name, instead of triggerarray id?
我正在创建一个触发器,稍后我要按名称删除该触发器.我试过了:
I am creating a trigger and later on I want to delete the trigger by name. I tried this:
ScriptApp.deleteTrigger("myfunction");
但是它不起作用.我不想使用这种方法:
but it does not work. I don't want to use this method:
var triggers = ScriptApp.getProjectTriggers();
ScriptApp.deleteTrigger(triggers[0]);
由于我同时有多个触发器,因此可能会删除错误的触发器!那么如何通过其名称而不是触发器数组ID删除触发器?
Since I have more than one trigger at same time and might delete the wrong one! So how to delete trigger by its name instead of trigger array id?
function demo(){
ScriptApp.newTrigger("myfunction")
.timeBased()
.everyMinutes(1)
.create();
}
function myfunction(){
// do some stuff here then stop the trigger by name
ScriptApp.deleteTrigger("myfunction");
}
}
推荐答案
如果遍历所有触发器,则可以使用以下命令访问处理程序函数名称:
If you loop through all the triggers you can access the handler function name with:
var triggers = ScriptApp.getProjectTriggers();
for ( var i in triggers ) {
var funcName = triggers[i].getHandlerFunction()
}
...as shown in the docs.
这篇关于通过触发器名称以编程方式删除Google Apps Script基于时间的触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!