简体   繁体   English

如何计算 ArrayFormula 中每行的最后 7 行的总和?

[英]How to I calculate the sum of the last 7 rows, for each row, inside an ArrayFormula?

I have a column D with numerical values and on an adjacent column, I want to display the sum for the last 7 rows relative to the current row.我有一个带有数值的 D 列,在相邻的列上,我想显示相对于当前行的最后 7 行的总和。

I've tried a couple of variants, most of them inspired by StackOverflow and other sites but they haven't worked as I need them.我已经尝试了几个变体,其中大部分都受到 StackOverflow 和其他网站的启发,但它们并没有按照我的需要工作。

I figured that since Offset() creates a range it would be simple to just "slice" the range I need and feed it to sum()我认为由于 Offset() 创建了一个范围,因此只需“切片”我需要的范围并将其提供给 sum()

ArrayFormula( Sum(Offset(D7:D500,-6,1,7,1)) )

However, it seems that it only calculates the cell I place this formula in.但是,它似乎只计算我放置这个公式的单元格。

I adapted a potential solution I found online to my situation我根据我的情况改编了我在网上找到的潜在解决方案

ArrayFormula(SUMIF(ROW(D7:D500),"<="&ROW(D7:D500),D7:D500))

This one calculates the sum for all the previous rows, so I figured using SUMIFS() would get me what I need, therefore这个计算所有前一行的总和,所以我想使用 SUMIFS() 会得到我需要的东西,因此

=ArrayFormula(SUMIFS(D7:D500, ROW(D7:D500),"<="&ROW(D7:D500), ROW(D7:D500),">"&ROW(D7:D500)-7))

But this one just doesn't work.但这个是行不通的。 I've also tried creating the range with Address() and Interpret() but it seems they don't work properly inside ArrayFormula()我也尝试使用 Address() 和 Interpret() 创建范围,但它们似乎在 ArrayFormula() 中无法正常工作

I'm fairly new at it so it might just be that I'm not using them properly.我对它相当陌生,所以可能只是我没有正确使用它们。 What am I doing wrong here?我在这里做错了什么? Is there a better way of doing this?有没有更好的方法来做到这一点? I would rather use ArrayFormula() if possible since the formula will live in a single cell.如果可能,我宁愿使用 ArrayFormula(),因为公式将存在于单个单元格中。

method 1. OFFSET function方法一、 OFFSET函数

COUNT(D7:D500)-7 tells EXCEL how many cells to step down from D7 . COUNT(D7:D500)-7告诉 EXCEL 从D7减少多少个单元格。

=SUM(OFFSET(D7:D500,COUNT(D7:D500)-7,,7,1))

no Ctrl + Shift + Enter没有Ctrl + Shift + Enter

method 2. Index function方法二、 Index函数

If you concern the volatileness of the OFFSET function, you may try如果您担心OFFSET函数的波动性,您可以尝试

=SUM(INDEX(D7:D500,COUNT(D7:D500)-6):INDEX(D7:D500,COUNT(D7:D500)))

no Ctrl + Shift + Enter没有Ctrl + Shift + Enter

try:尝试:

=SUM(IFERROR(QUERY(D7:D500, "limit 7 offset "&COUNTA(D7:D500)-7)))

0

You can accomplish this via a custom function created in Google Apps Script.您可以通过在 Google Apps 脚本中创建的自定义函数来完成此操作。 To achieve this, follow these steps:为此,请执行以下步骤:

  • In your spreadsheet, select Tools > Script editor to open a script bound to your file.在电子表格中,选择工具 > 脚本编辑器以打开绑定到文件的脚本。
  • Copy this function in the script editor, and save the project:在脚本编辑器中复制此函数,并保存项目:
function SUM7ROWS(input) {
  var output = [];
  var sum;
  for (var row = 0; row < input.length; row++) {
    sum = 0;
    for (var i = 6; i >= 0; i--) {
      if (row - i >= 0) {
        sum = sum + Number(input[row - i]);
      }
    }
    output.push(sum);
  }
  return output;
}
  • Now, if you go back to your spreadsheet, you can use this function just as you would do with any other function.现在,如果您返回到您的电子表格,您可以像使用任何其他函数一样使用此函数。 You just have to provide the appropriate range as a parameter (in this case it would be D7:D500 ), as you can see here:您只需要提供适当的范围作为参数(在本例中为D7:D500 ),如下所示:

在此处输入图片说明

I hope this is of any help.我希望这有任何帮助。

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

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