Originally published at: How to add comments to every instance of a word with ONLYOFFICE macros | ONLYOFFICE Blog
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 using 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;
We set the word where we want our comments to appear. Next, we calculate the length of the word using a for loop, and store it in wordLength.
// Find all occurrences of the target word in the document 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; //returns the index where the word starts. let nextMatchPosition = matchPosition + wordLength; // Add a comment at the position of the match 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 entire 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();
})();
Now, let us see how our macro performs.
We 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. 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!