以编程方式删除定时触发器

时间:2013-03-12 17:34:24

标签: google-apps-script

我有一个测试脚本,我正在尝试测试定时触发器。我需要能够以编程方式创建和删除触发器 - 根据文档,这是可能的。但是,当我需要时,我的触发器不会删除:

function myFunction() {
  var db = ScriptDb.getMyDb();
  var user = Session.getActiveUser().getEmail();
  var triggerID = ScriptApp.newTrigger("timed").timeBased().everyMinutes(1).create().getUniqueId();

  var ob = {
    id: user,
    count: 1,
    trigger: triggerID
  }

  db.save(ob);
}

function timed(){
  var db = ScriptDb.getMyDb();
  var user = Session.getActiveUser().getEmail();
  var ob = db.query({id: user}).next();
  var count = ob.count;
  var triggerID = ob.trigger;

  if (count > 4){
    deleteTrigger(triggerID);
    db.remove(ob);

  } else {
    Logger.log(count + " " + Utilities.formatDate(new Date(), "GMT", "HH:mm:ss") + " TriggerID=" + triggerID);
    ob.count = count+1;
    db.save(ob);
  }
}

function deleteTrigger(triggerID){
  var allTriggers = ScriptApp.getProjectTriggers();

  for(var i=0; i < allTriggers.length; i++){
    if(allTriggers[i].getUniqueId() = triggerID){
      ScriptApp.deleteTrigger(allTriggers[i]);
      break;
    }
  }
}

1 个答案:

答案 0 :(得分:5)

要测试相等性,您只需在以下行中==代替=

if (allTriggers[i].getUniqueId() = triggerID) {