Researching how to make the script not continue until IMPORTXML has finished collecting data, I found this function as one of the most used indications:
function testWait(){
var lock = LockService.getScriptLock(); lock.waitLock(300000);
SpreadsheetApp.flush(); lock.releaseLock();
}
Original source:
https://stackoverflow.com/a/43444080/11462274
I would like to know exactly where I should add it and how many times it is necessary, if I should add it below each line of script that calls the IMPORTXML function in the spreadsheet or if I should use it only once at the beginning or at the end of the script.
If possible, please show me an example of what my script would look like after the modifications.
My Script example (it is much bigger, but for the question not to be huge, I will publish only a part):
function myFunction() {
var ss = SpreadsheetApp.getActive();
ss.getRange('Monster!A1').setFormula('=IMPORTXML(Gerais!R1,"//*[@class=\'header-label\']"');
ss.getRange('Monster!A1:A').copyTo(ss.getRange('Page 2!A1:A'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.getRange('Monster!A1').clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange('Monster!B1').setFormula('=IMPORTXML(Gerais!R2,"//*[@class=\'header-label\']"');
ss.getRange('Monster!B1:B').copyTo(ss.getRange('Page 2!B1:B'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.getRange('Monster!B1').clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange('Monster!C1').setFormula('=IMPORTXML(Gerais!R3,"//*[@class=\'header-label\']"');
ss.getRange('Monster!C1:C').copyTo(ss.getRange('Page 2!C1:C'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.getRange('Monster!C1').clear({contentsOnly: true, skipFilteredRows: true});
}