Replace words in the document with ONLYOFFICE macro

1 November 2023By Serge

Document editing is a fundamental aspect of our daily tasks, and we frequently find ourselves yearning for tools that can ease the burden of repetitive chores. In this blog post, we’ll build a macro designed to replace words in the document within a selection.

Replace words in the document with ONLYOFFICE macro

Building the macro

First, we access the current file in the document editor. Then, we target the selected text and capture the values. This range is where we intend to find and replace words:

const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();

Then we delete the selected text:

oRange.Delete();

We assign the word we are looking for to the variable wordToFind, and the word we want to replace it with to the replacementWord:

// Define the word to find and the word to replace it with
const wordToFind = "oldWord"; // Replace "oldWord" with the word you want to find
const replacementWord = "newWord"; // Replace "newWord" with the word you want to replace it with

We apply a regular expression to the rawText (the text within our selected range) to find all instances of wordToFind and replace them with replacementWord. The regular expression with the “g” flag ensures that all occurrences are replaced. The modified text is stored in the cleanedText variable:

// Use regular expression to find and replace the word
const cleanedText = rawText.replace(new RegExp(wordToFind, "g"), replacementWord);

Once the find-and-replace is complete, we create a new paragraph within the document using Api.CreateParagraph(). In this paragraph, we add text with the replaced words. Finally we insert the modified paragraph back into the document:

// Insert the cleanedText with the original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph]);

The entire macro is the following:

(function()
{
const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();
oRange.Delete();
// Define the word to find and the word to replace it with
const wordToFind = "oldWord"; // Replace "oldWord" with the word you want to find
const replacementWord = "newWord"; // Replace "newWord" with the word you want to replace it with
// Use regular expression to find and replace the word
const cleanedText = rawText.replace(new RegExp(wordToFind, "g"), replacementWord);
// Insert the cleanedText with the original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph]);
})();

Now let’s run our macro and see how it works!

We hope that this macro will be a useful addition to your arsenal and streamline your document editing. The ONLYOFFICE macros are designed to elevate your workflow to new heights.

Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is your key to transforming your ideas into reality. If you have any questions or innovative concepts, we encourage you to share them with us. Your input is highly valued, and we are excited about the possibility of collaborating with you. Best of luck in your exploratory endeavors!