How to delete triggers that own by other users in Google Apps Script?

允我心安 提交于 2020-06-17 09:33:21

问题


I have an installable trigger that should supposed to created once. But I found out that some of my coworkers have rerun my code and recreated another 3 triggers that exactly the same with it. I felt so silly that I didn't add any conditional statement of that...

Anyway, now I want to delete these extra triggers. But I cannot find the delete/edit button on the console UI of the GAS project. And I've also tried ScriptApp.getProjectTriggers(), but it still list the triggers of the project that only owned by me.

How can I delete these extra triggers that created by others (owned by other users)? Or can I restart my project from a clean start again?


回答1:


In addition to @Alan Wells comment - no you definitely cannot. What you can do, though, is make your users run a remover function on their behalf with something like this (if your script is container-bound, add it to onOpen() trigger if you have one, or to any other function that you expect to be called by others):

function deleteAllTriggers() {

  var ts = ScriptApp.getProjectTriggers();

  ts.forEach(function(trigger){

    var handlerName = trigger.getHandlerFunction();

    if(handlerName === 'yourFunctionName') { //check if you are deleting target trigger;
      ScriptApp.deleteTrigger(trigger);
      Utilities.sleep(1000); //wait (in this sample 1s) to avoid "too many times" error;
    }

  });

}

ES6-style:

const deleteAllTriggers = () => {
    const triggers = ScriptApp.getProjectTriggers();
    triggers.forEach((trigger) => ScriptApp.deleteTrigger(trigger));
};


来源:https://stackoverflow.com/questions/56507537/how-to-delete-triggers-that-own-by-other-users-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!