附加组件触发器未运行,如何进行故障排除

时间:2015-01-23 03:15:05

标签: google-apps-script google-sheets

我的附加组件创建了一个每小时运行一次的触发器。当我从脚本运行它时它可以工作,但是当它作为附件发布时它似乎不起作用。我想知道解决问题的最佳方法是什么。

下面是一小时运行的脚本。也许那里有一些东西可能有助于弄清楚发生了什么。

function updateDay(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets(); 

  for (i in sheets){
    var sheetName = sheets[i].getName();
    var cella2 = sheets[i].getRange(1, 2).getValue();
    if (sheetName == cella2){

      //check if weekend
      var cellA3 =  new Date(sheets[i].getRange(3, 1).getValue());
      if (cellA3 == "-"){
        sheets[i].deleteRow(3);
        SpreadsheetApp.flush();
        cellA3 =  new Date(sheets[i].getRange(3, 1).getValue());
      }

      var cellA3F = Utilities.formatDate(cellA3, "EST", "MM/dd/yyyy");
      var today = new Date();
      var todayF = Utilities.formatDate(today, "EST", "MM/dd/yyyy");

      if (cellA3F < todayF){

        //write to Log
        var sendToLog = [];
        var logSheet = ss.getSheetByName("Log");
        var logDate = sheets[i].getRange("A3").getValue();
        var sheetLastColumn = sheets[i].getLastColumn();

        sendToLog.push(logDate);
        sendToLog.push(sheetName);
        for (var j=2; j<sheetLastColumn+1; j++){
          var colHeading = sheets[i].getRange(2, j).getValue();
          var colName = sheets[i].getRange(3, j).getValue();
          sendToLog.push(colHeading+":"+colName);
        }

//        ["12/16/14","Lab1","Per1:bj","Per2:Sara"];
        logSheet.appendRow(sendToLog);

        //add new day
        var lastDay =  new Date(sheets[i].getRange(sheets[i].getLastRow(), 1).getValue());
        var dayOfWeek = Utilities.formatDate(lastDay, "EST", "EEE");
        if (dayOfWeek == 'Fri'){
          var nextMon = new Date(lastDay);// This needs to be two lines for some reason.
          nextMon.setDate(lastDay.getDate()+3);
          sheets[i].appendRow(["-"]);
          sheets[i].appendRow([nextMon]);
        } else{
          var nextDay = new Date(lastDay);// This needs to be two lines for some reason.
          nextDay.setDate(lastDay.getDate()+1);
          sheets[i].appendRow([nextDay]);
        }
        sheets[i].getRange("a:a").setNumberFormat('DDD, MM/dd');

        //remove tody
        var dayOfWeek2 = Utilities.formatDate(cellA3, "EST", "EEE");
        if (dayOfWeek2 == 'Fri'){
          sheets[i].deleteRow(4);
        }
        sheets[i].deleteRow(3); 
      }
    }
  }
  // for testing
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Log").getRange(1,4,1,2).setValues([["Last Update", new Date()]]);
}

这是我用来创建触发器的代码。

  //  Configure trigger
  var projectTriggers = ScriptApp.getProjectTriggers();
  if (projectTriggers.length == 0){
    ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create();
  }

2 个答案:

答案 0 :(得分:1)

您不希望使用项目触发器(这些是在项目范围内设置)并添加...您需要使用用户触发器,每个文档实例的每个附加组件设置为1。附加组件用户触发器仅限于时钟和表单类型,并遵循一组不同的规则,包括减少时间触发器的频率(每小时最多1x),并且每个用户每个文档仅限于每种类型一个触发器。 / p>

答案 1 :(得分:0)

对于加载项,您需要以编程方式创建触发器,在这里您可以找到文档和示例:https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically