I marked it as solved just few days ago. My problem of more than 900 find and replacement not get solved. I solved nearabout 20-30 find and replace commands. I recorded the macro then used mail merge and with the help of mail merge I put those more than 900 find and replace texts. When I tried to find and replace with your solution my PC gets hanged. So I leave that problem and now I just want that the find and replace should work in tables and in calc.
Kruti Dev Find and Replace with Special Character.odt (23.7 KB)
Hallo
Its pretty simple with python
import re
rex = re.compile(r"yka|k")
def repl(match):
if match.group() == "yka":
return "ą¤²ą¤¾ą¤"
else:
return "ą¤·"
def replace_in_selections(*_):
doc = XSCRIPTCONTEXT.getDocument()
selections = doc.CurrentSelection
for selection in selections:
selection.String = rex.sub(repl, selection.String)
To organize and run python, you should install apso.oxt from here
@karolus Yes, youāre right, this approach is better than JeJeās idea - why search for all available occurrences of the search string in the entire document, if we can just replace the strings in the selections? This works much faster:
Sub ReplaceInSelection
Dim OriginalStrings As Variant, TargetStrings As Variant
Dim oSelections As Variant, sText As String
Dim i As Long, k As Long
OriginalStrings = Array("yka", "k")
TargetStrings = Array("ą¤²ą¤¾ą¤", "ą¤·")
oSelections =ThisComponent.getCurrentSelection()
For k = 0 To oSelections.getCount()-1
sText = oSelections.getByIndex(k).getString()
For i = LBound(OriginalStrings) To UBound(OriginalStrings)
sText = Replace(sText,OriginalStrings(i),TargetStrings(i),1,-1,0)
Next i
oSelections.getByIndex(k).setString(sText)
Next k
End Sub
If you want to replace only the simple characters and you donāt need the regular expressions for finding, then it is possible to search the regular expression from searched characters and replace the found parts. The advantage is, it will keep up the formatting; and it can be faster than searching the selection again and again for every item in array.
I havenāt installed the fonts for your example yka, so the example for replacing is: bc ā @1; op ā #:. So the macro searches the (bc|op) and then replaces the found part bc or op.
I donāt know how large document you need to search, so I put there also the lockControllers (it could be faster), and Undo Manager for only one step in Undo (it could be more comfortable for mistake in replacing).
Edit the variable p() to your replacements.
find-repl-in-selection.odt (15.0 kB)
This isnāt quite correct. An inserted replacement always gets the attributes from the next position left of its start. If the finding getting replaced is not inside of an uniformly formatted text range there will be a change.
See attachment-in-return:
find_repl_in_selection_ReExcept.odt (16.6 KB)
To avoid his, you should firszt insert the replacement behind the finding, and annullated the found part subsequently. If the search string isnāt formatted uniformly at all, there isnāt an unambiguos requirement concerning the attributes to use for the replacement anyway.
I forgot this behavior :-).
Here is the fix, of course it supposes only one formatting for wanted items.
find_repl_in_selection_ReExcept-3.odt (17.0 kB)
I tried to make the multiformat F&R in past, but I still didnāt finish it.
Thanks a lot
My PC hangs if I put hundreds of items in the array.