问题
Stumbled upon a strange behavior. This formula:
=ARRAYFORMULA(ROW($A$1:$E$5))
Returns a 5 element column with the row numbers from 1 to 5. I thought that ARRAYFORMULA
would iterate through every cell in the range specified and execute the ROW()
on it - resulting in a new range of the same size (5 columns, 5 rows), but with the row numbers in cells.
For example =ARRAYFORMULA(ISBLANK($A$1:$E$5))
returns a 5 on 5 range.
I found a way to force the desired behavior (see the answer below), but is there a better way? Am I missing something?
回答1:
I think a lot of people use:
=ARRAYFORMULA(IF(COLUMN(A1:E5),ROW(A1:E5)))
There are a lot of ways to do it, but almost all will involve using two refs.
This is about as small as I could get it. Depends on what your inputs look like as to whether this would make sense to use.
=ARRAYFORMULA(SEQUENCE(1,5,0,0)+SEQUENCE(5))
回答2:
not rly much to say here just that ROW
and COLUMN
are only one dimensional. in other words, ROW
will work only with one single column and COLUMN
will work only with one single row - no matter that you feed it with range A1:Z or A1:A
as mentioned you can use MattKing's SEQUENCE
to generate the matrix
or shorter:
=ARRAYFORMULA(ROW(A1:A5)*COLUMN(A:E)^0)
or shorter:
=INDEX(ROW(A1:A5)*COLUMN(A:E)^0)
or shorter:
=INDEX(ROW(A1:A5)*{1,1,1,1,1})
回答3:
Here is a workaround for you, man:
=ARRAYFORMULA(ROW($A$1:$E$5) + 0 * ISBLANK(A1:E5))
0 * ISBLANK(A1:E5)
part is there just to force the desired behavior without modifying (see that multiplication by 0
?) the values you need.
来源:https://stackoverflow.com/questions/62432752/how-to-get-a-matrix-with-the-row-numbers-for-every-cell-of-the-original-matrix