我正在编写一个python宏,它需要调用LibreOffice Calc的Sort函数。Python文档很少,但我发现了一个明确的Basic example,我正试图将其转换为Python。在
直到Section 1
和{oSortFields()
对象数组,但是python解释器不接受oSortFields()
。oSortFields
是我所能接近的。在
因此,当它调用第3节中的sort命令时,不匹配将导致AttributeError。在
Basic的oSortFields()
的python等价物是什么?在
#basic# Dim oSortFields(1) As New com.sun.star.util.SortField
from com.sun.star.util import SortField
oSortFields = SortField
#basic# Dim oSortDesc(0) As New com.sun.star.beans.PropertyValue
from com.sun.star.beans import PropertyValue
oSortDesc = PropertyValue
#basic# oSheet = ThisComponent.Sheets.getByName("Sheet1")
oSheet = ThisComponent.getSheets().getByIndex(0)
#basic# REM Get the cell range to sort
#basic# oCellRange = oSheet.getCellRangeByName("A1:C5")
oCellRange = oSheet.getCellRangeByName("B1:M30")
################# Section 1 #################
#basic# REM Sort column B (column 1) descending.
#basic# oSortFields(0).Field = 1
#basic# oSortFields(0).SortAscending = FALSE
oSortFields.Field = 11 # close as I could get
oSortFields.SortAscending = False
################# Section 2 #################
#basic# REM If column B has two cells with the same value,
#basic# REM then use column A ascending to decide the order.
#basic# oSortFields(1).Field = 0 ### Skipped and prayed
#basic# oSortFields(1).SortAscending = True
# Now I'm really in trouble
#basic# oSortDesc(0).Name = "SortFields"
#basic# oSortDesc(0).Value = oSortFields()
oSortDesc.Name = "SortFields"
oSortDesc.Value = oSortFields
################# Section 3 #################
#basic# REM Sort the range.
#basic# 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 )]
创建
SortField
类型的对象并将它们放入元组中。以下是对我有效的方法:另请参见:Sorting cell range in a calc document with pyuno。在
相关问题 更多 >
编程相关推荐