How to create a table from the numbered list using ONLYOFFICE macros

12 February 2025By Serge

When working with documents, we often need to convert numbered lists into tables to improve readability. Manually performing this task can be time-consuming. Fortunately, ONLYOFFICE macros allow users to automate repetitive tasks efficiently. In this blog post, we’ll provide a step-by-step guide on developing an ONLYOFFICE macro that seamlessly transforms numbered lists into structured tables.

How to to create a table from the numbered list using ONLYOFFICE macros

Building the macro

Retrieving the document and numbered paragraphs

The macro begins by accessing the active document and retrieving all paragraphs that contain numbering:

const oDocument = Api.GetDocument();
const paragraphs = oDocument.GetAllNumberedParagraphs();
  • Api.GetDocument() retrieves the currently open document.
  • oDocument.GetAllNumberedParagraphs() gathers all paragraphs that use numbering styles.

 Organizing numbered items into rows

The script iterates through each numbered paragraph and sorts them into table rows:

let tableData = [];
let currentRow = [];

for (let i = 0; i < paragraphs.length; i++) {
    let level = paragraphs[i].GetNumbering().GetLevelIndex();
    let text = paragraphs[i].GetText().trim();

    if (level === 0) {
        if (currentRow.length > 0) {
            tableData.push(currentRow);
        }
        currentRow = [text];
    } else if (level === 1 && currentRow.length > 0) {
        currentRow.push(text);
    }
}

if (currentRow.length > 0) {
    tableData.push(currentRow);
}
  • The script loops through all paragraphs and checks their numbering levels.
  • Level 0 items start a new row, while level 1 items are appended as columns in the current row.
  • After processing, all gathered rows are stored in tableData.

Determining table dimensions

Before creating the table, the macro determines the number of columns needed. The script calculates maxColumns by finding the longest row in tableData. This ensures the table has enough columns to accommodate all items correctly:

let maxColumns = 0;
for (let j = 0; j < tableData.length; j++) {
    if (tableData[j].length > maxColumns) {
        maxColumns = tableData[j].length;
    }
}

Creating and populating the table

Now that we have the data organized, the macro creates a table and inserts the extracted content:

let table = Api.CreateTable(maxColumns, tableData.length);
oDocument.Push(table);

for (let row = 0; row < tableData.length; row++) {
    for (let col = 0; col < tableData[row].length; col++) {
        table
            .GetCell(row, col)
            .GetContent()
            .GetElement(0)
            .AddText(tableData[row][col]);
    }
}
  • Api.CreateTable(maxColumns, tableData.length) creates a table with the required dimensions.
  • The script loops through tableData and inserts the corresponding text into each cell.

Error handling

To ensure smooth execution, a try…catch block is used to catch and log errors. If an error occurs, it will be logged in the console, helping users debug issues:

try {
    // Code execution
} catch (error) {
    console.log("Error: " + error.message);
}

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

We hope this macro becomes a valuable addition to your toolkit, helping you simplify repetitive tasks in your workflow. ONLYOFFICE macros are designed to streamline your work and boost efficiency. We encourage you to explore our extensive API library and create custom macros tailored to your needs. If you have any ideas or suggestions, feel free to reach out. We’re always open to new ideas and collaboration! 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.