How to build an ONLYOFFICE macro to tally word occurrences in documents

23 May 2024By Eeshaan

Whether you’re auditing content, looking at important words, or just trying to understand your data better, knowing how often a word appears is pretty handy. In this blog post, we’ll show you how to create a macro that counts how many times a specific word shows up in your document.

Building an ONLYOFFICE macro to tally word occurrences in your document

Building the macro

(function () {
  const oDocument = Api.GetDocument();
  const oRange = oDocument.GetRangeBySelect();
  const targetWord = "apple";

To start off, we get the working document in the oDocument variable, and the text selection in oRange. The targetWord variable holds the word you want to search for. This should be replaced with the word you want to search for in your document.

    const rawText = oRange.GetText();
    var wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
    var matches = rawText.match(wordRegex);

Next, the text in the selection is extracted into rawText. A regular expression checks for the specified word throughout the document. This regular expression is stored in wordRegex. Then, using the match method and wordRegex, we store all instances of the targetWord in matches array.

    let count = 0;
    for (let i = 0; i < matches.length; i++) {
      count++;
    }
    const wordInstances = count;

The length of the targetWord is calculated and stored the wordInstances variable.

    return wordInstances;
  }

The function concludes by returning the wordInstances variable at the end of its scope.

  const ans = addCommentToWord();
  oRange.AddComment(
    `The word ${targetWord} is repeated ${ans} times throughout this text.`
  );

Finally, we store the function’s returned value in ans, accompanied by a comment detailing the total instances of the repeated word within the selection.

The full macro code

Here is the entire code of the macro:

(function () {
  const oDocument = Api.GetDocument();
  const oRange = oDocument.GetRangeBySelect();
  const targetWord = "apple";

  function addCommentToWord() {
    const rawText = oRange.GetText();

    // Specify the target word and the length of the word
    // Find all occurrences of the target word in the document
    var wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
    var matches = rawText.match(wordRegex);

    let count = 0;
    for (let i = 0; i < matches.length; i++) {
      count++;
    }
    const wordInstances = count;
    return wordInstances;
  }

  // `The word `${targetword} has been repeated ${wordInstances} times.`
  const ans = addCommentToWord();

  oRange.AddComment(
    `The word ${targetWord} is repeated ${ans} times throughout this text.`
  );

})();

Let’s see our macro in action!

We believe this macro will make your document analysis a breeze by effortlessly tallying word occurrences, boosting your overall workflow, simplifying and refining your content assessments.

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!