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");
}
}
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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With