将 Libre/Star/Openoffice 基本宏转换为 python - 对象类型问题
Converting a Libre/Star/Openoffice Basic macro to python - issue with object types
我正在编写一个需要调用 LibreOffice Calc 的排序功能的 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 )]
创建 SortField
类型的对象并将它们放入一个元组中。这是对我有用的:
import uno
from com.sun.star.beans import PropertyValue
from com.sun.star.util import SortField
def create_sort_field(column, sort_ascending):
oSortField = SortField()
oSortField.Field = column
oSortField.SortAscending = sort_ascending
return oSortField
def sort_cols():
oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
oCellRange = oSheet.getCellRangeByName("B1:M30")
oSortFields = (
create_sort_field(11, False), # column M
create_sort_field(0, True),) # column B
oSortDesc = [PropertyValue()]
oSortDesc[0].Name = "SortFields"
oSortDesc[0].Value = uno.Any(
'[]com.sun.star.util.SortField', oSortFields)
oCellRange.sort(oSortDesc)
# Functions that can be called from Tools -> Macros -> Run Macro.
g_exportedScripts = sort_cols,
另请参阅:Sorting cell range in a calc document with pyuno。
我正在编写一个需要调用 LibreOffice Calc 的排序功能的 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 )]
创建 SortField
类型的对象并将它们放入一个元组中。这是对我有用的:
import uno
from com.sun.star.beans import PropertyValue
from com.sun.star.util import SortField
def create_sort_field(column, sort_ascending):
oSortField = SortField()
oSortField.Field = column
oSortField.SortAscending = sort_ascending
return oSortField
def sort_cols():
oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
oCellRange = oSheet.getCellRangeByName("B1:M30")
oSortFields = (
create_sort_field(11, False), # column M
create_sort_field(0, True),) # column B
oSortDesc = [PropertyValue()]
oSortDesc[0].Name = "SortFields"
oSortDesc[0].Value = uno.Any(
'[]com.sun.star.util.SortField', oSortFields)
oCellRange.sort(oSortDesc)
# Functions that can be called from Tools -> Macros -> Run Macro.
g_exportedScripts = sort_cols,
另请参阅:Sorting cell range in a calc document with pyuno。