Need help to modify Python macro

Joined
Jan 24, 2025
Messages
4
Reaction score
0
I want to use this macro in LibreOffice Calc spreadsheet but instead get “Button Name”

I what to get “Button Label”.

Can anyone help me change this code? Thank you

def insert_button_name(oEvent):
"""Insert the button name into the next empty row in column E.
Update B2 to track the next available row number.
"""
oButton = oEvent.Source
button_name = oButton.getModel().getName()
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getCurrentController().getActiveSheet()
oCell = oSheet.getCellRangeByName("B2")
COLUMN_E = 4
row_num = oCell.getValue() - 1
oTargetCell = oSheet.getCellByPosition(COLUMN_E, id)
oTargetCell.setString(button_name)
oCell.setValue(oCell.getValue() + 1)
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
I want to use this macro in LibreOffice Calc spreadsheet but instead get “Button Name”

I what to get “Button Label”.

Can anyone help me change this code? Thank you

def insert_button_name(oEvent):
"""Insert the button name into the next empty row in column E.
Update B2 to track the next available row number.
"""
oButton = oEvent.Source
button_name = oButton.getModel().getName()
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.getCurrentController().getActiveSheet()
oCell = oSheet.getCellRangeByName("B2")
COLUMN_E = 4
row_num = oCell.getValue() - 1
oTargetCell = oSheet.getCellByPosition(COLUMN_E, id)
oTargetCell.setString(button_name)
oCell.setValue(oCell.getValue() + 1)
You need to adjust the code to access the button's label property. The getLabel() method can be utilized for this purpose.
Example:
Code:
button_name = oButton.getModel().getLabel()
 
Joined
Jan 24, 2025
Messages
4
Reaction score
0
You need to adjust the code to access the button's label property. The getLabel() method can be utilized for this purpose.
Example:
Code:
button_name = oButton.getModel().getLabel()
I modified as suggested in Thonny
 
Joined
Jan 24, 2025
Messages
4
Reaction score
0
Thank you for your reply.
Following your suggesting I modified in Thonny. run the script in Calc it gave a error.
I don’t understand this error can you please help to fix this.
Thank you


Com.sun.star.uno.RuntimeException:Error during invoking function
insert_button_name in module file:/// home/luis/.config/libreoffice/4/user/
Scripts/python/Incrementcells01.py(<class ‘AttributeError’>: getLabel

File “/usr/lib/libreoffice/program/pythonscript.py”, line 916, in invoke
ret = self.func( *args )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File “/home/luis/.config/libreoffice/4/user/Scripts/python/
Incrementcells01.py”, line 6, in insert_button_name

button_name = oButton.getModel().getLabel()
^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
Thank you for your reply.
Following your suggesting I modified in Thonny. run the script in Calc it gave a error.
I don’t understand this error can you please help to fix this.
Thank you


Com.sun.star.uno.RuntimeException:Error during invoking function
insert_button_name in module file:/// home/luis/.config/libreoffice/4/user/
Scripts/python/Incrementcells01.py(<class ‘AttributeError’>: getLabel

File “/usr/lib/libreoffice/program/pythonscript.py”, line 916, in invoke
ret = self.func( *args )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File “/home/luis/.config/libreoffice/4/user/Scripts/python/
Incrementcells01.py”, line 6, in insert_button_name

button_name = oButton.getModel().getLabel()
^^^^^^^^^^^^^^^^^^^^^^^^^^
The AttributeError suggests that the oButton object does not have a model that supports the getLabel() method. This could be due to several reasons, such as the oButton not being properly initialized or not being of the expected type.
You can add a check to ensure that oButton is indeed a valid button object and that it has a model before calling getLabel().
 
Joined
Jan 24, 2025
Messages
4
Reaction score
0
The AttributeError suggests that the oButton object does not have a model that supports the getLabel() method. This could be due to several reasons, such as the oButton not being properly initialized or not being of the expected type.
You can add a check to ensure that oButton is indeed a valid button object and that it has a model before calling getLabel().
First I have to bring to your attention that the macro “getName()” work perfect well as is the different is I want the “getLabel()”.

I apologize if I am wrong to mention this.

I start learning Python about a month ago I am complete ignorant how to the changes that

you suggest. So I need you please help complete the macro.

Once again a very big Thank You.
 
Joined
Jan 14, 2025
Messages
25
Reaction score
6
First I have to bring to your attention that the macro “getName()” work perfect well as is the different is I want the “getLabel()”.

I apologize if I am wrong to mention this.

I start learning Python about a month ago I am complete ignorant how to the changes that

you suggest. So I need you please help complete the macro.

Once again a very big Thank You.
The macro currently retrieves the button's name using oButton.getModel().getName(), but you want to get the "Button Label" instead of the "Button Name".
To fix this, we can replace the line that retrieves the button name with a line that retrieves the button label. The correct method to use is getLabel(). Here’s the corrected code:
Code:
def insert_button_name(oEvent):
    """Insert the button label into the next empty row in column E.
    Update B2 to track the next available row number.
    """
    oButton = oEvent.Source
    button_label = oButton.getModel().getLabel()  # Changed from getName() to getLabel()
    oDoc = XSCRIPTCONTEXT.getDocument()
    oSheet = oDoc.getCurrentController().getActiveSheet()
    oCell = oSheet.getCellRangeByName("B2")
    COLUMN_E = 4
    row_num = oCell.getValue() - 1
    oTargetCell = oSheet.getCellByPosition(COLUMN_E, row_num)  # Fixed variable name from 'id' to 'row_num'
    oTargetCell.setString(button_label)  # Use button_label instead of button_name
    oCell.setValue(oCell.getValue() + 1)
Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top