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

**URL:** https://community.onlyoffice.com/t/how-to-create-a-table-from-the-numbered-list-using-onlyoffice-macros/12954
**Category:** News
**Created:** [12 February 2025 09:14 UTC](https://community.onlyoffice.com/t/how-to-create-a-table-from-the-numbered-list-using-onlyoffice-macros/12954 "2025-02-12T09:14:48Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ONLYOFFICE\_team](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/onlyoffice_team/32/431_2.png) [@ONLYOFFICE\_team](https://community.onlyoffice.com/u/ONLYOFFICE_team)
#### Post date: [12 February 2025 09:14 UTC](https://community.onlyoffice.com/t/how-to-create-a-table-from-the-numbered-list-using-onlyoffice-macros/12954/1 "2025-02-12T09:14:48Z")

</div>

Originally published at: [How to create a table from the numbered list using ONLYOFFICE macros | ONLYOFFICE Blog](https://www.onlyoffice.com/blog/2025/02/how-to-create-a-table-from-the-numbered-list-using-onlyoffice-macros/)

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](https://static-blog.onlyoffice.com/wp-content/uploads/2025/02/12112200/8.3-macro-list-to-table-macro.png)

## **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 &gt; 0) {
        tableData.push(currentRow);
    }
    currentRow = [text];
} else if (level === 1 &amp;&amp; currentRow.length &gt; 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!

https://www.youtube.com/embed/GAgxPj_0rYk?si=8JVUpZBaN4KZFfuA

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](https://www.onlyoffice.com/contacts.aspx?utm_source=blog&utm_medium=post&utm_campaign=mistral_onlyoffice&utm_id=list_to_table_macro_article). We’re always open to new ideas and collaboration! Best of luck in your exploratory endeavors!

### **Useful links**

[ONLYOFFICE API methods](https://api.onlyoffice.com/docs/Office%20API/Get%20Started/Overview?utm_source=blog&utm_medium=post&utm_campaign=mistral_onlyoffice&utm_id=list_to_table_macro_article)

[ONLYOFFICE on GitHub](https://github.com/ONLYOFFICE)

[More ONLYOFFICE macros](https://www.onlyoffice.com/blog/search?s=macros&utm_source=blog&utm_medium=post&utm_campaign=mistral_onlyoffice&utm_id=list_to_table_macro_article)

[Get a free desktop suite](https://www.onlyoffice.com/download-desktop.aspx?utm_source=blog&utm_medium=post&utm_campaign=mistral_onlyoffice&utm_id=list_to_table_macro_article)

[Free tutorial on ONLYOFFICE Macros](https://www.youtube.com/watch?v=drOa-uMpaPk&list=PLCF48HEKMOYNBygGjr9lSw3PLpVXQ6s_t&ab_channel=ONLYOFFICE)
