How to not install trigger more than once with onO

2019-08-26 02:32发布

问题:

I don't really understand how to prevent adding the same trigger twice. Except storing data in properties and comparing each time are there any other ways?

回答1:

You can get project triggers with getProjectTriggers, then loop them and check for needed one.

For example if you want to check if onEdit trigger already set:

var allTriggers = ScriptApp.getProjectTriggers();
var editTriggerSet = false;
for (var i = 0; i < allTriggers.length; i++)
{
  if (allTriggers[i].getEventType() == ScriptApp.EventType.ON_EDIT)
  {
    editTriggerSet = true;
    break;
  }
}

// log the result
Logger.log(editTriggerSet);