Page 1 of 1

[Solved] Embedded Python Script Error when reoppening file

PostPosted: Fri Aug 29, 2014 11:30 pm
by morgan_sickness
Hello,

I have made a python script (strength_lib.py) with the following content:
Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
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.

Re: Embedded Python Script Error when reoppening the file

PostPosted: Sat Aug 30, 2014 7:00 am
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.

Re: Embedded Python Script Error when reoppening the file

PostPosted: Sat Aug 30, 2014 7:55 am
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.

Re: Embedded Python Script Error when reoppening the file

PostPosted: Sat Aug 30, 2014 2:56 pm
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.

Re: Embedded Python Script Error when reoppening the file

PostPosted: Mon Sep 01, 2014 4:33 pm
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   Expand viewCollapse view
# -*- 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?

Re: Embedded Python Script Error when reoppening the file

PostPosted: Mon Sep 01, 2014 6:44 pm
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.

Re: Embedded Python Script Error when reoppening the file

PostPosted: Mon Sep 01, 2014 8:22 pm
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.