Combine Text in ArrayFormula

 ̄綄美尐妖づ 提交于 2021-01-27 16:21:16

问题


I have a table using Google Sheets. It has three columns that will always have a null value or a specific value for that column. Each line will have one, two, or three values; it will never have three null values on one line. In the fourth column, I want an ArrayFormula that will combine those values and separate the values with a comma if there is more than one.

Here is a photo of what I am trying to accomplish.

I've tried several ideas so far and this formula is the closest I've gotten so far but it's still not quite working correctly; I think it is treating each column as an array before joining rather than doing the function line by line. I'm using the LEN function rather than A2="" or ISBLANK(A2) because columns A-C are ArrayFormulas as well. I realize this probably isn't the most efficient formula to use but I think it covers every possibility. I'm definitely open to other ideas as well.

={"Focus";
  ArayFormula(
    IFS(
      $A$2:$A="", "",
      (LEN(A2:A)>0 & LEN(B2:B)>0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, A2:A, B2:B, C2:C),
      (LEN(A2:A)>0 & LEN(B2:B)>0 & LEN(C2:C)=0), TEXTJOIN(", ", TRUE, A2:A, B2:B),
      (LEN(A2:A)>0 & LEN(B2:B)=0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, A2:A, C2:C),
      (LEN(A2:A)=0 & LEN(B2:B)>0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, B2:B, C2:C),
      (LEN(A2:A)>0 & LEN(B2:B)=0 & LEN(C2:C)=0), A2:A,
      (LEN(A2:A)=0 & LEN(B2:B)>0 & LEN(C2:C)=0), B2:B,
      (LEN(A2:A)=0 & LEN(B2:B)=0 & LEN(C2:C)>0), C2:C
    )
  )
}

Is it possible to achieve this with Google Sheets?


回答1:


Sample File

Please try:

=ARRAYFORMULA(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(A2:C,ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99)))," ",", "))

Notes:

  • The formula will work incorrectly if some names have space inside: like "Aston Martin"
  • So if you have spaces, please try this:

=ARRAYFORMULA(SUBSTITUTE( SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(SUBSTITUTE(A2:C," ",char(9)),ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99)))," ",", "), CHAR(9)," "))

EDIT

Noticed the shorter variant (without *COLUMN(A2:C)^0) will work:

=ARRAYFORMULA(SUBSTITUTE( SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(SUBSTITUTE(A2:C," ",char(9)),ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C),0)))),,2^99)))," ",", "), CHAR(9)," "))

Notes:

  • I used an old trick to join strings with an array-formula. See sample file

Explanations

If you like to understand any tiered formula, the best way is to split it by parts:

Part 1. Filter the data

  1. FILTER(any_columns,ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0))). this is my way to limit the data range.
  2. The range is open, means it starts from the second row (A2) and ends in any row.
  3. I want to get the limited array in this step to reduce work that the formula should do. This is done with a condition, if.
  4. ROW(A2:C) must be less or equal to the max row of data. MAX(IF(LEN(A2:C), some_rows) gives the max row.
  5. If(len.. part checks if a cell has some text inside it.
  6. Note some_rows part: MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99))). ROW(A2:C) must be multiplied by columns, because filter formula takes only one row into its condition. That is why I multiply by COLUMN(A2:C)^0 which is columns with 1s. Edit. Now noticed, that the formula works fine without *COLUMN(A2:C)^0, so it's an overkill.

Part 2. Join the text

  1. query formula has 3 arguments: data, query_text, and a number_of_header_rows.

  2. data is made with a filter.

  3. query_text is empty, which gives us equivalent to select all ("select *").

  4. And the number of rows of a header is some big number (2^99). This is a trick: when a query has more headers then one row, it will join them with space.

  5. After a union is made, transpose function will convert the result back to the column.

Part 3. Substitute and trim

  1. The function trim deletes extra spaces.

  2. Then we replace spaces with the delimiter: ", ". That is why the formula needs to be modified if spaces are in strings. Correct result: "Ford, Aston Martin". Incorrect: "Ford, Aston, Martin". But if we previously replace spaces with some char (char(9) is Tab), then we do not replace it in this step.



来源:https://stackoverflow.com/questions/48456012/combine-text-in-arrayformula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!