问题
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.
- Join all cell values by ignoring the empty cells using
TEXTJOIN
. - Put
@
to the joined text value for 4 columns usingREGEXREPLACE
. - Split the text value with
@
usingSPLIT
. - Transpose the splitted values using
TRANSPOSE
. - Split the each row with
,
usingSPLIT
.
- Join all cell values by ignoring the empty cells using
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