Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse columns of spreadsheet

I want to reverse columns of spreadsheet. Sample is "p227", "s121", "p117", "p252", "s215" in column "A1:E1". Values are random. I want to reverse this column like "s215", "p252","p117", "s121", "p227".

I tried =TRANSPOSE(SORT(TRANSPOSE(A1:E1),1,false)). But output is "s255", "s121", "p212", "p187", "p121". Values are sorted. This doesn't reverse columns. Is there way to solve this? Should I use GAS?

Sample

      A    B    C    D    E

1   p227 s121 p117 p252 s215

Expected result

      A    B    C    D    E

1   s215 p252 p117 s121 p227

Thank you so much for your time.

like image 461
Elsa Avatar asked Dec 11 '22 06:12

Elsa


2 Answers

Try

=TRANSPOSE(SORT(TRANSPOSE(A1:E1),TRANSPOSE(COLUMN(A1:E1)),0))
  • SORTs using COLUMN numbers in descending order
  • TRANSPOSE is needed as SORT only works on rows
like image 174
TheMaster Avatar answered Dec 26 '22 01:12

TheMaster


Here's a general one

Method:

Transpose
Add row numbers
Sort descending by row number
Transpose
Remove row numbers

Formula:

=ArrayFormula(query(TRANSPOSE(sort({row(indirect("1:"&columns(A1:E2))),transpose(A1:E2)},1,false)),"select * offset 1"))

enter image description here

EDIT

On reflection this would have been a bit neater, adding column numbers before transposing and avoiding the Indirect:

=ArrayFormula(query(TRANSPOSE(sort(transpose({COLUMN(A1:E2);A1:E2}),1,false)),"select * offset 1"))
like image 34
Tom Sharpe Avatar answered Dec 26 '22 00:12

Tom Sharpe