Google Apps脚本 - 超出了最长执行时间

时间:2014-01-14 12:02:25

标签: google-apps-script google-sheets google-spreadsheet-api

我正在为我的公司开发一个新的电子表格,使我们每周执行的任务之一更容易 - 它的工作方式是将数据导入到此电子表格中,然后运行脚本以生成报告我们用。下面是我为编制电子表格的一部分而制作的一个脚本示例。

手动导入电子表格的数据最多可包含3000个信息单元格,因此用于检查单元格的循环 - 但无论何时运行此脚本,它都会到达第1617行,然后给出超出最大执行次数的错误时间 - 无论如何我都可以避免这个问题,因为我有几个需要在之后运行的脚本来帮助生成我们使用的报告。

有6张纸,每张纸都输入了数据,具体取决于导入数据中单元格的值。

function WorkoutTotals() {
// -----------------------------------------------------------------------------
// This function is used for working out the totals of each centre
// -----------------------------------------------------------------------------
var value;
for (var j = 2; j < abignumber; j++) {
    var rawcentres = rawdata.getRange(j, 1);
    var rawcategory = rawdata.getRange(j, 6);
    switch (rawcentres.getValue()) {
    case centres[0]:
        centresheet = ss.getSheetByName(sheets[0]);
        switch (rawcategory.getValue()) {
        case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
    case centres[1]:
        centresheet = ss.getSheetByName(sheets[1]);
        switch (rawcategory.getValue()) {
        case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
    case centres[2]:
        centresheet = ss.getSheetByName(sheets[2]);
        switch (rawcategory.getValue()) {
        case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
    case centres[3]:
        centresheet = ss.getSheetByName(sheets[3]);
        switch (rawcategory.getValue()) {
                    case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
    case centres[4]:
        centresheet = ss.getSheetByName(sheets[4]);
        switch (rawcategory.getValue()) {
                    case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
    case centres[5]:
        centresheet = ss.getSheetByName(sheets[5]);
        switch (rawcategory.getValue()) {
                    case "a1":
            range = centresheet.getRange(cramrow, 1)
            range.setValue(range.getValue() + 1)
            break;
        case "A2":
            range = centresheet.getRange(cramrow, 2)
            range.setValue(range.getValue() + 1)
            break;
        case "a3":
            range = centresheet.getRange(cramrow, 3)
            range.setValue(range.getValue() + 1)
            break;
        case "a4":
            range = centresheet.getRange(cramrow, 4)
            range.setValue(range.getValue() + 1)
            break;
        case "a5":
            range = centresheet.getRange(cramrow, 5)
            range.setValue(range.getValue() + 1)
            break;
        case "a6":
            range = centresheet.getRange(cramrow, 6)
            range.setValue(range.getValue() + 1)
            break;
        case "a7":
            range = centresheet.getRange(cramrow, 7)
            range.setValue(range.getValue() + 1)
            break;
        case "a8":
            range = centresheet.getRange(cramrow, 8)
            range.setValue(range.getValue() + 1)
            break;
        case "a9":
            range = centresheet.getRange(cramrow, 9)
            range.setValue(range.getValue() + 1)
            break;
        case "a10":
            range = centresheet.getRange(cramrow, 10)
            range.setValue(range.getValue() + 1)
            break;
        }
        break;
        }
    break;
    }
}
}

1 个答案:

答案 0 :(得分:1)

这里的主要问题是脚本获取/设置每次迭代的值。相反,您可以获取整个范围,计算,然后更新范围以减少执行时间。这是开发人员网站的similar example