|
| 1 | +import Automizer from '../src/index'; |
| 2 | + |
| 3 | +test('create presentation, add and modify an existing table.', async () => { |
| 4 | + const automizer = new Automizer({ |
| 5 | + templateDir: `${__dirname}/pptx-templates`, |
| 6 | + outputDir: `${__dirname}/pptx-output`, |
| 7 | + }); |
| 8 | + |
| 9 | + const data = [ |
| 10 | + [ |
| 11 | + { label: 'my header 1' }, |
| 12 | + { label: 'my header 2' }, |
| 13 | + { label: 'my header 3' }, |
| 14 | + { label: 'my header 4' } |
| 15 | + ], |
| 16 | + [ |
| 17 | + { label: 'my cell 1-1' }, |
| 18 | + { label: 'my cell 1-2' }, |
| 19 | + ], |
| 20 | + [], // we don't want to change body row 2 |
| 21 | + [ |
| 22 | + {}, // we also want to skip body row3/col1 and row3/col2 |
| 23 | + {}, |
| 24 | + { label: 'my cell 3-3' }, |
| 25 | + { label: 'my cell 3-4' }, |
| 26 | + ] |
| 27 | + ] |
| 28 | + |
| 29 | + const pres = automizer |
| 30 | + .loadRoot(`RootTemplate.pptx`) |
| 31 | + .load(`SlideWithTable.pptx`, 'tables'); |
| 32 | + |
| 33 | + const result = await pres |
| 34 | + .addSlide('tables', 1, (slide) => { |
| 35 | + slide.modifyElement('TableHeader', (table) => { |
| 36 | + |
| 37 | + // uncomment next line to dump table's xml in console |
| 38 | + // modify.dump(table) |
| 39 | + |
| 40 | + data.forEach((row,r) => { |
| 41 | + const tabRow = table.getElementsByTagName('a:tr')[r] |
| 42 | + row.forEach((cell,c) => { |
| 43 | + const tabCell = tabRow.getElementsByTagName('a:tc')[c] |
| 44 | + |
| 45 | + // We can hopefully found a corresponding text node |
| 46 | + // and update textContent by our own label. |
| 47 | + tabCell.getElementsByTagName('a:t') |
| 48 | + [0].firstChild.textContent = String(cell.label) |
| 49 | + }) |
| 50 | + }) |
| 51 | + }); |
| 52 | + }) |
| 53 | + .write(`modify-existing-table.test.pptx`); |
| 54 | + |
| 55 | + // expect(result.tables).toBe(2); // tbd |
| 56 | +}); |
0 commit comments