.setValue仅在脚本完成时执行

时间:2018-03-04 01:56:54

标签: google-apps-script google-sheets

d 我的脚本有问题。  我脚本的一部分:

    var a = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("A");
    var b = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B");

    a.getRange("A14").setValue("external File");
    Utilities.sleep(2000);
    a.getRange("A14:C29").copyTo(b.getRange("A1:C15"), {contentsOnly:true});
    a.getRange("A14:C29").clearContent();
现在我唯一的问题是,不会设置Value(“外部文件”),我的脚本在脚本停止时首先设置该值。 是否有一个可行的方法来解决它,请在谷歌脚本中保持它不是那么好:-D

1 个答案:

答案 0 :(得分:2)

详细说明@Jack Brown的评论,当写入和阅读电子表格界面时,Google Apps脚本不一定立即执行写入 - 它将尝试通过电子表格界面优化调用尽量减少所需的资源。 使用SpreadsheetApp.flush()指示Apps脚本引擎执行对电子表格的任何挂起更改(写入,由于新写入的数据而计算单元格公式等)。

OP的片段将是:

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();
相关问题