The script below is activated every 5 minutes, sometimes the importxml function takes a long time to return the result, causing the script not to wait for the query to finish and to move on to the next steps, causing an error in copying the data.
I would like to know where I should put the SpreadsheetApp.flush();
so that it only goes to the next step when the importxml loads completely and if only it does not solve, how should I add the Utilities.sleep(5000);
?
When I put only the Utilities.sleep(5000);
the script does not wait for the indicated milliseconds and passes directly, it only works when I add the SpreadsheetApp.flush();
. So I would like to know if I need to use both or if only SpreadsheetApp.flush();
solves my problem and where it should be positioned.
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});
}