For 循环超时:JavaScript/Google Apps 脚本 [英] For Loops timing out: JavaScript / Google Apps Script
问题描述
所以我在使用 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.
计划是:
- 获取初始
range
- 使用
var data = range.getValues(); 获取数据;
- 创建空数组以写入新值
array = [];
- 循环数据并创建新数组,用适当的值填充它
[["value1"], ["value2"], ...,]
.注意.它应该是一个二维数组. - 在循环之后定义一个范围来粘贴新值:
rangeTo
- 使用
rangeTo.setValues(array);
粘贴新值.
- get the initial
range
- get data with
var data = range.getValues();
- make empty array to write new values
array = [];
- Loop the data and make new array, fill it with proper values
[["value1"], ["value2"], ...,]
. Note. It should be a 2D-Array. - After the loop define a range to paste new values:
rangeTo
- Use
rangeTo.setValues(array);
to paste new values.
查看更多:
- https://developers.google.com/apps-script/guides/support/best-practices
- https://sheetswithmaxmakhrov.wordpress.com/2017/11/02/scripts-working-with-arrays-in-sheets/
查看有关主题的更多问题:
See more questions on topic:
http://stackoverflow.com/questions/39859421/google-script-internal-error-after-15-seconds
http://stackoverflow.com/questions/44021567/google-sheet-script-multiple-getrange-looping
这篇关于For 循环超时:JavaScript/Google Apps 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!