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
Global searchandreplace macro for documents
Re: Global searchandreplace macro for documents
Hello Dan,
The SearchAndReplace method is applied to the whole document. Here is an example:
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
Thank you sir!
Re: Global searchandreplace macro for documents
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
(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
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
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