Readjust four column values by ignoring blank cells (Google Sheets)

断了今生、忘了曾经 提交于 2021-02-17 05:43:22

问题


I have this values:

And I would like the formula to ignore blank cells, with the final result in the following sequence:

I tried to use the QUERY + TRANSPOSE, but I couldn't adjust it as I need it, I failed to try to include IF to remove the blank cells with IF(A1:D="" and continue QUERY, but it also failed.

Failed test:

=ArrayFormula(query(trim(split(transpose(query(transpose(A1:D),,999^99))," ",true,true)), "where Col1 <> '' "))

回答1:


How about this modified formula?

Modified formula:

=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",TRUE,A1:D),"(([\w\s]+,){3}[\w\s]+)","$1@"),"@")),",")))
  • The flow of this formula is as follows.
    1. Join all cell values by ignoring the empty cells using TEXTJOIN.
    2. Put @ to the joined text value for 4 columns using REGEXREPLACE.
    3. Split the text value with @ using SPLIT.
    4. Transpose the splitted values using TRANSPOSE.
    5. Split the each row with , using SPLIT.

Result:

Note:

  • If , and @ are included in the cell values, please change them in the formula.

References:

  • TEXTJOIN
  • REGEXREPLACE
  • SPLIT
  • TRANSPOSE

Added 1:

About I guess this will fail, if cell values are more than 1 character. of TheMaster's comment, I tested it as follows.

Added 2:

For OP's new situation, I added the below modified formula. In this case, the regex is modified.

Modified formula:

=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",TRUE,A1:D),"(([\w\s\S]+?,){4})","$1@"),"@")),",")))

Result:



来源:https://stackoverflow.com/questions/64165736/readjust-four-column-values-by-ignoring-blank-cells-google-sheets

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