[英]Google Sheets transform variable width/height sheet into list with ARRAYFORMULA
I can't for the life of me figure out how to create a generalized formula to convert the following: 我一辈子都想不通如何创建一个通用公式来转换以下内容:
Name | cool | smart | funny | ... |
Bill | | x | x | |
Sally | x | x | | |
Bob | x | | x | |
Cindy | | | | |
Steve | x | | x | |
Carol | | | x | |
Bob | | x | | |
... | | | | |
Into something like this: 变成这样的东西:
Bill:smart,funny
Sally:cool,smart
Bob:cool,funny
Cindy:
Steve:cool,funny
Carol:funny
Bob:smart
I'd like it to be able to work for an arbitrary number of columns and rows. 我希望它能够处理任意数量的列和行。 Is there any way to achieve this without the need for "dragging?" 有什么方法可以实现而无需“拖动”? Ie with ARRAYFORMULA
s. 即与ARRAYFORMULA
一起ARRAYFORMULA
。
Here is a link to an example Google Sheet. 这里是示例Google表格的链接 。
Well, this is a bear of a hack, but it works. 好吧,这是一个骇人听闻的做法,但它确实有效。 It will work for arbitrary rows and arbitrary columns, without any formula dragging. 它适用于任意行和任意列,而无需拖动任何公式。
=ARRAYFORMULA(REGEXREPLACE(REGEXREPLACE(TRANSPOSE(SPLIT(CONCATENATE({Data!A2:A&":", REGEXREPLACE(Data!B2:O, "x", REGEXREPLACE(Data!B1:O1, "$", ",")), IF(LEN(Data!A2:A)=LEN(Data!A2:A), "|")}), "|")), ",$", ""), "^:$", ""))
A few caveats: 一些警告:
I put the formula to work - you can view the spreadsheet by clicking here 我使用了公式-您可以通过单击此处查看电子表格
The "Result" sheet has the formula and output that you wanted. “结果”表具有所需的公式和输出。
hope, this works: 希望,这可行:
=ArrayFormula(TRANSPOSE(SPLIT(ArrayFormula(CONCATENATE(A2:A8&": /")),"/"))&SUBSTITUTE(trim(transpose(query(transpose(IFERROR(ArrayFormula(HLOOKUP(REGEXREPLACE(B2:D8,"x","1")*1*COLUMN(B2:D8)*row(B2:D8)^0,{COLUMN(B2:D8);B1:D1},2,0)),"")),,counta(B1:D1))))," ",", "))
my sample in action =) 我的样本在运行 =)
Update 更新
I figured out how to make the formula shorter: 我想出了缩短公式的方法:
=ARRAYFORMULA(TRANSPOSE(SPLIT(ArrayFormula(CONCATENATE(A2:A8&": /")),"/"))&SUBSTITUTE(trim(transpose(query(transpose(REPT(B1:D1,B2:D8="x")),,counta(B1:D1))))," ",", "))
The formula above work's the same. 上面的公式是相同的。 It uses the trick, proposed here . 它使用这里提出的技巧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.