How to add consistent table stylings with an ONLYOFFICE macro

3 September 2024By Eeshaan

While we’ve covered a significant portion of macros for managing spreadsheet tables, there are still situations where you might need to work with tables in the Document Editor. With this macro, users can set specific formatting rules—such as font styles, cell borders, background colors, and alignment— that can be applied to all the tables in the document. Let’s build this simple macro and see how it works in action.

How to add consistent table stylings with an ONLYOFFICE macro

Building the macro

  const oDocument = Api.GetDocument();
  const allTables = oDocument.GetAllTables();

Starting off, we get the active worksheet in the oDocument  variable. Then, we use the GetAllTables() method to retrieve all the tables in the document.

  allTables.ForEach(function (table) {
    const oTable = table;

Next, we iterate through the allTables array using a ForEach loop, aiming to perform operations on one table at a time.

    //modifyable part
    oTable.SetWidth("percent", 100);
    oTable.SetTableBorderTop("single", 32, 0, 51, 51, 51);
    oTable.SetTableBorderBottom("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderLeft("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderRight("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderInsideV("single", 4, 0, 255, 111, 61);
    oTable.SetTableBorderInsideH("single", 4, 0, 255, 111, 61);

This is the core of the ForEach loop, where the formatting rules for tables are defined. These rules will be applied consistently to all the tables in the document. We have included some methods to give you a clearer understanding of how the macro functions. For a deep dive into all the formatting options and methods, check out the ONLYOFFICE API Documentation for the Table Object.

This is an example of how a table will appear after applying the macro with the existing styles defined in the ForEach loop.

Note: The methods inside the ForEach loop are just for reference and can be changed as per your use case.

The full macro code

Here is the entire code for the macro:

(function () {
  //get the active sheets and comments
  const oDocument = Api.GetDocument();
  const allTables = oDocument.GetAllTables();

  allTables.ForEach(function (table) {
    const oTable = table;

    //modifyable part
    oTable.SetWidth("percent", 100);
    oTable.SetTableBorderTop("single", 32, 0, 51, 51, 51);
    oTable.SetTableBorderBottom("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderLeft("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderRight("single", 4, 0, 51, 51, 51);
    oTable.SetTableBorderInsideV("single", 4, 0, 255, 111, 61);
    oTable.SetTableBorderInsideH("single", 4, 0, 255, 111, 61);
  });
})();

Now, let’s see our macro in action.

That was it. A simple, no rocket-science macro that helps you apply styles to all your tables. The ONLYOFFICE API is a powerful tool, capable of performing a wide range of tasks and providing immense potential for developing even more advanced macros and plugins. With this API, users can harness the full power of ONLYOFFICE to enhance their productivity and streamline their workflows.

If you have any questions or innovative concepts, we encourage you to share them with us. We value your input and look forward to 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.