在以下所有选项卡上添加相同单元格的 Google 表格

问题描述 投票:0回答:2

我有一张名为摘要的摘要表。 然后我有多个适用于人们的标签 - 这些标签以他们的名字命名,例如约翰·史密斯、威尔·琼斯、莎莉·史密斯…… 我想对每个命名选项卡中的相同单元格求和或计数 - 我该如何自动执行此操作?

google-apps-script google-sheets sum countif
2个回答
1
投票

您实际上可以做的是遍历所有工作表,然后将它们加起来。 下面的示例显示了如何对所有工作表中的所有单元格“A1”求和。

function sumCells() {
    const cellLocation = "A1"; 
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheets = ss.getSheets().filter(function (sheet) {
      return sheet.getSheetName() != "Summary";
    }); // added filter to exclude Summary sheet from the calculation

    var sum = 0;
    var sheetName, cell, value;
    sheets.forEach((sheet) => {
        sheetName = sheet.getName();
        cell = sheet.getRange(cellLocation)
        value = cell.getValue();
        sum += parseFloat(value); // if has decimal
        // sum += parseInt(value); // if has whole numbers (no decimal)
    })
    Logger.log(sum);
}

0
投票

我在 NighEye 的脚本没有将值传回我的电子表格时遇到了同样的问题。代替

Logger.log(sum);
我添加这个以将值传递回我的电子表格:
SpreadsheetApp.getActive().getRange("Sheet1!L1").setValue(sum);

© www.soinside.com 2019 - 2024. All rights reserved.