2011-06-10 38 views
0

我一直在试图掀起一个快速的谷歌脚本来计数邀请响应电子表格rsvps婚礼。该脚本完美工作了一个星期的新条目添加到电子表格,然后突然停了下来,在每个单元下面的错误信息工作:谷歌电子表格脚本问题 - 错误:服务时间了:应用程序脚本

错误:服务超时:Google Apps脚本

剧本本身很简单。它查询相关列(有多个事件),然后检查用户是否有一些响应 - 通常是“YES”,“NO”或空白。

这个错误是什么意思,有没有人有任何修复建议?

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; 
} 
+1

你能提供调用此函数的代码?有些超时错误偶尔会发生,有时候最好的做法是等待几分钟,然后重试。 – Eduardo 2011-07-09 20:48:58

回答

4

希望婚礼进展顺利。这是前一段时间被问到的,但在这篇文章中已被观看了300多次,我认为很重要:

数据应该是不能从循环中的电子表格中提取。所需的数据应该批量提取到数组中,并在循环中评估数组。

见在文档参考: 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; 
} 
相关问题