在 Google 工作表中将二维范围转换为带有空格的一维数组

Converting a 2D range into a 1D array with spaces in Google Sheets

我想像这样转换 range/array

| Fruit     | Strawberry |
| Fruit     | Blueberry  |
| Fruit     | Banana     |
| Vegetable | Lettuce    |
| Vegetable | Cucumber   |
| Vegetable | Carrot     |
| Vegetable | Celery     |
| Dairy     | Milk       |
| Dairy     | Butter     |
| Dairy     | Cheese     |

像这样用空格分隔类别的一维数组

| Fruit      |
| Banana     |
| Blueberry  |
| Strawberry |
|            |
| Vegetable  |
| Carrot     |
| Celery     |
| Cucumber   |
| Lettuce    |
|            |
| Dairy      |
| Butter     |
| Cheese     |
| Milk       |

在 Google 张中。我可以使用 =UNIQUE(FLATTEN(A2:B11)) 轻松实现没有空格的一维数组 其中 A2:B11 是原始数据范围;但是,我无法获得所需的分隔类别的空间。

有人能帮忙吗?

示例如下 sheet:https://docs.google.com/spreadsheets/d/1ww1fkDRBUsVx-pLDJ4Qo9VXlzAZDEyYoANRdRK7Yum4/edit?usp=sharing

这是说明一种可能方法的草稿:

=ArrayFormula(query(regexreplace(unique({"";flatten({A2:B999,if(A2:A999<>A3:A1000,row(A2:A999)&"","")})}),"\d*",""),"offset 1"))

显然需要对没有 1000 行的工作表进行概括:

=ArrayFormula(query(regexreplace(unique({"";flatten({A2:B,if(A2:A<>{A3:A;""},row(A2:A)&"","")})}),"\d*",""),"offset 1"))

尝试:

=INDEX(QUERY(FLATTEN(TRANSPOSE(QUERY({A2:B, ROW(A2:A)},
 "select max(Col2) group by Col3 pivot Col1"))), 
 "where Col1 is not null", ))


更新:

=INDEX(SUBSTITUTE(QUERY(FLATTEN(IFERROR(SPLIT(FLATTEN(TRANSPOSE(
 QUERY({"♦×"&A2:A, B2:B, ROW(A2:A)},
 "select max(Col2) group by Col3 pivot Col1"))), "×"))), 
 "where Col1 is not null offset 2", ), "♦", ))

其他答案都很棒。这也行

=ARRAYFORMULA(
  FLATTEN(
   QUERY(
    QUERY(
     {A2:A,B2:B,COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))},
     "select Col1, Max(Col2)
      where Col2 is not null
      group by Col1
      pivot Col3"),
    "offset 1",0)))

它使用类别的迭代计数创建一个枢轴 table,然后是第二个 QUERY 以丢弃这些数字,然后变平。

再添加一个想法。您的样本 sheet 上有一个名为 MK.Idea 的新选项卡,单元格 D2 中包含此公式。

=ARRAYFORMULA(QUERY(FLATTEN(SPLIT(IF(A2:A11=A1:A10,," |"&A2:A11)&"|"&B2:B11,"|")),"where Col1<>'' offset 1"))