Bjorn Behrendt
Bjorn Behrendt

Reputation: 1264

Add-on Trigger not running, how can I troubleshoot

My add-on creates a trigger that runs every hour. It works when I run it from the script, but it does not seem to work when published as an add-on. I am wondering what is the best way to trouble-shoot this.

Below is the script that runs on the hour. Maybe there is something in there that might help figure out what is going on.

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()]]);
}

And here is the code that I use to create the trigger.

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

Upvotes: 2

Views: 1146

Answers (2)

Andrew
Andrew

Reputation: 113

You don't want to use project triggers (these are set at project scope) with an add on... you need to use user triggers instead, which are set at 1 per Add-on per document instance. Add-ons user triggers are limited to clock and form types, and follow a different set of rules, including reduced frequency of time triggers (max 1x per hour), and they are limited to only one trigger per type per user per doc.

Upvotes: 2

Gerardo
Gerardo

Reputation: 3845

For add-ons you need to create the trigger programmatically, here you can find the documentation and examples for doing it: https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically

Upvotes: 0

Related Questions