Page 1 of 1

Global searchandreplace macro for documents

Posted: Thu Aug 20, 2020 3:25 pm
by CMTech
I'm looking for an example of a document editor macro to do a global search and replace.

Do I need a loop using GetElement to fetch each paragraph one at a time for the search and replace or is there a way to select the entire document and search and replace in that manner?

Thanks,

Dan

Re: Global searchandreplace macro for documents

Posted: Tue Aug 25, 2020 9:02 am
by Carl
Hello Dan,

The SearchAndReplace method is applied to the whole document. Here is an example:

Code: Select all

var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oParagraph.AddText("This is just a sample text. Nothing special.");
oParagraph.AddText("This is just a sample text. Nothing special.");
oParagraph.AddText("This is just a sample text. Nothing special.");
oDocument.SearchAndReplace({"searchString": "just", "replaceString": "111111"})

Re: Global searchandreplace macro for documents

Posted: Tue Aug 25, 2020 1:09 pm
by CMTech
Thank you sir!

Re: Global searchandreplace macro for documents

Posted: Wed Aug 26, 2020 3:57 pm
by CMTech
With your example Carl, I wrote a macro to replace smart quotes with straight quotes:

(function()
{
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oDocument.SearchAndReplace({"searchString": '“', "replaceString": '"'});
oDocument.SearchAndReplace({"searchString": '”', "replaceString": '"'});
})();

Is there a way of making macros global? I'd like to make this macro available to all OO users for all documents.

Thanks,

Dan

Re: Global searchandreplace macro for documents

Posted: Thu Aug 27, 2020 4:56 pm
by Carl
Hello Dan,

Unfortunately, macros are attached to specific documents and there is no way to make them global. However, you can write a plugin that will be loaded for all users. Here are our example plugins that work with search&replace functionality:
https://github.com/ONLYOFFICE/sdkjs-plu ... ch_replace
https://github.com/ONLYOFFICE/sdkjs-plu ... aceOnStart

How to install plugins:
https://api.onlyoffice.com/plugin/installation