.setValue is only executed when the script is finished

后端 未结 1 421
臣服心动
臣服心动 2020-12-04 02:53

D I have a problem with my Script. a part of my script:

<
相关标签:
1条回答
  • 2020-12-04 03:31

    To elaborate on @Jack Brown's comment, when both writing to and reading from the Spreadsheet interface, Google Apps Script does not necessarily immediately perform the write - it will attempt to optimize calls over the Spreadsheet interface to minimize resources needed. Using SpreadsheetApp.flush() instructs the Apps Script engine to perform any pending changes to the spreadsheet (writes, calculations of cell formulas due to newly-written data, etc).

    OP's snippet would then be:

    var a = SpreadsheetApp.getActive().getSheetByName("A");
    var b = SpreadsheetApp.getActive().getSheetByName("B");
    
    a.getRange("A14").setValue("external File");
    
    // Force the above value to be written (and any cells that refer to A14 to update).
    SpreadsheetApp.flush();
    
    // Without flush(), Apps Script may wait until making these calls to perform the write.
    a.getRange("A14:C29").copyTo(b.getRange("A1:C16"), {contentsOnly: true});
    a.getRange("A14:C29").clearContent();
    
    0 讨论(0)
提交回复
热议问题