【发布时间】:2016-08-23 22:25:24
【问题描述】:
我正在编写一个需要调用 LibreOffice Calc 的 Sort 函数的 python 宏。 Python 文档很少,但我找到了一个明确的Basic example,我正在尝试将其转换为 python。
在下面的Section 1 和Section 2 之前进展顺利。 Basic 创建一个oSortFields() 对象array,但python 解释器不接受oSortFields()。 oSortFields 尽可能接近。
所以当它调用第 3 节中的排序命令时,不匹配会导致 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 )]
【问题讨论】:
-
该语言被称为 LibreOffice Basic、StarBasic 或只是 Basic,但从来没有 VBA,它用于 Microsoft 技术。不过问题写得很好。
-
好点!我将所有“VBA”引用更改为“Basic”。
标签: python macros libreoffice libreoffice-calc