Reputation: 665
I have disabled time.based triggers that are created from within a function. These tend to accumulate, so I created this script to remove the triggers, which seems to work.
Ideally though I only want to remove the triggers when the status = disabled. How can I get the status of a trigger? Is this possible?
function deleteTriggers() {
var doc = SpreadsheetApp.openById('id');
var triggers = ScriptApp.getUserTriggers(doc);
// Log the handler function for the first trigger in the array.
Logger.log("triggers length" + triggers.length)
for (var i = 0; i < triggers.length; i++) {
if(triggers[i].getHandlerFunction() == "name of function") {
ScriptApp.deleteTrigger(triggers[i]);
Logger.log("trigger deleted" + triggers[i].getHandlerFunction());
} // if
} // loop
Upvotes: 1
Views: 894
Reputation: 26836
UPDATE:
There is the method isDisabled()
that is not documented in th eApps Script documentation, however works corrects to assess eithe a trigger has been disabled.
if(triggers[i].getEventType() == "CLOCK")
or
if(triggers[i].getTriggerSource() == "CLOCK")
.
If you want to take a step further, with the Apps Script API you can inderectly retrieve the execution time of a function through Processes.
The Resource: Process
gives you information about executed functions including their name and Process type.
Process type TIME_DRIVEN
specifies that the function has been run by a time-based trigger. You can retrieve the corresponding Process
startTime
.
So, if the functionName
of a function executed in the past on time-driven trigger corresponds to triggers[i].getHandlerFunction()
and triggers[i].getEventType() == "CLOCK"
, you can assume that the corresponding
trigger has already been fired in the past and thus, is disabled now.
This is certainly a convoluted workaround and might not work if you have different time-driven triggers bound to the same handler function.
Upvotes: 0