[英]Custom Function re-running itself periodically
I've created a google spreadsheet that uses a custom function to call a paid API service and parse that results based on a few input parameters. 我创建了一个Google电子表格,该电子表格使用自定义函数来调用付费API服务,并根据一些输入参数来解析结果。 I am able to call and parse the data successfully using an activation button (A1 is a cell that if its value is "ON" the script is called).
我可以使用激活按钮成功调用和解析数据(A1是一个单元格,如果其值为“ ON”,则将调用脚本)。
My issue is that if I leave the data parsed by leaving the activation button "ON", the custom function re-runs itself. 我的问题是,如果我通过使激活按钮保持在“ ON”状态来解析数据,则自定义函数会重新运行。 If I were to change an input parameter I would understand it re-running itself, but I can sit there staring at the screen, and it will once again show "Loading..." and parse the data again, as if I had set the button to "OFF" and "ON" again.
如果要更改输入参数,我会理解它会重新运行,但是我可以坐在那里盯着屏幕,它会再次显示“ Loading ...”并再次解析数据,就像我已经设置了一样按钮再次设为“ OFF”和“ ON”。 This seems to happen at random - it can be 5 minutes or 2 hours.
这似乎是随机发生的-可能需要5分钟或2个小时。
I'm attaching the function below, as well as the logic of the spreadsheet 我附上下面的功能以及电子表格的逻辑
/// This function injects the variables into the HTTP service and if the data can be parsed returns an array.
function getCategory (category,key){
var apiurl = "https://SERVICE/"+category+"/Country?Format=JSON&id="+key
var result = []
try {
var category_data = parse(apiurl)
var data_dictionary = category_data.TopCountryShares
for (var i in data_dictionary){
result.push(data_dictionary[i].CountryCode)
}
}
catch(e) {
result.push("No Data")
}
return result
}
//// This is the function that parses the data above
function parse(url){
var parsing_url = url
var fetchapi = UrlFetchApp.fetch(parsing_url)
Utilities.sleep(2000)
var data=JSON.parse(fetchapi)
return data
}
In the Spreadsheet I would have A1 as the ON/OFF button, and A5 down as individual categories (ie A5 = Games, A6 = Shopping, etc). 在电子表格中,我将A1作为“打开/关闭”按钮,将A5向下作为单独的类别(即A5 =游戏,A6 =购物等)。 The below excel formulas would be used:
将使用以下excel公式:
B5 would be =IF(AND($A$1="ON",ISBLANK(A5)=FALSE),TRANSPOSE(getCategory(A5,'API KEY'!$B$6)),"") B5将是= IF(AND($ A $ 1 =“ ON”,ISBLANK(A5)= FALSE),TRANSPOSE(getCategory(A5,'API KEY'!$ B $ 6)),“”)
B6 would be =IF(AND($A$1="ON",ISBLANK(A6)=FALSE),TRANSPOSE(getCategory(A6,'API KEY'!$B$6)),"") B6将是= IF(AND($ A $ 1 =“ ON”,ISBLANK(A6)= FALSE),TRANSPOSE(getCategory(A6,'API KEY'!$ B $ 6)),“”)
In case I didn't explain it correctly, a scenario would be as follows: 如果我没有正确解释它,那么情况如下:
Number 6 seems to happen at random time intervals, and I can confirm that the "Recalculation" is set to "On Change" (found in File -> Spreadsheet Settings -> Calculation) 数字6似乎发生在随机的时间间隔,我可以确认“重新计算”设置为“按更改”(在“文件”->“电子表格设置”->“计算”中找到)
The above is a sample script, but the real script returns JSONs that can be quite large, so caching isn't an option based on what I've read of the current limitations. 上面是一个示例脚本,但是实际的脚本返回的JSON可能很大,因此根据我对当前限制的了解,缓存不是一个选项。
Any help would be appreciated - I'm at my wits end trying to figure this out! 任何帮助将不胜感激-我正竭尽全力试图解决这个问题!
I assume if you set the button to off to doesn't refresh? 我假设您将按钮设置为“关闭”不会刷新? Would it be an option to add a line of code to the end of your script to set the button to off after the script has finished?
是否可以选择在脚本末尾添加一行代码,以在脚本完成后将按钮设置为关闭?
sheet.getRange('A1').setValue('OFF');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.