如何在Google Apps脚本中刷新工作表的单元格值 [英] How to refresh a sheet's cell value in Google Apps Script

查看:327
本文介绍了如何在Google Apps脚本中刷新工作表的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Blockspring ,它提供了Google Sheets插件,例如,它可以运行一个从web服务返回数据的函数,例如

  = BLOCKSPRING(get-stock-current-stats,ticker, MSFT)

我想刷新单元格的数据,但没有看到'刷新'调用()

 函数onOpen(){
createTimeDrivenTriggers()
}

函数createTimeDrivenTriggers(){
//每分钟触发
ScriptApp.newTrigger('myFunction')
.timeBased()
.everyMinutes(1)
。创建();


$ b函数myFunction(){
Logger.log('I ran'); //可以在日志中看到
SpreadsheetApp.getActiveSheet()。getRange('B4')。getValue()//假定返回一个值给脚本

}


解决方案使用 flush() method:

Google文档

  SpreadsheetApp.flush(); 

文件引用:


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


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



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


I'm experimenting with Blockspring which provides a Google Sheets add-on which can, for example, run a function which returns data from a webservice e.g.

=BLOCKSPRING("get-stock-current-stats", "ticker", "MSFT")

I want to refresh a cell's data but don't see a 'refresh' call in the docs.

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

}

解决方案

Use the flush() method:

Google Documentation

SpreadsheetApp.flush();

Quote from documentation:

Applies all pending Spreadsheet changes

If there were no changes to the spreadsheet, but you want to force formulas recalculate, you will need to make a change, and then use SpreadsheetApp.flush();

For example, you could get the value from cell A1, then set the same value to cell A1. So, there is no chance of loosing data because you are getting and setting the same value. The change allows. SpreadsheetApp.flush(); to recalculate all the formulas.

这篇关于如何在Google Apps脚本中刷新工作表的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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