Build an ONLYOFFICE macro to replace words in your presentations

15 November 2023By Eeshaan

There are several instances when making a presentation where we need to change all occurences 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 spreadsheet.

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.RemoveAllElements(); //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("\\b" + wordToFind + "\\b", "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.RemoveAllElements() 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.

//if you want to delete the word entirely, use this replace method instead
            // const cleanedText = rawText.replace(
            //     new RegExp("\\b" + wordToFind + "\\b", "g"),
            //     ""
            //   );

If you want to remove the word entirely from the text, comment out the existing replace method and uncomment this alternative.

            const oRun = Api.CreateRun();
            oRun.AddText(cleanedText);
            element.AddElement(oRun);
            content.Push(element);

In the end, we make a new Run, insert the cleanedText into it, and then place it back on the slide, which essentially removes or replaces the target word.

Note: After running this macro, the text it affects will revert to the default formatting. Remember to review all your slides once after running the macro to ensure consistency.

Full macro code

Here is the entire code of the macro:

(function () {
  const oPresentation = Api.GetPresentation(); //get the current presentation
  for (
    let slideIndex = 0;
    slideIndex < oPresentation.GetSlidesCount();
    slideIndex++
  ) {
    const oSlide = oPresentation.GetSlideByIndex(slideIndex);
    const aShape = oSlide.GetAllShapes(); //get all the shapes from different slides

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

      // Check if content exists before proceeding
      if (content) {
        const count = content.GetElementsCount();
        for (let elementIndex = 0; elementIndex < count; elementIndex++) {
          const 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.RemoveAllElements(); //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("\\b" + wordToFind + "\\b", "g"),
              replacementWord
            );

            //if you want to delete the word entirely, use this replace method instead
            // const cleanedText = rawText.replace(
            //     new RegExp("\\b" + wordToFind + "\\b", "g"),
            //     ""
            //   );

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

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!

Create your free ONLYOFFICE account

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