2012-08-17 44 views
0

我有下面的代码,当我运行它时,我得到sheetFormulas(4)中的正确数量的项目,并且数组值正确地形成。getRange(),setFormulas不想工作

但是,弹出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 = []; 
    } 
} 

任何帮助表示赞赏,

菲尔

回答

0

首先,sheetFormulas必须是2D阵列。因此,尝试做

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

如果你仍然可以看到其他的问题,然后把你的代码尝试{}赶上{}块内,并打印出catch块除外。

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