Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause App Scripts until spreadsheet finishes calculation

Because google spreadsheets does not support iterations, I wrote my own simple app script to adjust an input based upon the calculation of the spreadsheet output. However, after I change the input variable, the spreadsheet recalculates but app scripts does not seem to wait for that recalculation so I end up retrieving values such as "Thinking..." or "#NA". Is there a way to pause a script and wait for the calculation to complete before moving to the next line in the script?

Currently, I am just using a loop to watch the cell but I wanted to find out if there was a more elegant way to pause the execution until the sheet was done calculating.

I write a lot of Excel Macros and Excel VBA always waits for the calculation to complete before moving to the next line in the code. Apps Script does not seem to do this so I am hoping there is an easy way to do this.

A second question: Because this iteration can take some time, how does one interrupt and terminate a script from running? I can't seem to find a way to do this.

like image 767
user1623107 Avatar asked Oct 03 '12 14:10

user1623107


People also ask

How do you stop sheets from calculating?

There is no way to disable automatic recalculation in Google Sheets. One option is to replace the formulas by the values either by using copy/paste as value only or by using a script. The advantage of using a script is that it also could be used to add again the formulas when needed.

How do I stop a script from running a sheet?

If you are in the code editor, and want to stop a script from running, you can click the "cancel" link in the toast display. Or you can click "View" -> "Executions" from the code editor and then terminate the script.

What is spreadsheet flush?

flush() Applies all pending Spreadsheet changes. Spreadsheet operations are sometimes bundled together to improve performance, such as when doing multiple calls to Range. getValue().


2 Answers

Here is a very simple way of preventing the next script from starting until the current script completes in google apps scripts. Just add a call for testWait() after each script you are processing successively. The SpreadsheetApp.flush() also seems to reset the timeout timer on the spreadsheet back to the default 5min so you have more time to process multiple scripts in one go.

//holds processing of next script till last one has completed
function testWait(){
  var lock = LockService.getScriptLock(); lock.waitLock(300000); 
  SpreadsheetApp.flush(); lock.releaseLock();
}
like image 194
MistyDawn Avatar answered Oct 24 '22 06:10

MistyDawn


I had the same problem. I resolved it by using 2 scripts as I needed to be sure the spreadsheet has the data I need.

The first script provides a seed value to populate the spreadsheet through a IMPORTXML function.

The second script processes this data.

I used time based triggers to run the scripts allowing for sufficient time for the first script to complete

like image 35
sharken Avatar answered Oct 24 '22 05:10

sharken