You are viewing a plain text version of this content. The canonical link for it is here.
Posted to api@openoffice.apache.org by "Igal @ Lucee.org" <ig...@lucee.org> on 2015/03/13 06:50:08 UTC

OpenOffice Calc Javascript Function

Hi all,

I want to write a simple function to be used in OpenOffice Calc.

It would return true if the cell's value contains a substring, e.g.

|function contains(string, substring) {

    return (string.indexOf(substring) > -1);
}|

But I can't find any documentation or examples on that.

Any ideas?

TIA

p.s. this is a crosspost with
http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function

-- 

Igal Sapir
Lucee Core Developer
Lucee.org <http://lucee.org/>


Re: OpenOffice Calc Javascript Function

Posted by "Igal @ Lucee.org" <ig...@lucee.org>.
got it, thanks.  it's possible that I had tried to use Ctrl-S instead of
Ctrl-N.

I am trying to call one function from another, but I guess I need to
"import" it somehow first?

I have that same JSLib1 module in the user location, and in it two
functions:  StringContains(input, substring, isCaseSensitive) and
SomeFunction(input)

SomeFunction(input) attempts to call StringContainse(), but I get an
exception of type
com.sun.star.script.provider.ScriptFrameworkErrorException:
ReferenceError: "StringContains" is not defined.

I expected it to find another function that is in the same module.

any idea how to "import" the other function?

thank you very much,

Igal Sapir
Lucee Core Developer
Lucee.org <http://lucee.org/>

On 3/15/2015 1:02 PM, Bernard Marcelly wrote:
> Igal @ Lucee.org a écrit le 2015-03-15 20:41 :
>> it works.  thank you Bernard!
>>
>> one last question -- how do I get the libraries to "refresh"?  when I
>> make a change to the libraries, sometimes it's visible immediately, and
>> some times I need to close OpenOffice and re-open it in order to make
>> sure that the new code is running.  is there a better way to refresh the
>> libs?
>
> I have not seen such problem, using the Rhino JavaScript debugger.
> Don't forget to save from the Rhino window (Ctrl-N or File > Save).
>
>>
>> thanks,
>>
>> Igal Sapir
>> Lucee Core Developer
>> Lucee.org <http://lucee.org/>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: api-help@openoffice.apache.org
>


Re: OpenOffice Calc Javascript Function

Posted by Bernard Marcelly <ma...@club-internet.fr>.
Igal @ Lucee.org a écrit le 2015-03-15 20:41 :
> it works.  thank you Bernard!
>
> one last question -- how do I get the libraries to "refresh"?  when I
> make a change to the libraries, sometimes it's visible immediately, and
> some times I need to close OpenOffice and re-open it in order to make
> sure that the new code is running.  is there a better way to refresh the
> libs?

I have not seen such problem, using the Rhino JavaScript debugger.
Don't forget to save from the Rhino window (Ctrl-N or File > Save).

>
> thanks,
>
> Igal Sapir
> Lucee Core Developer
> Lucee.org <http://lucee.org/>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
For additional commands, e-mail: api-help@openoffice.apache.org


Re: OpenOffice Calc Javascript Function

Posted by "Igal @ Lucee.org" <ig...@lucee.org>.
it works.  thank you Bernard!

one last question -- how do I get the libraries to "refresh"?  when I
make a change to the libraries, sometimes it's visible immediately, and
some times I need to close OpenOffice and re-open it in order to make
sure that the new code is running.  is there a better way to refresh the
libs?

thanks,

Igal Sapir
Lucee Core Developer
Lucee.org <http://lucee.org/>

On 3/15/2015 7:21 AM, Bernard Marcelly wrote:
> Sorry, correction of my correction...
>
> if the JavaScript script is in My Macros the location becomes user
>
> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=user")
>
>
> The URI spec of the Scripting Framework is somewhat complex...
> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification>
>
>
>>
>> Bernard Marcelly a écrit le 2015-03-15 14:56 :
>>>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>>>> - Create in your document a JavaScript library named Library1
>>>
>>> You created the macro in OpenOffice application (My Macros), not in
>>> the document.
>>>
>>> If you really want to have your macro in My Macros, you have to
>>> change the call
>>> to the macro:
>>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=share")
>>>
>>>
>>>
>>> The location is changed from "document" to "share"
>>>
>>>> when I enter into a cell =contains("abc", "b") I get Err:508
>>> In a Calc function, arguments are separated by ; instead of ,
>>>
>>> Reminder : your Basic function contains() must be in the Standard
>>> library of
>>> your document. Not in the Standard library of My Macros (getting the
>>> Script
>>> Provider would be different).
>>>
>>> Bernard
>>>
>>>
>>> Igal @ Lucee.org a écrit le 2015-03-14 23:43 :
>>>> hi Bernard,
>>>>
>>>> thank you for your reply.  I created a javascript library named
>>>> "JSLib1" and in
>>>> it a macro named "StringContains", and I placed in it your example
>>>> JS code.
>>>>
>>>>
>>>>
>>>> then in OpenOffice Basic Macros I have the module "Module1".  you
>>>> can see the
>>>> code at
>>>> https://gist.github.com/TwentyOneSolutions/38f38c1ebf99a85b6da5
>>>>
>>>> when I enter into a cell =getScriptVersion() I see the expected
>>>> value of 1.04
>>>>
>>>> when I enter into a cell =contains("abc", "b") I get Err:508 which
>>>> according to
>>>> https://wiki.openoffice.org/wiki/Calc_Error_Codes means that a
>>>> parenthesis is
>>>> not closed properly.
>>>>
>>>> trying to call the macro TestJsCall shows the following error:
>>>>
>>>>
>>>>
>>>> Macro Library JSLib1 not found, which seems to be different from
>>>> Err:508.
>>>>
>>>> what am I missing?
>>>>
>>>> thanks again,
>>>>
>>>> Igal Sapir
>>>> Lucee Core Developer
>>>> Lucee.org <http://lucee.org/>
>>>>
>>>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>>>>> Hi,
>>>>>
>>>>> If you intend to use a JavaScript function in a cell formula,
>>>>> forget about it.
>>>>> First, you cannot directly call a JavaScript function from a cell
>>>>> formula. The
>>>>> formula should call a Basic function that calls the JavaScript
>>>>> function.
>>>>> Second, the formula will provide an error at document loading,
>>>>> because at this
>>>>> instant the script library is not yet accessible.
>>>>> _____________
>>>>>
>>>>> If you want to use a JavaScript function from a Basic macro, you
>>>>> have to use
>>>>> the script provider.
>>>>>
>>>>> Suppose you want to create function ExistsTerm(arg1, arg2) in
>>>>> JavaScript.
>>>>>
>>>>> - Create in your document a JavaScript library named Library1
>>>>> - Then create a JavaScript module named : ExistsTerm
>>>>> - Edit this module : replace all the lines by this line:
>>>>> ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
>>>>>   Explanations:
>>>>> - The name of the function is the name of the module.
>>>>> - ARGUMENTS is an array of all the arguments.
>>>>> - The result of the function is by default the last evaluation.
>>>>>
>>>>> Here is a Basic macro that uses the JavaScript function.
>>>>> Attention, the email
>>>>> has split long lines.
>>>>>
>>>>> Sub Main3
>>>>> Dim oDocScriptProvider As Object, oScript As Object
>>>>> Dim result As Variant
>>>>>
>>>>> oDocScriptProvider = ThisComponent.getScriptProvider()
>>>>> oScript =
>>>>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
>>>>>
>>>>>
>>>>>
>>>>> result = oScript.invoke(Array("Apache OpenOffice is the best",
>>>>> "OpenOffice"),
>>>>> Array(), Array())
>>>>> MsgBox("Term exists : " & result)
>>>>> End Sub
>>>>>
>>>>> The Scripting Framework is described in the Developer's Guide
>>>>> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> The arguments of the invoke method is described in
>>>>> <https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
>>>>>> Hi all,
>>>>>>
>>>>>> I want to write a simple function to be used in OpenOffice Calc.
>>>>>>
>>>>>> It would return true if the cell's value contains a substring, e.g.
>>>>>>
>>>>>> |function contains(string, substring) {
>>>>>>
>>>>>>      return (string.indexOf(substring) > -1);
>>>>>> }|
>>>>>>
>>>>>> But I can't find any documentation or examples on that.
>>>>>>
>>>>>> Any ideas?
>>>>>>
>>>>>> TIA
>>>>>>
>>>>>> p.s. this is a crosspost with
>>>>>> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>>>>> For additional commands, e-mail: api-help@openoffice.apache.org
>>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>>> For additional commands, e-mail: api-help@openoffice.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>> For additional commands, e-mail: api-help@openoffice.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: api-help@openoffice.apache.org
>


Re: OpenOffice Calc Javascript Function

Posted by Bernard Marcelly <ma...@club-internet.fr>.
Sorry, correction of my correction...

if the JavaScript script is in My Macros the location becomes user

oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=user")

The URI spec of the Scripting Framework is somewhat complex...
<https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification>

>
> Bernard Marcelly a écrit le 2015-03-15 14:56 :
>>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>>> - Create in your document a JavaScript library named Library1
>>
>> You created the macro in OpenOffice application (My Macros), not in the document.
>>
>> If you really want to have your macro in My Macros, you have to change the call
>> to the macro:
>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=share")
>>
>>
>> The location is changed from "document" to "share"
>>
>>> when I enter into a cell =contains("abc", "b") I get Err:508
>> In a Calc function, arguments are separated by ; instead of ,
>>
>> Reminder : your Basic function contains() must be in the Standard library of
>> your document. Not in the Standard library of My Macros (getting the Script
>> Provider would be different).
>>
>> Bernard
>>
>>
>> Igal @ Lucee.org a écrit le 2015-03-14 23:43 :
>>> hi Bernard,
>>>
>>> thank you for your reply.  I created a javascript library named "JSLib1" and in
>>> it a macro named "StringContains", and I placed in it your example JS code.
>>>
>>>
>>>
>>> then in OpenOffice Basic Macros I have the module "Module1".  you can see the
>>> code at
>>> https://gist.github.com/TwentyOneSolutions/38f38c1ebf99a85b6da5
>>>
>>> when I enter into a cell =getScriptVersion() I see the expected value of 1.04
>>>
>>> when I enter into a cell =contains("abc", "b") I get Err:508 which according to
>>> https://wiki.openoffice.org/wiki/Calc_Error_Codes means that a parenthesis is
>>> not closed properly.
>>>
>>> trying to call the macro TestJsCall shows the following error:
>>>
>>>
>>>
>>> Macro Library JSLib1 not found, which seems to be different from Err:508.
>>>
>>> what am I missing?
>>>
>>> thanks again,
>>>
>>> Igal Sapir
>>> Lucee Core Developer
>>> Lucee.org <http://lucee.org/>
>>>
>>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>>>> Hi,
>>>>
>>>> If you intend to use a JavaScript function in a cell formula, forget about it.
>>>> First, you cannot directly call a JavaScript function from a cell formula. The
>>>> formula should call a Basic function that calls the JavaScript function.
>>>> Second, the formula will provide an error at document loading, because at this
>>>> instant the script library is not yet accessible.
>>>> _____________
>>>>
>>>> If you want to use a JavaScript function from a Basic macro, you have to use
>>>> the script provider.
>>>>
>>>> Suppose you want to create function ExistsTerm(arg1, arg2) in JavaScript.
>>>>
>>>> - Create in your document a JavaScript library named Library1
>>>> - Then create a JavaScript module named : ExistsTerm
>>>> - Edit this module : replace all the lines by this line:
>>>> ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
>>>>   Explanations:
>>>> - The name of the function is the name of the module.
>>>> - ARGUMENTS is an array of all the arguments.
>>>> - The result of the function is by default the last evaluation.
>>>>
>>>> Here is a Basic macro that uses the JavaScript function. Attention, the email
>>>> has split long lines.
>>>>
>>>> Sub Main3
>>>> Dim oDocScriptProvider As Object, oScript As Object
>>>> Dim result As Variant
>>>>
>>>> oDocScriptProvider = ThisComponent.getScriptProvider()
>>>> oScript =
>>>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
>>>>
>>>>
>>>> result = oScript.invoke(Array("Apache OpenOffice is the best", "OpenOffice"),
>>>> Array(), Array())
>>>> MsgBox("Term exists : " & result)
>>>> End Sub
>>>>
>>>> The Scripting Framework is described in the Developer's Guide
>>>> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
>>>>
>>>>
>>>>
>>>> The arguments of the invoke method is described in
>>>> <https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
>>>>> Hi all,
>>>>>
>>>>> I want to write a simple function to be used in OpenOffice Calc.
>>>>>
>>>>> It would return true if the cell's value contains a substring, e.g.
>>>>>
>>>>> |function contains(string, substring) {
>>>>>
>>>>>      return (string.indexOf(substring) > -1);
>>>>> }|
>>>>>
>>>>> But I can't find any documentation or examples on that.
>>>>>
>>>>> Any ideas?
>>>>>
>>>>> TIA
>>>>>
>>>>> p.s. this is a crosspost with
>>>>> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>>>> For additional commands, e-mail: api-help@openoffice.apache.org
>>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>> For additional commands, e-mail: api-help@openoffice.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: api-help@openoffice.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
For additional commands, e-mail: api-help@openoffice.apache.org


Re: OpenOffice Calc Javascript Function

Posted by Bernard Marcelly <ma...@club-internet.fr>.
Correction:

if the JavaScript script is in My Macros the location becomes application

oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=application")



Bernard Marcelly a écrit le 2015-03-15 14:56 :
>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>> - Create in your document a JavaScript library named Library1
>
> You created the macro in OpenOffice application (My Macros), not in the document.
>
> If you really want to have your macro in My Macros, you have to change the call
> to the macro:
> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=share")
>
> The location is changed from "document" to "share"
>
>> when I enter into a cell =contains("abc", "b") I get Err:508
> In a Calc function, arguments are separated by ; instead of ,
>
> Reminder : your Basic function contains() must be in the Standard library of
> your document. Not in the Standard library of My Macros (getting the Script
> Provider would be different).
>
> Bernard
>
>
> Igal @ Lucee.org a écrit le 2015-03-14 23:43 :
>> hi Bernard,
>>
>> thank you for your reply.  I created a javascript library named "JSLib1" and in
>> it a macro named "StringContains", and I placed in it your example JS code.
>>
>>
>>
>> then in OpenOffice Basic Macros I have the module "Module1".  you can see the
>> code at
>> https://gist.github.com/TwentyOneSolutions/38f38c1ebf99a85b6da5
>>
>> when I enter into a cell =getScriptVersion() I see the expected value of 1.04
>>
>> when I enter into a cell =contains("abc", "b") I get Err:508 which according to
>> https://wiki.openoffice.org/wiki/Calc_Error_Codes means that a parenthesis is
>> not closed properly.
>>
>> trying to call the macro TestJsCall shows the following error:
>>
>>
>>
>> Macro Library JSLib1 not found, which seems to be different from Err:508.
>>
>> what am I missing?
>>
>> thanks again,
>>
>> Igal Sapir
>> Lucee Core Developer
>> Lucee.org <http://lucee.org/>
>>
>> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>>> Hi,
>>>
>>> If you intend to use a JavaScript function in a cell formula, forget about it.
>>> First, you cannot directly call a JavaScript function from a cell formula. The
>>> formula should call a Basic function that calls the JavaScript function.
>>> Second, the formula will provide an error at document loading, because at this
>>> instant the script library is not yet accessible.
>>> _____________
>>>
>>> If you want to use a JavaScript function from a Basic macro, you have to use
>>> the script provider.
>>>
>>> Suppose you want to create function ExistsTerm(arg1, arg2) in JavaScript.
>>>
>>> - Create in your document a JavaScript library named Library1
>>> - Then create a JavaScript module named : ExistsTerm
>>> - Edit this module : replace all the lines by this line:
>>> ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
>>>   Explanations:
>>> - The name of the function is the name of the module.
>>> - ARGUMENTS is an array of all the arguments.
>>> - The result of the function is by default the last evaluation.
>>>
>>> Here is a Basic macro that uses the JavaScript function. Attention, the email
>>> has split long lines.
>>>
>>> Sub Main3
>>> Dim oDocScriptProvider As Object, oScript As Object
>>> Dim result As Variant
>>>
>>> oDocScriptProvider = ThisComponent.getScriptProvider()
>>> oScript =
>>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
>>>
>>> result = oScript.invoke(Array("Apache OpenOffice is the best", "OpenOffice"),
>>> Array(), Array())
>>> MsgBox("Term exists : " & result)
>>> End Sub
>>>
>>> The Scripting Framework is described in the Developer's Guide
>>> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
>>>
>>>
>>> The arguments of the invoke method is described in
>>> <https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>
>>>
>>>
>>>
>>>
>>>
>>> Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
>>>> Hi all,
>>>>
>>>> I want to write a simple function to be used in OpenOffice Calc.
>>>>
>>>> It would return true if the cell's value contains a substring, e.g.
>>>>
>>>> |function contains(string, substring) {
>>>>
>>>>      return (string.indexOf(substring) > -1);
>>>> }|
>>>>
>>>> But I can't find any documentation or examples on that.
>>>>
>>>> Any ideas?
>>>>
>>>> TIA
>>>>
>>>> p.s. this is a crosspost with
>>>> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>>> For additional commands, e-mail: api-help@openoffice.apache.org
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: api-help@openoffice.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
For additional commands, e-mail: api-help@openoffice.apache.org


Re: OpenOffice Calc Javascript Function

Posted by Bernard Marcelly <ma...@club-internet.fr>.
> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
> - Create in your document a JavaScript library named Library1

You created the macro in OpenOffice application (My Macros), not in the document.

If you really want to have your macro in My Macros, you have to change the call 
to the macro:
oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=share")
The location is changed from "document" to "share"

> when I enter into a cell =contains("abc", "b") I get Err:508
In a Calc function, arguments are separated by ; instead of ,

Reminder : your Basic function contains() must be in the Standard library of 
your document. Not in the Standard library of My Macros (getting the Script 
Provider would be different).

Bernard


Igal @ Lucee.org a écrit le 2015-03-14 23:43 :
> hi Bernard,
>
> thank you for your reply.  I created a javascript library named "JSLib1" and in
> it a macro named "StringContains", and I placed in it your example JS code.
>
>
>
> then in OpenOffice Basic Macros I have the module "Module1".  you can see the
> code at
> https://gist.github.com/TwentyOneSolutions/38f38c1ebf99a85b6da5
>
> when I enter into a cell =getScriptVersion() I see the expected value of 1.04
>
> when I enter into a cell =contains("abc", "b") I get Err:508 which according to
> https://wiki.openoffice.org/wiki/Calc_Error_Codes means that a parenthesis is
> not closed properly.
>
> trying to call the macro TestJsCall shows the following error:
>
>
>
> Macro Library JSLib1 not found, which seems to be different from Err:508.
>
> what am I missing?
>
> thanks again,
>
> Igal Sapir
> Lucee Core Developer
> Lucee.org <http://lucee.org/>
>
> On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
>> Hi,
>>
>> If you intend to use a JavaScript function in a cell formula, forget about it.
>> First, you cannot directly call a JavaScript function from a cell formula. The
>> formula should call a Basic function that calls the JavaScript function.
>> Second, the formula will provide an error at document loading, because at this
>> instant the script library is not yet accessible.
>> _____________
>>
>> If you want to use a JavaScript function from a Basic macro, you have to use
>> the script provider.
>>
>> Suppose you want to create function ExistsTerm(arg1, arg2) in JavaScript.
>>
>> - Create in your document a JavaScript library named Library1
>> - Then create a JavaScript module named : ExistsTerm
>> - Edit this module : replace all the lines by this line:
>> ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
>>   Explanations:
>> - The name of the function is the name of the module.
>> - ARGUMENTS is an array of all the arguments.
>> - The result of the function is by default the last evaluation.
>>
>> Here is a Basic macro that uses the JavaScript function. Attention, the email
>> has split long lines.
>>
>> Sub Main3
>> Dim oDocScriptProvider As Object, oScript As Object
>> Dim result As Variant
>>
>> oDocScriptProvider = ThisComponent.getScriptProvider()
>> oScript =
>> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
>> result = oScript.invoke(Array("Apache OpenOffice is the best", "OpenOffice"),
>> Array(), Array())
>> MsgBox("Term exists : " & result)
>> End Sub
>>
>> The Scripting Framework is described in the Developer's Guide
>> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
>>
>> The arguments of the invoke method is described in
>> <https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>
>>
>>
>>
>>
>> Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
>>> Hi all,
>>>
>>> I want to write a simple function to be used in OpenOffice Calc.
>>>
>>> It would return true if the cell's value contains a substring, e.g.
>>>
>>> |function contains(string, substring) {
>>>
>>>      return (string.indexOf(substring) > -1);
>>> }|
>>>
>>> But I can't find any documentation or examples on that.
>>>
>>> Any ideas?
>>>
>>> TIA
>>>
>>> p.s. this is a crosspost with
>>> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
>> For additional commands, e-mail: api-help@openoffice.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
For additional commands, e-mail: api-help@openoffice.apache.org


Re: OpenOffice Calc Javascript Function

Posted by "Igal @ Lucee.org" <ig...@lucee.org>.
hi Bernard,

thank you for your reply.  I created a javascript library named "JSLib1"
and in it a macro named "StringContains", and I placed in it your
example JS code.



then in OpenOffice Basic Macros I have the module "Module1".  you can
see the code at
https://gist.github.com/TwentyOneSolutions/38f38c1ebf99a85b6da5

when I enter into a cell =getScriptVersion() I see the expected value of
1.04

when I enter into a cell =contains("abc", "b") I get Err:508 which
according to https://wiki.openoffice.org/wiki/Calc_Error_Codes means
that a parenthesis is not closed properly.

trying to call the macro TestJsCall shows the following error:



Macro Library JSLib1 not found, which seems to be different from Err:508.

what am I missing?

thanks again,

Igal Sapir
Lucee Core Developer
Lucee.org <http://lucee.org/>

On 3/13/2015 6:09 AM, Bernard Marcelly wrote:
> Hi,
>
> If you intend to use a JavaScript function in a cell formula, forget
> about it.
> First, you cannot directly call a JavaScript function from a cell
> formula. The formula should call a Basic function that calls the
> JavaScript function.
> Second, the formula will provide an error at document loading, because
> at this instant the script library is not yet accessible.
> _____________
>
> If you want to use a JavaScript function from a Basic macro, you have
> to use the script provider.
>
> Suppose you want to create function ExistsTerm(arg1, arg2) in JavaScript.
>
> - Create in your document a JavaScript library named Library1
> - Then create a JavaScript module named : ExistsTerm
> - Edit this module : replace all the lines by this line:
> ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
>   Explanations:
> - The name of the function is the name of the module.
> - ARGUMENTS is an array of all the arguments.
> - The result of the function is by default the last evaluation.
>
> Here is a Basic macro that uses the JavaScript function. Attention,
> the email has split long lines.
>
> Sub Main3
> Dim oDocScriptProvider As Object, oScript As Object
> Dim result As Variant
>
> oDocScriptProvider = ThisComponent.getScriptProvider()
> oScript =
> oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
> result = oScript.invoke(Array("Apache OpenOffice is the best",
> "OpenOffice"), Array(), Array())
> MsgBox("Term exists : " & result)
> End Sub
>
> The Scripting Framework is described in the Developer's Guide
> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
>
> The arguments of the invoke method is described in
> <https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>
>
>
>
>
> Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
>> Hi all,
>>
>> I want to write a simple function to be used in OpenOffice Calc.
>>
>> It would return true if the cell's value contains a substring, e.g.
>>
>> |function contains(string, substring) {
>>
>>      return (string.indexOf(substring) > -1);
>> }|
>>
>> But I can't find any documentation or examples on that.
>>
>> Any ideas?
>>
>> TIA
>>
>> p.s. this is a crosspost with
>> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: api-help@openoffice.apache.org
>


Re: OpenOffice Calc Javascript Function

Posted by Bernard Marcelly <ma...@club-internet.fr>.
Hi,

If you intend to use a JavaScript function in a cell formula, forget about it.
First, you cannot directly call a JavaScript function from a cell formula. The 
formula should call a Basic function that calls the JavaScript function.
Second, the formula will provide an error at document loading, because at this 
instant the script library is not yet accessible.
_____________

If you want to use a JavaScript function from a Basic macro, you have to use the 
script provider.

Suppose you want to create function ExistsTerm(arg1, arg2) in JavaScript.

- Create in your document a JavaScript library named Library1
- Then create a JavaScript module named : ExistsTerm
- Edit this module : replace all the lines by this line:
ARGUMENTS[0].indexOf(ARGUMENTS[1]) > -1;
   Explanations:
- The name of the function is the name of the module.
- ARGUMENTS is an array of all the arguments.
- The result of the function is by default the last evaluation.

Here is a Basic macro that uses the JavaScript function. Attention, the email 
has split long lines.

Sub Main3
Dim oDocScriptProvider As Object, oScript As Object
Dim result As Variant

oDocScriptProvider = ThisComponent.getScriptProvider()
oScript = 
oDocScriptProvider.getScript("vnd.sun.star.script:Library1.ExistsTerm.js?language=JavaScript&location=document")
result = oScript.invoke(Array("Apache OpenOffice is the best", "OpenOffice"), 
Array(), Array())
MsgBox("Term exists : " & result)
End Sub

The Scripting Framework is described in the Developer's Guide
<https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework>
The arguments of the invoke method is described in
<https://www.openoffice.org/api/docs/common/ref/com/sun/star/script/XInvocation.html#invoke>



Igal @ Lucee.org a écrit le 2015-03-13 06:50 :
> Hi all,
>
> I want to write a simple function to be used in OpenOffice Calc.
>
> It would return true if the cell's value contains a substring, e.g.
>
> |function contains(string, substring) {
>
>      return (string.indexOf(substring) > -1);
> }|
>
> But I can't find any documentation or examples on that.
>
> Any ideas?
>
> TIA
>
> p.s. this is a crosspost with
> http://stackoverflow.com/questions/29025195/openoffice-calc-javascript-function
>

---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscribe@openoffice.apache.org
For additional commands, e-mail: api-help@openoffice.apache.org