Build an ONLYOFFICE macro to replace words in your presentations

Originally published at: Build an ONLYOFFICE macro to replace words in your presentations | ONLYOFFICE Blog


There are several instances while making a presentation where we need to change all occurrences of a particular word. Manual corrections can be time consuming, especially if you have lots of slides to work with. To ease this process, we will build a macro to replace a specific word in the Presentation Editor.

An ONLYOFFICE Macro that replaces a word in the Presentation Editor

Building the macro

const oPresentation = Api.GetPresentation(); //get the current presentation

First, we get the current presentation in the oPresentation variable.

for (
    var slideIndex = 0;
    slideIndex < oPresentation.GetSlidesCount();
    slideIndex++
  ) {

This macro involves nested for loops. In this first for loop, we iterate through all the slides in the presentation.

var oSlide = oPresentation.GetSlideByIndex(slideIndex);
var aShape = oSlide.GetAllShapes(); //get all the shapes from different slides

These lines get all the shapes that may be present in a particular slide and push their details into the aShape array.

for (var shapeIndex = 0; shapeIndex < aShape.length; shapeIndex++) {
      var content = aShape[shapeIndex].GetDocContent();

In this second for loop, we get the content for each shape on a particular spreadhsheet.

if (content) {
        var count = content.GetElementsCount();
        for (var elementIndex = 0; elementIndex < count; elementIndex++) {
          var element = content.GetElement(elementIndex);

We make sure that the content variable exists, and get the count of all the elements present in content.

if (element) {
            const rawText = element.GetText(); //gets the text from a particular element
            element.Delete(); //delete the content
            const wordToFind = "apple"; // Replace "apple" with the word you want to find
            const replacementWord = "banana"; // Replace "banana" with the word you want to replace it with
            const cleanedText = rawText.replace(
              new RegExp(wordToFind, "g"),
              replacementWord
            );

First we make sure that the variable element is valid. If the element exists, we copy the original text and delete the content using element.delete() method.

Following this, we specify the word to be replaced and the replacement word in their respective variables, and perform the replacement, storing the new text in the cleanedText variable.

 var oParagraph = Api.CreateParagraph();
 var oRun = Api.CreateRun();
 oRun.AddText(cleanedText);
 oParagraph.AddElement(oRun);
 content.Push(oParagraph);

Finally, we create a new paragraph, and add the cleanedText to it. After the paragraph element is setup, we add it back to the slide, replacing the one we had deleted above, using the above code.

Full macro code

Here is the entire code of the macro:

(function () {
  const oPresentation = Api.GetPresentation(); //get the current presentation
  for (
    var slideIndex = 0;
    slideIndex < oPresentation.GetSlidesCount();
    slideIndex++
  ) {
    var oSlide = oPresentation.GetSlideByIndex(slideIndex);
    var aShape = oSlide.GetAllShapes(); //get all the shapes from different slides
for (var shapeIndex = 0; shapeIndex &lt; aShape.length; shapeIndex++) {
  var content = aShape[shapeIndex].GetDocContent();

  // Check if content exists before proceeding
  if (content) {
    var count = content.GetElementsCount();
    for (var elementIndex = 0; elementIndex &lt; count; elementIndex++) {
      var element = content.GetElement(elementIndex);

      // Check if element is valid before using it
      if (element) {
        const rawText = element.GetText(); //gets the text from a particular element

        element.Delete(); //delete the content

        const wordToFind = "apple"; // Replace "apple" with the word you want to find
        const replacementWord = "banana"; // Replace "banana" with the word you want to replace it with
        const cleanedText = rawText.replace(
          new RegExp(wordToFind, "g"),
          replacementWord
        );

        //creates a new paragragh, and restores the content which was deleted, with the cleaned text.
        var oParagraph = Api.CreateParagraph();
        var oRun = Api.CreateRun();
        oRun.AddText(cleanedText);
        oParagraph.AddElement(oRun);
        content.Push(oParagraph);
      }
    }
  }
}

}
})();

Let’s run our macro and see the results!

We hope this macro was of help to you, and streamlined your presentation workflow.

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!

Useful links

Macro samples

Replace words macro for Document Editor

ONLYOFFICE on GitHub

ONLYOFFICE Presentation API

Free online presentation editor