Use ONLYOFFICE macro to replace shapes with charts

23 November 2022By Serge

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:

var oPresentation = Api.GetPresentation();

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:

var oPresentation = Api.GetPresentation();
var oPresentation = Api.GetPresentation();
for (let i = 0; i < 10; i++) {
    var oSlide = oPresentation.GetSlideByIndex(i);
}

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

var oPresentation = Api.GetPresentation();
for (let i = 0; i < 10; i++) {
    var oSlide = oPresentation.GetSlideByIndex(i);
    var aShape  = oSlide.GetAllShapes();
}

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:

var oPresentation = Api.GetPresentation();
for (let i = 0; i < 10; i++) {
    var oSlide = oPresentation.GetSlideByIndex(i);
    var aShape  = oSlide.GetAllShapes();
      aShape[0].Delete();
}

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

 var oChart = Api.CreateChart("bar3D", [
  [200, 240, 280],
  [250, 260, 280]
  ], ["Projected Revenue", "Estimated Costs"], [2014, 2015, 2016], 4051300, 2347595, 24);

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:

oChart.SetSize(150 * 36000, 90 * 36000);
oChart.SetPosition(3267200, 1000000);
oSlide.AddObject(oChart);

The entire macro code is the following:

(function()
{
    var oPresentation = Api.GetPresentation();
for (let i = 0; i < 10; i++) {
    var oSlide = oPresentation.GetSlideByIndex(i);
    var aShape = oSlide.GetAllShapes();
     aShape[0].Delete();
    var oChart = Api.CreateChart("bar3D", [
       [200, 240, 280],
    [250, 260, 280]
       ], ["Projected Revenue", "Estimated Costs"], [2014, 2015, 2016], 4051300, 2347595, 24);
     oChart.SetSize(150 * 36000, 90 * 36000);
     oChart.SetPosition(3267200, 1000000);
     oSlide.AddObject(oChart);
}
})();

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!