如何在Google Apps脚本中刷新工作表的单元格值

cod*_*boy 8 google-sheets google-apps-script

我正在尝试使用Blockspring,它提供了一个Google表格附加组件,例如,它可以运行一个从Web服务返回数据的函数,例如

=BLOCKSPRING("get-stock-current-stats", "ticker", "MSFT")
Run Code Online (Sandbox Code Playgroud)

我想刷新单元格的数据但是没有在文档中看到"刷新"调用.

function onOpen() {
  createTimeDrivenTriggers()
}

function createTimeDrivenTriggers() {
  // Trigger every minute
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .everyMinutes(1)
      .create();

}

function myFunction() {
  Logger.log('I ran'); // can see this in the logs
  SpreadsheetApp.getActiveSheet().getRange('B4').getValue() //presumably returns a value to the script

}
Run Code Online (Sandbox Code Playgroud)

Ala*_*lls 20

使用flush()方法:

Google文档

SpreadsheetApp.flush();
Run Code Online (Sandbox Code Playgroud)

从文档中引用:

应用所有待处理的电子表格更改

如果电子表格没有更改,但您想强制重新计算公式,则需要进行更改,然后使用 SpreadsheetApp.flush();

例如,您可以从单元格A1获取值,然后将相同的值设置为单元格A1.因此,没有机会丢失数据,因为您正在获取并设置相同的值.这种变化允许.SpreadsheetApp.flush();重新计算所有公式.


小智 11

flush您可以不使用,而是通过将函数传递到预期参数末尾的公式来告诉电子表格该函数依赖于哪些单元格。可以传递比函数使用的更多的参数。这样,电子表格将假设如果输入不同,函数返回的值可能不同,并且正常的更新过程会处理它。

如果您希望它在任何单元格更改时重新评估,只需传入一个包含所有单元格的范围。

例如:

=MyFunction("this", "that", A1:Z10000)
Run Code Online (Sandbox Code Playgroud)

如果您花时间准确传递函数在逻辑上依赖的单元格,那么在不需要时不重新评估会更有效。

=MyFunction("this", "that", A10, C5, G6:H9 )
Run Code Online (Sandbox Code Playgroud)

如果它并不真正依赖于任何单元格,但您想手动评估该函数,请根据需要制作一个单元格,当用户单击它时增加其隐藏值(请参阅按钮),然后将该单元格地址传递给函数。

=MyFunction("this", "that", A10 )
// make A10 change its value on a button click
Run Code Online (Sandbox Code Playgroud)