[Solved] Replace Text in selection

Creating a macro - Writing a Script - Using the API
Post Reply
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

[Solved] Replace Text in selection

Post by Lumberjack »

Hi
I have small problem. I would like to replace some text to another , but not i whole document , in selected range only . 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();

                     // here i have error for oCurs ,

                        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);
In this code (c#) i select range between '"<el:strony>"' and '"<el:strony/>"' and i want to replace '"<el:imie />" to '"<el:imie />_1"' in this range. Object oDoc is Component all document.

If i use below code its works perfectly but for whole document.

Code: Select all

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

                       ((XReplaceable)oDoc).replaceAll(replaceSelec);
How to do this? I want to keep formated text after replace (bolds , fonts ) its important.
Last edited by Hagar Delest on Fri Mar 05, 2021 2:39 pm, edited 1 time in total.
Reason: tagged solved.
OpenOffice 3.1 on Windows Vista
JeJe
Volunteer
Posts: 1928
Joined: Wed Mar 09, 2016 2:40 pm

Re: Replace Text in selection

Post by JeJe »

Openoffice 4.1.6
Windows 8
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Replace Text in selection

Post by Lumberjack »

It isn't the same.
OpenOffice 3.1 on Windows Vista
User avatar
Hagar Delest
Moderator
Posts: 30338
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Replace Text in selection

Post by Hagar Delest »

Not sure if that helps but with Basic, I use the subdispatchhelper to select all the content of a cell in a Writer table (but it should be equivalent to any other selection method I guess). And then I use it again to replace all, and it is limited to the selection.
Note: had to adjust the code when moving from AOO to LO.

Code: Select all

sub Main ...
subdispatch("SelectAll")
Replace("search_string", "replace_string", 3)
end sub

sub subDispatch(sCommand as string)
dim oFrame as object, oDispatcher as object
oFrame = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
end sub

sub Replace(sFind as string, sReplace as string, iReplace)
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args2(21) as new com.sun.star.beans.PropertyValue
args2(0).Name = "SearchItem.StyleFamily"
args2(0).Value = 2
args2(1).Name = "SearchItem.CellType"
args2(1).Value = 0
args2(2).Name = "SearchItem.RowDirection"
args2(2).Value = true
args2(3).Name = "SearchItem.AllTables"
args2(3).Value = false
args2(4).Name = "SearchItem.SearchFiltered"
args2(4).Value = false
args2(5).Name = "SearchItem.Backward"
args2(5).Value = false
args2(6).Name = "SearchItem.Pattern"
args2(6).Value = false
args2(7).Name = "SearchItem.Content"
args2(7).Value = false
args2(8).Name = "SearchItem.AsianOptions"
args2(8).Value = false
args2(9).Name = "SearchItem.AlgorithmType"
args2(9).Value = 1
args2(10).Name = "SearchItem.SearchFlags"
args2(10).Value = 71680
args2(11).Name = "SearchItem.SearchString"
args2(11).Value = sFind
args2(12).Name = "SearchItem.ReplaceString"
args2(12).Value = sReplace
args2(13).Name = "SearchItem.Locale"
args2(13).Value = 255
args2(14).Name = "SearchItem.ChangedChars"
args2(14).Value = 2
args2(15).Name = "SearchItem.DeletedChars"
args2(15).Value = 2
args2(16).Name = "SearchItem.InsertedChars"
args2(16).Value = 2
args2(17).Name = "SearchItem.TransliterateFlags"
args2(17).Value = 1073743104
args2(18).Name = "SearchItem.Command"
args2(18).Value = iReplace
args2(19).Name = "SearchItem.SearchFormatted"
args2(19).Value = false
args2(20).Name = "SearchItem.AlgorithmType2"
args2(20).Value = 2
args2(21).Name = "Quiet"
args2(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args2())
end sub
It would be better to have an actual piece of code instead of the dispatchhelper but well, that works as is.
LibreOffice 7.2.6 on Xubuntu 22.04 and 7.2.6 portable on Windows 10
JeJe
Volunteer
Posts: 1928
Joined: Wed Mar 09, 2016 2:40 pm

Re: Replace Text in selection

Post by JeJe »

Yep, that's the same answer I gave in the other thread.
Openoffice 4.1.6
Windows 8
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

[Solved] Replace Text in selection

Post by Lumberjack »

Hi
I have found the solution.
1. First I copy selection to another new document using getTransferable() and insertTransferable(...).
2. In this new document replace text to another for all document
3. Next step select and transfer all content from new document to oryginal document.

Below is my code. In this code i duplicate some text between strStart and strStop text , howMany value counts and doing replace for each range.

Code: Select all

  private void DuplicateAndRename(XComponent oDoc , string strStart , string strStop , int howMany , bool onlyRename)
        {
            XSearchDescriptor xSearchDecriptor1 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
            xSearchDecriptor1.setSearchString(strStart);
            XTextRange objAdresatStr = ((XTextRange)((unoidl.com.sun.star.util.XSearchable)oDoc).findFirst(xSearchDecriptor1)).getEnd();

            xSearchDecriptor1 = ((unoidl.com.sun.star.util.XSearchable)oDoc).createSearchDescriptor();
            xSearchDecriptor1.setSearchString(strStop);
            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();
            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();
         

            object selection = ((unoidl.com.sun.star.frame.XModel)oDoc).getCurrentSelection();

            unoidl.com.sun.star.datatransfer.XTransferable selectedContent = ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).getTransferable();

            if (onlyRename) howMany = 1;
            for (int j = 0; j < howMany; j++)
            {


                List<PropertyValue> properties = new List<PropertyValue>();
                PropertyValue propertyHidden = new PropertyValue() { Name = "Hidden", Value = new uno.Any(true) };
                properties.Add(propertyHidden);

                XComponent newDoc = oDesk.loadComponentFromURL("private:factory/swriter", "_blank", 0, properties.ToArray());
                XText xTextNewDoc = ((unoidl.com.sun.star.text.XTextDocument)newDoc).getText();
                XTextCursor oCursNewDoc = (XTextCursor)xTextNewDoc.createTextCursor();
                oCursNewDoc.gotoStart(false);
                XController xControlerNewDoc = ((unoidl.com.sun.star.frame.XModel)newDoc).getCurrentController();

                uno.Any anySelNewDoc = new uno.Any(oCursNewDoc.GetType(), oCursNewDoc);
                bool iSelectNewDoc = ((unoidl.com.sun.star.view.XSelectionSupplier)xControlerNewDoc).select(anySelNewDoc);
                ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControlerNewDoc).insertTransferable(selectedContent);

                XReplaceDescriptor replace = ((XReplaceable)newDoc).createReplaceDescriptor();
                string snewDoc = "<" + strStart.Replace("<el:", "").Replace(">", "") + "_" + (j+1).ToString() + "_";
                replace.setSearchString("<el:");
                replace.setReplaceString(snewDoc);

                ((XReplaceable)newDoc).replaceAll(replace);


                oCursNewDoc.gotoStart(true);

                uno.Any anySelOrgDoc = new uno.Any(oCursNewDoc.GetType(), oCursNewDoc);
                bool iSelectOrgDoc = ((unoidl.com.sun.star.view.XSelectionSupplier)xControlerNewDoc).select(anySelOrgDoc);
                unoidl.com.sun.star.datatransfer.XTransferable selectedContentOrgDoc = ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControlerNewDoc).getTransferable();
                
                ((unoidl.com.sun.star.datatransfer.XTransferableSupplier)xControler).insertTransferable(selectedContentOrgDoc);

                try
                {
                    ((XCloseable)newDoc).close(false);
                }
                catch
                {
                    xControlerNewDoc.dispose();
                }

            }
OpenOffice 3.1 on Windows Vista
User avatar
Hagar Delest
Moderator
Posts: 30338
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Replace Text in selection

Post by Hagar Delest »

Well, strange that something that works with the Basic code doesn't with another language.
LibreOffice 7.2.6 on Xubuntu 22.04 and 7.2.6 portable on Windows 10
Post Reply