Ask Your Question
1

Solved: How to call JavaScript function from Basic

asked 2017-07-14 09:20:21 +0200

owlbrudder gravatar image

updated 2017-07-20 06:38:39 +0200

I have searched for this and found numerous responses for how to attach a JavaScript function to a button or event, but I have not found the answer to this question.

To explain: I want to apply a regex to a given string and return the result. I can find no way of doing this in Basic, so decided to write it in JavaScript. The function is very simple, but I can't see how to invoke it from Basic. The 'Invoke' method is self-explanatory, but it seems I need to establish some kind of environment for the invocation first.

Am I missing something obvious?

Any help would be appreciated.

Thanks, Doug


much later:

I am almost there, but not quite.

I have a javascript function:

/* Applies strReplace to all instances of strRegexp in strSearch and returns the result */
function jsRegExp(strSearch, strRegexp, strReplace) {
    var strRetVal;
    strRetVal = strSearch.replace(strRegexp, strReplace);
    return strRetVal;
}

and an LO Basic subroutine:

Sub CallJavaScript
    Dim oScriptProvider, oScript
    Dim aParams(), aOutParamIndex(), aOutParam()
    Dim MyArray(2)
    MyArray(0)="John O'Brien"
    MyArray(1)="'"
    MyArray(2)="''"
    oScriptProvider = ThisComponent.getScriptProvider()
    oScript = oScriptProvider.getScript( "vnd.sun.star.script:abpaJavaScripts.jsRegExp.js?language=JavaScript&location=document" )
    oScript.invoke( MyArray, aOutParamIndex, aOutParam)
End Sub

I intend the javascript to be extended after I get this much working.

If I call the javascript as follows, it works fine:

/* Harness to test jsRegExp */
var strFixedIt = jsRegExp("John O'Brien","'","''");

but called from the Basic sub, all I get is the arrays aOutParamIndex() and aOutParam() sized (0 to-1) in the watched variable list.

Am I trying to do something impossible, have I misunderstood what I have been told, or is my code incorrect?

It's a good thing I am doing this for charity, because I could not charge for the hours I have spent on it [grin].

As always, any help would be greatly appreciated.

edit retag flag offensive close merge delete

Comments

If the answer solves your question please tick the ✔.

m.a.riosv gravatar imagem.a.riosv ( 2017-07-16 18:33:34 +0200 )edit

Search and replace all with regular expression it's possible in Basic...

mauricio gravatar imagemauricio ( 2017-07-20 16:44:34 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2017-07-14 15:27:31 +0200

mauricio gravatar image

Look this: https://wiki.openoffice.org/wiki/Docu...

With EasyDev you can call macros in others languages: http://easydev.readthedocs.io/en/late...

Best regards

edit flag offensive delete link more

Comments

Thanks mauricio - the Wiki entry was exactly what I was looking for.

owlbrudder gravatar imageowlbrudder ( 2017-07-16 00:36:22 +0200 )edit

could you please share your solution, i try to get json object data from .js into the spreadsheet

creativeflower gravatar imagecreativeflower ( 2017-07-16 09:58:50 +0200 )edit
0

answered 2017-07-16 18:28:17 +0200

Ratslinger gravatar image

updated 2017-07-21 03:30:53 +0200

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.

If this answers your question please click on the ✔ (upper left area of my answer).

edit flag offensive delete link more

Comments

Hi Ratslinger. That is what I was looking for. I knew I had seen it somewhere, but lost track of it. Thanks very much.

owlbrudder gravatar imageowlbrudder ( 2017-07-18 09:08:44 +0200 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-07-14 09:20:21 +0200

Seen: 66 times

Last updated: Jul 21