For 循环超时:JavaScript/Google Apps 脚本 [英] For Loops timing out: JavaScript / Google Apps Script

查看:26
本文介绍了For 循环超时:JavaScript/Google Apps 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在使用 Google Apps 脚本时遇到了这个问题.超时,因为应用程序服务器请求花费的时间太长.我只是想看看我的代码是否可以稍微清理一下以运行得更快,还是有其他可行的方法?

So I am have this issue with Google Apps Script. 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?

代码如下:

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");

   }
  }
 }   
}

感谢您的任何回复:)

推荐答案

不要在循环中使用 .getValue();.这个操作很繁重.

Do not use .getValue(); in a loop. This operation is heavy.

请使用range.getValues()然后循环数组获取值.

Please use range.getValues() and then loop the array to get values.

计划是:

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

查看更多:

查看有关主题的更多问题:

See more questions on topic:

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/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

代码运行太慢

这篇关于For 循环超时:JavaScript/Google Apps 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆