Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse columns order in libre office calc

Suppose I have This This Cells:

Initial Enteries

And then for some reason I want to reverse the order of columns automatically to become something like This:

enter image description here

any help will be appreciated!

like image 250
SirSaleh Avatar asked Jan 05 '23 14:01

SirSaleh


2 Answers

quick steps:

  • insert new row above 1
  • fill row with monoton increasing integer index to the right.
  • Then select your data and sort descending, so highest indexed right col is first
  • copy and paste the result


https://gr-plus.blogspot.com/2015/01/reverse-column-or-row-order-in-excel-or.html

like image 71
qrtLs Avatar answered Jan 31 '23 00:01

qrtLs


So here is an approach using Libreoffice Basic and the Libreoffice API.

sub ReverseColumns()

 oThisWorkbook = ThisComponent
 oActiveSheet = oThisWorkbook.CurrentController.ActiveSheet

 oRow1 = oActiveSheet.getRows().getByIndex(0)
 aFilledCellsRow1 = oRow1.queryContentCells(1+2+4+16).getRangeAddresses()

 if ubound(aFilledCellsRow1) = -1 then exit sub

 lLastFilledColumnRow1 = aFilledCellsRow1(ubound(aFilledCellsRow1)).EndColumn

 c = 0
 for i = lLastFilledColumnRow1 to 1 step -1
  oCellTargetColumn = oActiveSheet.getCellByPosition(c, 0)
  oRangeAddressTargetColumn = oCellTargetColumn.RangeAddress
  oActiveSheet.insertCells(oRangeAddressTargetColumn, com.sun.star.sheet.CellInsertMode.COLUMNS)

  oCellTargetColumn = oActiveSheet.getCellByPosition(c, 0)
  oCellAddressTargetColumn = oCellTargetColumn.CellAddress

  oRangeSource = oActiveSheet.Columns.getByIndex(lLastFilledColumnRow1 + 1)
  oRangeAddressSource = oRangeSource.RangeAddress
  oActiveSheet.moveRange(oCellAddressTargetColumn, oRangeAddressSource)
  c = c + 1
 next

end sub

This first determines the last filled column in row 1. The column reversing process will then be done until that column.

For learning about Macros in Libreoffice start here: https://wiki.documentfoundation.org/Macros

like image 30
Axel Richter Avatar answered Jan 31 '23 02:01

Axel Richter