Global searchandreplace macro for documents

Integration questions/issues
Post Reply
CMTech
Posts: 4
Joined: Thu Aug 20, 2020 2:50 pm

Global searchandreplace macro for documents

Post by CMTech » Thu Aug 20, 2020 3:25 pm

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

Carl
Posts: 472
Joined: Thu Apr 12, 2018 10:00 am

Re: Global searchandreplace macro for documents

Post by Carl » Tue Aug 25, 2020 9:02 am

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"})

CMTech
Posts: 4
Joined: Thu Aug 20, 2020 2:50 pm

Re: Global searchandreplace macro for documents

Post by CMTech » Tue Aug 25, 2020 1:09 pm

Thank you sir!

CMTech
Posts: 4
Joined: Thu Aug 20, 2020 2:50 pm

Re: Global searchandreplace macro for documents

Post by CMTech » Wed Aug 26, 2020 3:57 pm

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

Carl
Posts: 472
Joined: Thu Apr 12, 2018 10:00 am

Re: Global searchandreplace macro for documents

Post by Carl » Thu Aug 27, 2020 4:56 pm

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

Post Reply