简体   繁体   English

在 Google 表格中添加带有 ARRAYFORMULA 的标题行

[英]Add title row with ARRAYFORMULA in Google Sheets

I watched a tutorial where the author uses an IF statement along with the ARRAYFORMULA function to add a title row to a column of data.我看过一个教程,其中作者使用IF 语句ARRAYFORMULA 函数将标题行添加到数据列。 Links are given to the docs;链接已提供给文档; however, for an example of how to use ARRAYFORMULA see this answer .但是,有关如何使用ARRAYFORMULA的示例,请参阅此答案

An example can be seen below:一个例子可以在下面看到:

在此处输入图片说明

I was able to populate the C column by placing the following formula in C1 :我能够通过在C1放置以下公式来填充C列:

=ARRAYFORMULA(if(row(A:A) = 1, "spent", B:B - A:A))

I'm confused about the syntax.我对语法感到困惑。 I understand that X:X references the entire X column but I don't understand how it's being used to check if we're at cell A1 in one context and then being used to apply mass formulas in another context.我知道X:X引用了整个X列,但我不明白它是如何被用来检查我们是否在一个上下文中位于单元格A1然后被用来在另一个上下文中应用质量公式的。

  • How does the above line work?上面的行是如何工作的?
  • Can you illustrate with some examples?你能用一些例子来说明吗?

It sounds to me that the information you learned led you to expect that row(A:A)=1 translates to row A1?在我看来,您学到的信息使您期望row(A:A)=1转换为 A1 行?

It works a little different than that, the syntax as your using it now, is basically saying if any row in A:A has a value of 1, then write "spent" else subtract BA它的工作原理与此略有不同,您现在使用它的语法基本上是说如果 A:A 中的任何行的值为 1,则写为“spent”,否则减去 BA

My suggestion:我的建议:

use a literal array to make your header, then use the if(arrayformula) to only populate rows with values, for aesthetics:使用文字数组来制作标题,然后使用 if(arrayformula) 仅使用值填充行,以达到美观:

Example:例子:

={"Spent";arrayformula(if(isnumber(A2:A),B2:B-A2:A,))}

Explanation:解释:

The {} allow you to build a literal array, and using a semicolon instead of a comma allows you to stack your cells vertically, following that we check if there is a value in column A, if so, subtract A from B, else leave it blank. {}允许您构建一个文字数组,使用分号代替逗号允许您垂直堆叠单元格,然后我们检查 A 列中是否有值,如果有,从 B 中减去 A,否则离开它是空白的。

在此处输入图片说明

why not just put the column title directly on the first row cell, and start the array formula from the 2nd row, using the A2:A, B2:B syntax?为什么不直接将列标题放在第一行单元格上,并使用 A2:A, B2:B 语法从第二行开始数组公式?

If something does not have to be in a formula, better put it directly on the cell - simpler for others to understand what's going on, and the formula will be simpler.如果某些内容不必包含在公式中,最好将其直接放在单元格上 - 其他人更容易理解发生了什么,公式也会更简单。

If you put the array formula in line 2, and someone sorts the data, then the arrayformula will move.如果将数组公式放在第 2 行,并且有人对数据进行排序,那么数组公式就会移动。 If it is in the header line, this is less likely to happen.如果它在标题行中,则不太可能发生这种情况。

You can also use the IFS function to achieve a similar effect to the array,也可以使用IFS函数来实现类似数组的效果,

=arrayformula(ifs(row(A1:A)=1,"Spent",A1:A="",,True,B1:B-A1:A)

Here the first condition checks the row number, and if it is row ONE, then inserts a Column Header.这里第一个条件检查行号,如果是第一行,则插入一个列标题。

The Second condition - A1:A="",, - ensures that blank lines are ignored.第二个条件 - A1:A="",, - 确保忽略空行。

The Third condition True (ELSE) performs the calculation.第三个条件True (ELSE) 执行计算。

This method also allows for different calculations to performed on different rows depending on requirements.此方法还允许根据要求对不同的行执行不同的计算。

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

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