Paragraphs to table
Converts a list of numbered paragraphs into a table.
(function () {
let doc = Api.GetDocument();
let paragraphs = doc.GetAllNumberedParagraphs();
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);
}
let maxColumns = 0;
for (let j = 0; j < tableData.length; j++) {
if (tableData[j].length > maxColumns) {
maxColumns = tableData[j].length;
}
}
let table = Api.CreateTable(maxColumns, tableData.length);
doc.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]);
}
}
})();
Methods used: GetDocument, GetAllNumberedParagraphs, GetNumbering, GetLevelIndex, GetText, CreateTable, Push, GetCell, GetContent, GetElement, AddText
Result