| View previous topic :: View next topic |
| Author |
Message |
nadim_sd General User

Joined: 04 Oct 2006 Posts: 30 Location: Lebanon
|
Posted: Wed Oct 04, 2006 1:02 am Post subject: Javascript Macros & Forms in Writer |
|
|
Hi,
I was just wondering how can I control a form in writer using Javascript macros?
I need the macro to remove or edit a text box for example or a list box or any other element in the form.
Are there any references that explain the UnoRuntime API to help developing Javascript macros?
Thank you. |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Wed Oct 04, 2006 1:55 am Post subject: |
|
|
As far as I can see in the code examples in OpenOffice.org Macros, JavaScript is handled like to Java.
So you should study the Developer's guide (part of the SDK).
If you can use Basic, it will certainly be easier (or rather less complex). |
|
| Back to top |
|
 |
nadim_sd General User

Joined: 04 Oct 2006 Posts: 30 Location: Lebanon
|
Posted: Thu Oct 05, 2006 2:02 am Post subject: |
|
|
The following Basic code used in a macro, can access a form in my document, then get any element in the form and modify it;
- change color
- modify text
- add elements
| Code: | Sub Main
oDoc = thisComponent
oDrawPage = oDoc.getDrawPage()
oForms = oDrawPage.getForms()
oForm = oForms.getByName("Standard")
oElement = oForm.getByName("GroupBox")
oElement.Label = "test"
End Sub |
How can I do something similar in a Javascript Macro?
Is there an Object corresponding to thisComponent in Javascript? or something similar to getDrawPage function?
I basically need to access my form and edit it as an object in Javascript...
Thanks... |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Thu Oct 05, 2006 7:11 am Post subject: |
|
|
| Quote: | | Is there an Object corresponding to thisComponent in Javascript? |
If you read the automatic code created by the Javascript editor of OpenOffice you get the answer:
| Code: | // XSCRIPTCONTEXT.getDocument() returns XModel
oDoc = XSCRIPTCONTEXT.getDocument(); |
| Quote: | | something similar to getDrawPage function? |
Whatever the programming language you have to use the API of OpenOffice.org.
And .getDrawPage is part of the API. But it is not an independent function, it is a method of an UNO object. To use it in Javascript you have to do the same twists as in Java (well, it's a little simpler, as you will see).
I have tested the following code on a Writer document. It retrieves the label of a button and writes it on the Writer document. I have used most part of the example code (Hello World) and added the necessary instructions.
| Code: |
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
// add these imports for your case
importClass(Packages.com.sun.star.drawing.XDrawPageSupplier);
importClass(Packages.com.sun.star.form.XFormsSupplier);
importClass(Packages.com.sun.star.container.XNameAccess);
importClass(Packages.com.sun.star.beans.XPropertySet);
oDoc = XSCRIPTCONTEXT.getDocument();
xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc);
xText = xTextDoc.getText();
xTextRange = xText.getEnd();
xSuppPage = UnoRuntime.queryInterface( XDrawPageSupplier, oDoc);
oDrawPage = xSuppPage.getDrawPage();
xSuppForms = UnoRuntime.queryInterface( XFormsSupplier, oDrawPage);
oForms = xSuppForms.getForms();
xNameAcc = UnoRuntime.queryInterface(XNameAccess, oForms);
oForm = xNameAcc.getByName("Standard");
xNameAcc2 = UnoRuntime.queryInterface(XNameAccess, oForm);
oElement = xNameAcc2.getByName("PushButton");
xElemProps = UnoRuntime.queryInterface(XPropertySet, oElement);
// print the label string
theLabel = xElemProps.getPropertyValue("Label");
xTextRange.setString( "Label is : " + theLabel );
|
Compared to Basic code, we add lots of instructions and intermediate variables to query the interfaces supporting the methods we need. To do this you need a lot of experience in OpenOffice.org API. Read and read again the Developer's Guide.
Note: not only did I read the Developer's Guide to write this, but I created the same routine in Basic and used Xray (with Display: Details) to find the interface of the needed methods. This helped me a lot. |
|
| Back to top |
|
 |
|