[Solved] Embedded Python Script Error when reoppening file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
morgan_sickness
Posts: 8
Joined: Fri Jun 28, 2013 4:56 am

[Solved] Embedded Python Script Error when reoppening file

Post by morgan_sickness »

Hello,

I have made a python script (strength_lib.py) with the following content:

Code: Select all

from math import *

def Element(p1,p2):
    return pi*p1*p2
Obviously, I will make a more complex content in this Python Script, but I'm using this for only for test.

After that, I embed this python script by modifying the "META-INF/manifest.xml" and putting strength_lib.py in "Scripts/python" location inside the pseudo-zip ods file.
THe manifest.xml file is modified by adding this lines:
<manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/python/strength_lib.py"/>
<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/python/"/>
<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/"/>


Then I open the OpenOffice Calc sheet (mySheet.ods) and create the following macro code in Basic language:

Code: Select all

Function Element(p1,p2)
    sURL = "vnd.sun.star.script:strength_lib.py$Element?language=Python&location=document" 
    oMSP = ThisComponent.getScriptProvider()
    oScript = oMSP.getScript(sURL)

    Element = oScript.invoke(Array(p1,p2), Array(), Array())
End Function
When I do this I successfully call my function Element in any cell of the sheet:
Image
So everything is OK and I save the file and close it.

However, everytime I re-open this file I get the following error message:
Image
and also the function does not works:
Image
See that it is written on the cell, but its result is not displayed.

Am I doing something wrong? Or is it a limitation of OpenOffice 3.2? I have to develop my work in this OO version..

Thanks.
Last edited by Hagar Delest on Tue Sep 02, 2014 9:29 pm, edited 1 time in total.
Reason: tagged [Solved].
Open Office 3.4.0. Windows 7 Ultimate.
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Embedded Python Script Error when reoppening the file

Post by hanya »

When both Basic modules and Python scripts were stored in the same file, the Python scripts were not loaded well.
It seems this is a bug.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Embedded Python Script Error when reoppening the file

Post by hanya »

pythonscript.MyUriHelper class has problem about constructing URL for loading python macro with "document" location.
If you execute embedded macro through UI, MyUrlHelper class is initialized with URI something like vnd.sun.star.tdoc:/N, N is sequential number that depends on the order of the documents created or opened.

I checked other script providers for Java based languages, they has the same problem. "document" location is not supported well.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Embedded Python Script Error when reoppening the file

Post by hanya »

I tried to find workaround but com.sun.star.frame.TransientDocumentsDocumentContentFactory service did not give me the identifier (transient document URI, having vnd.sun.star.tdoc protocol) during the file loading. It works well after loading the file finished. Without the service, I can not obtain the transient document URI for the given document object.

The master script provider factory does not provide information about document model that the script bound to for "document" location specified to initialize the script provider. There is no way to know which document object is the one which the script is stored in.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
morgan_sickness
Posts: 8
Joined: Fri Jun 28, 2013 4:56 am

Re: Embedded Python Script Error when reoppening the file

Post by morgan_sickness »

Ok. Thank you Hanya for the tries.

I tried to workaround it by doing the following:
1. Created 2 new functions in python file. One to Clear the formulas in the sheet and other to Insert the formulas. Like this:

Code: Select all

# -*- coding: cp1252 -*-
from math import *

def ClearFormulas(event):
    document = XSCRIPTCONTEXT.getDocument()
    sheet=document.Sheets.Sheet1   
    sheet.getCellRangeByName("A1").setString("")
    
def InsertFormulas(event):
    document = XSCRIPTCONTEXT.getDocument()
    sheet=document.Sheets.Sheet1 
    sheet.getCellRangeByName("A1").FormulaLocal = "=ELEMENT(A2;A3)"

def Element(p1,p2):
    return pi*p1*p2
2. I opened mySheet.ods and went to Tools -> Customize... and changed the Events field to:
Image

3. Saved and now everything works fine. Reopened and everything is ok.

However, after doing this I have found another problem when trying to plot a chart in Sheet2.
Let me first explain what I want to do:
In Sheet1 I want cells with results and in Sheet2 I want some charts using the content of python script functions.
Here what I have in Sheet1:
Image

Here what I have in Sheet2:
Image

Everything works fine, except when I close the file, viewing Sheet2 and click on "Save" button at the dialog window that opens.
The following Error Msg appears:
Image

This error does not happen when we close the file viewing Sheet1 or when I plot the chart in Sheet1.

Maybe could you have some idea to workaround it?
Open Office 3.4.0. Windows 7 Ultimate.
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Embedded Python Script Error when reoppening the file

Post by hanya »

I cound not reproduce the problem you attach the message with a file made by myself on AOO 4.0.1.
PyUNO and the scripting framework should be maintained to support embedded macros much better.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
morgan_sickness
Posts: 8
Joined: Fri Jun 28, 2013 4:56 am

Re: Embedded Python Script Error when reoppening the file

Post by morgan_sickness »

Ah yeah.
The problem only happen in 3.2 OpenOffice version.
Tried here in a 3.4 version and everything worked well.
Open Office 3.4.0. Windows 7 Ultimate.
Post Reply