[Solved] createSearchDescriptor Does Not Work

Creating a macro - Writing a Script - Using the API

[Solved] createSearchDescriptor Does Not Work

Postby Robitron » Thu Mar 22, 2018 11:05 pm

Okay, I copied and pasted this example taken from https://wiki.openoffice.org/wiki/Docume ... ity_Search
Here is the example:

Code: Select all   Expand viewCollapse view
Sub Test
Dim SearchDesc As Object
Dim Doc As Object

Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor
SearchDesc.SearchString="turnover"
SearchDesc.SearchSimilarity = True
SearchDesc.SearchSimilarityAdd = 2
SearchDesc.SearchSimilarityExchange = 2
SearchDesc.SearchSimilarityRemove = 2
SearchDesc.SearchSimilarityRelax = False
Found = Doc.findFirst (SearchDesc)

Do Until IsNull(Found)
  Found.CharWeight = com.sun.star.awt.FontWeight.BOLD
  Found = Doc.findNext( Found.End, SearchDesc)
Loop
End Sub


Here is the error:

Screenshot (21).png


So is this outdated or is there something else I'm missing?

What I am looking to do is set up a code that will search the contents of the cell and determine if it contains a phrase regardless of capitalization. So if this is outdated, could someone point me in a good direction to achieve this end? Thanks.
Last edited by robleyd on Fri Mar 23, 2018 1:16 am, edited 2 times in total.
Reason: Add green tick [robleyd, Moderator]
OpenOffice 4.1.5 on Windows 10

What am I doing here? What are you doing here? Wait, where are we?
Robitron
 
Posts: 94
Joined: Thu Nov 15, 2012 5:27 pm

Re: createSearchDescriptor Does Not Work

Postby JeJe » Thu Mar 22, 2018 11:24 pm

That macro is for a text document and won't work in a spreadsheet document. If you do a search here or google openoffice search contents of cell you should get something for a spreadsheet
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1091
Joined: Wed Mar 09, 2016 2:40 pm

Re: createSearchDescriptor Does Not Work

Postby Robitron » Fri Mar 23, 2018 12:07 am

JeJe wrote:That macro is for a text document and won't work in a spreadsheet document. If you do a search here or google openoffice search contents of cell you should get something for a spreadsheet


Wow, how did I miss that? lol Thanks. I acutally found the UCase and LCase functions so I just converted the text in the cell to lowercase with the RType = LCase(Cell1.String) and used the Select Case option to put it right.

At least it's doable but if I had several options, it would be a bit much to set up the Select Case for all of them. Fortunately, there are only two choices for this cell that are acceptable.
OpenOffice 4.1.5 on Windows 10

What am I doing here? What are you doing here? Wait, where are we?
Robitron
 
Posts: 94
Joined: Thu Nov 15, 2012 5:27 pm

Re: createSearchDescriptor works on sheets

Postby dandv » Wed Jan 15, 2020 1:23 am

JeJe wrote:That macro is for a text document and won't work in a spreadsheet document.


That is not exactly true, or useful. createSearchDescriptor is a method of individual sheets, not the whole spreadsheet document. But it does work in Calc.

JeJe wrote:If you do a search here or google openoffice search contents of cell you should get something for a spreadsheet


Really. Do try that please.

The OO/LO API is so stuffy, confusing and the documentation so spread all over the place and hard (or impossible, for JavaScript macros) to find, that it helps to be more specific than "Google it".

Here's the code to do a reverse search in LibreCalc for the text in the current cell. Useful to navigate to its last occurrence.

Code: Select all   Expand viewCollapse view
Sub FindBackwards
    doc = ThisComponent
    sheet = doc.getCurrentController.activeSheet
    search = sheet.createSearchDescriptor()
    With search
        .SearchString = doc.currentSelection.String
        .SearchCaseSensitive = true
        .SearchBackwards = true
        .SearchByRow = true
    End With

    result = sheet.findNext(doc.currentSelection, search)
    doc.CurrentController.Select(result)
End Sub
LibreOffice 6.3 on Ubuntu 18.04
dandv
 
Posts: 2
Joined: Wed Jan 15, 2020 1:18 am

Re: createSearchDescriptor works on sheets

Postby JeJe » Wed Jan 15, 2020 3:05 am

dandv wrote:That is not exactly true, or useful. createSearchDescriptor is a method of individual sheets, not the whole spreadsheet document. But it does work in Calc.


The reason the OP's code failed was because there isn't a method ThisComponent.createSearchDescriptor if its a spreadsheet. That was the problem and I think I explained it.

dandv wrote:Really. Do try that please.


I did. I suggested an exact search which I will have found yielded results I thought might be useful.

dandv wrote:The OO/LO API is so stuffy, confusing and the documentation so spread all over the place and hard (or impossible, for JavaScript macros) to find, that it helps to be more specific than "Google it".


So few people use JavaScript in OO that you're not going get much in the way of documentation. Hmmm... you seem to have resurrected this old thread mainly to have a go at me... which I find strange... especially for a first post...


Edit:
People here try and help each other... if you think someone's solution isn't quite there... well that happens a lot... all you need do is post your better one...
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1091
Joined: Wed Mar 09, 2016 2:40 pm

Re: createSearchDescriptor works on sheets

Postby dandv » Wed Jan 15, 2020 5:20 am

JeJe wrote:
dandv wrote:Really. Do try that please.


I did. I suggested an exact search which I will have found yielded results I thought might be useful.


Why not post the actual result then?

JeJe wrote:So few people use JavaScript in OO that you're not going get much in the way of documentation.


Why does a JavaScript API even exist then, if it's undocumented?

JeJe wrote:Hmmm... you seem to have resurrected this old thread mainly to have a go at me...


Huh? I have no idea who you are. I'm peeved at this archaic API. I come from the web development world, where API quality (and documentation) is paramount. Have a look at the Twitter, GitHub, or Firebase APIs to see what a well-documented non-verbose API looks like. If OO/LO were a SAAS startup competing with other modern startups, this API would absolutely not fly.

JeJe wrote:if you think someone's solution isn't quite there... well that happens a lot... all you need do is post your better one...


That's exactly what I did in the code I posted.
LibreOffice 6.3 on Ubuntu 18.04
dandv
 
Posts: 2
Joined: Wed Jan 15, 2020 1:18 am

Re: [Solved] createSearchDescriptor Does Not Work

Postby RusselB » Wed Jan 15, 2020 6:27 am

dandv wrote: I'm peeved at this archaic API.

The API, to my knowledge, falls under the same licencing agreement as the rest of the Apache OpenOffice software.
Simply put, you are free to look at and modify the software to your own needs.
OpenOffice 4.1.7 and LibreOffice 6.3.3.2 on Windows 7 Pro & Ultimate
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
RusselB
Moderator
 
Posts: 6062
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: [Solved] createSearchDescriptor Does Not Work

Postby Villeroy » Wed Jan 15, 2020 11:45 am

There is no JavaScript API. The API is the same for all languages with a lot of extra glue for StarBasic. There is a bridge for JavaScript, BeanShell and there is one for Python because these bridges had been implemented at some early stage around 2005 proving that it possible to plug any object oriented language to this API without much effort. There is also a bridge for the Rexx langauge which can be added. I've never seen any JavaScript macros other than proof of concept snippets. Number one is StarBasic, number two is Python when it comes to macro scripts. Python and Java are the commonly used languages for extensionsSince since we Basic can not define its own UNO classes. The API may be arcane but it is self-documenting. There are two similar extensions MRI and XRay to unveil the self documentation. [Tutorial] Introduction into object inspection with MRI
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: Google [Bot] and 6 guests