T340B
T340B

Reputation: 55

Exceeded maximum execution time google sheets script work around

I have a script that runs a function for a set of data that is drawn from a master sheet. Each time the function runs, it draws and processes a data set based on a date designated in a cell in a sheet (cell A3). The date in this cell is updated via a 'for' loop for a date range that I specified. the script can execute 5 complete runs (aka each run execute the function for a day worth of data) of these loops before I run into "Exceeded maximum execution time" some time during the 6th run. All of my data are already saved to the spreadsheet in a continual updating fashion after every run so what I have to do after five cycles is to manually restart the run for another 5 days starting at the 6th day by modifying my script. I am reading about time driven trigger where someone is pausing the script every 5 minutes to get around the 6 minutes execution time limit but it does not suit my needs because I would like a break after every 5 cycles, (not based on time). What I need is to write a script where after running 5 cycles, the script can break and then resume running again for another 5 cycles. Here is what my code looks like:

function runMultipleDates() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var start = new Date("2018-11-05");
  var end = new Date("2018-11-09"); 
  var step = 1;

  for (var date1 = start; date1 <= end; date1.setDate(date1.getDate() + step)) {
    var date2 = new Date(date1.getTime());
    date2.setDate(date2.getDate() + 1);
    ss.getSheetByName('Time Range').getRange("A3").setValue(date2);
    runEverything();

  };
}

As you can see, right now I am adjusting the start and end date manually for 5 days, let it run and then restart the process again by adjusting my script start date to be 2018-11-09 and end date to be 2018-11-13 for the next run. This function, on its own, is to able to run for far more than 5 days cycles without the execution time restriction. So the question is whether there is away to pause the script and resume running after 5 cycles of the above script. Any help is greatly appreciated.

Upvotes: 0

Views: 3612

Answers (1)

T340B
T340B

Reputation: 55

Thanks for the ideas sent my way. I have found an answer for this problem with the following codes adapted slightly from benlcollins on gitHub. As @Cooper suggested, I ran my function every 5.5 minutes which was just a little more than what it took for me to complete the main function runMultipleDates() (this function processes 5 days at a time on its own) ; I am repeating this 20 times which means it will cover 20x5=100 days which is good enough for me. I am posting this in case it helps someone.

// -----------------------------------------------------------------------------
// add menu
// -----------------------------------------------------------------------------
function onOpen() { 
  var ui = SpreadsheetApp.getUi();
  ui.createMenu("Auto Trigger")
    .addItem("Run","runAuto")
    .addToUi();
}
function runAuto() {  
  // resets the loop counter if it's not 0
  refreshUserProps(); 
  // create trigger to run program automatically
  createTrigger();
}

function refreshUserProps() {
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('loopCounter', 0);
}

function createTrigger() {
    ScriptApp.newTrigger('runGlobal')
      .timeBased()
      .everyMinutes(5.5)
      .create();
}

// -----------------------------------------------------------------------------
// function to delete triggers
// -----------------------------------------------------------------------------
function deleteTrigger() {
  
  // Loop over all triggers and delete them
  var allTriggers = ScriptApp.getProjectTriggers();
  
  for (var i = 0; i < allTriggers.length; i++) {
    ScriptApp.deleteTrigger(allTriggers[i]);
  }
}

// -----------------------------------------------------------------------------
// function to run called by trigger once per each iteration of loop
// -----------------------------------------------------------------------------
function runGlobal() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Data');  
  // get the current loop counter
  var userProperties = PropertiesService.getUserProperties();
  var loopCounter = Number(userProperties.getProperty('loopCounter'));  
  var limit = 20; // create trigger to run program automatically  
  // if loop counter < limit number, run the repeatable action
  if (loopCounter < limit) {       
    // do stuff
    runMultipleDates();   
    // increment the properties service counter for the loop
    loopCounter +=1;
    userProperties.setProperty('loopCounter', loopCounter);    
    // see what the counter value is at the end of the loop
    Logger.log(loopCounter);
  }
  // if the loop counter is no longer smaller than the limit number
  else {
    Browser.msgBox("Run Completed");    // Log message to confirm loop is finished
    deleteTrigger(); 
  }  
}

Upvotes: 0

Related Questions