Button forgets macros after file save python, basic Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
puppyledger
Posts: 4
Joined: Tue Apr 30, 2024 9:48 pm

Button forgets macros after file save python, basic Calc

Post by puppyledger »

Howdy,

Problem:

I created a new calc file, a new script, and bound a button to the script. It worked. Yay! If I save-close the file, and reopen it, the button no longer finds the script. This is true not only of script I write, but also of the included helloworld scripts (in both python and basic). After I re-open the file, If I check in control-properties for the button, the binding is still present, and I can also manually run the macro using:

Code: Select all

Tools->Macros->RunMacro
I started Calc thusly to see if I could get some more info:

Code: Select all

urxvt -e $XOFFICEPATH/scalc -env:PYSCRIPT_LOG_LEVEL=DEBUG --nologo --nofirststartwizard --norestore --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" & 
When I run the script manually, the debug output shows it running. When the file is new (never closed and re-opened) I see exactly the same thing when I press the button. However after closing the file and re-opening it, the debug shows nothing at all when I click the button. The button doesn't even seem to be looking for binding.

I have tried playing with APSO. No change. I have embedded the script. No change. I have backed my JRE down from openjre 24 to openjre 17 and 11. No change.

The problem seems to be in whatever code dispatches the script, but I have no visibility into that code, and it isn't showing anything useful. Can I get a debug of what the button is doing when I click it?

The code is as follows, but seems irrelevant, since it runs. It just doesn't run a button for some reason.

Code: Select all

from __future__ import unicode_literals

import sys
import socket
import operator
import re
import uno
import unohelper

def button_foo1(*args):
        self=FOO()      # get the instance handle
        self.importuno()        # run the methods
        thisclass = str(self.__class__)
        self.helloworld(thisclass) 
        return True

class FOO():
        def importuno(self): 
                self.ctx = uno.getComponentContext()
                self.desktop = self.ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.ctx) 
                self.document = self.desktop.getCurrentComponent()
                self.controller = self.document.getCurrentController()

        def helloworld(self,hellostr): 
                sheet = self.controller.ActiveSheet

                cellc3 = sheet.getCellRangeByName("C3")
                cellc3.String = hellostr                

I am using:

LibreOffice 24.2.2.2 420(Build:2)
OpenJDK Runtime Environment (build 11.0.22+6-void-r1)
VOID Linux 6.6.25 MUSL


Thanks in advance!
LibreOffice 24.2.1.2 420(Build:2)
Void Linux 6.6.5 MUSL
User avatar
robleyd
Moderator
Posts: 5105
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Button forgets macros after file save python,basic Calc

Post by robleyd »

In what format are you saving the Calc file?
Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.3.2; SlackBuild for 24.2.3 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue
Unexpected '{' on line 32
.
puppyledger
Posts: 4
Joined: Tue Apr 30, 2024 9:48 pm

Re: Button forgets macros after file save python,basic Calc

Post by puppyledger »

I am saving the spreadsheet as FOO.ods, and the script is located as: $HOME/.config/libreoffice/4/user/Scripts/python/FOO.py
LibreOffice 24.2.1.2 420(Build:2)
Void Linux 6.6.5 MUSL
ms777
Volunteer
Posts: 179
Joined: Mon Oct 08, 2007 1:33 am

Re: Button forgets macros after file save python, basic Calc

Post by ms777 »

can you attach your calc file with the embedded python to your post?

Edit: I created the file myself, with embedded python. Works like a charm, also after saving / reopening. Tested on LO 7.6.5.2 and AO 4.1.6, Win10
Attachments
PyButton.ods
(10.78 KiB) Downloaded 24 times
puppyledger
Posts: 4
Joined: Tue Apr 30, 2024 9:48 pm

Re: Button forgets macros after file save python, basic Calc

Post by puppyledger »

Here it is:

Thanks!
Attachments
FOO.ods
(9.6 KiB) Downloaded 23 times
LibreOffice 24.2.1.2 420(Build:2)
Void Linux 6.6.5 MUSL
ms777
Volunteer
Posts: 179
Joined: Mon Oct 08, 2007 1:33 am

Re: Button forgets macros after file save python, basic Calc

Post by ms777 »

Your FOO.ods works like a charm for me. Open, press button, saving, closing / leaving LO, reopening, clear C3, press button, and the macro works again.
One minor thing, which is probably not an issue: You assigned the machro to the mouse pressed event, better maybe is the execute action event
One other thing: You use self.desktop.getCurrentComponent() to get to the sheet. It may be safer to use the event passed (*args in your code) and its .Source property to find the current sheet, if you have other UNO components, like debugger / APSO / other documents open.

Does my PyButton.ods show the same symptoms?

Code: Select all

def button_foo1(event):
    doc = event.Source.Model.Parent.Parent.Parent
    doc.getCurrentController().ActiveSheet.getCellRangeByName("C3").String = "hello"
    return True
puppyledger
Posts: 4
Joined: Tue Apr 30, 2024 9:48 pm

Re: Button forgets macros after file save python, basic Calc

Post by puppyledger »

I have loaded your Pyuno.ods and the button doesn't work for me. This I believe confirms that it isn't anything to do with Python, but rather the build of LO I am using.

My libreoffice was installed from the distro build. If I was to guess, the problem is likely either with the MUSL lib, or Qt. MUSL is a drop in replacement for GlibC, but it isn't an exact replacement. I have had to add headers to fix problems with MariaDB because of it before. I suspect Qt, because a certain vendor with a long history of subterfuge funds that project, and that vendor also recently added Python to their own flagship spreadsheet. Of course I have no evidence of either at this point, but those are the "usual suspects".

I have run a previous build of libreoffice on VOID and been able to implement macros with buttons successfully. So I am thinking I will try using a different build, and if that doesn't work I will back off the version. I will follow up either way. This seems like it may end up as a bug report. I wish I could get some more debugging out of LO though.
Thanks!
LibreOffice 24.2.1.2 420(Build:2)
Void Linux 6.6.5 MUSL
Post Reply