.setValue仅在脚本完成时执行 [英] .setValue is only executed when the script is finished

查看:96
本文介绍了.setValue仅在脚本完成时执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

D
我的脚本有问题。
a部分脚本:


现在我唯一的问题是,Value 外部文件)将不会被设置,脚本停止时我的脚本首先设置此值。
是否有一个可行的方法来解决它,请保持它eZ我不是那么好在谷歌脚本:-D

<要详细说明 @Jack Brown 的评论,当写入和阅读Spreadsheet界面时,Google应用程序脚本不一定会立即执行写入 - 它将尝试优化电子表格界面上的调用,以最大限度地减少所需的资源。
使用 SpreadsheetApp.flush () 指示Apps脚本引擎对电子表格执行任何未决更改(写入,由于新写入的数据计算单元格公式等)。

OP的片段将会是:

  var a = SpreadsheetApp.getActive()。getSheetByName(一个); 
var b = SpreadsheetApp.getActive()。getSheetByName(B);

a.getRange(A14)。setValue(external File);

//强制写入上面的值(以及引用A14更新的任何单元格)。
SpreadsheetApp.flush();

//如果没有flush(),Apps脚本可能会等待,直到这些调用执行写操作。
a.getRange(A14:C29)。copyTo(b.getRange(A1:C16),{contentsOnly:true});
a.getRange(A14:C29)。clearContent();


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

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

now my only problem is,the Value("external File") will not be set, my script set this value first when the script stops. Is there a posible methode to solve it, pls hold it eZ im not that well at google script :-D

解决方案

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

这篇关于.setValue仅在脚本完成时执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆