In Microsoft Word and LibreOffice Writer, it’s possible to find and replace Paragraph Styles using the normal Find and Replace dialogue box.
While the OnlyOffice Search and Replace feature allows finding and replacing special characters – such as line breaks, etc. – it doesn’t appear to allow doing it for Paragraph Styles.
For example, I would like to find Paragraph Styles that are set to “Heading 2” and change them to “Heading 3”. Presumably, this is something that would be possible with Macros, however I am not familiar with JavaScript.
The furthest I’ve got is to replace the first paragraph GetElement(0) with “Heading 3”. However, I do not know how to put it together so that it gets styles that are set to “Heading 2” and replaces them instead.
let doc = Api.GetDocument();
let headingStyleTo = doc.GetStyle("Heading 3");
let paragraph = doc.GetElement(0);
let paraPr = paragraph.GetParaPr();
paraPr.SetStyle(headingStyleTo);
Hello,
At the moment, replacing paragraph styles via the standard Find and Replace dialog is not supported in ONLYOFFICE, but we already have a registered request to extend the Find and Replace functionality. Unfortunately, there is no ETA for this enhancement yet.
As a workaround, you can use a macro. We checked the following example for the Heading 2 → Heading 3 scenario, and it works correctly with heading paragraphs in a DOCX document.
(function () {
let oDocument = Api.GetDocument();
let oSourceStyle = oDocument.GetStyle("Heading 2");
let oTargetStyle = oDocument.GetStyle("Heading 3");
let oParagraphs = oDocument.GetAllHeadingParagraphs();
let nReplaced = 0;
if (!oSourceStyle || !oTargetStyle) {
let oInfo = Api.CreateParagraph();
oInfo.AddText('Styles "Heading 2" or "Heading 3" were not found.');
oDocument.Push(oInfo);
return;
}
for (let i = 0; i < oParagraphs.length; i++) {
let oParagraph = oParagraphs[i];
let oCurrentStyle = oParagraph.GetStyle();
if (oCurrentStyle && oCurrentStyle.GetName() === "Heading 2") {
oParagraph.SetStyle(oTargetStyle);
nReplaced++;
}
}
let oResult = Api.CreateParagraph();
oResult.AddText("Replaced paragraphs: " + nReplaced);
oDocument.Push(oResult);
})();
To test it, please create a DOCX file with several separate paragraphs and apply Heading 2 to some of them, Heading 3 to some others, and Normal to the rest. Then run the macro from the macro editor in ONLYOFFICE. The macro will change only the paragraphs that currently use the Heading 2 style and add a final line with the number of replaced paragraphs.