OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Javascript Macros & Forms in Writer

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
nadim_sd
General User
General User


Joined: 04 Oct 2006
Posts: 30
Location: Lebanon

PostPosted: Wed Oct 04, 2006 1:02 am    Post subject: Javascript Macros & Forms in Writer Reply with quote

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
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Wed Oct 04, 2006 1:55 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nadim_sd
General User
General User


Joined: 04 Oct 2006
Posts: 30
Location: Lebanon

PostPosted: Thu Oct 05, 2006 2:02 am    Post subject: Reply with quote

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
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Thu Oct 05, 2006 7:11 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group