[Python] Problems with Python macro embedded in worksheet

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have been trying to run a Python macro embedded in the worksheet. that hides / unhides a pushbutton based on a cell value. I am not a code person, so please forgive any errors I make here.

    I got the code snippet from this thread: https://ask.libreoffice.org/t/macro...ble-a-push-button-based-on-a-cell-value/69794 The macro is run by attaching the the sheet events....Content changed. I modified it as such (PushButton1 is the name from the properties panel):

    1 def button_state_on_cell_Value(event):
    2 “””
    3 “””
    4 if event.AbsoluteName.endswith('$O$1'):
    5 sheet = event.Spreadsheet
    6 # next button
    7 PushButton1 = sheet.DrawPage[1].Control
    8 PushButton1.EnableVisible = event.Value==3.0


    I found how to embed the macro with the spreadsheet from this thread: How to embed python scripts into LibreOffice Calc No modifications. This code is attached to a pushbutton .. Execute action event.

    12 def test(args=None):
    13 raise Exception("Hello from Python!")


    When I tested the First macro, this error message appeared:

    com.sun.star.uno.RuntimeException: Error during invoking function button_state_on_cell_Value in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'AttributeError'>: Control File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 7, in button_state_on_cell_Value)

    When I tested the second macro, this error message appeared:

    com.sun.star.uno.RuntimeException: Error during invoking function test in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'Exception'>: Hello from Python! File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 13, in test )

    Anyone have an idea on how to correct these errors. If there is a way to hide and unhide the pushbutton with LO Basic, that would be great!

    Thank you very much in advance!

    Not sure I want to try to change anything else myself. Don't want to break thingss.

    Continue reading...

Compartilhe esta Página