I was working on a Google Spreadsheet today, and I needed to generate a list of integers to use in conjunction with a built in function. I actually needed to find the gradient of different (one-dimensional) array.
The problem was, the "SLOPE" function built into Spreadsheets requires both x and y data (at least at the time of writing). More info on that can be found on their official help page for SLOPE. However, I just had one set of data (i.e. just the y values), and I didn't want to display a list of integers in the spreadsheet, just for aesthetic reasons.
On this note it would have been really useful if the y-data argument supplied to SLOPE could have been optional (perhaps defaulting to [1, 2, 3, ... ] if only one array is supplied?). It seems there is no way of doing this using SLOPE so I needed to generate a list of integers, on the fly, with exactly the same number of elements as there are in the array I am supplying as y data.
It turns out this is actually quite hard to do in Google Spreadsheets - there is certainly no built-in way to do it. After googling the problem for a while I still couldn't really find a good way to do it, but I did come up with something a bit hacky as a workaround. Seeing as I failed to do it any other way, I thought I would post my method here!
How to generate a list of integers
Here is what I used to generate a list of integers from 1 to 10:
ARRAY_CONSTRAIN(ARRAYFORMULA(ROW(INDEX(A1:A))), 10,1)
Essentially what the formula does is to use the row numbers in the spreadsheet itself, and generates a list of integers from that. In other words, it converts [A1, A2, A3, ... ] into [1, 2, 3, ... ]. If you change the '10' supplied in the formula above, it will change the length of the list of integers.
The one drawback to this method is that it will only generate a maximum length equal to the number of rows in your spreadsheet. If there are only 30 rows in your spreadsheet then it will only be able to create a list of integers up to 30. If you run into this problem, then you may be able to use the columns instead of the rows, using the column numbers to source the list of integers instead:
ARRAY_CONSTRAIN(ARRAYFORMULA(COLUMN(INDEX(A1:1))), 1,10)
Again, you can change the '10' in the formula above to whatever number you need.
Extra tips:
- Use the TRANSPOSE function if you need to change the orientation of the array generated. This will change a vertical list (i.e. one that fills several rows in one column) into a horizontal list (i.e. one that displays within one row).
- Use the COUNT function if you need to make your list of integers match the length of another array. Replace the '10' (in the code above) with COUNT(requiredarray). I actually need to use this in the sheet that prompted this whole blog post. I needed to create an array of integers to act as x-values, to match a variable array of y-values, in order to find the trend (the gradient) of those y-values.
Anyway, that's it! I hope this is useful to somebody out there.
