ITHelpGuy
ITHelpGuy

Reputation: 1037

Time based triggers in google apps script

I am working on pulling data from mySQL database via jdbc into a google spreadsheet everyday at 8 am. I created the following time based trigger.

function createTimeBasedTrigger() {

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

ScriptApp.newTrigger('myFunction')
.timeBased()
.everyDays(1)
.atHour(8)
.create();
}

I also tried to create the trigger in the script editor and they either execute multiple times or do not execute at all.

Upvotes: 0

Views: 1161

Answers (1)

user3717023
user3717023

Reputation:

A trigger is not a string, it's an object. It can't be equal to 'myFunction'. A correct comparison looks like

if (triggers[i].getHandlerFunction() === 'myFunction') {
  ScriptApp.deleteTrigger(triggers[i]);
}

That said, managing triggers via script editor interface is convenient, and they run as expected, provided that the function you are calling actually works.

Upvotes: 1

Related Questions