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..2dfc051e22 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, @@ -51,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()); + cells.push(new TableCell(this, i, j)); } const row = new TableRow(cells); this.rows.push(row); @@ -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,7 +100,13 @@ 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 { @@ -115,7 +128,7 @@ export class TableRowProperties extends XmlComponent { export class TableCell extends XmlComponent { private readonly properties: TableCellProperties; - constructor() { + constructor(private readonly tableReference: Table, private readonly x: number, private readonly y: number) { super("w:tc"); this.properties = new TableCellProperties(); this.root.push(this.properties); @@ -142,6 +155,15 @@ 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; }