Warm tip: This article is reproduced from serverfault.com, please click

How to solve execution time issue of AppScripts for non-G suite users?

发布于 2020-12-06 11:04:07

I have an attendance system which is working fine for G suite users (execution time is 30 minutes) but I want to make it running for all gmail users (execution time 6 minutes). When a teacher presses a specific menu at my Google Sheets page, it allows students to report their attendance from an Android App to Google Sheet for 10 minutes.

The whole code is working fine for G Suite Users but I want to make it for all Gmail users. I tried to execute it from a regular Gmail but it shows Timed Out at the end.

I have the following two lines of code for students to have 10 minutes to respond:

function refreshSheet(){

  //Few other lines of codes are here

  var val = dashboard.getRange("C6").getValue(); //C6 is a dropdown list in Sheets of "Start 1-Period" and "Start 2-Period"
  if (val == "Start 1-Period"){ ScriptApp.newTrigger("onePeriod").timeBased().after(10*60*1000).create();}
  if (val == "Start 2-Period"){ ScriptApp.newTrigger("twoPeriod").timeBased().after(10*60*1000).create();}
  
  //These few lines of codes below within the refreshSheet() should be executed after the above called trigger and its function execution.

  for (var i=0; i<students.length; i++) {
      if (students[i][0] !== '') ss.removeEditor(students[i][0].toString());
  } 
  
  protectionm.remove();  
  SpreadsheetApp.flush();
}

function onePeriod(){}

function twoPeriod(){}

I reviewed few posts here but none of them is working for me. How can I solve it?

Questioner
Tamjid Taha
Viewed
22
ziganotschka 2020-12-07 17:34:17

There are two things you can do:

  1. If you deploy your script as a WebApp, you can execute it as "You" - that is the user from a GSuite domain who has an execution limit of 30 min.

  2. Design your script in a different way.

  • Instead of waiting 10 minutes with Utilities.sleep(), incorporate a time-driven trigger
  • You can do so with the trigger builder
  • This means - end the current function and specify that the continuation of the script (embedded in the second function) will start running 10 minutes later
  • The time inbetween will not count as execution time and will not lead to a timeout.

Sample:

function myFirstFunction(){
// do something
//now set-up the trigger:
    ScriptApp.newTrigger("mySecondFunction").timeBased().after(10*60*1000).create();
}

// will be called after the amount of seconds specified in `after()`
function mySecondFunction(){
//now implement the rest of your code
}