Google Sheets 查询总计列数

Google Sheets query sum counted columns

我正在尝试对所有匹配正则表达式模式的单元格进行计数。到目前为止,我已经想出了一个解决方案,每列使用 query,然后使用 SUM() 对不同列的计数求和。

这相当笨拙,查询中的更改意味着我需要更改大量单元格和部分公式。理想情况下,我有一个解决方案可以对查询中的计数求和,或者其他更易于维护的解决方案。

我现在拥有的:

=sum(
  query(I10:I19,"select count(I) where I matches '^[A-Za-z\s]{4,64}$' label count(I) ''"),
  query(J10:J19,"select count(J) where J matches '^[A-Za-z\s]{4,64}$' label count(J) ''"),
  query(K10:K19,"select count(K) where K matches '^[A-Za-z\s]{4,64}$' label count(K) ''"),
  [etc. etc.]
)

我最理想的是:

=query(I10:L19,"select sum(count(J),count(K),count(L)) where L matches '^[A-Za-z0-9\s\-]{4,64}$' OR J matches '^[A-Za-z0-9\s\-]{4,64}$' OR K matches '^[A-Za-z0-9\s\-]{4,64}$' label sum(*) ''")

但似乎不支持在 sum() 中嵌套 count()

有没有办法实现这样的目标?

注意label count(X) '' 是去掉任何标签,header,结果放在第一个单元格。

您或许可以用完全不同的公式替换您的查询。

请试试下面的

=ArrayFormula(COUNTIF(REGEXMATCH(I10:K19,"^[A-Za-z\s]{4,64}$"),true))

如果您真的坚持使用查询,请尝试以下操作

=query(flatten(I10:K19),"select count(Col1) where Col1 matches '^[A-Za-z\s]{4,64}$' label count(Col1) ''")

(正则表达式只是复制你分享的,如果还有需要,请分享测试sheet。)

使用的函数:

您可以使用字符串连接

查询只是一个字符串,因此如果您的正则表达式位于名为“Regex”的 sheet 中并且在单元格 A1 中,值为 ="'^[A-Za-z\s]{4,64}$'"

=sum(
  query(I10:I19,"select count(I) where I matches" & Regex!A1 & "label count(I) ''"),
  query(J10:J19,"select count(J) where J matches" & Regex!A1 & "label count(J) ''"),
  query(K10:K19,"select count(K) where K matches" & Regex!A1 & "label count(K) ''"),
  [etc. etc.]
)

参考

The value for query must either be enclosed in quotation marks or be a reference to a cell containing the appropriate text.