Google 工作表 - 计算 arrayformula 中值之间的行

Google sheets - Numerate rows between values in arrayformula

我有一个数组公式,它根据在一个范围内找到单词“foo”对“章节”进行编号。我还需要它为这些章节的“子章节”编号,从 1 开始并为每一章重新设置。

有点难以解释,但它是这样的:

What I have
1 foo


2 foo



3 foo
What I need
1 foo
1
2
2 foo
1
2
3
3 foo

到目前为止我发现有效的数组公式是这样的:

=ARRAYFORMULA(IF(C:C="foo",COUNTIFS(ROW(C:C),"<="&ROW(C:C),C:C,"foo"),""))

https://docs.google.com/spreadsheets/d/1ckKTsbJ7xV0SCA7tUZ8J2hB30YpTByIBPDk9OKBE_AU/edit?usp=sharing

使用:

=ARRAYFORMULA(ARRAY_CONSTRAIN({IF(B2:B="", COUNTIFS(
 VLOOKUP(ROW(B2:B), IF(B2:B<>"", {ROW(B2:B), B2:B}), 2, 1), 
 VLOOKUP(ROW(B2:B), IF(B2:B<>"", {ROW(B2:B), B2:B}), 2, 1), 
 ROW(B2:B), "<="&ROW(B2:B))-1, B2:B), C2:C}, 
 MAX((B2:B<>"")*(ROW(B2:B)))-1, 2))