I am trying to write a Python calc add-in that returns multiple values
including range-cells (or arrays of numbers). E.g., if I want the method to take two input <long> arguments 'inarg1' and 'inarg2' and return a status string and two arrays of numbers, how would I specify the IDL routine and python method? I've searched, but I can't find much information about this topic in the PyUNO documentation... |
On 5/17/07, Joachim Dahl <[hidden email]> wrote:
> > I am trying to write a Python calc add-in that returns multiple values > including range-cells (or arrays of numbers). > > E.g., if I want the method to take two input <long> arguments 'inarg1' and > 'inarg2' and return a status string and two > arrays of numbers, how would I specify the IDL routine and python method? > > > I've searched, but I can't find much information about this topic in the > PyUNO documentation... > Maybe my question is too generic... There is a nice C++ tutorial about add-ins at the wikipage: http://wiki.services.openoffice.org/wiki/CompleteAddIn Is it possible to rewrite "methodFour" in Python? |
In reply to this post by Joachim Dahl-2
Hi Joachim,
Joachim Dahl wrote: > There is a nice C++ tutorial about add-ins at the wikipage: > http://wiki.services.openoffice.org/wiki/CompleteAddIn > > Is it possible to rewrite "methodFour" in Python? > that is what I expect, but actually I never dived into the Python stuff. Joerg Budischewski should know. Joerg? By the way, if you get it working, it would be nice if you can write some words about it into the wiki. Thanks Kay --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
In reply to this post by Joachim Dahl-2
Joachim Dahl wrote:
> On 5/17/07, Joachim Dahl <[hidden email]> wrote: >> >> I am trying to write a Python calc add-in that returns multiple values >> including range-cells (or arrays of numbers). >> >> E.g., if I want the method to take two input <long> arguments 'inarg1' >> and >> 'inarg2' and return a status string and two >> arrays of numbers, how would I specify the IDL routine and python method? >> >> >> I've searched, but I can't find much information about this topic in the >> PyUNO documentation... >> > > > Maybe my question is too generic... > > There is a nice C++ tutorial about add-ins at the wikipage: > http://wiki.services.openoffice.org/wiki/CompleteAddIn > > Is it possible to rewrite "methodFour" in Python? to implement a sequence of sequence of long in Python. I am no Python expert and not really familiar with the Python binding but you can probably find more examples on the net. Anyway independent of the used language. You can simplify the implementation by using a new config file to define some necessary add-in properties. See for example a generated Java add-in project in Netbeans using our Netbeans plugin. Juergen > --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
In reply to this post by Joachim Dahl-2
On Thu, 17 May 2007 14:30:36 -0500, Joachim Dahl <[hidden email]>
wrote: > I am trying to write a Python calc add-in that returns multiple values > including range-cells (or arrays of numbers). > > E.g., if I want the method to take two input <long> arguments 'inarg1' > and > 'inarg2' and return a status string and two > arrays of numbers, how would I specify the IDL routine and python method? > > I've searched, but I can't find much information about this topic in the > PyUNO documentation... You can check the Python page: http://wiki.services.openoffice.org/wiki/Python There are some add-ins on Calc. -- Alexandro Colorado OpenOffice.org Community Contact // Mexico http://www.openoffice.org Twitter: http://www.twitter.com/jza Jabber: [hidden email] --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
all of the Python add-in tutorials I've seen are fairly simple, e.g., they
take two numbers as input and return their sum. I cannot find any documentation or examples on how to return multiple values in Python. This is done in the C++ method called "methodfour" in the CompleteAddin tutorial mentioned above. If I could figure out how to reimplement "methodfour" in Python, that would probably answer my questions... Handling input range-cells via Python is straightforward, since they are just mapped into tuples, but output range-cells are still illuding me. So my problem is not so much of how to write a prototype of an addin, but rather how to implement something more advanced like "methodFour". Joachim |
Maybe I got a small step further. I received a few advices from JÃ¶rg
Budischewski: Say, I define my IDL like this: interface Xcvxopt : com::sun::star::uno::XInterface { string lp( [in] sequence < sequence < long > > c, [out] sequence < sequence < long > > x); }; The JÃ¶rg suggested I define my Python method as: def lp(self, c, x): x = (1,2) return "test", x but whenever I try to invoke it from the spreadsheet as =lp(A1:A2,B1:B2) (and all sorts of variations) I get a #NAME? error, so it's like oocalc doesnt parse my Python add-in because of an error. I should mention that if I only have [in] fields in the IDL, then my add-in works fine. How else would I invoke it from the spreadsheet? |
Hi,
when answering your question (privately), I wasn't aware, that it should be a calc addin. I never created a calc addin, but my impression from reading http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/AddIn.html is, that there can only be exactly one return value, at least it is not explicitly mentioned, that out parameters are supported. Maybe someone who knows Calc better can confirm this. Assuming this is correct, your idl description can't work, you then cant return a string and a 2 dimensional double array at the same time. What should work is interface Xcvxopt : com::sun::star::uno::XInterface { sequence < sequence < long > > lp( [in] sequence <sequence<long>> c); }; Your code below was still errornously, because you just passed a 1 dimensional array (and this will not convert correctly). This should work: def lp(self, c): x = (1,2), # the additional , makes this a 2 dimensional array return x BTW: you seem to have a weakness for short/cryptic function/inferface/argument names, haven't you ? You should at least stick to the convention to write the 2nd letter of the interface name in capitals. Bye, Joerg Joachim Dahl wrote: > Maybe I got a small step further. I received a few advices from JÃ¶rg > Budischewski: > > Say, I define my IDL like this: > interface Xcvxopt : com::sun::star::uno::XInterface > { > string lp( [in] sequence < sequence < long > > c, > [out] sequence < sequence < long > > x); > }; > > The JÃ¶rg suggested I define my Python method as: > def lp(self, c, x): > x = (1,2) > return "test", x > > but whenever I try to invoke it from the spreadsheet as > =lp(A1:A2,B1:B2) (and all sorts of variations) > I get a #NAME? error, so it's like oocalc doesnt parse my Python add-in > because of an error. I should mention > that if I only have [in] fields in the IDL, then my add-in works fine. > > How else would I invoke it from the spreadsheet? > --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
thanks, this brought me a step further...
Doing like Joerg suggest, at least I don't get the #NAME error. I invoke my add-in as =LP(A1:A2) but only a single element of the (1,2), array is returned (the first element). Joachim |
what I actually wonder about is that you choose a "matrix" signature,
but your sample only takes vectors, maybe change your addin to vectors or your sample to matrixes, just to see, how it behaves. But as I said, i dont know calc good enough. Bye, Joerg Joachim Dahl wrote: > thanks, this brought me a step further... > > Doing like Joerg suggest, at least I don't get the #NAME error. > > I invoke my add-in as > =LP(A1:A2) > > but only a single element of the (1,2), array is returned (the first > element). > > Joachim > --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
Matrix parameters work fine as input, but if I change the output to just
a sequence of longs, then I get the #NAME error again (and I remembered to updated the Python return value to (1,2) ). Another possibility could be to interface my Python program from C, which is moderately easy: http://abel.ee.ucla.edu/cvxopt/examples/miscellaneous/embed_cvxopt.c/view (and I guess that C++ is not much harder), but I doubt than many Spreadsheet optimization users would bother (or know how) to compile their own add-ins. |
In reply to this post by Joachim Dahl-2
Hi,
Joerg pinted out that our parameters are not allowed in add-ins and he ahs also defined a correct function which returns a sequence< sequence < long > >. When you have implement it correct and when use this function later on in calc you have to use it as array function. Either you finish your input with ctrl-shift or use the function dialog and check the array check box in the lower left cormer. Juergen Joachim Dahl wrote: > thanks, this brought me a step further... > > Doing like Joerg suggest, at least I don't get the #NAME error. > > I invoke my add-in as > =LP(A1:A2) > > but only a single element of the (1,2), array is returned (the first > element). > > Joachim > --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
In reply to this post by Joerg Budischewski
Joerg,
thanks for all the help; I managed to get it working... The output must be a sequence of sequence, otherwise I get the #NAME error as if the addin is not properly loaded. My last problem was actually caused by not flagging the function as an 'array' function. Just reference, this is my IDL file: interface Xcvxopt : com::sun::star::uno::XInterface { sequence < sequence <long> > lp( [in] sequence < sequence < long > > c); }; and this is my Python method: def lp(self, c): x = ((1,),(2,)) return x I prefer to return the results in a column, but a row works also: def lp(self, c): x = (1,2), return x It would still be nice to be able to return multiple arguments (as you can in, e.g., gnumeric). For example, I would like to return both primal and dual variables for the optimization variables, and a status message about the solution (feasible, infeasible, unbounded, etc.) but I can live with the current solution. - Joachim On 5/20/07, JÃ¶rg Budischewski <[hidden email]> wrote: > > what I actually wonder about is that you choose a "matrix" signature, > but your sample only takes vectors, maybe change your addin to vectors > or your sample to matrixes, just to see, how it behaves. > > But as I said, i dont know calc good enough. > > Bye, > > Joerg > > Joachim Dahl wrote: > > thanks, this brought me a step further... > > > > Doing like Joerg suggest, at least I don't get the #NAME error. > > > > I invoke my add-in as > > =LP(A1:A2) > > > > but only a single element of the (1,2), array is returned (the first > > element). > > > > Joachim > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email] > For additional commands, e-mail: [hidden email] > > |
I just wanted to add an update...
One way to output several values seems to be via the XCellRange type; I guess that's what have been looking for all along... After learning more by trial and error, the Python UNO bridge is very nice! - Joachim |
In reply to this post by Joachim Dahl-2
Joachim Dahl wrote:
> me. > > So my problem is not so much of how to write a prototype of an addin, but > rather how to > implement something more advanced like "methodFour". > > Joachim > Hi Joachim, http://wiki.services.openoffice.org/wiki/CompleteAddIn has a chapter "Transforming sequence of sequence into array". This describes the same problem as I tried to explain with my simple Basic function TWICE(array) in http://www.oooforum.org/forum/viewtopic.phtml?t=57196. Only array-functions, such as the built-in LINEST, can return more than one value. The resulting array has to be converted to a 2-dimensional matrix rather than a list of lists. So it is possible that python alone is not suitable for add-ins that deal with sheet functions in array context. Andreas --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
Hi Andreas,
I think the Python UNO bridge works fine; I managed to return an array of values by using an array function. I needed to return multiple arrays of different lengths; to give a specific example I made a wrapper around a linear programming solver with input arguments 'c' (a vector), 'G' (a matrix) and 'h' (a vector). This routine outputs a status string, a vector 'x' and a vector 'z', and I used an IDL as string lp ([in] sequence < sequence <double> > c, [in] com::sun::star::table::XCellRange x, [in] sequence < sequence <double> > G, [in] sequence < sequence <double> > h, [in] com::sun::star::table::XCellRange z); i.e., I used the XCellRange as a way to return additional variables by "writing back" values in x and z. This approach works well for me in Python. The only problem I have left is that I have optional arguments, some of which are also "output" argument (albeit, in the sense of being XCellRanges), but that's not possible with a Calc add-in. So my work-around for this problem is to have a different function 'lp2' with more arguments (all mandatory). But in summary, the Python UNO bridge seems fine for writing add-ins. Joachim On 5/25/07, Andreas Saeger <[hidden email]> wrote: > > Joachim Dahl wrote: > > me. > > > > So my problem is not so much of how to write a prototype of an > addin, but > > rather how to > > implement something more advanced like "methodFour". > > > > Joachim > > > > Hi Joachim, > http://wiki.services.openoffice.org/wiki/CompleteAddIn has a chapter > "Transforming sequence of sequence into array". This describes the same > problem as I tried to explain with my simple Basic function TWICE(array) > in http://www.oooforum.org/forum/viewtopic.phtml?t=57196. Only > array-functions, such as the built-in LINEST, can return more than one > value. The resulting array has to be converted to a 2-dimensional matrix > rather than a list of lists. So it is possible that python alone is not > suitable for add-ins that deal with sheet functions in array context. > > Andreas > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email] > For additional commands, e-mail: [hidden email] > > |
In reply to this post by Joachim Dahl-2
Hi Joachim,
glad to hear that your add-in works now. Well not everything was possible but you are able to workaround the multiple returns. I would suggest that you take a look on new add-in with a configuration file to define the new add-in function 8with localization if you want). This new approach has several advantages and it would be nice if you can support this. I know it is not so important for you because your add-in works but i think it is worth to support the latest features at least for new implementations. Or did you already have an xcu file for your add-in? Juergen Joachim Dahl wrote: > Hi Andreas, > > I think the Python UNO bridge works fine; I managed to return an array of > values by using an array function. > I needed to return multiple arrays of different lengths; to give a > specific > example I made a wrapper around a > linear programming solver with input arguments 'c' (a vector), 'G' (a > matrix) and 'h' (a vector). This routine > outputs a status string, a vector 'x' and a vector 'z', and I used an > IDL > as > > string lp ([in] sequence < sequence <double> > c, > [in] com::sun::star::table::XCellRange x, > [in] sequence < sequence <double> > G, > [in] sequence < sequence <double> > h, > [in] com::sun::star::table::XCellRange z); > > i.e., I used the XCellRange as a way to return additional variables by > "writing back" values in x and z. This approach > works well for me in Python. > > The only problem I have left is that I have optional arguments, some of > which are also "output" argument (albeit, in > the sense of being XCellRanges), but that's not possible with a Calc > add-in. So my work-around for this problem > is to have a different function 'lp2' with more arguments (all mandatory). > > But in summary, the Python UNO bridge seems fine for writing add-ins. > > Joachim > > > > On 5/25/07, Andreas Saeger <[hidden email]> wrote: >> >> Joachim Dahl wrote: >> > me. >> > >> > So my problem is not so much of how to write a prototype of an >> addin, but >> > rather how to >> > implement something more advanced like "methodFour". >> > >> > Joachim >> > >> >> Hi Joachim, >> http://wiki.services.openoffice.org/wiki/CompleteAddIn has a chapter >> "Transforming sequence of sequence into array". This describes the same >> problem as I tried to explain with my simple Basic function TWICE(array) >> in http://www.oooforum.org/forum/viewtopic.phtml?t=57196. Only >> array-functions, such as the built-in LINEST, can return more than one >> value. The resulting array has to be converted to a 2-dimensional matrix >> rather than a list of lists. So it is possible that python alone is not >> suitable for add-ins that deal with sheet functions in array context. >> >> Andreas >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [hidden email] >> For additional commands, e-mail: [hidden email] >> >> > --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
Hi Juergen,
I haven't seen anything about xcu files. I just zip up the META-INF files, the python script, and the binary version of my IDL file. Do you have a pointer handy to the newest format? thanks joachim On 5/25/07, Juergen Schmidt <[hidden email]> wrote: > > Hi Joachim, > > glad to hear that your add-in works now. Well not everything was > possible but you are able to workaround the multiple returns. > > I would suggest that you take a look on new add-in with a configuration > file to define the new add-in function 8with localization if you want). > This new approach has several advantages and it would be nice if you can > support this. I know it is not so important for you because your add-in > works but i think it is worth to support the latest features at least > for new implementations. > > Or did you already have an xcu file for your add-in? > > Juergen > > Joachim Dahl wrote: > > Hi Andreas, > > > > I think the Python UNO bridge works fine; I managed to return an array > of > > values by using an array function. > > I needed to return multiple arrays of different lengths; to give a > > specific > > example I made a wrapper around a > > linear programming solver with input arguments 'c' (a vector), 'G' (a > > matrix) and 'h' (a vector). This routine > > outputs a status string, a vector 'x' and a vector 'z', and I used an > > IDL > > as > > > > string lp ([in] sequence < sequence <double> > c, > > [in] com::sun::star::table::XCellRange x, > > [in] sequence < sequence <double> > G, > > [in] sequence < sequence <double> > h, > > [in] com::sun::star::table::XCellRange z); > > > > i.e., I used the XCellRange as a way to return additional variables by > > "writing back" values in x and z. This approach > > works well for me in Python. > > > > The only problem I have left is that I have optional arguments, some of > > which are also "output" argument (albeit, in > > the sense of being XCellRanges), but that's not possible with a Calc > > add-in. So my work-around for this problem > > is to have a different function 'lp2' with more arguments (all > mandatory). > > > > But in summary, the Python UNO bridge seems fine for writing add-ins. > > > > Joachim > > > > > > > > On 5/25/07, Andreas Saeger <[hidden email]> wrote: > >> > >> Joachim Dahl wrote: > >> > me. > >> > > >> > So my problem is not so much of how to write a prototype of an > >> addin, but > >> > rather how to > >> > implement something more advanced like "methodFour". > >> > > >> > Joachim > >> > > >> > >> Hi Joachim, > >> http://wiki.services.openoffice.org/wiki/CompleteAddIn has a chapter > >> "Transforming sequence of sequence into array". This describes the same > >> problem as I tried to explain with my simple Basic function > TWICE(array) > >> in http://www.oooforum.org/forum/viewtopic.phtml?t=57196. Only > >> array-functions, such as the built-in LINEST, can return more than one > >> value. The resulting array has to be converted to a 2-dimensional > matrix > >> rather than a list of lists. So it is possible that python alone is not > >> suitable for add-ins that deal with sheet functions in array context. > >> > >> Andreas > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: [hidden email] > >> For additional commands, e-mail: [hidden email] > >> > >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [hidden email] > For additional commands, e-mail: [hidden email] > > |
The XCU most point to your script so it can be properly packaged, there is
a site that is pretty good: http://udk.openoffice.org/python/scriptingframework/index.html it list on 3 stages, the application level, the document level and the add-in level. On Sat, 26 May 2007 04:24:49 -0500, Joachim Dahl <[hidden email]> wrote: > Hi Juergen, > > I haven't seen anything about xcu files. I just zip up the META-INF > files, > the python script, and the binary > version of my IDL file. > > Do you have a pointer handy to the newest format? > > thanks > joachim > > > On 5/25/07, Juergen Schmidt <[hidden email]> wrote: >> >> Hi Joachim, >> >> glad to hear that your add-in works now. Well not everything was >> possible but you are able to workaround the multiple returns. >> >> I would suggest that you take a look on new add-in with a configuration >> file to define the new add-in function 8with localization if you want). >> This new approach has several advantages and it would be nice if you can >> support this. I know it is not so important for you because your add-in >> works but i think it is worth to support the latest features at least >> for new implementations. >> >> Or did you already have an xcu file for your add-in? >> >> Juergen >> >> Joachim Dahl wrote: >> > Hi Andreas, >> > >> > I think the Python UNO bridge works fine; I managed to return an >> array >> of >> > values by using an array function. >> > I needed to return multiple arrays of different lengths; to give a >> > specific >> > example I made a wrapper around a >> > linear programming solver with input arguments 'c' (a vector), 'G' (a >> > matrix) and 'h' (a vector). This routine >> > outputs a status string, a vector 'x' and a vector 'z', and I used >> an >> > IDL >> > as >> > >> > string lp ([in] sequence < sequence <double> > c, >> > [in] com::sun::star::table::XCellRange x, >> > [in] sequence < sequence <double> > G, >> > [in] sequence < sequence <double> > h, >> > [in] com::sun::star::table::XCellRange z); >> > >> > i.e., I used the XCellRange as a way to return additional variables by >> > "writing back" values in x and z. This approach >> > works well for me in Python. >> > >> > The only problem I have left is that I have optional arguments, some >> of >> > which are also "output" argument (albeit, in >> > the sense of being XCellRanges), but that's not possible with a Calc >> > add-in. So my work-around for this problem >> > is to have a different function 'lp2' with more arguments (all >> mandatory). >> > >> > But in summary, the Python UNO bridge seems fine for writing add-ins. >> > >> > Joachim >> > >> > >> > >> > On 5/25/07, Andreas Saeger <[hidden email]> wrote: >> >> >> >> Joachim Dahl wrote: >> >> > me. >> >> > >> >> > So my problem is not so much of how to write a prototype of an >> >> addin, but >> >> > rather how to >> >> > implement something more advanced like "methodFour". >> >> > >> >> > Joachim >> >> > >> >> >> >> Hi Joachim, >> >> http://wiki.services.openoffice.org/wiki/CompleteAddIn has a chapter >> >> "Transforming sequence of sequence into array". This describes the >> same >> >> problem as I tried to explain with my simple Basic function >> TWICE(array) >> >> in http://www.oooforum.org/forum/viewtopic.phtml?t=57196. Only >> >> array-functions, such as the built-in LINEST, can return more than >> one >> >> value. The resulting array has to be converted to a 2-dimensional >> matrix >> >> rather than a list of lists. So it is possible that python alone is >> not >> >> suitable for add-ins that deal with sheet functions in array context. >> >> >> >> Andreas >> >> >> >> --------------------------------------------------------------------- >> >> To unsubscribe, e-mail: [hidden email] >> >> For additional commands, e-mail: [hidden email] >> >> >> >> >> > >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [hidden email] >> For additional commands, e-mail: [hidden email] >> >> -- Alexandro Colorado OpenOffice.org Community Contact // Mexico http://www.openoffice.org Twitter: http://www.twitter.com/jza Jabber: [hidden email] --------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email] |
I have a final question that I'd like to ask you...
I developed and tested the add-in on Ubuntu Feisty that comes with OOo 2.2.0. The add-in seems to work perfectly, but the function wizard is incredibly sluggish for my add-in (and not for the regular OOo add-in)... For example, when I select a parameter in the add-in, CPU usage goes to 100% and it takes 5-10s before the new parameter and the corresponding cell ranges are highlighted - and the examples are all very small. But if I update the add-in parameters directly without the function wizard it works fast and smoothly, so it's not just simply Python being slow. Can anyone think of a potential error in the add-in that would cause this? If I can solve this problem also, I will be happy to the add a small Python example to the wiki in return for all your help... thanks Joachim |
Powered by Nabble | Edit this page |