diff --git a/demo/demo29.ts b/demo/demo29.ts index 10a3318e03..373ca64266 100644 --- a/demo/demo29.ts +++ b/demo/demo29.ts @@ -5,33 +5,29 @@ import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -doc - .createTable(2, 2) - .getCell(0, 0) - .addContent(new Paragraph("Hello")) - .setHorizontalSpan(2); +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(); -doc - .createTable(2, 3) - .getCell(0, 0) - .addContent(new Paragraph("World")) - .setHorizontalSpan(3); +table = doc.createTable(2, 3); +table.getCell(0, 0).addContent(new Paragraph("World")); +table.getRow(0).mergeCells(0, 2); doc.createParagraph("Another table").heading2(); -const table = doc.createTable(2, 4); -table - .getCell(0, 0) - .addContent(new Paragraph("Foo")) - .setHorizontalSpan(4); +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) => { diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 2dfc051e22..f6201acedc 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -52,7 +52,7 @@ export class Table extends XmlComponent { for (let i = 0; i < rows; i++) { const cells: TableCell[] = []; for (let j = 0; j < cols; j++) { - cells.push(new TableCell(this, i, j)); + cells.push(new TableCell()); } const row = new TableRow(cells); this.rows.push(row); @@ -109,14 +109,20 @@ export class TableRow extends XmlComponent { 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 { @@ -128,7 +134,7 @@ export class TableRowProperties extends XmlComponent { export class TableCell extends XmlComponent { private readonly properties: TableCellProperties; - constructor(private readonly tableReference: Table, private readonly x: number, private readonly y: number) { + constructor() { super("w:tc"); this.properties = new TableCellProperties(); this.root.push(this.properties); @@ -155,15 +161,6 @@ export class TableCell extends XmlComponent { return para; } - public setHorizontalSpan(span: number): TableCell { - for (let i = 1; i < span; i++) { - this.tableReference.getCell(this.x, this.y + i).delete(); - } - this.properties.addGridSpan(span); - - return this; - } - public get CellProperties(): TableCellProperties { return this.properties; }