Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Links are given to the docs; however, for an example of how to use ARRAYFORMULA see this answer.

An example can be seen below:

enter image description here

I was able to populate the C column by placing the following formula in C1:

=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.

  • How does the above line work?
  • Can you illustrate with some examples?
like image 282
mbigras Avatar asked Mar 06 '17 23:03

mbigras


People also ask

How to use the arrayformula in Google Sheets?

How To Use The ARRAYFORMULA in Google Sheets In order to work with it, you need to change the single cell references in the original function. Instead of cell references, you would have to pass column or row references. As an example, let’s see how can you use an ARRAYFORMULA to copy a formula down an entire column.

How do you apply a formula across a row in Google Sheets?

ARRAYFORMULA in Google Sheets You rarely want to apply a formula to a single cell. Usually, you want to use them across a row or column and apply them to many cells. One solution is dragging the formula down to apply it to the whole column.

How do I apply a formula to an entire column?

The ARRAYFORMULA function can apply a formula to an entire column. It converts your original formula into an array, thus allowing you to use the same formula across multiple rows by writing only a single formula. You only need to put a formula in the first cell and define the size of the array.

How to move an array formula to the header line?

If you put the array formula in line 2, and someone sorts the data, then the arrayformula will move. 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, Here the first condition checks the row number, and if it is row ONE, then inserts a Column Header.


1 Answers

It sounds to me that the information you learned led you to expect that row(A:A)=1 translates to row 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 B-A

My suggestion:

use a literal array to make your header, then use the if(arrayformula) to only populate rows with values, for aesthetics:

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.

enter image description here

like image 87
Aurielle Perlmann Avatar answered Sep 22 '22 20:09

Aurielle Perlmann