Modify Tables
You can use a PowerPoint table and add/modify data and style. It is also possible to add rows and columns and to style cells.
const pres = automizer
.loadRoot(`RootTemplate.pptx`)
.load(`SlideWithTables.pptx`, 'tables');
const result = await pres.addSlide('tables', 3, (slide) => {
slide.modifyElement('TableWithEmptyCells', [
modify.setTable({
// Use an array of rows to insert data.
// use `label` key for your information only
body: [
{ label: 'item test r1', values: ['test1', 10, 16, 12, 11] },
{ label: 'item test r2', values: ['test2', 12, 18, 15, 12] },
{ label: 'item test r3', values: ['test3', 14, 12, 11, 14] },
],
}),
]);
});
Note that the table has to be a native table in the template — a grouped-shape "fake table" cannot be filled with setTable.
A \n in a cell value is a line break within the cell (\v, what PowerPoint
uses for Shift+Enter, works the same). It becomes a soft break (<a:br/>) in
the cell's paragraph, and every line keeps the cell's text style:
slide.modifyElement('TableDefault', [
modify.setTable({
body: [{ label: 'head', values: ['Stick\n(solid)', 'Spray'] }],
}),
]);
Data that is narrower than the template table trims the table to the data:
surplus grid columns and their cells are removed, and a row that is shorter than
the grid is padded with an empty clone of its last cell. Data that is wider
than the template adds cells by cloning the row's last cell after it was
filled, so a per-cell style applied to that neighbour (colour, bold) carries over
into the added column. Size template tables to the largest column count you fill,
or grow them with expand (below) before filling.
Table helpers
ModifyTableHelper provides rich control over existing tables.
- Fill table data and auto-adjust size
slide.modifyElement('MyTable', [
ModifyTableHelper.setTable({
body: [
{ label: 'r1', values: ['A', 1] },
{ label: 'r2', values: ['B', 2] },
],
}),
]);
- Expand rows/columns by tag before filling
slide.modifyElement('MyTable', [
ModifyTableHelper.setTable(
{
body: [ /* ... */ ],
},
{
expand: [
{ tag: '<<ROW>>', count: 3, mode: 'row' },
{ tag: '<<COL>>', count: 2, mode: 'column' },
],
adjustHeight: true,
adjustWidth: true,
}
),
]);
- Set fixed row heights / column widths
slide.modifyElement('MyTable', [
ModifyTableHelper.updateRowHeight(0, CmToDxa(1)),
ModifyTableHelper.updateColumnWidth(1, CmToDxa(3)),
]);
- Apply a table style and header/column banding flags
slide.modifyElement('MyTable', [
ModifyTableHelper.setTableStyle('TableStyleMedium2', [
'firstRow', 'bandRow',
]),
]);
Additional convenience methods:
ModifyTableHelper.setTableData(data)– just set data without sizingModifyTableHelper.adjustHeight(data)/adjustWidth(data)– recompute sizes only