OpenOffice Calc macro in Python - macro triggered by a click of a button, how to get a name of that button?

2

I am really new to this and OpenOffice Developer's Guide is too difficult for me to understand or I'd help myself.

I have a large number of rows with data and in each row I want a button that would trigger a particular macro rowMacro. That macro will analyze/modify data in that particular row. For simplicity, let's say it it just writes an arbitrary string somewhere in the row:

import uno
oDoc = XSCRIPTCONTEXT.getDocument()

def rowMacro(*args):
  oSheet = oDoc.CurrentController.ActiveSheet
  oCell1 = oSheet.getCellRangeByName("A4")
  CurContr=oDoc.getCurrentController().getSelection()
  oCell1.String = "Tada!"

I don't want to create a dozen of separate near-identical scripts for each row - so no rowMacroRowA triggered by a specific button in row A, etc. I'd rather have a number of buttons all triggering the same python macro. That macro somehow needs to determine which button was used (each button has a unique name) and modify only the appropriate row. So I need to figure out the way to get a button name in python (I'll know how to handle it from there).

I stumbled upon a webpage with an excerpt like this:

# 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

but I do not know how to deduct my problem's solution from that.

To summarize:

  1. How to get a name of a button that was used to trigger a macro?
  2. Better yet - how to get its position, in particular a row? (this one I'd rather figure out on my own if you push me in the right direction).

user2551153

Posted 2017-03-22T14:12:44.433

Reputation: 91

Answers

2

The button name can be determined from the ActionEvent that gets passed as a parameter. For the example below, I named the button btnRow4.

Getting the position is harder, but possible by obtaining the XShape of the button from the DrawPage. Here is code that illustrates how all of this works:

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)

A discussion about getPosition() is at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=82422.

Jim K

Posted 2017-03-22T14:12:44.433

Reputation: 2 601

That's it! Thanks a dozen. No follow-up needed, the example helped me a lot. – user2551153 – 2017-03-23T13:44:06.773