1

The Story

I'm working on publishing my first Spreadsheet add-on. It's a communication tool for teachers, and I need the script to add a time-based trigger on install. I've been working on this for the past day or so, and I think I'm close, but my time-based trigger / function still isn't working for testers.

Setting Up an Installation Logic that Creates the Trigger

Here's my logic so far:
onInstall(e) calls onOpen(e) in onOpen(e), a few lines of code call a function that checks to see whether or not a trigger exists (using the ScriptApp.getProjectTriggers(); method) that calls the desired function if one doesn't yet exist, it calls another function to create the trigger The code looks like this, basically:

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Standard Install Logic ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function onInstall(e) {
   onOpen(e);
}

function onOpen(e) {
  // sets up the UI in case it's not in the right auth mode
  var menu = SpreadsheetApp.getUi().createAddonMenu();
  if (e && e.authMode !== ScriptApp.AuthMode.FULL) {
    menu.addItem("Set Up the Addon", "initialSetup") // Add a menu item that works in all auth modes to get to full authorization.
    .addToUi();

  } else {
    menu
    .addItem("Menu Stuff", "someFunction")
    .addToUi();

    var checkTrigger = triggerCheck(); // ensure the the trigger is installed
    if (checkTrigger != true) {
      createHoneybeeTrigger();
    }
  }
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Functions to Help Set Up the Trigger if It's Not There ////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

// function that's callable from the UI if the user isn't yet in auth mode FULL
function initialSetup () {
  triggerCheck();
  onOpen();
  SpreadsheetApp.getUi().alert('Everything looks good! You can now use the addon.')
}

// function to see if a trigger yet exists that calls the desired function
function triggerCheck () {

  var triggers = ScriptApp.getProjectTriggers(); // get current project's triggers
  var triggerFunctions = [];
  triggers.forEach(function (trigger) {
    var handlerFunction = trigger.getHandlerFunction();
    triggerFunctions.push(handlerFunction); // push the functions called by triggers into array
  });

  // check the array to see if one of the current trigger functions is myFunction
  var correctTrigger = function(functionName) { // sets up the filter
    return functionName == 'myFunction';
  }
  var triggerPresent = triggerFunctions.some(correctTrigger);

  return triggerPresent;
}

// function to create the trigger if it's not yet there
function createTrigger () {
  ScriptApp.newTrigger('myFunction')
  .forSpreadsheet(SpreadsheetApp.getActive())
  .timeBased()
  .everyHours(12)
  .create();
  return true;
}

Weird Verification Results

I've also set up a little function, callable from the user's UI, that uses the triggerCheck () function (listed above) to check whether or not the trigger is installed and then alert them regarding the result. Something weird is happening though. In my test account (which is in auth mode FULL), I can run this menu item, check to see if the trigger is there, and see that it is. However, the trigger still won't fire. Why / how could this be happening?

This stuff is really hard to test, because I can't get much visibility into the triggers that are active for the add-on when installed for a test account. Any tips there?


A similar, but not exact question on SO is here, but my question is regarding installable Google Add-ons, so the use case is different.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Davis Jones
  • 1,504
  • 3
  • 17
  • 25
  • Stack Snippet should be used only for executable HMTL/CSS/JavaScript (Google Apps Script isn't executable here) – Rubén Jul 24 '19 at 21:41

1 Answers1

2

Long story super-short, you have to change your add-on logic.

Why?

Simple triggers like onOpen, when they are triggered by an event, in this case opening a spreadsheet run in LIMITED authorization mode, meaning that certain actions like getting the project triggers are not possible.

Tips

  • Keep onOpen and other simple triggers to do "simple" things. Start by limiting onOpen to create the add-on custom menu.
  • Use an on open installable trigger to do the add-on (script) / spreadsheet / user setup/initialization verification
  • Checkout https://developers.google.com/apps-script/support/troubleshooting to learn about the basic troubleshooting techniques for Google Apps Script projects. In this specific case, the Execution Transcript and the error logging on https://scripts.google.com/executions could help to figured where is occurring an error and what error is it.

Remarks

Yes, add-ons are hard to test, but you aren't alone. Here we have a lot of questions that could help you on your journey, like How can I test a trigger function in GAS?

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166