Ask Your Question
0

Converting a LibreOffice VBA macro to python - problem with object types

asked 2016-04-29 00:58:51 +0200

yonkiman gravatar image

updated 2016-04-29 01:01:10 +0200

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 )]
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-04-29 05:45:13 +0200

mauricio gravatar image

Try with:

def test_sort():
    from com.sun.star.table import TableSortField
    from com.sun.star.table.TableSortFieldType import AUTOMATIC

    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.getSheets().getByIndex(0)
    rango = sheet.getCellRangeByName('A1:C11')

    field = TableSortField()
    field.Field = 1
    field.IsAscending = True
    field.FieldType = AUTOMATIC

    sd = rango.createSortDescriptor()
    sd[1].Name = 'ContainsHeader'
    sd[1].Value = True
    sd[3].Name = 'SortFields'
    sd[3].Value = uno.Any('[]com.sun.star.table.TableSortField', (field,))

    uno.invoke(rango, 'sort', (sd,))
    return

Best regards

edit flag offensive delete link more

Comments

Worked perfectly and is super-clean - thanks! (Will upvote when/if I get enough karma.)

yonkiman gravatar imageyonkiman ( 2016-04-29 22:22:06 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-04-29 00:58:51 +0200

Seen: 468 times

Last updated: Apr 29 '16