
我對此非常陌生,OpenOffice 開發人員指南對我來說太難理解,否則我會幫助自己。
我有大量包含資料的行,並且在每一行中我想要一個可以觸發特定巨集的按鈕rowMacro
。該巨集將分析/修改該特定行中的資料。為簡單起見,假設它只是在行中的某個位置寫入任意字串:
import uno
oDoc = XSCRIPTCONTEXT.getDocument()
def rowMacro(*args):
oSheet = oDoc.CurrentController.ActiveSheet
oCell1 = oSheet.getCellRangeByName("A4")
CurContr=oDoc.getCurrentController().getSelection()
oCell1.String = "Tada!"
我不想為每一行創建十幾個獨立的幾乎相同的腳本 - 因此不會rowMacroRowA
由 A 行中的特定按鈕觸發,等等。該巨集以某種方式需要確定使用了哪個按鈕(每個按鈕都有一個唯一的名稱)並僅修改適當的行。所以我需要找出在 python 中獲取按鈕名稱的方法(我會知道如何從那裡處理它)。
我偶然發現了一個網頁摘錄如下:
# get the sheet
accueil_sheet = model.Sheets.getByName("Accueil")
# access the draw page
oDrawPage = accueil_sheet.DrawPage
# count the number of form
oDrawPage.getForms().getCount()
# get the list box of the control element
ListBox = oDrawPage.getForms().getByIndex(0).getByName("Listbox")
# get the list box item list
ListBox.StringItemList
# get the list box controller
ListBoxCtrl = model.getCurrentController().getControl(ListBox)
# get the selected items:
ListBoxCtrl.SelectedItems
但我不知道如何從中推斷出我的問題的解決方案。
總結一下:
- 如何取得用於觸發巨集的按鈕的名稱?
- 更好的是 - 如何獲得它的位置,特別是一行? (如果你把我推向正確的方向,我寧願自己弄清楚)。
答案1
按鈕名稱可以從動作事件作為參數傳遞。對於下面的範例,我將按鈕命名為btnRow4
。
要獲得該職位比較困難,但可以透過獲得X形狀DrawPage 中的按鈕。下面的程式碼說明了這一切是如何運作的:
def rowMacro(action_event=None):
## Get the button name.
if action_event:
button_name = action_event.Source.Model.getName()
else:
button_name = ''
if button_name == 'btnRow4':
rowname = "4"
else:
rowname = "5"
## Get the button position.
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.CurrentController.ActiveSheet
oDrawPage = oSheet.DrawPage
oShape = None
for i in range(oDrawPage.Count):
aShape = oDrawPage.getByIndex(i)
if aShape.supportsService("com.sun.star.drawing.ControlShape"):
if aShape.getControl().getName() == button_name:
oShape = aShape
if oShape:
ypos = oShape.getPosition().Y
else:
ypos = "(didn't click on a button)"
## Show results.
oCell = oSheet.getCellRangeByName("A" + rowname)
oCell.String = "Y Position: " + str(ypos)
關於的討論getPosition()
位於https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=82422。