私はLibreOffice CalcのSort関数を呼び出す必要があるPythonマクロを書いています。 Pythonのドキュメントは不十分ですが、私はPythonに変換しようとしていることを明確にしたBasic exampleが見つかりました。Libre/Star/Openoffice BasicマクロをPythonに変換する - オブジェクト型の問題
以下はSection 1
とSection 2
までうまくいっていました。 BasicはoSortFields()
オブジェクトの配列を作成しますが、PythonインタプリタはoSortFields()
を受け入れません。 oSortFields
は私が得ることができるほど近かった。
したがって、セクション3でsortコマンドを呼び出すと、不一致が原因でAttributeErrorが発生します。
PythonのBasicのoSortFields()
に相当するものは何ですか?
#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と呼ばれますが、Microsoft技術用のVBAではありません。しかし、よく書かれた質問。 –
良い点!私はすべての 'VBA'リファレンスを 'Basic'に変更しました。 –