Use ONLYOFFICE macro to replace shapes with charts

23 November 2022 By Serge 0 35
Share

Presentations have become an essential part of our life. Be it a business meeting or a school project we need them to present our vision to the audience. Adjusting your 100-slide presentation might be a bit of a tedious job. This blog post will show you how to tackle this task with ONLYOFFICE macro.

Use ONLYOFFICE macro to replace shapes with charts

Our little macro will replace shapes with charts on every slide. Before we proceed, let’s elaborate on the positioning of the elements. Unlike spreadsheets where rows and columns can be reached by index, we can target a presentation element only by its position on the slide. The elements are placed on top of each other. And that implies that we can locate them by pushing all the elements into an array and iterating through it.

Building a macro

So, first, we target the active presentation with Api.GetPresentation method:

Then we add a for-loop that iterates through each slide in the presentation. So i here is the total number of slides. And we fetch each slide by passing it in as an index:

After that, we execute the GetAllShapes method. It collects all the shapes on the slide and returns them in an array:

Now we need to remove shapes from slides. Shapes and drawings inherit the same proprieties, and that allows us to use the Delete method. It removes a shape or a drawing from the slide. In our case, the shapes are placed at the bottom. So we pass in 0 as an index to remove all the bottom elements:

Then we add a chart by implementing the APi.CreateChart method. In the parameters, we specify the chart style and the data displayed:

After that, we set the size and the position on the slide. In our case, we make a chart smaller and center it. This way, it does not overlap the text. And we insert the chart into the slide by executing the AddObject method:

The entire macro code is the following:

Now, let’s run our macro and see how it works!

We hope that this little quirky macro will elevate some of your mundane tasks. ONLYOFFICE macros are extremely versatile and can be tailored to your needs. We encourage you to take advantage of our API methods and create your own macros. Feel free to ask questions or share your ideas with us. We are open to discussion and cooperation. Best of luck in your exploratory endeavors!

Share

Add a comment