2

I have a google script with 4 different functions that need to run one after another, but a function can run just after the previous one has finished/completed.

The time that takes each function depends, but on average each function takes about 15-20 minutes.

Each function completes an spreadsheet with a lot of data, so I want to run the first function, wait until the first one ends, run the second one, etc. inside a trigger to load the information every (for example) 4 hours.

Thanks!

Patricia Trejo
  • 21
  • 1
  • 1
  • 2
  • 2
    Since the runtime of a google script is capped at 6 min, I'm confused that you can run a 15-20 min function. – Liora Haydont Apr 26 '18 at 20:11
  • You can use an installable trigger based on time, scheduled after 15 or 20 minutes: https://developers.google.com/apps-script/guides/triggers/installable – Michele Pisani Apr 26 '18 at 22:38
  • 1
    @LioraHaydont members of the Google Early Adopters Program have a 30 minute Apps Script execution limit. https://developers.google.com/apps-script/guides/apps-script-eap – Cameron Roberts Apr 27 '18 at 00:03

1 Answers1

5

In the general case, to run function A after function B, one writes a controller routine that first calls one, then calls the other:

function a() { /* stuff */ }
function b() { /* other stuff */ }
function doStuff() {
  a();
  b();
}

For your case, where you have an execution time limit to deal with that would otherwise kill they execution of one of your functions and prevent the rest from running, you need to write a scheduling routine that makes use of Apps Scripts ScriptApp class. You have a number of ways to do this, from scheduling the first with a set interval trigger and using each function to tell Google when to run the next function, rather than to run the next function, or using a script property to indicate the next function which be run, to manually running one and having the last one set up a call for the first.

Example of chaining:

function a() {
  ...
  // Google will run b() within +/-15 minutes of the indicated time
  // Schedule it for 16 minutes from now, so we know a() has
  // completed and shut down before b runs.
  var next = ScriptApp.newTrigger("b").timeBased();
  next.after(16 * 60 * 1000).create();
}
function b(e) {
  /* Add code to delete the one-time trigger used to call this function
  (The trigger uid is in the event object e) */
  ...

Property method:

function doStuff() {
  var props = PropertiesService().getScriptProperties();
  var next;
  switch (props.getProperty("runNext")) {
    case "a":
      a();
      next = "b";
      break;
    ...
  }
  props.setProperty("runNext", next);
}
function a() {...}
...

If you implement a solution that chains functions (vs. one that uses a property to indicate which to run), you will want to make sure you delete previous triggers to prevent the trigger count from growing endlessly.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • Hey @tehhowch can you help me with this, function updateData() { Logger.log(10) let trig_ID = ScriptApp.newTrigger("updateData1").timeBased().after(60000).create().getUniqueId() delete_trigger() } function updateData1() { Logger.log(20) } How to make second function run? Would prefer some clarity. – Kaustubh Dec 14 '21 at 11:38