简体   繁体   English

自定义函数慢的 Google Sheets 脚本性能

[英]Google Sheets Script Performance With Slow Custom Function

Setup: I have a Google sheet where I would like to run a custom function implemented using a script.设置:我有一个 Google 表格,我想在其中运行使用脚本实现的自定义函数。 This script is used to perform a relatively lengthy URL lookup and decode process (10 ms per call, dependent on bandwidth and ping).此脚本用于执行相对较长的 URL 查找和解码过程(每次调用 10 毫秒,取决于带宽和 ping)。 The custom function uses one column as input, and returns the result.自定义函数使用一列作为输入,并返回结果。

Problem: When my Google sheet is opened, the column that uses this calculation is refreshed.问题:当我的 Google 表格打开时,使用此计算的列被刷新。 This grinds the Google sheet to a halt for about 10 seconds, until each cell in the column is recalculated.这会使 Google 工作表停止大约 10 秒,直到重新计算列中的每个单元格。 This will only become worse as I add to my spreadsheet.当我添加到我的电子表格时,这只会变得更糟。

Question: Can I change my function script, or change a setting in Google sheets so that the slow custom function is only calculated when the input cell is changed?问题:我可以更改我的函数脚本,或更改 Google 表格中的设置,以便仅在更改输入单元格时计算慢速自定义函数吗?

For anyone curious, here is a demo sheet with my problem对于任何好奇的人, 这是我的问题的演示表

On my answer to In google sheets can I wrap a standard function in a custom function to control when it is run?在我对In google sheet 的回答中,我可以将标准函数包装在自定义函数中以控制它何时运行吗? I shared the idea of having a couple of buttons called "freeze" / "unfreeze" to control when the recalculation of "expensive formulas" is done.我分享了一个想法,即使用几个名为“冻结”/“解冻”的按钮来控制何时完成“昂贵的公式”的重新计算。

On this case, you could "freeze" the range with your custom function before closing the spreadsheet so the next time that you open it will open faster, then when you need to update the frozen range you "unfreeze" it.在这种情况下,您可以在关闭电子表格之前使用自定义函数“冻结”范围,以便下次打开它时会更快地打开,然后当您需要更新冻结的范围时,您可以“解冻”它。

After posting the original version the OP asked for a simple version but as my original implementation was something quick and dirty with stuff in Spanish I shared there just the most important code lines.在发布原始版本后,OP 要求提供一个简单的版本,但由于我的原始实现是用西班牙语快速而肮脏的东西,我只在那里分享了最重要的代码行。 The core methods services are核心方法服务是

  • A global variable to store the formula if it will always be the same如果公式始终相同,则用于存储公式的全局变量
  • copyTo to overwrite the formula results range with the values a la copy-paste-values-only copyTo用值 a la copy-paste-values-only 覆盖公式结果范围
  • clear to delete the formula results pasted previously clear删除之前粘贴的公式结果
  • setFormula to add the formula back to the spreadsheet. setFormula将公式添加回电子表格。

Then I realized that I could improved that published an unlisted Google Sheets add-on that use the PropertiesService to implement a "formula store" with functions to add/remove/list the formulas and a dynamic menu instead of buttons.然后我意识到我可以改进发布一个未列出的 Google Sheets 附加组件,它使用PropertiesService来实现一个“公式存储”,其中包含添加/删除/列出公式的功能和一个动态菜单而不是按钮。

Something else to try is to use a time-driven trigger that runs, let say nightly just in case that you forgot to freeze the range for the custom function results.要尝试的另一件事是使用运行的时间驱动触发器,例如每晚运行,以防您忘记冻结自定义函数结果的范围。

Slightly better performance if there are less custom functions.如果自定义函数较少,则性能会稍好一些。 You can rewrite your function to take in an array, so you'd only have one function instead of 100 running at once.您可以重写您的函数以接收一个数组,这样您一次只能运行一个函数而不是 100 个。

function mySlowFunction(x) {
  //Utilities.sleep(x*100); //100 ms
  if (x.map) {
    return x.map(function(y) {return mySlowFunction(y) });
  } else {
    return x * 100;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM