mcansado
mcansado

Reputation: 2164

How to programatically find and distinguish triggers in Apps Script?

I have a function that updates a spreadsheet. It runs every morning and, every time it does, it creates 4 triggers. I also have time-driven triggers, which I want to keep there.

Since there is a limit on the triggers, I need to remove the triggers created by the function (while maintaining the other ones). However, when I run this:

function Triggers () {
  Logger.log(ScriptApp.getProjectTriggers())
}

I get this as a response:

 [Trigger, Trigger, Trigger, Trigger, Trigger, Trigger, Trigger, Trigger]

How can I identify, of these, the ones created by that function so I can eliminate those only?

Upvotes: 1

Views: 1657

Answers (2)

Synergy Reporting
Synergy Reporting

Reputation: 1

Our solution has been the following:

  1. Run first this code:
var triggers = ScriptApp.getProjectTriggers();

for (i in triggers){

    Logger.log(triggers[i].getUniqueId());

}

Then, identify the unique ID(s) from the trigger you want to preserve (usually a positive number). Say the ID = 123456789

  1. Now, add this code:
var triggers = ScriptApp.getProjectTriggers();  

for (i in triggers){

    if (triggers[i].getUniqueId() != 123456789) {

        ScriptApp.deleteTrigger(triggers[i])

    }

}

Upvotes: 0

mcansado
mcansado

Reputation: 2164

I used this code:

function Triggers () {
  var triggers = ScriptApp.getProjectTriggers()
  for (i in triggers)
    if ((triggers[i].getHandlerFunction()) == "createStats") {
      ScriptApp.deleteTrigger(triggers[i])
    }
}

Upvotes: 2

Related Questions