对于循环超时:JavaScript / Google Apps脚本 [英] For Loops timing out: JavaScript / Google Apps Script
问题描述
所以我在Google Apps Script中遇到了这个问题。它正在超时,因为应用程序服务器请求时间太长。我只是想看看我的代码是否可以清理一下,以便运行得更快,或者是否有另一种方法可行?
以下代码:
$ (var y = 1; yvar matchor = listSheet.getRange(b
$ b
B+ y).getValue();
var listEmCo = listSheet.getRange(A+ y).getValue();
if(matchor ==Match){
Logger.log(Do Nothing);
} else {
for(var x = 0; x
if(listEmCo == formData [x]){
listSheet.getRange(B+ [y])。setValue(Match);
休息;
$ b} else {
listSheet.getRange(B+ [y])。setValue(No Match);
$ b code
$ b感谢您的回复: code>在循环中。这个操作很重。
请使用
range.getValues()
,然后循环数组以获取值。 / p>
计划是:
- 取得初始
范围
- 使用
获取数据> var data = range.getValues();
- 使空数组写入新值
array = [];
[[value1],[value2],...,]
。 注意它应该是2D数组。 rangeTo $ c
使用 rangeTo.setValues(array);
来粘贴新值。
ol>
查看更多:
- https://developers.google.com/apps-script/guides/support/best-practices
- https ://sheetswithmaxmakhrov.wordpress.com/2017/11/02/scripts-working-with-arrays-in-sheets/
标签#另一个获取数据范围问题
查看更多关于主题的问题:
http://stackoverflow.com/questions/39859421/google-scrip t-internal-error-after-15-seconds
http:// stackoverflow。 com / questions / 44021567 / google-sheet-script-multiple-getrange-looping
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?
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();
in a loop. This operation is heavy.
Please use range.getValues()
and then loop the array to get values.
The plan is:
- 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.
See more:
- https://developers.google.com/apps-script/guides/support/best-practices
- https://sheetswithmaxmakhrov.wordpress.com/2017/11/02/scripts-working-with-arrays-in-sheets/
Tag #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/44021567/google-sheet-script-multiple-getrange-looping
这篇关于对于循环超时:JavaScript / Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!