简体   繁体   English

对于循环超时:JavaScript / Google Apps脚本

[英]For Loops timing out: JavaScript / Google Apps Script

So I am have this issue with Google Apps Script. 所以我对Google Apps脚本有这个问题。 It is timing out because the app server requests are taking too long. 它超时是因为应用服务器请求耗时太长。 I was just wanting to see if my coding could be cleaned up a bit to run faster or is there another method which would work? 我只是想知道我的编码是否可以清理一下以便更快地运行,还是有另一种方法可以工作?

Code below: 代码如下:

for (var y = 1; y < listLast ; y++) {

  var matchor = listSheet.getRange("B" + y).getValue();
  var listEmCo = listSheet.getRange("A" + y).getValue();
  if(matchor == "Match") {
    Logger.log("Do Nothing");
  } else {
  for (var x = 0; x < formLast; x++) {

  if(listEmCo == formData[x]){

    listSheet.getRange("B"+ [y]).setValue("Match");
    break;

  } else {

    listSheet.getRange("B"+ [y]).setValue("No Match");

   }
  }
 }   
}

Thanks for any responses :) 谢谢你的回复:)

Do not use .getValue(); 不要使用.getValue(); in a loop. 在一个循环中。 This operation is heavy. 这个操作很重。

Please use range.getValues() and then loop the array to get values. 请使用range.getValues()然后循环数组以获取值。

The plan is: 计划是:

  1. get the initial range 得到初始range
  2. get data with var data = range.getValues(); 使用var data = range.getValues();获取数据var data = range.getValues();
  3. make empty array to write new values array = []; 使空数组写入新值array = [];
  4. Loop the data and make new array, fill it with proper values [["value1"], ["value2"], ...,] . 循环数据并创建新数组,用适当的值填充[["value1"], ["value2"], ...,] Note. 注意。 It should be a 2D-Array. 它应该是2D阵列。
  5. After the loop define a range to paste new values: rangeTo 循环后定义一个范围来粘贴新值: rangeTo
  6. Use rangeTo.setValues(array); 使用rangeTo.setValues(array); to paste new values. 粘贴新值。

See more: 查看更多:

Tag #Another-get-data-from-range-question 标记#Another-get-data-from-range-question

See more questions on topic: 查看有关主题的更多问题:

http://stackoverflow.com/questions/39859421/google-script-internal-error-after-15-seconds http://stackoverflow.com/questions/39859421/google-script-internal-error-after-15-seconds

http://stackoverflow.com/questions/39586911/google-script-exceeded-maximum-execution-time-help-optimize http://stackoverflow.com/questions/39586911/google-script-exceeded-maximum-execution-time-help-optimize

http://stackoverflow.com/questions/38618266/google-sheet-script-times-out-need-a-new-way-or-flip-it-upside-down http://stackoverflow.com/questions/38618266/google-sheet-script-times-out-need-a-new-way-or-flip-it-upside-down

http://stackoverflow.com/questions/44021567/google-sheet-script-multiple-getrange-looping http://stackoverflow.com/questions/44021567/google-sheet-script-multiple-getrange-looping

Code runs too slow 代码运行速度太慢

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

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