简体   繁体   English

Google表格通过ARRAYFORMULA将可变宽度/高度表转换为列表

[英]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: 一些警告:

  • There must not be any unrelated data below or to the right of the start of the data table (ie except for extra header rows or columns, the data must be in a sheet by itself. 数据表开头下方或右边不得有任何不相关的数据(即,除了多余的标题行或列之外,数据本身必须位于工作表中)。
  • The names and labels must not contain "|", as it's used internally as a split character (this can be changed to another character) 名称和标签不得包含“ |”,因为它在内部用作拆分字符(可以更改为另一个字符)
  • The data can't extend past a given column. 数据不能超出给定列。 In the formula above, it maxes out at column "O", but that can be any arbitrary column (column "ZZ", for example) 在上面的公式中,它在列“ O”处最大化,但是可以是任意列(例如,列“ ZZ”)

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.

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