问题
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]);
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");
}
}
回答1:
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.
回答2:
@code-guy
// Deletes all triggers in the current project.
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
From Docs
回答3:
Go to Edit > Current Project Triggers. This is where all your triggers are stored and managed. The handlerFunction reflects the 'Run' name in the 'Current Project's Triggers'. Google kind of mixes these around in my experience so you have to cycle through, find the trigger by the handlerFunction, and then delete. If you have multiple triggers with the same 'Run' name you may have to break the loop and get more creative. But the following works for me.
function Delete() {
var Triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < Triggers.length; i++) {
if (Triggers[i].getHandlerFunction() == "'Run' Name") {
ScriptApp.deleteTrigger(Triggers[i])
}
}
}
回答4:
Loop through the triggers searching for any that match on the handler function, then delete them.
Although the OP only mentioned one trigger, it's possible for multiple triggers to target the same handler function, so the following snippet caters for single and multiple matches.
function myfunction(){
// do some stuff here then stop the trigger(s) by name
var triggers = getProjectTriggersByName('myfunction');
for (var i = 0; i < triggers.length; ++i)
ScriptApp.deleteTrigger(triggers[i]);
}
function getProjectTriggersByName(name) {
return ScriptApp.getProjectTriggers().filter(
function(s) {return s.getHandlerFunction() === name;}
);
}
See documentation on ScriptApp and Trigger.
Based on Bryan P's answer, but with code.
回答5:
getHandlerFunction() returns function fully as a text(name, brackets, arguments, body).
Common solution: use indexOf.
For example,
- loop through triggers
- if triggers.getHandlerFunction().indexOf(name) != -1
- delete trigger
But be carefull, because in this case you can get another trigger by text in body. To avoid this problem, you can:
- add brackets, parameters or some unique chars to indexOf - .indexOf(name + "(){") and format your code.
- or/and create new function with unique name, like a triggerMyFunction, which will be call myFunction. Then you can delete trigger by name "triggerMyFunction"
Samples
this function delete by name ALL matched triggers
function deleteTriggersByName(name){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++){
if (triggers[i].getHandlerFunction().indexOf(name) != -1)
{
ScriptApp.deleteTrigger(triggers[i]);
}
}}
this function delete by name first matched trigger
function deleteTriggerByName(name){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++){
if (triggers[i].getHandlerFunction().indexOf(name) != -1)
{
ScriptApp.deleteTrigger(triggers[i]);
break;
}
}}
回答6:
You can delete it precisely when the trigger is triggered based on the unique id,code like:
function trigger4SendMediaGroup(event) {
try {
var triggerUid = event.triggerUid;
var triggers = ScriptApp.getProjectTriggers();
if(triggers && triggers.length > 0){
triggers.forEach(item=>{
if(item.getUniqueId() == triggerUid){
ScriptApp.deleteTrigger(item);
}
})
}
//other process
来源:https://stackoverflow.com/questions/33188514/programmatically-delete-a-google-apps-script-time-based-trigger-by-trigger-name