Search code examples
google-apps-scripttriggers

How to not install trigger more than once with onOpen in google script?


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?


Solution

  • 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);