Skip to content

Commit 3255521

Browse files
authored
Merge pull request #16 from singerla/feature-modify-table
feat: modify data, row count, col count and size of a pptx table
2 parents c673b6e + 996cc05 commit 3255521

File tree

7 files changed

+143
-23
lines changed

7 files changed

+143
-23
lines changed

src/dev.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ const automizer = new Automizer({
66
});
77

88
const pres = automizer
9-
.loadRoot(`RootTemplate.pptx`)
9+
.loadRoot(`EmptyTemplate.pptx`)
1010
.load(`SlideWithTables.pptx`, 'table');
1111

12-
// const data1 = {
13-
// body: [
14-
// { label: 'item test r1', values: [10, 16, 12] },
15-
// { label: 'item test r2', values: [12, 18, 15] },
16-
// { label: 'item test r3', values: [14, 12, 11] },
17-
// ],
18-
// };
12+
const data1 = {
13+
body: [
14+
{ label: 'item test r1', values: ['test1', 10, 16, 12] },
15+
{ label: 'item test r2', values: ['test2', 12, 18, 15] },
16+
{ label: 'item test r3', values: ['test3', 14, 12, 11] },
17+
// { label: 'item test r4', values: ['test4', 14, 12, 11] },
18+
// { label: 'item test r5', values: ['test5', 14, 12, 11] },
19+
// { label: 'item test r6', values: ['test6', 999, 12, 11] },
20+
// { label: 'item test r6', values: ['test7', 999, 12, 11] },
21+
// { label: 'item test r6', values: ['test8', 999, 12, 11] },
22+
// { label: 'item test r6', values: ['test9', 999, 12, 11] },
23+
],
24+
};
1925

2026
const data2 = [
2127
[10, 16, 12],
@@ -25,7 +31,7 @@ const data2 = [
2531

2632
pres
2733
.addSlide('table', 1, (slide) => {
28-
slide.modifyElement('TableWithHeader', [modify.setTableData(data2)]);
34+
slide.modifyElement('TableWithHeader', [modify.setTableData(data1)]);
2935
})
3036
.write(`modify-table.test.pptx`)
3137

src/helper/modify-table-helper.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { XmlHelper } from './xml-helper';
22
import { TableData } from '../types/table-types';
3+
import { ModifyTable } from '../modify/modify-table';
34

45
export default class ModifyTableHelper {
56
/**
67
* @TODO: Set table data of modify table helper
78
*/
89
static setTableData = (data: TableData) => (
9-
element: XMLDocument | Document | Element,
10+
element: Element,
1011
): void => {
11-
console.log(data);
12-
XmlHelper.dump(element);
12+
const modTable = new ModifyTable(element, data);
13+
14+
modTable
15+
.modify()
16+
.adjustHeight();
17+
18+
// console.log(data);
19+
// XmlHelper.dump(element);
1320
};
1421
}

src/helper/modify-xml-helper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { GeneralHelper } from './general-helper';
55
import { XmlHelper } from './xml-helper';
66

77
export default class ModifyXmlHelper {
8-
root: XMLDocument;
8+
root: XMLDocument | Element;
99

10-
constructor(root: XMLDocument) {
10+
constructor(root: XMLDocument | Element) {
1111
this.root = root;
1212
}
1313

@@ -40,7 +40,7 @@ export default class ModifyXmlHelper {
4040
}
4141
}
4242

43-
static text = (label: string) => (element: Element): void => {
43+
static text = (label: number | string) => (element: Element): void => {
4444
element.firstChild.textContent = String(label);
4545
};
4646

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ const setChartData = ModifyChartHelper.setChartData;
1919
const setChartVerticalLines = ModifyChartHelper.setChartVerticalLines;
2020
const setChartBubbles = ModifyChartHelper.setChartBubbles;
2121

22-
export { Automizer, ModifyHelper, ModifyShapeHelper, ModifyTableHelper, ModifyChartHelper }
22+
export {
23+
Automizer,
24+
ModifyHelper,
25+
ModifyShapeHelper,
26+
ModifyTableHelper,
27+
ModifyChartHelper,
28+
};
2329

2430
export const modify = {
2531
dump,

src/modify/modify-table.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
import { XmlHelper } from '../helper/xml-helper';
3+
import ModifyXmlHelper from '../helper/modify-xml-helper';
4+
import { TableData, TableRow } from '../types/table-types';
5+
import { Modification, ModificationTags } from '../types/modify-types';
6+
7+
export class ModifyTable {
8+
data: TableData;
9+
table: ModifyXmlHelper;
10+
xml: Element;
11+
12+
constructor(
13+
table: Element,
14+
data: TableData,
15+
) {
16+
this.data = data;
17+
18+
this.table = new ModifyXmlHelper(table);
19+
this.xml = table;
20+
}
21+
22+
modify(): ModifyTable {
23+
this.setContents()
24+
this.sliceRows()
25+
26+
return this
27+
}
28+
29+
setContents() {
30+
this.data.body.forEach((row: TableRow, r: number) => {
31+
row.values.forEach((cell: number | string, c: number) => {
32+
this.table.modify(
33+
this.row(r,
34+
this.column(c,
35+
this.cell(cell)
36+
)
37+
)
38+
)
39+
})
40+
})
41+
}
42+
43+
sliceRows() {
44+
this.table.modify({
45+
'a:tbl': this.slice('a:tr', this.data.body.length)
46+
})
47+
}
48+
49+
row = (index: number, children: ModificationTags): ModificationTags => {
50+
return {
51+
'a:tr': {
52+
index: index,
53+
children: children
54+
}
55+
}
56+
}
57+
58+
column = (index: number, children: ModificationTags): ModificationTags => {
59+
return {
60+
'a:tc': {
61+
index: index,
62+
children: children
63+
}
64+
}
65+
}
66+
67+
cell = (value: number | string): ModificationTags => {
68+
return {
69+
'a:t': {
70+
modify: ModifyXmlHelper.text(value),
71+
}
72+
}
73+
}
74+
75+
slice(tag: string, length: number): Modification {
76+
return {
77+
children: {
78+
[tag]: {
79+
collection: (collection: HTMLCollectionOf<Element>) => {
80+
XmlHelper.sliceCollection(collection, length);
81+
},
82+
},
83+
},
84+
};
85+
}
86+
87+
adjustHeight() {
88+
const tableHeight = Number(this.xml
89+
.getElementsByTagName('p:xfrm')[0]
90+
.getElementsByTagName('a:ext')[0]
91+
.getAttribute('cy'))
92+
93+
const rowHeight = tableHeight / this.data.body.length
94+
95+
this.data.body.forEach((row: TableRow, r: number) => {
96+
this.table.modify({
97+
'a:tr': {
98+
index: r,
99+
modify: ModifyXmlHelper.attribute('h', Math.round(rowHeight)),
100+
}
101+
})
102+
})
103+
}
104+
}

src/types/table-types.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
export type TableCell = {
2-
value: number | string;
3-
};
1+
42

53
export type TableRow = {
6-
label: string;
7-
values: TableCell['value'][] | TableCell[];
4+
label?: string;
5+
values: (string | number)[];
86
};
97

108
export type TableData = {
119
header?: TableRow | TableRow[];
1210
body?: TableRow[];
1311
footer?: TableRow | TableRow[];
14-
[index: number]: TableRow['values'];
1512
};

src/types/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ElementType } from '../enums/element-type';
33

44
export type SlideModificationCallback = (document: Document) => void;
55
export type ShapeModificationCallback = (
6-
XMLDocument: XMLDocument,
6+
XMLDocument: XMLDocument | Element,
77
arg1?: Document,
88
arg2?: Workbook,
99
) => void;

0 commit comments

Comments
 (0)