问题
I want to split a cell, reorder the pieces and recombine.
How do I do that inline?
For example:
| name | reordered name |
| Page,Larry | Larry Page |
| Brin,Sergey | Sergey Brin |
How can I do this?
I know that:
=SPLIT(A2, ",")
will split the values into the next two columns and I know that I can concatenate values with:
=CONCATENATE(A2, " ", B2)
How do I bring these things together in a single formula?
Can I access the values from SPLIT
and reorder them?
I know I can do:
=CONCATENATE(SPLIT(A2))
(but obviously that is stupid).
In Ruby I would just do something like:
def reorder_name(cell)
names = cell.split(",")
"#{names[1]} #{names[0]}"
end
回答1:
Script
In Google Apps Script, a custom function like this would work:
function reorder_name(cell) {
var names = cell.split(",");
return names[1] + ' ' + names[2];
}
Spreadsheet Functions
If you've got Lastname, Firstname
in A1
, this spreadsheet function will render Firstname Lastname
:
=TRIM(MID(A1,FIND(",",A1)+1,50)) &" " &TRIM(LEFT(A1,FIND(",",A1)-1))
If you want to go from Firstname Lastname
to Lastname, Firstname
:
=TRIM(MID(A1,FIND(" ",A1)+1,50)) &", " &TRIM(LEFT(A1,FIND(" ",A1)-1))
回答2:
Shorter formula version:
=index(split(A1,","),,2)&" "&index(split(A1,","),,1)
来源:https://stackoverflow.com/questions/18704431/split-reorder-concat-inline