Dimo Dzhurenov
Dimo Dzhurenov

Reputation: 61

App script time trigger not executing my script at all

I have two functions that are responsible for starting and stopping the execution of my script from UI.

function runScript() {
  stopScript();
  ScriptApp.newTrigger("start").timeBased().everyHours(1).create();
}

and

function stopScript() {
  let triggers = ScriptApp.getProjectTriggers();

  for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}

They both are triggered by the UI for which I'm using this piece of code:

function onOpen(e) {
  let sheetUi = SpreadsheetApp.getUi();

  sheetUi.createMenu('Menu')
  .addItem('Run', 'runScript')
  .addItem('Stop', 'stopScript')
  .addToUi();
}

They work as expected: stopScript() remove the old time trigger and runScript() create a new one.

Once I run the function runScript() a new time trigger is created and I can see it in my dashboard. However, the time trigger never executes the script. For example, for the past 2 days, the time trigger which was set to execute the script every hour didn't execute it even once. I don't understand why this is happening.

The script can be used from any Google Sheet. I tried any solution I found on the internet and here but it keeps not running. I'm out of options.

Am I missing something?

Upvotes: 1

Views: 2632

Answers (1)

Dimo Dzhurenov
Dimo Dzhurenov

Reputation: 61

After 2 weeks in testing and reaching even to G Suite Support (who didn't manage to help either), I found the problem.

Every part of my script is correct and it's working. The problem comes when you need to test a time-trigger or even an installable trigger in general for your add-on (Test as add-on option from the script editor) - it's impossible and it will never work because that's the way Google created it. From Test an editor add-on:

Installable triggers aren't supported when testing. Functionality that depends on installable triggers is not testable.

So how you should test your time-triggers then?

In my case when I was testing the script through the editor the time-trigger was created and it was running periodically (every 1h). And that's the answer. If your time-trigger (or installable trigger in general) is running that way it means that it's working and it will work on a production as well.

Upvotes: 5

Related Questions