如何计算Google表格中2个非空单元格之间的行及其中间的空单元格?

时间:2019-05-28 11:00:49

标签: google-sheets

我正在尝试计算两个非空单元格之间的空单元格的每一行,并在空单元格/行数为时将高非空单元格计数(或低非空单元格计数)添加到结果中可变且不规则的方式:

A1 Empty
A2 Non-empty
A3 Empty
A4 Empty
A5 Empty
A6 Empty
A7 Non-empty

(A2到A5不规则-可以是任意数量的中间空行/单元格,即10、2、30、5等,并且变量-可以位于A列内的任何位置)

结果应返回如下:

=IFS(

AND(A2="",A3=""),"",

AND(A2="",A3<>""),"",

AND(A2<>"",A3<>""),ROWS(A2),

AND(A2<>"",A3=""),ROWS(A2:UP TO NEXT NON-EMPTY ROW BENEATH (NOT COMPRISED (or minus the next non-empty row(-1)))

当空的中间行的计数是可变的/未知的时,我们如何对Google表格进行编程以对行进行计数,直到“下一个非空行(不包括(或减去下一个空的行(-1))”)发生和不规则/中间行可以有任意数量吗?

我以以下数据为例:

enter image description here

到目前为止,我已经提出了部分解决方案:

=IFS(AND(A2="",A3=""),"", AND(A2="",A3<>""),"", AND(A2<>"",A3<>""),ROWS(A2), AND(A2<>"",A3=""),ROWS(A2:UP TO NEXT NON-EMPTY ROW BENEATH (NOT COMPRISED (or minus the next non-empty row(-1)))))

enter image description here

预期结果应返回如下:

enter image description here

工作表副本: https://docs.google.com/spreadsheets/d/1eLXdFINKVtS5Fxon8mIQDmBUTZokyY_Fb6tfWJRDOVI/edit?usp=sharing

3 个答案:

答案 0 :(得分:1)

好吧,我认为您可以使用反向查询更优雅地做到这一点。想法是按行号的降序对数据进行排序,然后使用匹配项-1作为最后一个参数,这等效于从数据末尾搜索下一个非空白。

=ArrayFormula(iferror(if(A2:A="","",rows(A2:A)+2-match(row(A2:A)+1,
query(SORT({IF(A2:A<>"",row(A2:A),""),row(A2:A)},2,false),"select Col1"),-1)-row(A2:A)),1))

enter image description here

如果您想保持简单,只使用下拉公式,那么普通匹配就足够了:

=ArrayFormula(iferror(if(A2="","",match(true,A3:A<>"",0)),1))

(由于A3:A <>“”是True和False的数组,因此仍必须作为数组公式输入)

答案 1 :(得分:0)

我找到了一个具有Monster公式但具有简单迭代逻辑和最多50个中间空格的解决方案(更多的只需要使公式变大即可。)

我要寻找的只是打印2个非空单元格之间的中间空单元格的单元格/行数,然后加上第一个非空单元格或最后一个非空单元格。

例如,如果单元格A1和A9之间有8个空单元格,则返回8 + 1(或10-1)。

使用的逻辑很简单: 探查A列中每2个非空单元之间的计数第一个非空单元之间可能有多少个空单元(在下面的公式中,最多可能有50个空单元),并针对每种情况返回中间行-1(最后一行)。

公式:

=IFS(AND(A1="",A2=""),"",AND(A1="",A2<>""),"",AND(A1<>"",A2<>""),ROWS(A1),AND(A1<>"",A2="",A3<>""),ROWS(A1:A3)-1,AND(A1<>"",A2="",A3="",A4<>""),ROWS(A1:A4)-1,AND(A1<>"",A2="",A3="",A4="",A5<>""),ROWS(A1:A5)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6<>""),ROWS(A1:A6)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7<>""),ROWS(A1:A7)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8<>""),ROWS(A1:A8)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9<>""),ROWS(A1:A9)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10<>""),ROWS(A1:A10)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11<>""),ROWS(A1:A11)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12<>""),ROWS(A1:A12)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13<>""),ROWS(A1:A13)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14<>""),ROWS(A1:A14)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15<>""),ROWS(A1:A15)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16<>""),ROWS(A1:A16)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17<>""),ROWS(A1:A17)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18<>""),ROWS(A1:A18)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19<>""),ROWS(A1:A19)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20<>""),ROWS(A1:A20)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21<>""),ROWS(A1:A21)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22<>""),ROWS(A1:A22)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23<>""),ROWS(A1:A23)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24<>""),ROWS(A1:A24)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25<>""),ROWS(A1:A25)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26<>""),ROWS(A1:A26)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27<>""),ROWS(A1:A27)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28<>""),ROWS(A1:A28)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29<>""),ROWS(A1:A29)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30<>""),ROWS(A1:A30)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31<>""),ROWS(A1:A31)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32<>""),ROWS(A1:A32)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33<>""),ROWS(A1:A33)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34<>""),ROWS(A1:A34)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35<>""),ROWS(A1:A35)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36<>""),ROWS(A1:A36)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37<>""),ROWS(A1:A37)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38<>""),ROWS(A1:A38)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39<>""),ROWS(A1:A39)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40<>""),ROWS(A1:A40)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41<>""),ROWS(A1:A41)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42<>""),ROWS(A1:A42)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43<>""),ROWS(A1:A43)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44<>""),ROWS(A1:A44)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45<>""),ROWS(A1:A45)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46<>""),ROWS(A1:A46)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47<>""),ROWS(A1:A47)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48<>""),ROWS(A1:A48)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48="",A49<>""),ROWS(A1:A49)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48="",A49="",A50<>""),ROWS(A1:A50)-1)

以下是结果的图片表:

enter image description here

enter image description here

The Sheet

可以用一种更简单的方法来完成,但仍需一个公式吗?

非常感谢您的下一次输入!

答案 2 :(得分:0)

同时,我对以前的Monster公式进行了一些简化,并在此过程中学习了ArrayFormula应用程序。

首先,我试图将大空白单元格的间隔缩短为范围公式,以查看其是否仍然有效。

例如,我更改了:

AND(A1<>"",A2="",A3="",A4<>""),ROWS(A1:A4)-1,

进入

AND(A1<>"",A2:A3="",A4<>""),ROWS(A1:A4)-1,

这使我的公式缩短了:

enter image description here

对此:

enter image description here

但是在粘贴新公式时,我得到了#VALUE!错误“找不到数组值”。

经过进一步的测试,我使用了ArrayFormula,并通过将以前用于Arrays的简单“范围”进行了调整来工作:

从此

AND(A1<>"",A2:A3="",A4<>""),ROWS(A1:A4)-1,

进入此

AND(A1<>"",ArrayFormula(A2:A3=""),A4<>""),ROWS(A1:A4)-1,

最终结果:

=IFS(AND(A1="",A2=""),"",AND(A1="",A2<>""),"",AND(A1<>"",A2<>""),ROWS(A1),AND(A1<>"",A2="",A3<>""),ROWS(A1:A3)-1,AND(A1<>"",ArrayFormula(A2:A3=""),A4<>""),ROWS(A1:A4)-1,AND(A1<>"",ArrayFormula(A2:A4=""),A5<>""),ROWS(A1:A5)-1,AND(A1<>"",ArrayFormula(A2:A5=""),A6<>""),ROWS(A1:A6)-1,AND(A1<>"",ArrayFormula(A2:A6=""),A7<>""),ROWS(A1:A7)-1,AND(A1<>"",ArrayFormula(A2:A7=""),A8<>""),ROWS(A1:A8)-1,AND(A1<>"",ArrayFormula(A2:A8=""),A9<>""),ROWS(A1:A9)-1,AND(A1<>"",ArrayFormula(A2:A9=""),A10<>""),ROWS(A1:A10)-1,AND(A1<>"",ArrayFormula(A2:A10=""),A11<>""),ROWS(A1:A11)-1,AND(A1<>"",ArrayFormula(A2:A11=""),A12<>""),ROWS(A1:A12)-1,AND(A1<>"",ArrayFormula(A2:A12=""),A13<>""),ROWS(A1:A13)-1,AND(A1<>"",ArrayFormula(A2:A13=""),A14<>""),ROWS(A1:A14)-1,AND(A1<>"",ArrayFormula(A2:A14=""),A15<>""),ROWS(A1:A15)-1,AND(A1<>"",ArrayFormula(A2:A15=""),A16<>""),ROWS(A1:A16)-1,AND(A1<>"",ArrayFormula(A2:A16=""),A17<>""),ROWS(A1:A17)-1,AND(A1<>"",ArrayFormula(A2:A17=""),A18<>""),ROWS(A1:A18)-1,AND(A1<>"",ArrayFormula(A2:A18=""),A19<>""),ROWS(A1:A19)-1,AND(A1<>"",ArrayFormula(A2:A19=""),A20<>""),ROWS(A1:A20)-1,AND(A1<>"",ArrayFormula(A2:A20=""),A21<>""),ROWS(A1:A21)-1,AND(A1<>"",ArrayFormula(A2:A21=""),A22<>""),ROWS(A1:A22)-1,AND(A1<>"",ArrayFormula(A2:A22=""),A23<>""),ROWS(A1:A23)-1,AND(A1<>"",ArrayFormula(A2:A23=""),A24<>""),ROWS(A1:A24)-1,AND(A1<>"",ArrayFormula(A2:A24=""),A25<>""),ROWS(A1:A25)-1,AND(A1<>"",ArrayFormula(A2:A25=""),A26<>""),ROWS(A1:A26)-1,AND(A1<>"",ArrayFormula(A2:A26=""),A27<>""),ROWS(A1:A27)-1,AND(A1<>"",ArrayFormula(A2:A27=""),A28<>""),ROWS(A1:A28)-1,AND(A1<>"",ArrayFormula(A2:A28=""),A29<>""),ROWS(A1:A29)-1,AND(A1<>"",ArrayFormula(A2:A29=""),A30<>""),ROWS(A1:A30)-1,AND(A1<>"",ArrayFormula(A2:A30=""),A31<>""),ROWS(A1:A31)-1,AND(A1<>"",ArrayFormula(A2:A31=""),A32<>""),ROWS(A1:A32)-1,AND(A1<>"",ArrayFormula(A2:A32=""),A33<>""),ROWS(A1:A33)-1,AND(A1<>"",ArrayFormula(A2:A33=""),A34<>""),ROWS(A1:A34)-1,AND(A1<>"",ArrayFormula(A2:A34=""),A35<>""),ROWS(A1:A35)-1,AND(A1<>"",ArrayFormula(A2:A35=""),A36<>""),ROWS(A1:A36)-1,AND(A1<>"",ArrayFormula(A2:A36=""),A37<>""),ROWS(A1:A37)-1,AND(A1<>"",ArrayFormula(A2:A37=""),A38<>""),ROWS(A1:A38)-1,AND(A1<>"",ArrayFormula(A2:A38=""),A39<>""),ROWS(A1:A39)-1,AND(A1<>"",ArrayFormula(A2:A39=""),A40<>""),ROWS(A1:A40)-1,AND(A1<>"",ArrayFormula(A2:A40=""),A41<>""),ROWS(A1:A41)-1,AND(A1<>"",ArrayFormula(A2:A41=""),A42<>""),ROWS(A1:A42)-1,AND(A1<>"",ArrayFormula(A2:A42=""),A43<>""),ROWS(A1:A43)-1,AND(A1<>"",ArrayFormula(A2:A43=""),A44<>""),ROWS(A1:A44)-1,AND(A1<>"",ArrayFormula(A2:A44=""),A45<>""),ROWS(A1:A45)-1,AND(A1<>"",ArrayFormula(A2:A45=""),A46<>""),ROWS(A1:A46)-1,AND(A1<>"",ArrayFormula(A2:A46=""),A47<>""),ROWS(A1:A47)-1,AND(A1<>"",ArrayFormula(A2:A47=""),A48<>""),ROWS(A1:A48)-1, AND(A1<>"",ArrayFormula(A2:A48=""),A49<>""),ROWS(A1:A49)-1, AND(A1<>"",ArrayFormula(A2:A49=""),A50<>""),ROWS(A1:A50)-1)

enter image description here

New Sheet here