Build an ONLYOFFICE macro that deletes specific comments in the document

17 January 2024By Eeshaan

Sometimes, you just need to get rid of comments in your document. In this blog post, we’ll show you how to create a simple macro for ONLYOFFICE that cleans specific or all comments, keeping your collaboration focused and clutter-free.

Building an ONLYOFFICE macro to tally word occurences in your document

Building the macro

var oDocument = Api.GetDocument();

Starting off, we get hold of the current working document in the oDocument variable.

var aComments = oDocument.GetAllComments();
var enteredText = "THE COMMENT YOU WANT TO DELETE";

Next, we get all the comments in the macro using the GetAllComments() method, and set the comment we want to search for. If you want to delete all the comments in the document, you can skip this step and follow the steps below.

  for (let i = 0; i < aComments.length; i++) {
    var sQuoteText = aComments[i].GetText();

Next, we set up the for loop which iterates through the aComments array and gets the comment text for each iteration in sQuoteText.

  //aComments[i].Delete();     Uncomment if you want to delete every comment in the document

If you want to delete all the comments in the document, you can uncomment this line and run the macro.

    if (sQuoteText === enteredText) {
      aComments[i].Delete();
    }
  }

At last, we check if the entered word (enteredText) matches the text in the comment. If it does, we delete the comment on that word with the delete() method.

The entire code for the macro

Here is the code for the entire macro.

(function () {
  //get the active sheets and comments
  var oDocument = Api.GetDocument();
  var aComments = oDocument.GetAllComments();

  var enteredText = "THE COMMENT YOU WANT TO DELETE"; //place your comment here

  //for loop to iterate through all the comments
  for (let i = 0; i < aComments.length; i++) {
    var sQuoteText = aComments[i].GetText();
    //aComments[i].Delete();     Uncomment if you want to delete every comment in the document

    //this if statement deletes all instances of the specified comment from the doc
    if (sQuoteText === enteredText) {
      aComments[i].Delete();
    }
  }
})();

Now let’s run our macro and see it in action!

Deleting all the comments:

Deleting specific comments:

And that wraps up our easy macro tutorial. We’re pretty sure this little tool will make your document editing smoother, saving you time and boosting collaboration. Happy editing!

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!

 

Create your free ONLYOFFICE account

View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.