Answer the question
In order to leave comments, you need to log in
How to create a trigger to run a program at a scheduled time?
How to create a trigger to run a program at a scheduled time?
I want to receive information about invoices from Nova Poshta, let's say at 21.00 daily. Can I write a trigger so that it runs the invoice checking program even when my table is not loaded? Or is this not realistic?
Answer the question
In order to leave comments, you need to log in
1) Create a per-minute trigger.
2) Inside the function, do a time check.
const time = new Date();
const hours = time.getHours();
const minutes = time.getMinutes();
if ((hours!=21)&&(minutes!=0)){return;};
Can I write a trigger so that it runs the invoice checking program even when my table is not loaded?
In order not to lose trigger time, and it is very limited for regular accounts, it is best to use a self-reproducing trigger
/**
*
*/
function runOnce() {
trigger_();
}
/**
*
*/
function trigger_() {
try {
triggerAction();
} catch (error) {
console.error(error.message, error);
} finally {
var hours = 10;
var minutes = 17;
var seconds = 56;
var now = new Date();
var nextTime = new Date();
nextTime.setHours(0, 0, 24 * 3600 + hours * 3600 + minutes * 60 + seconds);
var delta = nextTime.getTime() - now.getTime();
ScriptApp.newTrigger('trigger_')
.timeBased()
.after(delta)
.create();
}
}
/**
*
*/
function triggerAction() {
console.log("I'm fine");
}
triggerAction
- this is what your script runOnce
does - this is what you should run once the first time you run your trigger. No other settings are required trigger_
- it's both the trigger and your trigger configuration. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question