From 11ce9a52067a372ed251999f7d1ae946934aa789 Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 12 Sep 2018 21:01:52 +0100 Subject: [PATCH 1/5] Add horizontal span --- src/file/table/grid.ts | 1 + src/file/table/table.ts | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) 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; } From f1b176670caf4334fb10ef5b64dd451bc470221b Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 12 Sep 2018 21:03:06 +0100 Subject: [PATCH 2/5] Add more table demos --- demo/demo28.ts | 18 ++++++++++++++++++ demo/demo29.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 demo/demo28.ts create mode 100644 demo/demo29.ts diff --git a/demo/demo28.ts b/demo/demo28.ts new file mode 100644 index 0000000000..b7eab7b919 --- /dev/null +++ b/demo/demo28.ts @@ -0,0 +1,18 @@ +// 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(4, 4); +table + .getCell(2, 2) + .addContent(new Paragraph("This text should be in the middle of the cell")) + .CellProperties.setVerticalAlign(VerticalAlign.CENTER); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo29.ts b/demo/demo29.ts new file mode 100644 index 0000000000..8a5bc3fc03 --- /dev/null +++ b/demo/demo29.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 } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(2, 2); +table + .getCell(0, 0) + .addContent(new Paragraph("Hello")) + .setHorizontalSpan(2); + +doc.createParagraph("Another table").heading2(); + +const table2 = doc.createTable(2, 3); +table2 + .getCell(0, 0) + .addContent(new Paragraph("World")) + .setHorizontalSpan(3); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); From a9167b48094c46186a5f82f0caafbf0050607b55 Mon Sep 17 00:00:00 2001 From: Dolan Date: Thu, 13 Sep 2018 01:54:37 +0100 Subject: [PATCH 3/5] Update demos --- demo/demo28.ts | 12 ++++++++++-- demo/demo29.ts | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/demo/demo28.ts b/demo/demo28.ts index b7eab7b919..366227102a 100644 --- a/demo/demo28.ts +++ b/demo/demo28.ts @@ -5,12 +5,20 @@ import { Document, Packer, Paragraph, VerticalAlign } from "../build"; const doc = new Document(); -const table = doc.createTable(4, 4); +const table = doc.createTable(2, 2); table - .getCell(2, 2) + .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) => { diff --git a/demo/demo29.ts b/demo/demo29.ts index 8a5bc3fc03..10a3318e03 100644 --- a/demo/demo29.ts +++ b/demo/demo29.ts @@ -5,20 +5,33 @@ import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(2, 2); -table +doc + .createTable(2, 2) .getCell(0, 0) .addContent(new Paragraph("Hello")) .setHorizontalSpan(2); doc.createParagraph("Another table").heading2(); -const table2 = doc.createTable(2, 3); -table2 +doc + .createTable(2, 3) .getCell(0, 0) .addContent(new Paragraph("World")) .setHorizontalSpan(3); +doc.createParagraph("Another table").heading2(); + +const table = doc.createTable(2, 4); +table + .getCell(0, 0) + .addContent(new Paragraph("Foo")) + .setHorizontalSpan(4); + +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")); + const packer = new Packer(); packer.toBuffer(doc).then((buffer) => { From 9f0b2f707446762b93f63b5b8f5cebf9490ddfcc Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 15 Oct 2018 21:54:33 +0100 Subject: [PATCH 4/5] Improve API --- demo/demo29.ts | 26 +++++++++++--------------- src/file/table/table.ts | 27 ++++++++++++--------------- 2 files changed, 23 insertions(+), 30 deletions(-) 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; } From 8ac19a83b2d55764553ad0a9e944907a75c26a21 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 15 Oct 2018 22:21:40 +0100 Subject: [PATCH 5/5] Rename demos --- demo/{demo28.ts => demo31.ts} | 0 demo/{demo29.ts => demo32.ts} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename demo/{demo28.ts => demo31.ts} (100%) rename demo/{demo29.ts => demo32.ts} (100%) diff --git a/demo/demo28.ts b/demo/demo31.ts similarity index 100% rename from demo/demo28.ts rename to demo/demo31.ts diff --git a/demo/demo29.ts b/demo/demo32.ts similarity index 100% rename from demo/demo29.ts rename to demo/demo32.ts