DEV Community

Eeshaan Sawant
Eeshaan Sawant

Posted on

Build an ONLYOFFICE macro that adds comments to every instance of a word in the Text Editor

When working on documents, there are times when it’s essential to include a comment for a specific word that appears repeatedly throughout the text. In this blog-post, we will show you how you can efficiently add comments to every instance of a specific word, streamlining your editing process and enhancing document clarity.


Building the macro

function addCommentToWord() {

The addCommentToWord() function is designed to add comments to every occurrence of a specified word within the document.

Here’s a breakdown of its key components:

const oDocument = Api.GetDocument();
const numberOfElements = oDocument.GetElementsCount();

Here, we get the current document in the oDocument variable. Next, we get all the elements in the document using the GetElementsCount() method.

   for (
        let elementIndex = 0;
        elementIndex < numberOfElements;
        elementIndex++
      )

A for loop is used here to iterate through all the elements. Doing this adds a comment to the desired word in every element (paragraph) of the document.

{
        const oElement = oDocument.GetElement(elementIndex);
        const rawText = oElement.GetText();

Inside the for loop, we get each element of the document by the GetElement method and passing in the elementIndex . Then, we extract the plain text in the rawText variable.

const targetWord = "sample word";
    let count = 0;
    for (let i = 0; i < targetWord.length; i++) {
      count++;
    }
    const wordLength = count - 1;

Here, we set the word we want our comments to be on. Next, we calculate the length of the word using a for loop, and store it in wordLength.

 const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");

Then, a regular expression (wordRegex) is created using the target word to find all occurrences in the document.

let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index;
      let nextMatchPosition = matchPosition + wordLength;
      const comment = "YOUR COMMENT GOES HERE";
      const oParagraph = oDocument.GetElement(0);
      const range = oParagraph.GetRange(matchPosition, nextMatchPosition);
      range.AddComment(comment, "Comment's Author Name");
}
    }
  }

Here, inside a while loop, we iterate through all the instances found and stored in wordRegex.
For each match, the starting position of the word (matchPosition) and the position of the next character after the word (nextMatchPosition) are calculated.
The comment variable holds the comment you want to insert. The AddComment method has an optional parameter which can be used to alter the author’s name. If you don’t want to use it, you can exclude the optional parameter, and just pass in the comment variable.

addCommentToWord();

Finally, the addCommentToWord function is called.

The full macro code

Here is the enitre code of the macro:

(function () {
  function addCommentToWord() {
    const oDocument = Api.GetDocument();
    const numberOfElements = oDocument.GetElementsCount();

    //looping through all the elements in the document.
    for (
        let elementIndex = 0;
        elementIndex < numberOfElements;
        elementIndex++
      ) {
        const oElement = oDocument.GetElement(elementIndex);
        const rawText = oElement.GetText();

      // Specify the target word and the length of the word
      const targetWord = "sample word";
      let count = 0;
      for (let i = 0; i < targetWord.length; i++) {
        count++;
      }
      const wordLength = count - 1;

      // Find all occurrences of the target word in the document
      const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
      let match;
      while ((match = wordRegex.exec(rawText)) !== null) {

        let matchPosition = match.index; // returns the index where the word starts.
        let nextMatchPosition = matchPosition + wordLength; //the index where the word ends.

        // Add a comment at the position of the match
        const comment = "YOUR COMMENT GOES HERE";
        const range = oElement.GetRange(matchPosition, nextMatchPosition);
        range.AddComment(comment, "Comment's Author Name");
      }
    }
  }

  addCommentToWord();
})();

Enter fullscreen mode Exit fullscreen mode

Now, let us see how our macro performs.


I trust this macro made handling repetitive word comments a breeze, providing a handy tool for users dealing with the document editor in ONLYOFFICE.
Don’t miss the chance to harness the power of the ONLYOFFICE API. If you have any questions or innovative concepts, I encourage you to share them with the ONLYOFFICE Team. Best of luck in your exploratory endeavors!

Useful links

Macro samples

ONLYOFFICE on GitHub

ONLYOFFICE Text Document API

Other macros


About Me

I am Eeshaan, a senior studying Computer Science in Pune, India. I am fond of Open-Source, Web Development, and Technical Writing. Feel free to give me a follow on my Twitter (now X), if my blog was of any use to you. Cheers. 🍻

Top comments (0)