Replace multiline

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Replace multiline

Post by BubikolRamios »

Code: Select all

 oReplace = oDoc.createReplaceDescriptor()
 oReplace.SearchRegularExpression=True 'Use regular expressions
 ...
Does this work multiline, I mean:

How would one merge thing like this
1-2jäh-
rig
into
1-2jährig
?
OPen office 4.1.5/ win 7
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Replace multiline

Post by JeJe »

Openoffice regular expressions don't work across paragraphs. If there's a line feed character at the end of 1-2jäh-then you can do a find replace but not if its a paragraph break.

If you learn how to use the view cursor and the text cursor you can move the cursor to the position in front of rig, goleft 1 character and delete the paragraph break. Or you can position the cursor in the correct place and use the macro recorder to record a macro for pressing the backspace character.

https://wiki.openoffice.org/wiki/Writer/API/View_cursor

https://wiki.openoffice.org/wiki/Docume ... ViewCursor
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Replace multiline

Post by BubikolRamios »

Recorded macro, finds "-\n", marks it obviously and deletes it. Wrapped into for next.
And now delete is executed regardless of find or not

how to do if here, like:

Code: Select all

if
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
then
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())

?

Code: Select all

sub DeleteLineBreaks

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 args1(18) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.Backward"
args1(4).Value = false
args1(5).Name = "SearchItem.Pattern"
args1(5).Value = false
args1(6).Name = "SearchItem.Content"
args1(6).Value = false
args1(7).Name = "SearchItem.AsianOptions"
args1(7).Value = false
args1(8).Name = "SearchItem.AlgorithmType"
args1(8).Value = 1
args1(9).Name = "SearchItem.SearchFlags"
args1(9).Value = 65536
args1(10).Name = "SearchItem.SearchString"
args1(10).Value = "-\n"
args1(11).Name = "SearchItem.ReplaceString"
args1(11).Value = ""
args1(12).Name = "SearchItem.Locale"
args1(12).Value = 255
args1(13).Name = "SearchItem.ChangedChars"
args1(13).Value = 2
args1(14).Name = "SearchItem.DeletedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.InsertedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.TransliterateFlags"
args1(16).Value = 1280
args1(17).Name = "SearchItem.Command"
args1(17).Value = 0
args1(18).Name = "Quiet"
args1(18).Value = true

dim i
For i = 1 To 10000

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())



Next i ' Increment counter

OPen office 4.1.5/ win 7
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Replace multiline

Post by JeJe »

you're doing a search 1000 times and then backspace which will delete everything found - 1000 times over.

Take a look at the Alt search extension
https://extensions.openoffice.org/en/pr ... -altsearch

You can use that to find the paragraph marks and replace them with something - and perhaps do what you want without a macro?

Assuming you have a number of selections from a find all operation which all begin at the start of the paragraph this will go through them and delete the paragraph mark - merging the paragraph with the previous one.

Code: Select all

sel= thiscomponent.currentcontroller.selection 'this gets the selections
with sel
for i = 0 to .count -1 'go through them all
tc = thiscomponent.text.createtextcursorbyrange(.getbyindex(i).getstart) 'create a text cursor at the start of the selection
tc.goleft(1,true) 'go left 1 so the text cursor is over the paragraph mark - assumes selection is at the start of a paragraph
tc.string = "" 'delete it
next
end with
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Replace multiline

Post by BubikolRamios »

Thanks. It appears this works:

Code: Select all

Dim res As Object

res = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

While res.Result = true

dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())
res = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

wend
OPen office 4.1.5/ win 7
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Replace multiline

Post by BubikolRamios »

BTW CTRL+F , "-\n", regex, finds first - but not second
tmp.jpg
tmp.jpg (11.94 KiB) Viewed 796 times
Selecting second with mouse can't be done, looks like selects something right to it, like extra space or something, see below


Any idea what is that ? second attachment CTRL+F, "-"
see how it finds something extra at second -
tmp1.jpg
tmp1.jpg (7.67 KiB) Viewed 796 times
Note also, if one goes and try click drag select entire upper line, from start to end, - at end can't be selected.
Format color red does not play any role in that.
OPen office 4.1.5/ win 7
Post Reply