简体   繁体   English

如何使用arrayformula“textjoin”(包括忽略空单元格)?

[英]How to "textjoin" with arrayformula (including ignoring empty cells)?

I love textjoin because of the "ignore empty cells" capability, it's really clean.我喜欢 textjoin 因为“忽略空单元格”功能,它真的很干净。 However, it won't work with ArrayFormula to auto-populate a column for me.但是,它不能与 ArrayFormula 一起为我自动填充列。 So it seems a messier workaround is needed.因此,似乎需要一个更混乱的解决方法。 This is what I have come up with.这就是我想出的。 Does anyone know of a simpler solution?有谁知道更简单的解决方案?

Formula in cell A2 should combine values in cells B, C, & D with a delimiter, and not have any extra delimiters like leading or trailing _s, or _ _ in the middle.单元格 A2 中的公式应将单元格 B、C 和 D 中的值与分隔符组合在一起,并且不能有任何额外的分隔符,如前导或尾随 _s 或中间的 _。

+----------------------+------------+------+-----------+
|       NiceName       | first_name | year |  country  |
+----------------------+------------+------+-----------+
| Kaylee_1999          | Kaylee     | 1999 |           |
| Selma_2003_Indonesia | Selma      | 2003 | Indonesia |
| Babbette_Morocco     | Babbette   |      | Morocco   |
| Erhart_1985_Japan    | Erhart     | 1985 | Japan     |
| 1997_France          |            | 1997 | France    |
| Derward_1985_China   | Derward    | 1985 | China     |
+----------------------+------------+------+-----------+


=arrayformula(substitute(if(isblank(B2:B),,B2:B&"_")&if(isblank(C2:C),,C2:C)&if(isblank(D2:D),,"_"&D2:D),"__","_"))

screenshot of data & formula数据和公式截图

For your current set of data try对于您当前的数据集尝试

=ArrayFormula(substitute(trim(transpose(query(transpose(B2:D),,rows(B2:B)))), " ", "_"))

and see if that works?看看这是否有效?

例子

Here's a solution to deal with double first names if they crop up (or Country names with a space).如果出现双重名字(或带有空格的国家名称),这是处理双重名字的解决方案。 Cell A2 :单元格A2

=arrayformula(regexreplace({B2:B&if(C2:C<>"","_"&C2:C&"_","_")&D2:D},"^\_|\_$",))

在此处输入图像描述

I would also recommend adding an array_constrain to prevent the formula working all the way down the sheet.我还建议添加一个array_constrain以防止公式一直在工作表中起作用。

=arrayformula(array_constrain(regexreplace({B2:B&if(C2:C<>"","_"&C2:C&"_","_")&D2:D},"^\_|\_$",),MAX(IF(B:D<>"",ROW(B:B))),1))

Also, to locate the formula in cell A1 , with a heading, you can use:此外,要在单元格A1中找到带有标题的公式,您可以使用:

=arrayformula(array_constrain({"NiceName";regexreplace({B2:B&if(C2:C<>"","_"&C2:C&"_","_")&D2:D},"^\_|\_$",)},MAX(IF(B:D<>"",ROW(B:B))),1))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM