Search code examples
google-sheetssplittransposearray-formulas

Inverse text in cell with SPLIT and ARRAYFORMULA


I have a column with names like this:

Doe, John
Smith, Sam
King, Martin Luther
***

The intent is to put inverse the names and remove the comma. If I use the formula: =REGEXREPLACE(JOIN(" "; SORT(TRANSPOSE(SPLIT(G14;" "));SEQUENCE(COLUMNS(SPLIT(G14;" ")));0)); ",$";)

It works and transforms the name from G14 (Doe, John) to John Doe. The issue is that this is done on over 400 values that change on a day to day basis, so I tried putting it all in an arrayformula: =ArrayFormula(iferror(if(not(ISBLANK(G14:G)); REGEXREPLACE(JOIN(" "; SORT(TRANSPOSE(SPLIT(G14:G;" "));SEQUENCE(COLUMNS(SPLIT(G14:G;" ")));0)); ",$";)); "error"))

Whenever I do this, all the cells retrieve errors, cause by

Function SPLIT parameter 1 value should be non-empty.

I have looked through some answers with the same error, but I cannot figure out how to properly apply them to my case.

=ArrayFormula(iferror(if(not(ISBLANK(G14:G)); REGEXREPLACE(JOIN(" "; SORT(TRANSPOSE(SPLIT(G14:G;" "));SEQUENCE(COLUMNS(SPLIT(G14:G;" ")));0)); ",$";)); "error"))


Solution

  • You may try this if its going to be fixed 2 parameters alike Last name, First name:

    =map(A:A,lambda(z,if(z="",,let(Σ,split(z,", ",0),index(Σ,,2)&" "&index(Σ,,1)))))
    

    this version will work for dynamic number of parameters Last name, Middle Name1, Middle Name2, First name...

    =map(A:A,lambda(z,if(z="",,let(Σ,split(z,", ",0),join(" ",sort(tocol(Σ),sequence(counta(Σ)),0))))))
    

    enter image description here