I’m writing a python macro that needs to call LibreOffice Calc’s Sort function. Python documentation is scarce, but I found a clear VBA example that I’m trying to convert to python.
It was going well until Section 1 and Section 2 below. VBA creates an oSortFields() object array, but the python interpreter would not accept oSortFields(). oSortFields was as close as I could get.
So when it calls the sort command in Section 3, the mismatch causes an AttributeError.
What is the python equivalent of VBA’s oSortFields() object?
#VBA# Dim oSortFields(1) As New com.sun.star.util.SortField
from com.sun.star.util import SortField
oSortFields = SortField
#VBA# Dim oSortDesc(0) As New com.sun.star.beans.PropertyValue
from com.sun.star.beans import PropertyValue
oSortDesc = PropertyValue
#VBA# oSheet = ThisComponent.Sheets.getByName("Sheet1")
oSheet = ThisComponent.getSheets().getByIndex(0)
#VBA# REM Get the cell range to sort
#VBA# oCellRange = oSheet.getCellRangeByName("A1:C5")
oCellRange = oSheet.getCellRangeByName("B1:M30")
################# Section 1 #################
#VBA# REM Sort column B (column 1) descending.
#VBA# oSortFields(0).Field = 1
#VBA# oSortFields(0).SortAscending = FALSE
oSortFields.Field = 11 # close as I could get
oSortFields.SortAscending = False
################# Section 2 #################
#VBA# REM If column B has two cells with the same value,
#VBA# REM then use column A ascending to decide the order.
#VBA# oSortFields(1).Field = 0 ### Skipped and prayed
#VBA# oSortFields(1).SortAscending = True
# Now I'm really in trouble
#VBA# oSortDesc(0).Name = "SortFields"
#VBA# oSortDesc(0).Value = oSortFields()
oSortDesc.Name = "SortFields"
oSortDesc.Value = oSortFields
################# Section 3 #################
#VBA# REM Sort the range.
#VBA# oCellRange.Sort(oSortDesc())
oCellRange.Sort(oSortDesc())
# Gemerates Error:
# <class 'AttributeError'>: Sort StockDataFromYahoo.py:212
# in function StockOptionParty() [oCellRange.Sort(oSortDesc())]
# pythonscript.py:870 in function invoke() [ret = self.func( *args )]