Quantcast
Channel: Apache OpenOffice Community Forum
Viewing all articles
Browse latest Browse all 1281

Macros and UNO API • Re: [Solved] How to pass parameters to a java macro from a Basic one

$
0
0
I was wrong saying the documentation is obsolete. It is just very incomplete and ambiguous.
Following the documentation, one's declare a Java script by

Code:

public static String maMacro(XScriptContext xScriptContext, Object[] args)
This works only if the array contains at least two different types. For instance :

Code:

Dim param1(2)param1(0)=thisComponent : param1(1)="un texte" : param1(2) = 12.5Dim params(0) : params(0) = param1result = script.invoke(params, outIndex, outParams)
The java script receives args[0] with type "any", args[1] with type String et args[2] with type Double.
But the script will not be called if parameters have the same type.

Code:

param1(0)="un texte": param1(1)="un deuxième texte" : param1(2) = "un troisième texte"]
In that case, the macro must be declared with the right type :

Code:

public static String maMacro(XScriptContext xScriptContext, String[] args)
And the use of an array is not compulsory. One can use a unique parameter, but with the proper type (Object is not possible). For instance :

Code:

public static String maMacro(XScriptContext xScriptContext, Double args)
can be called by :

Code:

Dim D as Double : D = 12.5Dim params(0) : params(0) = Dresult = script.invoke(params, outIndex, outParams)
Passing numeric parametres that way is not really practical, for the ScriptManager doesn't consider the type delared by Dim, but only the real value of the data. That means that, if you change D = 12.5 by D = 12.0, the Script Manager will consider the data as a Byte, and the java macro will be called only if it declares the parameter by 'Byte args' (or 'Short args' or 'Long args' depending of the shortest bit number necessary to code the data). That means that the only secure way to transmit a numerical value is to put it in an array for an Objet[] parameter, and to cope with the different types received.

And last, it is possible to have as many parameters as desired in the Java prototype. For instance

Code:

public static String maMacro(XScriptContext xScriptContext, String[] args1, Object[] args2, String args3, ActionEvent args4)
where all parameters must follow the above rules.

Statistics: Posted by Cormic — Sat Aug 10, 2024 2:39 pm



Viewing all articles
Browse latest Browse all 1281

Trending Articles