From efd89f24e697ac313b64e31faaeddcaa855831ac Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 4 Mar 2019 22:50:04 +0000 Subject: [PATCH] Add table column and vertical merging --- demo/demo43.ts | 17 ++++++++++++++ src/file/table/table-cell/table-cell.ts | 8 ++++++- src/file/table/table-column.ts | 22 +++++++++++++++++++ .../table-properties/table-properties.ts | 2 +- .../table/table-properties/table-width.ts | 3 ++- src/file/table/table.ts | 13 ++++++++--- 6 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 demo/demo43.ts create mode 100644 src/file/table/table-column.ts diff --git a/demo/demo43.ts b/demo/demo43.ts new file mode 100644 index 0000000000..41c0747164 --- /dev/null +++ b/demo/demo43.ts @@ -0,0 +1,17 @@ +// Add image to table cell +// 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(); + +const table = doc.createTable(4, 4); +table.getCell(2, 2).addParagraph(new Paragraph("Hello")); +table.getColumn(3).mergeCells(1, 2); +// table.getCell(3, 2).addParagraph(new Paragraph("Hello")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/table/table-cell/table-cell.ts b/src/file/table/table-cell/table-cell.ts index 3526e788bd..e5b9401fbf 100644 --- a/src/file/table/table-cell/table-cell.ts +++ b/src/file/table/table-cell/table-cell.ts @@ -3,7 +3,7 @@ import { Paragraph } from "file/paragraph"; import { IXmlableObject, XmlComponent } from "file/xml-components"; import { Table } from "../table"; -import { TableCellBorders, VerticalAlign } from "./table-cell-components"; +import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellProperties } from "./table-cell-properties"; export class TableCell extends XmlComponent { @@ -58,6 +58,12 @@ export class TableCell extends XmlComponent { return this; } + public addVerticalMerge(type: VMergeType): TableCell { + this.properties.addVerticalMerge(type); + + return this; + } + public get Borders(): TableCellBorders { return this.properties.Borders; } diff --git a/src/file/table/table-column.ts b/src/file/table/table-column.ts new file mode 100644 index 0000000000..b06a9f29a4 --- /dev/null +++ b/src/file/table/table-column.ts @@ -0,0 +1,22 @@ +import { TableCell, VMergeType } from "./table-cell"; + +export class TableColumn { + constructor(private readonly cells: TableCell[]) {} + + public getCell(index: number): TableCell { + const cell = this.cells[index]; + + if (!cell) { + throw Error("Index out of bounds when trying to get cell on column"); + } + + return cell; + } + + public mergeCells(startIndex: number, endIndex: number): TableCell { + this.cells[startIndex].addVerticalMerge(VMergeType.RESTART); + this.cells[endIndex].addVerticalMerge(VMergeType.CONTINUE); + + return this.cells[startIndex]; + } +} diff --git a/src/file/table/table-properties/table-properties.ts b/src/file/table/table-properties/table-properties.ts index c6184d70c1..f577cd56c9 100644 --- a/src/file/table/table-properties/table-properties.ts +++ b/src/file/table/table-properties/table-properties.ts @@ -17,7 +17,7 @@ export class TableProperties extends XmlComponent { this.root.push(this.cellMargin); } - public setWidth(width: number | string, type: WidthType = WidthType.AUTO): TableProperties { + public setWidth(width: number, type: WidthType = WidthType.AUTO): TableProperties { this.root.push(new PreferredTableWidth(type, width)); return this; } diff --git a/src/file/table/table-properties/table-width.ts b/src/file/table/table-properties/table-width.ts index 1c76f82c6b..957fe49094 100644 --- a/src/file/table/table-properties/table-width.ts +++ b/src/file/table/table-properties/table-width.ts @@ -1,3 +1,4 @@ +// http://officeopenxml.com/WPtableWidth.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { WidthType } from "../table-cell"; @@ -12,7 +13,7 @@ class TableWidthAttributes extends XmlAttributeComponent { } export class PreferredTableWidth extends XmlComponent { - constructor(type: WidthType, w: number | string) { + constructor(type: WidthType, w: number) { super("w:tblW"); this.root.push(new TableWidthAttributes({ type, w })); } diff --git a/src/file/table/table.ts b/src/file/table/table.ts index a4f8f5ac28..8721358cb8 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -3,6 +3,7 @@ import { XmlComponent } from "file/xml-components"; import { TableGrid } from "./grid"; import { TableCell, WidthType } from "./table-cell"; +import { TableColumn } from "./table-column"; import { ITableFloatOptions, TableProperties } from "./table-properties"; import { TableRow } from "./table-row"; @@ -51,8 +52,8 @@ export class Table extends XmlComponent { } } - public getRow(ix: number): TableRow { - const row = this.rows[ix]; + public getRow(index: number): TableRow { + const row = this.rows[index]; if (!row) { throw Error("Index out of bounds when trying to get row on table"); @@ -61,11 +62,17 @@ export class Table extends XmlComponent { return row; } + public getColumn(index: number): TableColumn { + // This is a convinence method for people who like to work with columns + const cells = this.rows.map((row) => row.getCell(index)); + return new TableColumn(cells); + } + public getCell(row: number, col: number): TableCell { return this.getRow(row).getCell(col); } - public setWidth(width: number | string, type: WidthType = WidthType.AUTO): Table { + public setWidth(width: number, type: WidthType = WidthType.AUTO): Table { this.properties.setWidth(width, type); return this; }