V
V
Vladimir Zuev2020-06-09 07:57:59
Google Apps Script
Vladimir Zuev, 2020-06-09 07:57:59

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

2 answer(s)
G
Grigory Boev, 2020-06-09
@vladd56

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?

The function starts by itself, you don't need to keep the table open.

A
Alexander Ivanov, 2020-06-09
@oshliaer

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
runOncedoes - 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.
This trigger is executed with a high degree of accuracy, often less than half a second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question