function main(spreadsheetReport) {
var positions = spreadsheetReport.sheets[0].getExpandedPositions("C4"); //获取数据列的前一列
var fromRow = positions[0].row;
var toRow = positions[positions.length - 1].row;
var cells = spreadsheetReport.workbook.worksheets.get(0).cells;
var levelColumnName = "A"; //层级标识所在的列
var fromColumnName = "D"; //需要汇总的所有列中的第一列的位置
var toColumnName = "E"; //需要汇总的所有列的最后一列的位置
var levelColumnIndex = cells.get(levelColumnName + "1").column; //数据列下标为2,从0开始计起
var fromColumnIndex = cells.get(fromColumnName + "1").column;
var toColumnIndex = cells.get(toColumnName + "1").column;
var formulaCells = [];
for (var i = fromRow; i < toRow; i++) {
var level = parseInt(cells.get(i, levelColumnIndex).value); //获取层级标识
var children = [];
for (var j = i + 1; j <= toRow; j++) {
var nextLevel = parseInt(cells.get(j, levelColumnIndex).value); //获取下一层级标识
//判断是否为其下第一层级
if (nextLevel == level) break;
if (nextLevel == level + 1) children.push(j); //将此层级下的子节点放入组数中
}
if (children.length > 0) {
for (var j = fromColumnIndex; j <= toColumnIndex; j++) {
var c = cells.get(i, j);
if (c.isFormula()) continue;
var formula = new Array();
for (var k = 0; k < children.length; k++) {
formula.push(cells.get(children[k], j).name);
}
formula = "=" + formula.join('+');
c.formula = formula;
formulaCells.push(i + ":" + j);
}
}
}
spreadsheetReport.customProperties.put("formulaCells", formulaCells);
spreadsheetReport.workbook.calculateFormula();
}