diff --git a/demo/demo31.ts b/demo/demo31.ts new file mode 100644 index 0000000000..366227102a --- /dev/null +++ b/demo/demo31.ts @@ -0,0 +1,26 @@ +// Example of how you would create a table and add data to it +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, VerticalAlign } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(2, 2); +table + .getCell(1, 1) + .addContent(new Paragraph("This text should be in the middle of the cell")) + .CellProperties.setVerticalAlign(VerticalAlign.CENTER); + +table + .getCell(1, 0) + .addContent( + new Paragraph( + "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", + ).heading1(), + ); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo32.ts b/demo/demo32.ts new file mode 100644 index 0000000000..373ca64266 --- /dev/null +++ b/demo/demo32.ts @@ -0,0 +1,35 @@ +// Example of how you would create a table and add data to it +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +let table = doc.createTable(2, 2); + +table.getCell(0, 0).addContent(new Paragraph("Hello")); +table.getRow(0).mergeCells(0, 1); + +doc.createParagraph("Another table").heading2(); + +table = doc.createTable(2, 3); +table.getCell(0, 0).addContent(new Paragraph("World")); +table.getRow(0).mergeCells(0, 2); + +doc.createParagraph("Another table").heading2(); + +table = doc.createTable(2, 4); +table.getCell(0, 0).addContent(new Paragraph("Foo")); + +table.getCell(1, 0).addContent(new Paragraph("Bar1")); +table.getCell(1, 1).addContent(new Paragraph("Bar2")); +table.getCell(1, 2).addContent(new Paragraph("Bar3")); +table.getCell(1, 3).addContent(new Paragraph("Bar4")); + +table.getRow(0).mergeCells(0, 3); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/table/grid.ts b/src/file/table/grid.ts index d9338e7dc4..0e477df946 100644 --- a/src/file/table/grid.ts +++ b/src/file/table/grid.ts @@ -1,3 +1,4 @@ +// http://officeopenxml.com/WPtableGrid.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; export class TableGrid extends XmlComponent { diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 0406bdf549..f6201acedc 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -1,3 +1,4 @@ +// http://officeopenxml.com/WPtableGrid.php import { GridSpan, TableCellBorders, @@ -60,7 +61,13 @@ export class Table extends XmlComponent { } public getRow(ix: number): TableRow { - return this.rows[ix]; + const row = this.rows[ix]; + + if (!row) { + throw Error("Index out of bounds when trying to get row on table"); + } + + return row; } public getCell(row: number, col: number): TableCell { @@ -93,17 +100,29 @@ export class TableRow extends XmlComponent { } public getCell(ix: number): TableCell { - return this.cells[ix]; + const cell = this.cells[ix]; + + if (!cell) { + throw Error("Index out of bounds when trying to get cell on row"); + } + + return cell; } - public addGridSpan(ix: number, cellSpan: number): TableCell { - const remainCell = this.cells[ix]; + public addGridSpan(index: number, cellSpan: number): TableCell { + const remainCell = this.cells[index]; remainCell.CellProperties.addGridSpan(cellSpan); - this.cells.splice(ix + 1, cellSpan - 1); - this.root.splice(ix + 2, cellSpan - 1); + this.cells.splice(index + 1, cellSpan - 1); + this.root.splice(index + 2, cellSpan - 1); return remainCell; } + + public mergeCells(startIndex: number, endIndex: number): TableCell { + const cellSpan = endIndex - startIndex + 1; + + return this.addGridSpan(startIndex, cellSpan); + } } export class TableRowProperties extends XmlComponent {