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.