【问题标题】:.setValue is only executed when the script is finished.setValue 仅在脚本完成时执行
【发布时间】:2023-04-06 14:45:01
【问题描述】:

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();
现在我唯一的问题是,不会设置值(“外部文件”),我的脚本在脚本停止时首先设置这个值。 有没有一种可行的方法来解决它,请拿着它,我在谷歌脚本上不太好:-D

【问题讨论】:

  • 你试过在setValue()之后使用SpreadsheetApp.flush()
  • 大声笑,谢谢。解决了我的问题:-D

标签: google-apps-script google-sheets


【解决方案1】:

详细说明@Jack Brown 的评论,在向电子表格界面写入和读取时,Google Apps 脚本不一定立即执行写入 - 它会尝试优化电子表格界面上的调用以最小化所需的资源。 使用 SpreadsheetApp.flush() 指示 Apps 脚本引擎对电子表格执行任何未决更改(写入、由于新写入的数据而计算单元格公式等)。

OP 的 sn-p 将是:

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();

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多