getRange(),setFormulas不想工作

时间:2012-08-17 17:17:07

标签: google-apps-script

我有以下代码,当我运行它时,我在sheetFormulas(4)中获得正确数量的项目,并且数组值看起来正确。

但是,在弹出了SheetFormulas Browser.msgBox后,我收到错误,表明getRange()。setFormulas行有问题,但我看不出它是什么。

function test(){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var testingTarget = ss.getSheetByName("Testing");
    var sheetFormulas = new Array();
    for (l=0;l<10;l++) {
        sheetRowCount = l + 2;
        var monthlyTotalCompanyCosts = '=F' + sheetRowCount + '/$F$29*$I$14';
        var monthlyTotalCompanyCostsPerEa = '=IFERROR(H' + sheetRowCount + '/C' + sheetRowCount + ')';
        var monthlyMargin = '=D' + sheetRowCount + '-F' + sheetRowCount;
        var monthlyMarginPctg = '=IFERROR(J' + sheetRowCount + '/D' + sheetRowCount + ')';
        sheetFormulas.push(monthlyTotalCompanyCosts,monthlyTotalCompanyCostsPerEa,monthlyMargin,monthlyMarginPctg);
    Browser.msgBox("sheetFormulas.length is: " + sheetFormulas.length);
    Browser.msgBox("sheetFormulas is: " + sheetFormulas);
    testingTarget.getRange(sheetRowCount,8,1,4).setFormulas(sheetFormulas);
    sheetFormulas = [];
    }
}

感谢任何帮助,

菲尔

1 个答案:

答案 0 :(得分:0)

首先,sheetFormulas必须是2D数组。所以试着做

/* Notice the [] around sheetFormulas */
testingTarget.getRange(sheetRowCount,8,1,4).setFormulas([sheetFormulas]);

如果您仍然看到其他问题,请将您的代码放在try {} catch {}块中,并在catch块中打印出异常。

我还建议您在电子表格上设置之前使用Logger.log()打印出公式。

相关问题