I want to change the columns order in google sheet from this:
I know I can do this manually but if I have a lot of columns it's pretty annoying. There's a simple way to do this? Thanks in advance!
Building on the idea proposed by Steve in the comment. This works for any arbitrary range, pasted as tested on a range of columns A to D:
=array_constrain(
transpose(sort(transpose({A:D; arrayformula(column(A:D))}), rows(A:D)+1, false)),
rows(A:D),
columns(A:D)
)
Explanation: the 1st argument does the inversion magic by inserting a row of column numbers, transposing, sorting by this row in descending order and transposing back. Then we get rid of this extra row by constraining the result to the original dimensions.
The formula works and is generic enough, but it still feels like a hack. I wonder if someone can come up with a cleaner solution.
=QUERY(A:D, "SELECT D, C, B, A")