Here is a sample:
Sub CallJavaScript
Dim oScriptProvider, oScript
Dim aParams(), aOutParamIndex(), aOutParam()
oScriptProvider = ThisComponent.getScriptProvider()
oScript = oScriptProvider.getScript( "vnd.sun.star.script:ModifySpreadsheet.modifysheet.js?language=JavaScript&location=user" )
oScript.invoke( aParams(), aOutParamIndex(), aOutParam() )
End Sub
The only lines which should need changes:
-
if any parameter need to be sent they go in aParams()
-
in defining string for oScript provider, actual script name & location of script (here it is user)
-
if return it needs adding on ‘invoke’ line
Please refer to docs mentioned by @mauricio
Edit 7/20/2017:
The problems you are having all have to do with parameters. While I have barely dealt with javascript (only second time), from using python I was fairly certain ‘aOutParamIndex()’ & ‘aOutParam’ did not come into play here.
The parameters in javascript enter as ARGUMENTS[0]
and the only way I found for a return was eval
.
Here is the code I used successfully:
BASIC:
Sub PrintMyOS
Dim aParams(0), aOutParamIndex(0), aOutParam(0)
Dim MyArray(2)
MyArray(0)="John O'Brien"
MyArray(1)="'"
MyArray(2)="''"
aParams(0) = MyArray
oScriptProvider = ThisComponent.getScriptProvider()
'Replace this oScript line with yours'
oScript = oScriptProvider.getScript( "vnd.sun.star.script:ModifySpreadsheet.modifysheet.js?language=JavaScript&location=user" )
x = oScript.invoke( aParams, aOutParamIndex, aOutParam)
print x
End Sub
FYI: I placed my script in the user section under the folder ModifySpreadsheet
with a script name of modifysheet.js
. Not descriptive for this application; was just using something had from first go at a javascript. But this should explain my oScript statement.
javascript:
event = ARGUMENTS[0];
strSearch = event[0];
strRegexp = event[1];
strReplace = event[2];
var strFixedIt = jsRegExp(strSearch, strRegexp, strReplace);
eval(strFixedIt);
function jsRegExp(strSearch, strRegexp, strReplace) {
var strRetVal;
strRetVal = strSearch.replace(strRegexp, strReplace);
return strRetVal;
}
Now I’ll be the first to say the javascript code may not be the best but it works.
Edit 08/10/2017:
Here’s where I am. Took your code (copy & paste) and names and set up in user directory:
You can see my original ‘ModifySpreadsheet’ just below the script with your names.
Used your Basic code & Javascript code (BOTH ways) and got the correct result each time:
BTW your comment about eval() not needed worked for me this time (may have some ideas about that). Again, only second time ever using Javascript.
Now the only thing never discussed has been ‘parcel-descriptor.xml’. Here is the contents I used on this last test:
<?xml version="1.0" encoding="UTF-8"?>
<!--
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->
<parcel language="JavaScript" xmlns:parcel="scripting.dtd">
<script language="JavaScript">
<locale lang="en">
<displayname value="ModifySpreadsheet"/>
<description>Just a test
</description>
</locale>
<functionname value="jsRegExp.js"/>
<logicalname value="abpaJavaScripts.JavaScript"/>
</script>
</parcel>
At this point I have no other answers. Everything I try works. My code, your code both ways. Have executed the code from the Basic IDE and from a push button on a spreadsheet, all with the correct results. Can’t get the error you seem to get.
If this answers your question please click on the (upper left area of my answer).