Google表格脚本挂起在getDataRange或getRange上

时间:2017-04-25 08:42:42

标签: google-sheets google-sheets-api

我有一个谷歌电子表格,可以让我跟踪成本和项目金额。我写了一个脚本来更新这些值;然而,今天它只是在getRange或getDataRange上挂起,即使在调试时也是如此。昨天工作正常。

<code>function onOpen() {
    var menuEntries = [{name: "Update P0", functionName: "UpdateP0"}];
    SpreadsheetApp.getActiveSpreadsheet().addMenu("PI Utils", menuEntries);
}
function showMsg(data, title, timeout) {
    if (typeof title == 'undefined') title = "";
    if (typeof timeout == 'undefined') timeout = 5;
    SpreadsheetApp.getActiveSpreadsheet().toast(data, title, timeout);
}
function UpdateP0() {
    showMsg("Updating...", "", -1);
    var piSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PI (Set1)');
    piSheet.activate();
    //SpreadsheetApp.flush();
    var numPlanets = 5;
    try {
        var dRange = piSheet.getRange('A1:F55'); // will not execute pass this line
    } catch (e) {
        throw (e);
        return;
    }
    for (var p = 1, prGroup = 1; p <= numPlanets; p++, prGroup += 11) {
        var cell1, cell2, v1, v2;
        // update extraction amounts
        for (var r = 0; r < 3; r++) {
            cell1 = dRange.getCell(prGroup + 2 + r, 3);
            cell2 = dRange.getCell(prGroup + 2 + r, 4);
            v1 = cell1.getValue().toString();
            v2 = cell2.getValue().toString();
            v1 = (v1.length == 0) ? parseFloat(0.0) : parseFloat(v1);
            v2 = (v2.length == 0) ? parseFloat(0.0) : parseFloat(v2);
            v2 += v1;
            cell1.setValue(0);
            cell2.setValue(v2);
        }
        // update production costs
        for (var c = 2; c <= 5; c++) {
            cell1 = dRange.getCell(prGroup + 9, c);
            cell2 = dRange.getCell(prGroup + 8, c);
            v1 = cell1.getValue().toString();
            v2 = cell2.getValue().toString();
            v1 = (v1.length == 0) ? parseFloat(0.0) : parseFloat(v1);
            v2 = (v2.length == 0) ? parseFloat(0.0) : parseFloat(v2);
            v2 += v1;
            cell1.setValue(0);
            cell2.setValue(v2);
        }
    }
    SpreadsheetApp.flush();
    showMsg("Completed.", "", 5);
}</code>

1 个答案:

答案 0 :(得分:0)

我为解决这个问题所做的工作(或者可能是巧合)是创建一个带有调整后名称的新工作表,然后剪切&amp;将旧表格中的所有内容粘贴到新表格中,然后最后删除旧表格。运行该功能在新工作表上运行。