I have a successful .py script that can open an existing LO Calc doc (saved as .xlsx so that I can use openpyxl) which appends information into the file which saves the file and closes it on termination of the script. The .xlsx file cannot already be open when the script runs, or the file gets corrupted and all data is lost.
But what I really need is to be able to see the data populating the spreadsheet as it happens so I can have a dynamic chart visually displaying the information.
I installed APSO (tq!) which I believed would let me to embed editable python user scripts. I want the script to run when I click on a programmable button I made in my sheet.
I saved the script at /home/USER/.config/libreoffice/4/user/Scripts/python which I made in accordance to instructions from the ScriptForge 'code assistant' software from the LO Conference 2021.
In the macro selector window, under "My Macros", I see my script, but access to the "OK" button is denied to me (grayed out), yet I am able to select a script from the "LibOCon_2021.py" ScriptForge file.
Comparing my .py script to the ones in "LibOCon_2021.py", It seemed the difference was that my script didn't have a definition, so at the top of my script I added "def to_be_embedded(args=None):" and saved it, but the "OK" button is still not available so I cannot tie it to the button press.
So how do I actually embed my script and get it to run when I click on the button?
Here are steps to create a button that runs an embedded python macro.
:w
if using vim).def test(args=None):
raise Exception("Hello from Python!")
Now turn off Design mode and click on the button. We can see that it runs because it produces the following message:
com.sun.star.uno.RuntimeException: Error during invoking function test in
module vnd.sun.star.tdoc:/1/Scripts/python/Module.py (<class 'Exception'>:
Hello from Python!