[Solved] [C#] Get text between two ranges

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

[Solved] [C#] Get text between two ranges

Post by Lumberjack »

Hi
I have small problem . I'd like copy (get) text between two strings in document. This text should by insert at the end of second string.
This is my code.

Code: Select all


XSearchDescriptor xSearchDecriptor0 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
  xSearchDecriptor0.setSearchString("<el:strony>");

 XSearchDescriptor xSearchDecriptor1 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
  xSearchDecriptor1.setSearchString("</el:strony>");

  XTextRange  objAdresatStr = ((XTextRange)((unoidl.com.sun.star.util.XSearchable)oDoc).findFirst(xSearchDecriptor0)).getEnd();
  XTextRange  objAdresatStop = ((XTextRange)((unoidl.com.sun.star.util.XSearchable)oDoc).findFirst(xSearchDecriptor1)).getStart();

    XText xPos = (objAdresatStr).getText();

xPos.insertString(objAdresatStop , "[text between <el:strony> and </el:strony> ]" , false);

<el:strony> and </el:strony> are this two string. I what get formated text between <el:strony> and </el:strony> and insert at the and of </el:strony> string.

How can i do this?
 Edit: Topic moved from Writer forum to Macros and UNO API 
Last edited by Lumberjack on Wed Mar 03, 2021 11:48 am, edited 1 time in total.
OpenOffice 3.1 on Windows Vista
FJCC
Moderator
Posts: 9231
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Get text between two ranges

Post by FJCC »

Here is a simple Basic macro that finds the text between the two markers and copies it to a position just after the second marker. The original text is not deleted and the formatting is not copied. I do not see a way to copy the formatting if it has been applied manually. If you know that the formatting has been set with a character or paragraph style, that style could be applied to the text in the new position.

Code: Select all

Desc = ThisComponent.createSearchDescriptor()
Desc.searchString = "<el:strony>"
Rng1 = ThisComponent.findFirst(Desc)

Desc.searchString = "</el:strony>"
Rng2 = ThisComponent.findFirst(Desc)

oCurs = ThisComponent.Text.createTextCursorByRange(Rng1.End)
oCurs.gotoRange(Rng2.Start, TRUE)

CursString = oCurs.getString()
oCurs.gotoRange(Rng2.End, FALSE)
oCurs.setString(CursString)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get text between two ranges

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2755
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get text between two ranges

Post by JeJe »

Select the range and use gettransferable to copy it, then move the cursor to where you want it and use inserttransferable

This, in basic, copies the current selection, collapses the selection, and inserts a 2nd copy after it

Code: Select all

trans =thiscomponent.currentcontroller.gettransferable

thiscomponent.currentcontroller.viewcursor.collapsetoend

thiscomponent.currentcontroller.inserttransferable trans

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Get text between two ranges

Post by Lumberjack »

Thanks for replied. Text is copied. Is this any way to copy formated text , for example with bold font.
Now i use insertString , mayby i should use insertTextContent.
OpenOffice 3.1 on Windows Vista
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Get text between two ranges

Post by Lumberjack »

Now i try to use get getTransferable(). How can i get CurrentController for XComponent?
OpenOffice 3.1 on Windows Vista
User avatar
Villeroy
Volunteer
Posts: 31264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get text between two ranges

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Get text between two ranges

Post by Lumberjack »

Hi
Now i try to use getTransfarable() ( c# ) , no success. I Where is the problem? Cursor is set correct. Value of 'CurString' object is ok. But value is not copy below CurString position. Mayby selection

Code: Select all

((unoidl.com.sun.star.view.XSelectionSupplier)xControler).select(anySel);
works wrong. I try to copy object with formated text.
I have text :

strona 1

i expect :

strona 1
strona 1

Code: Select all

                       XText xText = ((unoidl.com.sun.star.text.XTextDocument)oDoc).getText();
                        XTextCursor oCurs = (XTextCursor)xText.createTextCursorByRange(objAdresatStr);
                        oCurs.gotoRange(objAdresatStop, true);

		       string CursString = oCurs.getString(); 
                        XController xControler = ((unoidl.com.sun.star.frame.XModel)oDoc).getCurrentController();
                        uno.Any anySel = new uno.Any(oCurs.GetType(), oCurs);
						
                        bool iSelect = ((unoidl.com.sun.star.view.XSelectionSupplier)xControler).select(anySel);
                        uno.Any selectedAny = ((unoidl.com.sun.star.view.XSelectionSupplier)xControler).getSelection();

                        unoidl.com.sun.star.datatransfer.XTransferable selectedContent = ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).getTransferable();
                        
                        ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).insertTransferable(selectedContent);
OpenOffice 3.1 on Windows Vista
JeJe
Volunteer
Posts: 2755
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get text between two ranges

Post by JeJe »

Here's a simple example in Basic that copies the document text

Code: Select all

Sub Main
c=thiscomponent.currentcontroller
c.select thiscomponent.gettext
trans = c.gettransferable
c.viewcursor.collapsetoend
c.inserttransferable trans
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Get text between two ranges

Post by Lumberjack »

Thanks. Its work. Below is my code :

Code: Select all

  XSearchDescriptor xSearchDecriptor1 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
                        xSearchDecriptor1.setSearchString("<el:strony>");
                        XTextRange objAdresatStr = ((XTextRange)((unoidl.com.sun.star.util.XSearchable)oDoc).findFirst(xSearchDecriptor1)).getEnd();

                        xSearchDecriptor1 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
                        xSearchDecriptor1.setSearchString("</el:strony>");
                        XTextRange objAdresatStop = ((XTextRange)((unoidl.com.sun.star.util.XSearchable)oDoc).findFirst(xSearchDecriptor1)).getStart();

                        XText xText = ((unoidl.com.sun.star.text.XTextDocument)oDoc).getText();
                        XTextCursor oCurs = (XTextCursor)xText.createTextCursorByRange(objAdresatStr);
                        oCurs.gotoRange(objAdresatStop, true);

                        XController xControler = ((unoidl.com.sun.star.frame.XModel)oDoc).getCurrentController();

                      //  ((unoidl.com.sun.star.frame.XModel)oDoc).
                        string s = oCurs.getString();
                      
;                        uno.Any anySel = new uno.Any(oCurs.GetType(), oCurs);
                        bool iSelect = ((unoidl.com.sun.star.view.XSelectionSupplier)xControler).select(anySel);
                        uno.Any selectedAny = ((unoidl.com.sun.star.view.XSelectionSupplier)xControler).getSelection();
                        bool b = selectedAny.hasValue();

                      
                     //   XReplaceDescriptor replaceSelec = ((XReplaceable)oCurs).createReplaceDescriptor();
                      //  replaceSelec.setSearchString("<el:imie />");
                      //  replaceSelec.setReplaceString("<el:imie />_1");

                     //   ((XReplaceable)oCurs).replaceAll(replaceSelec);

                        unoidl.com.sun.star.datatransfer.XTransferable selectedContent = ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).getTransferable();
                        oCurs.collapseToEnd();
                  
                        ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).insertTransferable(selectedContent);
But i have the question.
Is the any way to replace text from one to another in selection only (not in all document). From '"<el:imie />"' to "<el:imie />_1" but only in selection range. How can i do it? I want to keep formated text after replace. If '"<el:imie />"' is bold , "<el:imie />_1" should by bold .
OpenOffice 3.1 on Windows Vista
JeJe
Volunteer
Posts: 2755
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get text between two ranges

Post by JeJe »

Not sure what your asking. Are you asking how to do a search operation just on the selected text? If so I think you have to either use the dispatchhelper (the macro record will produce some code in BASIC for that which you'll have to translate) or go through each result and compare the range of what's found to the selection using XTextRangeCompare

https://www.openoffice.org/api/docs/com ... mpare.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Get text between two ranges

Post by Lumberjack »

It is possible i want to search and replace text in selected range. Now i use code to replace text in all document in success. This code replace 'text1' to 'text2' but in whole document (oDoc is all document).
I want use similar code to replace but in selection.

Code: Select all

XComponent oDoc = null;
     
oDoc = oDesk.loadComponentFromURL(fileName, fileName, 0, propVals);
			
XReplaceDescriptor replace = ((XReplaceable)oDoc).createReplaceDescriptor();
  
replace.setSearchString("text1");
replace.setReplaceString("text2");

((XReplaceable)oDoc).replaceAll(replace);
OpenOffice 3.1 on Windows Vista
JeJe
Volunteer
Posts: 2755
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get text between two ranges

Post by JeJe »

Record a macro and translate it into your language - I'm pretty sure you have to do that and you can't do it another way. Search in selection isn't an option in the searchdescriptor API method.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply