如何在打开 sheet 时不执行函数?

How can I not execute a function at open a sheet?

我在 google sheet 上创建了一个函数,请求外部 link。我这样调用函数:

=functionimade(params)

但是,我总是打开 sheet,调用函数并发出请求。我该如何解决?我要申请一次

答案:

很遗憾,这是不可能的。

更多信息:

当在 Sheet 中调用自定义函数时,单元格的值即函数的 return - 这是调用它的公式。每次打开 Sheet 时都会计算函数的 return 值,因为唯一保存的是公式;不是数据。

此外,根据 documentation,自定义函数不支持 SpreadsheetApp.set*() 方法:

Spreadsheet Service: Read only (can use most get*() methods, but not set*()).

这意味着无法直接使用 .getRange(cell).setValue(functionimade(params)) 设置单元格的值,因为自定义函数的范围不允许您直接设置它。

解决方法:

在你的函数结束时,而不是 returning 值,你可以使用前面提到的 .setValue() 方法设置内容,只是你必须在函数本身中指定单元格:

function functionimade(params) {
  var myReturnValue = // some cool code here

  var cell = 'A1'; // << change this
  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(cell)
                                                        .setValue(myReturnValue);
}

参考文献:


相关问题: