[英]Google Spreadsheet Script problem - Error: Service Times Out: Apps Script
I've been trying to whip up a quick google script to count rsvps for the invite response spreadsheet for a wedding.我一直在尝试编写一个快速的谷歌脚本来计算婚礼邀请响应电子表格的 rsvps。 The script worked perfectly for a week as new entries were added to the spreadsheet, then suddenly stopped working with the following error message in each cell:
该脚本完美地运行了一周,因为新条目被添加到电子表格中,然后突然停止工作,每个单元格中出现以下错误消息:
Error: Service Times Out: Apps Script错误:服务超时:Apps 脚本
The script itself is simple.脚本本身很简单。 It queries the relevant column (there are multiple events) and then checks to see whether there is some response spefied by the user - "YES", "NO", or a blank, typically.
它查询相关列(有多个事件),然后检查是否有用户指定的一些响应——通常是“是”、“否”或空白。
What does this error mean, and does anyone have any suggestions for fixes?这个错误是什么意思,有没有人有任何修复建议?
function sumRSVP(response, rsvpType) {
var rsvpCol = 7;
if (rsvpType == "rehearsal") rsvpCol = 8;
if (rsvpType == "brunch") rsvpCol = 9;
var mySum = 0;
var sh = SpreadsheetApp.getActiveSheet();
for( i=2; i<177; i++){
var rsvp = sh.getRange(i, rsvpCol).getValue();
var nguests = sh.getRange(i, 6).getValue();
if(nguests != "" && rsvp == response){
mySum = mySum + parseFloat(nguests);
}
}
return mySum;
}
Hopefully the wedding went well.希望婚礼顺利。 This was asked some time ago but has been viewed over 300 times at this post and I believe is important:
前段时间有人问过这个问题,但在这篇文章中已经被浏览了 300 多次,我认为这很重要:
Data should not be extracted from a spreadsheet in a loop .不应循环从电子表格中提取数据。 The data needed should be extracted in a batch to an array and the array evaluated in the loop.
需要的数据应该批量提取到一个数组中,并在循环中评估该数组。
See docs reference at: https://developers.google.com/apps-script/guide_common_tasks#OptimizeScripts请参阅文档参考: https://developers.google.com/apps-script/guide_common_tasks#OptimizeScripts
You can write scripts to take maximum advantage of the built-in caching, by minimizing the number of reads and writes.
您可以编写脚本以最大限度地利用内置缓存,最大限度地减少读取和写入次数。 Alternating read and write commands is slow.
交替读取和写入命令很慢。 To speed up a script, read all data into an array with one command, perform any operations on the data in the array, and write the data out with one command.
为了加快脚本的速度,用一个命令将所有数据读入数组,对数组中的数据执行任何操作,然后用一个命令写出数据。
function sumRSVP(response, rsvpType) {
var rsvpCol = 7;
if (rsvpType == "rehearsal") rsvpCol = 8;
if (rsvpType == "brunch") rsvpCol = 9;
var mySum = 0;
var sh = SpreadsheetApp.getActiveSheet();
// start at row 2 - uses columns 6-9
var data = sh.getRange(2, 6, 177 - 1 , 4).getValues();
for(var i=0; i<data.length; i++){
var rsvp = data[i][rsvpCol - 6];
var nguests = data[i][0];
if(nguests != "" && rsvp == response){
mySum = mySum + parseFloat(nguests);
}
}
return mySum;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.