Change font family and size on every slide with ONLYOFFICE macro

11 October 2023By Serge

In today’s world, presentations play a vital role in both business meetings and academic projects, serving as a tool to articulate our vision to the audience. In one of our previous posts, we introduced a macro that replaced shapes with charts on each slide. Today we are taking it up a notch with a new macro designed to modify the font family and size across every slide in the presentation.

Change font family and size on every slide with ONLYOFFICE macro

Building the macro

First we fetch a presentation object using the Api.GetPresentation() method and assign it to the variable oPresentation:

var oPresentation = Api.GetPresentation();

Then the loop iterates through each slide in the presentation using the slideIndex variable. The condition ensures that the loop continues until all slides have been processed:

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

Inside the loop, we retrieve a specific slide using the oPresentation.GetSlideByIndex(slideIndex) method and assign it to the variable oSlide:

var oSlide = oPresentation.GetSlideByIndex(slideIndex);

Then we retrieve all the shapes on the current slide oSlide and store them in an array called aShape:

var aShape = oSlide.GetAllShapes();

Another loop iterates through each shape on the current slide using the shapeIndex variable:

for (var shapeIndex = 0; shapeIndex < aShape.length; shapeIndex++) {
}

Inside the shape loop, we get the document content of the current shape using aShape[shapeIndex].GetDocContent() and store it in the variable content:

var content = aShape[shapeIndex].GetDocContent();

We check if the content is valid (i.e., not null or undefined) before proceeding with further operations:

  if (content) {
}

In case it’s valid, we get the count of elements within the content using content.GetElementsCount() and store it in the count variable:

var count = content.GetElementsCount();

The third loop iterates through each element within the content using the elementIndex variable:

for (var elementIndex = 0; elementIndex < count; elementIndex++) {
}

Inside the loop, we retrieve the current element using content.GetElement(elementIndex) :

var element = content.GetElement(elementIndex);

We check if the element is valid to avoid possible errors:

if (element) {
}

If the element is valid, we set the font size and the font family:

element.SetFontSize(10);
element.SetFontFamily("Comic Sans");

The entire macro is the following:

(function()
{
    var oPresentation = Api.GetPresentation();
for (var slideIndex = 0; slideIndex < oPresentation.GetSlidesCount(); slideIndex++) {
    var oSlide = oPresentation.GetSlideByIndex(slideIndex);
    var aShape = oSlide.GetAllShapes();

    for (var shapeIndex = 0; shapeIndex < aShape.length; shapeIndex++) {
        var content = aShape[shapeIndex].GetDocContent();
        // Check if content is valid before proceeding
        if (content) {
            var count = content.GetElementsCount();
            for (var elementIndex = 0; elementIndex < count; elementIndex++) {
                var element = content.GetElement(elementIndex);
                // Check if element is valid before using it
                if (element) {
                    element.SetFontSize(10);
                    element.SetFontFamily("Comic Sans");
                }
            }
        }
    }
}

})();

Let’s run our macro and see how it works!

We hope this macro will be a useful asset in your arsenal, adding a touch of efficiency to your everyday tasks. With ONLYOFFICE macros, you gain the ability to boost your productivity and streamline your work flow.

As you engage in crafting macros, seize the chances provided by the ONLYOFFICE API. The flexibility of ONLYOFFICE macros allows you to fine-tune them to your specific needs. Feel free to reach out with any questions or share your creative ideas with us. We’re here for discussion and collaboration. Best of luck in your explanatory endeavors!