| View previous topic :: View next topic |
| Author |
Message |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1313
|
Posted: Thu Jun 30, 2005 1:58 pm Post subject: Calling JavaScript from Basic: regular expressions |
|
|
With OO2.0 beta, JavaScript support has been added to OO. The JavaScript features concerning regular expressions are more elaborate and easier to handle than the UNO approach. For example, in UNO there is no way to address the () subexpressions in the replace term.
This shows (i) how to call JavaScript from BASIC and (ii) applies that to a regular expression replace function, applicable to BASIC strings.
The BASIC: | Code: | Sub Main
'use this, if you install the JavaScript subroutine in My Macros.
'Then the Javascript routine is available in all documents
'
oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScriptReplace = oMasterScriptProvider.getScript("vnd.sun.star.script:Tools.Replace.js?language=JavaScript&location=user")
'use this, if you install the JavaScript locally in the current doc
'
'oDocScriptProvider = ThisComponent.getScriptProvider()
'oScriptReplace = oDocScriptProvider.getScript("vnd.sun.star.script:Library1.Replace.js?language=JavaScript&location=document")
sReturn = RegExpReplace(oScriptReplace, "127,0,0,1", "([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})", "gi", "$1 A $2 B $3 C $4")
msgbox sReturn
end sub
function RegExpReplace(oScriptReplace as Object, sSource as String, sRegExp as String, sGlobUpcase as String, sReplace as String) as String
RegExpReplace = oScriptReplace.invoke(Array(sSource, sRegExp, sGlobUpcase, sReplace ), Array(), Array())
end function
|
You have to install the following routine in JavaScript / My Macros in a Library "Tools" under the name Replace.js | Code: | sSource = String(ARGUMENTS[0])
sRegExp = String(ARGUMENTS[1])
sGlobUpcase = String(ARGUMENTS[2])
sReplace = String(ARGUMENTS[3])
myRe = new RegExp(sRegExp, sGlobUpcase)
ARGUMENTS[0] = sSource.replace(myRe, sReplace) |
|
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1572 Location: Frankfurt, Germany
|
Posted: Fri Jul 01, 2005 6:58 am Post subject: |
|
|
Hi there, nice example but things still seem to be pretty cryptic.
What do you want to do here: sReturn = RegExpReplace(oScriptReplace, "127,0,0,1", "([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})", "gi", "$1 A $2 B $3 C $4") ?
Looks ugly, doesn't it? :/
Christian _________________ - Knowledge is Power - |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1313
|
Posted: Fri Jul 01, 2005 7:20 am Post subject: |
|
|
looks ugly, indeed. But regular expressions pay for their strength by ugliness ...
I admit, I have not shown a particular useful example, but one example, which demos a Javascript regexp capability, which UNO regexp does not have: The usage of subexpressions, marked by (...), in the replace term (the $s).
So
"127,0,0,1"
is expected to transform to
"127 A 0 B 0 C 1"
You cant do that by UNO regexp to the best of my knowledge. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 4021 Location: Lawrence, Kansas, USA
|
Posted: Wed Jul 06, 2005 8:25 am Post subject: |
|
|
The key useful technique I see is how to call a function written in another scripting language, passing parameters, and returning a result.
The Regular Expressions example is but one useful application of this technique. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1313
|
|
| Back to top |
|
 |
tulipa Newbie

Joined: 06 Dec 2008 Posts: 1
|
Posted: Wed Dec 10, 2008 5:26 am Post subject: |
|
|
How do you get rid of JavaScript worms on your PC? Apparently I was infected with JavaScript worms, and I can't go into some forums without getting banned from them for "posting" them! How can I get rid of them? _________________ what is javascript |
|
| Back to top |
|
 |
xavic69 Newbie

Joined: 15 Mar 2009 Posts: 3
|
Posted: Wed Mar 25, 2009 1:19 pm Post subject: |
|
|
| Quote: |
| Code: |
sSource = String(ARGUMENTS[0])
sRegExp = String(ARGUMENTS[1])
sGlobUpcase = String(ARGUMENTS[2])
sReplace = String(ARGUMENTS[3])
myRe = new RegExp(sRegExp, sGlobUpcase)
ARGUMENTS[0] = sSource.replace(myRe, sReplace)
|
|
This JavaScript code gives me to Frame Script errors on OpenOffice 3.0:
1) "invalid flag after regular expression", and
2) "Cannot read property "0" from null"
Error #1 is corrected by not using the String casting, and error #2 using a returning variable different from ARGUMENTS[i], so
| Code: |
sSource = String(ARGUMENTS[0])
sRegExp = String(ARGUMENTS[1])
sGlobUpcase = ARGUMENTS[2])
sReplace = String(ARGUMENTS[3])
myRe = new RegExp(sRegExp, sGlobUpcase)
ret = sSource.replace(myRe, sReplace)
|
works fine. |
|
| Back to top |
|
 |
zottona Newbie

Joined: 14 Oct 2009 Posts: 1
|
Posted: Wed Oct 21, 2009 2:23 am Post subject: |
|
|
I'm not allowed to use spellcheck, cannot edit my questions, cannot press "show" on hidden (-5) answers. It says "must have Javascript enabled." I've gone to Tools, Internet Options, followed that whole thing. I've gone to Java.com and downloaded the latest version. Any other ideas?
__________________
keyword research ~ keyword tool ~ keyword tracking ~ affiliate elite |
|
| Back to top |
|
 |
wseverin Newbie

Joined: 29 Jul 2010 Posts: 1
|
Posted: Thu Jul 29, 2010 3:36 pm Post subject: |
|
|
With very minor tweaking this becomes a decent regex find/replace function for use with Calc. I'm going to include a bit more detail in this description for the benefit of folks who don't want to become experts; they just want to get some work done.
I'm here using OpenOffice 3.2, build 9483. If you're using a different version YMMV.
What we're going to do is create a function called RegRep that can be used just like any other function in a Calc sheet cell. It will be called with four string arguments:
1) sSource — the text to be searched
2) sRegExp — the regex search pattern
3) sGlobUpcase — the search rules; "g" for global, "i" for case insensitive
4) sReplace — the replacement string/pattern
Here's an example of its use to return the source string in A2 with all of the lower case vowels removed except where they are followed by a space or the end of the string:
| Code: | | =REGREP(A2,"[aeiou](?!( |$))","g","") |
This requires two snippets of code; one in ooBasic and the other in javascript. The javascript is necessary because it has decent regex handling ability whereas ooBasic does not.
To create the ooBasic code, create a new basic module in My Macros and paste in the code. Step-by-step: First, create a new macros file. To do this, on the Calc tool bar select: Tools–>Macros–>Organize Dialogs... and select the Modules tab. Select 'New' and name the module whatever you like; I called mine RegReplace. Then make sure your new module is highlighted and click 'Edit'. Copy the code below and paste it into the editor window. Then close the window.
| Code: | function RegRep (sSource as String, sRegExp as String, sGlobUpcase as String, sReplace as String) as String
oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScriptReplace = oMasterScriptProvider.getScript("vnd.sun.star.script:Tools.Replace.js?language=JavaScript&location=user")
RegRep = oScriptReplace.invoke(Array(sSource, sRegExp, sGlobUpcase, sReplace ), Array(), Array())
end function |
To create the necessary javascript code we do something similar (but pay attention to the differences). From the toolbar select: Tools–>Macros–>Organize Macros–>Javascript... In the dialog that appears make sure My Macros is highlighted and click 'Create...'. In the new dialog window type in the name of the library, in this case 'Tools'. (Note- if you decide to use different names for the library or the javascript source, make sure you also make appropriate changes to the definition of oScriptReplace back in the ooBasic code.) Close the library name dialog. On the javascript macros window click 'Create...'. Another window will pop up asking you to name the javascript macro. Call it 'Replace' and close the window. (The window dialog code will automatically append a '.js' to the name.) Make sure the name of your javascript source is highlighted in the Javascript Macros window and click 'Edit'. Delete everything in the window (there may be a "Hello World" example). Copy the code below and paste it into the editor window:
| Code: | sSource = String(ARGUMENTS[0]);
sRegExp = String(ARGUMENTS[1]);
sGlobUpcase = ARGUMENTS[2];
sReplace = String(ARGUMENTS[3]);
myRe = new RegExp(sRegExp, sGlobUpcase);
ret = sSource.replace(myRe, sReplace); |
Then from the toolbar File–>Save, then close the editor window.
Ready to Go! |
|
| Back to top |
|
 |
|
|
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
|