Wait for formula to be completed before executing script

不羁的心 提交于 2020-01-16 18:48:29

问题


I have this scenario:

  • A sheet "BQ" is connected with BigQuery using 1

  • A second sheet "F" contains a Formula which reads data from "BQ"

  • An AppsScript that trigger BigQuery data reload

Here is the script sample:

var spreadsheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
SpreadsheetApp.enableAllDataSourcesExecution();
spreadsheet.getRange("A1").getCell(1,1).getDataSourceTables()[0].refreshData();

This appscript perform these actions:

  • trigger BigQuery data reload, as mentioned
  • "wait" the query to be completed
  • "wait" the formula to process new data and update second sheet
  • parse data in second sheet and do what it has to do...

Here is when the query is running:

Here is when sheet is updating basing on formula:

I have a problem, I don't know how to do the "wait" part. At first I thought that only SpreadsheetApp.flush(); was enough, but at the end the script starts to read even if query is still running or Spreadsheet is still updating formula, so this command is useless for my need.

At the moment I'm using a fixed Utilities.sleep(20000); basing on the time I saw that is needed for both operation (query and sheet update), but I'm wondering if there is a more high-level approach on perform a "sleep" until the Sheet is fully updated


回答1:


I'm not a Google Sheet expert. There might be a pragmatical way of just knowing when the formula is finished. But even without it, can you inject certain "signature" to indicate formula result being changed? Naively, it could be concat( <original_formula_output>, "|||||", NOW()).

So the waiting loop can be:

var cell_content_before = FetchingFormulaCellContent();
TriggerFormulaUpdate();
WHILE (cell_content_before == FetchingFormulaCellContent() 
       AND waiting_timeout_is_not_reached) {
  Utilities.sleep(1000)`;
}


来源:https://stackoverflow.com/questions/56965058/wait-for-formula-to-be-completed-before-executing-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!