简体   繁体   English

对 Google 表格工作簿中选定的工作表中的单元格求和

[英]Sum cells from a selection of sheets in a Google Sheets Workbook

I have created a workbook for tracking inter-store item transfers.我创建了一个工作簿来跟踪店间物品转移。 There are pages for every day of the month.每个月的每一天都有页面。 I want to make a totals page to tally the total transfers on a separate sheet.我想制作一个总计页面以在单独的工作表上计算总转账。 (There are other pages in the workbook that I do not want added to the total.) (工作簿中还有其他页面我不想添加到总数中。)

I am trying to make a script that will take the value of cell A1 on sheets 1 through 31 and put the sum on sheet 32 in cell A1 and likewise for every other cell.我正在尝试制作一个脚本,该脚本将获取工作表 1 到 31 上单元格 A1 的值,并将工作表 32 上的总和放在单元格 A1 中,并且对于每个其他单元格也是如此。

Right now I am using a google sheets add-on called 'Custom Count and Sum' which lets me add up all the pages together like I want but the page names are passed as strings so you cant copy and paste the formula across cells.现在我正在使用一个名为“自定义计数和总和”的谷歌表格插件,它让我可以像我想要的那样将所有页面加在一起,但页面名称作为字符串传递,所以你不能在单元格之间复制和粘贴公式。 I found a workaround for this by using address(row(),column(),4,true) to reference the cell position so I don't have to manually type every cell in. Now I am trying to make it so the values will update without me having to reenter the formula.我通过使用 address(row(),column(),4,true) 来引用单元格位置找到了一个解决方法,这样我就不必手动输入每个单元格了。现在我正在尝试使其值将更新而无需我重新输入公式。 I cant do this using the add-on function I installed because as far as I can tell I need to pass another argument but I cant do this in the add on formula.我不能使用我安装的附加函数来做到这一点,因为据我所知,我需要传递另一个参数,但我不能在附加公式中做到这一点。 I am trying to write my own script and I have tried to use the information in the question below but I am not having any success.我正在尝试编写自己的脚本,并尝试使用以下问题中的信息,但没有成功。

Google Sheets Sum values from ALL sheets in a workbook 来自工作簿中所有工作表的 Google Sheets Sum 值

Any help would be appreciated任何帮助,将不胜感激

Generally it's best to collect all your data for every day, for every month and through all the years all in one tab.通常,最好在一个选项卡中收集每天、每月和所有年份的所有数据。 each item on it's own line with a date, like a database.每个项目在它自己的行上都有一个日期,就像一个数据库。

It's MUCH easier to disaggregate data that's altogether on one tab into different tabs for summing/counting/analysis than it is to aggregate data that is in different tabs onto one.与将不同选项卡中的数据聚合到一个选项卡上相比,将一个选项卡上的所有数据分解到不同选项卡中进行求和/计数/分析要容易得多。

Let's see if this workaround works for you.让我们看看这个解决方法是否适合您。

In this case if you want a function to get all the cells and sum, you should try to match the sheets you want to get your values from.在这种情况下,如果您想要一个函数来获取所有单元格和求和,您应该尝试匹配要从中获取值的工作表。 In your case you could look for all the values that are a number .在您的情况下,您可以查找所有数字值。

/**
 * Sum all the values in the cells/range of all the sheets that match the
 * regular expressión `(\d)+`
 *
 * @param {string} A1Notation - Range in A1Notation that would be sum for all the cells  
 */
function SUM_SHEETS(A1Notation){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();

  var regEx = /\b\d+\b/; // The regEx that will match the sheets

  var result = 0;
  for(var i=0; i < sheets.length; i++){
    var sheet = sheets[i];

    // Check that your sheet is matching your regular Expression
    if( regEx.test(sheet.getName())){

      var range = sheet.getRange(A1Notation);
      var values = range.getValues();

      // Iterate through all the range from all the current sheet
      for(var j=0; j < values.length; j++){
        for(var k=0; k < values[j].length; k++){
          var value = values[j][k];

          // Only sum when the cell is a number
          if (typeof value == "number"){
            result += value;
          }
        }
      }
    }
  }

  return result;
}

Take into consideration that the input parameter is a string representing your range so you can iterate through the sheets.考虑到输入参数是一个表示您的范围的字符串,因此您可以遍历工作表。

第一个值 第二值 值的总和

Also care with the sheets, in this workaround I'm using a regExp that will sum all the values that are a number (only digits).还要注意工作表,在这个解决方法中,我使用了一个 regExp,它将对所有数字(仅数字)的值求和。

When using =SUM_SHEETS(A1Annotation) , what I did to make this work (for the 13 rows I needed), was typing in L1,L2,...,L13 in Column A that would reference the cells from my sheets that I wanted to sum with this function In the Formula Column (B) I replaced the L1 portion of SUM_SHEETS() with the Cell A1 (since I just typed the L1 in Cell A13 ).当使用=SUM_SHEETS(A1Annotation) ,我所做的工作(对于我需要的 13 行)是在 A 列中输入L1,L2,...,L13来引用我想要的工作表中的单元格用这个函数求和 在公式列 (B) 中,我用单元格A1替换了SUM_SHEETS()L1部分(因为我刚刚在单元格A13输入了L1 )。

Example: |示例: | Column A | A列| Column B | B栏| |----------------------|---------------------| |----------------------|-----------------------| | | L1 | L1 | =SUM_SHEETS(A13) | =SUM_SHEETS(A13) | | | L2 | L2 | =SUM_SHEETS(A14) | =SUM_SHEETS(A14) |
| | ... | ... | | |
| | L13 | L13 | =SUM_SHEETS(A25) | =SUM_SHEETS(A25) |

Instead of: |而不是: | Column A | A列| |--------------------| |--------------------| | | =SUM_SHEETS("L1") | =SUM_SHEETS("L1") |
| | =SUM_SHEETS("L2") | =SUM_SHEETS("L2") | | | ... | ... | | | =SUM_SHEETS("L13") | =SUM_SHEETS("L13") |

Lastly, to refresh when I wanted it to, I created a button and a macro to literally, delete the data in cells A13:A25 and then put them right back in.最后,为了在我想要的时候刷新,我创建了一个按钮和一个宏,从字面上删除单元格A13:A25的数据,然后将它们重新放入。

 function Refresh_Summary() { var spreadsheet = SpreadsheetApp.getActive(); spreadsheet.getRange('A13:A25').activate(); spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true}); spreadsheet.getRange('A13').activate(); spreadsheet.getCurrentCell().setValue('L1'); spreadsheet.getRange('A14').activate(); spreadsheet.getCurrentCell().setValue('L2'); spreadsheet.getRange('A15').activate(); spreadsheet.getCurrentCell().setValue('L3'); spreadsheet.getRange('A16').activate(); spreadsheet.getCurrentCell().setValue('L4'); spreadsheet.getRange('A17').activate(); spreadsheet.getCurrentCell().setValue('L5'); spreadsheet.getRange('A18').activate(); spreadsheet.getCurrentCell().setValue('L6'); spreadsheet.getRange('A19').activate(); spreadsheet.getCurrentCell().setValue('L7'); spreadsheet.getRange('A20').activate(); spreadsheet.getCurrentCell().setValue('L8'); spreadsheet.getRange('A21').activate(); spreadsheet.getCurrentCell().setValue('L9'); spreadsheet.getRange('A22').activate(); spreadsheet.getCurrentCell().setValue('L10'); spreadsheet.getRange('A23').activate(); spreadsheet.getCurrentCell().setValue('L11'); spreadsheet.getRange('A24').activate(); spreadsheet.getCurrentCell().setValue('L12'); spreadsheet.getRange('A25').activate(); spreadsheet.getCurrentCell().setValue('L13'); };

表格列的图像

(a bit of a workaround when the SUM(First:Last!L1) would be way faster BUT it works) (当SUM(First:Last!L1)会更快但它可以工作时,这是一种解决方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM