简体   繁体   English

自定义功能会定期重新运行

[英]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: 如果我没有正确解释它,那么情况如下:

  1. Button is set to OFF 按钮设置为OFF
  2. I enter 20 categories into A5:A25 我在A5:A25中输入20个类别
  3. I turn Button ON 我打开按钮
  4. Loading... 载入中...
  5. Data is parsed for each category from BF 从BF中为每个类别解析数据
  6. I don't touch anything else in the document and on occasion I see "Loading..." again as the script is re-running 我没有触摸文档中的任何其他内容,有时在脚本重新运行时再次看到“正在加载...”

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.

相关问题 重新运行将 Google 表格数据注入 Google 幻灯片的脚本时,如何清除所有现有数据? - How to flush out all existing data when re-running a script which injects Google Sheet data to Google slides? 停止自定义函数自动刷新/定期调用外部 API - Stop custom function from auto refreshing/periodically calling external API 从自定义菜单运行 function 时出现“很抱歉,发生服务器错误。请稍等,然后重试”错误 - "We're sorry, a server error occurred. Please wait a bit and try again" error when running a function from a custom menu 定期刷新 IMPORTXML() 电子表格 function - Periodically refresh IMPORTXML() spreadsheet function Google Sheets App Script Custom Function 重新执行 - Google Sheets App Script Custom Function Re-executing 如何在打开工作表后禁用自定义 function 重新运行? - How to disable custom function re-run after opening a sheet? 如何在 Google 表格自定义 function 中编写“重新运行自定义功能”按钮的代码? - How to code "re-run custom function" button in Google Sheet custom function? 从 appscript 到工作表上运行自定义 function 返回错误 - Running a custom function from appscript onto sheets returns an error Google Spreadsheet自定义功能:正在运行太多脚本 - Google Spreadsheet Custom Function: Too many scripts running onSelectionChange 在更改选项卡时重新触发自身 - onSelectionChange re-triggers itself when changing tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM