How to not install trigger more than once with onO

2019-08-26 02:14发布

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条回答
太酷不给撩
2楼-- · 2019-08-26 02:28

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);
查看更多
登录 后发表回答