Skip to content

Commit 09cf5fa

Browse files
committed
feature(tables): add columns to table / adjust table width
1 parent 4cdef7a commit 09cf5fa

File tree

4 files changed

+124
-23
lines changed

4 files changed

+124
-23
lines changed

__tests__/modify-existing-table.test.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,32 @@ test('create presentation, add and modify an existing table.', async () => {
77
});
88

99
const data1 = {
10+
body: [
11+
{ label: 'item test r1', values: ['test1', 10, 16, 12, 11] },
12+
{ label: 'item test r2', values: ['test2', 12, 18, 15, 12] },
13+
{ label: 'item test r3', values: ['test3', 14, 12, 11, 14] },
14+
],
15+
};
16+
17+
const data2 = {
1018
body: [
1119
{ label: 'item test r1', values: ['test1', 10, 16, 12] },
1220
{ label: 'item test r2', values: ['test2', 12, 18, 15] },
1321
{ label: 'item test r3', values: ['test3', 14, 12, 11] },
14-
// { label: 'item test r4', values: ['test4', 14, 12, 11] },
15-
// { label: 'item test r5', values: ['test5', 14, 12, 11] },
16-
// { label: 'item test r6', values: ['test6', 999, 12, 11] },
17-
// { label: 'item test r6', values: ['test7', 999, 12, 11] },
18-
// { label: 'item test r6', values: ['test8', 999, 12, 11] },
19-
// { label: 'item test r6', values: ['test9', 999, 12, 11] },
22+
{ label: 'item test r4', values: ['test4', 14, 12, 18] },
23+
{ label: 'item test r5', values: ['test5', 14, 13, 15] },
24+
{ label: 'item test r6', values: ['test6', 999, 14, 14] },
25+
{ label: 'item test r7', values: ['test7', 998, 15, 13] },
26+
{ label: 'item test r8', values: ['test8', 997, 16, 19] },
27+
{ label: 'item test r9', values: ['test9', 996, 17, 18] },
28+
],
29+
};
30+
31+
const data3 = {
32+
body: [
33+
{ label: 'item test r1', values: ['test1', 10, 16] },
34+
{ label: 'item test r2', values: ['test2', 12, 18] },
35+
{ label: 'item test r3', values: ['test3', 14, 12] },
2036
],
2137
};
2238

@@ -26,7 +42,20 @@ test('create presentation, add and modify an existing table.', async () => {
2642

2743
const result = await pres
2844
.addSlide('tables', 1, (slide) => {
29-
slide.modifyElement('TableWithHeader', [modify.setTableData(data1)]);
45+
slide.modifyElement('TableDefault', [
46+
modify.setTable(data1),
47+
]);
48+
49+
slide.modifyElement('TableWithLabels', [
50+
modify.setTable(data2),
51+
// modify.dump
52+
]);
53+
54+
slide.modifyElement('TableWithHeader', [
55+
modify.setTableData(data3),
56+
modify.adjustHeight(data3),
57+
modify.adjustWidth(data3),
58+
]);
3059
})
3160
.write(`modify-existing-table.test.pptx`);
3261

src/helper/modify-table-helper.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@ import { TableData } from '../types/table-types';
33
import { ModifyTable } from '../modify/modify-table';
44

55
export default class ModifyTableHelper {
6-
/**
7-
* @TODO: Set table data of modify table helper
8-
*/
6+
static setTable = (data: TableData) => (
7+
element: XMLDocument | Element,
8+
): void => {
9+
const modTable = new ModifyTable(element, data);
10+
modTable.modify()
11+
modTable.adjustWidth()
12+
modTable.adjustHeight();
13+
};
14+
915
static setTableData = (data: TableData) => (
1016
element: XMLDocument | Element,
1117
): void => {
1218
const modTable = new ModifyTable(element, data);
19+
modTable.modify();
20+
};
1321

14-
modTable.modify().adjustHeight();
22+
static adjustHeight = (data: TableData) => (
23+
element: XMLDocument | Element,
24+
): void => {
25+
const modTable = new ModifyTable(element, data);
26+
modTable.adjustHeight();
27+
};
1528

16-
// console.log(data);
17-
// XmlHelper.dump(element);
29+
static adjustWidth = (data: TableData) => (
30+
element: XMLDocument | Element,
31+
): void => {
32+
const modTable = new ModifyTable(element, data);
33+
modTable.adjustWidth();
1834
};
1935
}

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const setPosition = ModifyShapeHelper.setPosition;
1313

1414
import ModifyTableHelper from './helper/modify-table-helper';
1515
const setTableData = ModifyTableHelper.setTableData;
16+
const adjustHeight = ModifyTableHelper.adjustHeight;
17+
const adjustWidth = ModifyTableHelper.adjustWidth;
18+
const setTable = ModifyTableHelper.setTable;
1619

1720
import ModifyChartHelper from './helper/modify-chart-helper';
1821
const setChartData = ModifyChartHelper.setChartData;
@@ -34,6 +37,9 @@ export const modify = {
3437
setText,
3538
setPosition,
3639
setTableData,
40+
adjustHeight,
41+
adjustWidth,
42+
setTable,
3743
setChartData,
3844
setChartVerticalLines,
3945
setChartBubbles,

src/modify/modify-table.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,47 @@ export class ModifyTable {
88
table: ModifyXmlHelper;
99
xml: XMLDocument | Element;
1010

11-
constructor(table: XMLDocument | Element, data: TableData) {
11+
constructor(table: XMLDocument | Element, data?: TableData) {
1212
this.data = data;
1313

1414
this.table = new ModifyXmlHelper(table);
1515
this.xml = table;
1616
}
1717

1818
modify(): ModifyTable {
19-
this.setContents();
19+
this.setRows();
20+
this.setGridCols();
21+
2022
this.sliceRows();
23+
this.sliceCols();
2124

2225
return this;
2326
}
2427

25-
setContents() {
28+
setRows() {
2629
this.data.body.forEach((row: TableRow, r: number) => {
2730
row.values.forEach((cell: number | string, c: number) => {
2831
this.table.modify(this.row(r, this.column(c, this.cell(cell))));
32+
this.table.modify({
33+
'a16:rowId': {
34+
index: r,
35+
modify: ModifyXmlHelper.attribute('val', r),
36+
}
37+
})
38+
});
39+
});
40+
}
41+
42+
setGridCols() {
43+
this.data.body[0].values.forEach((cell, c: number) => {
44+
this.table.modify({
45+
'a:gridCol': {
46+
index: c
47+
},
48+
'a16:colId': {
49+
index: c,
50+
modify: ModifyXmlHelper.attribute('val', c),
51+
}
2952
});
3053
});
3154
}
@@ -36,6 +59,12 @@ export class ModifyTable {
3659
});
3760
}
3861

62+
sliceCols() {
63+
this.table.modify({
64+
'a:tblGrid': this.slice('a:gridCol', this.data.body[0].values.length),
65+
});
66+
}
67+
3968
row = (index: number, children: ModificationTags): ModificationTags => {
4069
return {
4170
'a:tr': {
@@ -75,13 +104,7 @@ export class ModifyTable {
75104
}
76105

77106
adjustHeight() {
78-
const tableHeight = Number(
79-
this.xml
80-
.getElementsByTagName('p:xfrm')[0]
81-
.getElementsByTagName('a:ext')[0]
82-
.getAttribute('cy'),
83-
);
84-
107+
const tableHeight = this.getTableSize('cy')
85108
const rowHeight = tableHeight / this.data.body.length;
86109

87110
this.data.body.forEach((row: TableRow, r: number) => {
@@ -92,5 +115,32 @@ export class ModifyTable {
92115
},
93116
});
94117
});
118+
119+
return this
120+
}
121+
122+
adjustWidth() {
123+
const tableWidth = this.getTableSize('cx')
124+
const rowWidth = tableWidth / this.data.body[0].values.length;
125+
126+
this.data.body[0].values.forEach((cell, c: number) => {
127+
this.table.modify({
128+
'a:gridCol': {
129+
index: c,
130+
modify: ModifyXmlHelper.attribute('w', Math.round(rowWidth)),
131+
},
132+
});
133+
});
134+
135+
return this
136+
}
137+
138+
getTableSize(orientation: string): number {
139+
return Number(
140+
this.xml
141+
.getElementsByTagName('p:xfrm')[0]
142+
.getElementsByTagName('a:ext')[0]
143+
.getAttribute(orientation),
144+
);
95145
}
96146
}

0 commit comments

Comments
 (0)