From c61d24d8d41a1d82a65c50ef9d7bb019697acfdb Mon Sep 17 00:00:00 2001 From: ramonmata Date: Mon, 21 Oct 2019 11:40:05 -0500 Subject: [PATCH 1/8] Allows user to specify style id for Tables Allows user to specify the style Id which will be added in the form of: As specified in table Structure in http://officeopenxml.com/WPtable.php --- .../table/table-properties/table-properties.ts | 6 ++++++ src/file/table/table-properties/table-style.ts | 15 +++++++++++++++ src/file/table/table.ts | 6 ++++++ 3 files changed, 27 insertions(+) create mode 100644 src/file/table/table-properties/table-style.ts diff --git a/src/file/table/table-properties/table-properties.ts b/src/file/table/table-properties/table-properties.ts index fd1f844978..245a56c444 100644 --- a/src/file/table/table-properties/table-properties.ts +++ b/src/file/table/table-properties/table-properties.ts @@ -6,6 +6,7 @@ import { TableBorders } from "./table-borders"; import { TableCellMargin } from "./table-cell-margin"; import { ITableFloatOptions, TableFloatProperties } from "./table-float-properties"; import { TableLayout, TableLayoutType } from "./table-layout"; +import { TableStyle } from "./table-style"; import { PreferredTableWidth } from "./table-width"; export class TableProperties extends IgnoreIfEmptyXmlComponent { @@ -46,4 +47,9 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent { return this; } + + public setStyle(styleId: string): TableProperties { + this.root.push(new TableStyle(styleId)); + return this; + } } diff --git a/src/file/table/table-properties/table-style.ts b/src/file/table/table-properties/table-style.ts new file mode 100644 index 0000000000..b46f853802 --- /dev/null +++ b/src/file/table/table-properties/table-style.ts @@ -0,0 +1,15 @@ +import { Attributes, XmlComponent } from "file/xml-components"; + +export class TableStyle extends XmlComponent { + public readonly styleId: string; + + constructor(styleId: string) { + super("w:tblStyle"); + this.styleId = styleId; + this.root.push( + new Attributes({ + val: styleId, + }), + ); + } +} diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 08bd47eda2..3872fe4df7 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -32,6 +32,7 @@ export interface ITableOptions { }; readonly float?: ITableFloatOptions; readonly layout?: TableLayoutType; + readonly style?: string; } export class Table extends XmlComponent { @@ -44,6 +45,7 @@ export class Table extends XmlComponent { margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 }, float, layout, + style, }: ITableOptions) { super("w:tbl"); this.properties = new TableProperties(); @@ -96,5 +98,9 @@ export class Table extends XmlComponent { if (layout) { this.properties.setLayout(layout); } + + if (style) { + this.properties.setStyle(style); + } } } From 358d578cd7e1c3b37f331c7ff820be944d84803c Mon Sep 17 00:00:00 2001 From: ramonmata Date: Mon, 21 Oct 2019 13:09:04 -0500 Subject: [PATCH 2/8] Moves setStyle to be the first set property This allows other settings as width to be overriden --- src/file/table/table.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 3872fe4df7..1a7b7d6549 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -52,6 +52,10 @@ export class Table extends XmlComponent { this.root.push(this.properties); this.properties.setBorder(); + if (style) { + this.properties.setStyle(style); + } + if (width) { this.properties.setWidth(width.size, width.type); } else { @@ -98,9 +102,5 @@ export class Table extends XmlComponent { if (layout) { this.properties.setLayout(layout); } - - if (style) { - this.properties.setStyle(style); - } } } From 4ca44d335dbf78f11e5cbeffd13031615a60b2a1 Mon Sep 17 00:00:00 2001 From: ramonmata Date: Mon, 21 Oct 2019 13:09:44 -0500 Subject: [PATCH 3/8] Adds setStyle to spec --- .../table/table-properties/table-properties.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/file/table/table-properties/table-properties.spec.ts b/src/file/table/table-properties/table-properties.spec.ts index 960d117b7c..88f7abde4e 100644 --- a/src/file/table/table-properties/table-properties.spec.ts +++ b/src/file/table/table-properties/table-properties.spec.ts @@ -19,6 +19,16 @@ describe("TableProperties", () => { }); }); + describe("#setStyle", () => { + it("should add a table style property", () => { + const tp = new TableProperties().setStyle("TableNormal"); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [{ "w:tblStyle": { _attr: { "w:val": "TableNormal" } } }], + }); + }); + }); + describe("#setWidth", () => { it("should add a table width property", () => { const tp = new TableProperties().setWidth(1234, WidthType.DXA); From 2cf1cce06d44fb185ebcfc9af3410577e907baec Mon Sep 17 00:00:00 2001 From: ramonmata Date: Thu, 24 Oct 2019 13:46:39 -0500 Subject: [PATCH 4/8] Adds demo to show XML StyleId applied to a table --- demo/48-table-xml-styles.ts | 50 +++++++++++++++++++++++++++++++++++ demo/assets/custom-styles.xml | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 demo/48-table-xml-styles.ts diff --git a/demo/48-table-xml-styles.ts b/demo/48-table-xml-styles.ts new file mode 100644 index 0000000000..e993c5be34 --- /dev/null +++ b/demo/48-table-xml-styles.ts @@ -0,0 +1,50 @@ +// 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, Table, TableCell, TableRow, WidthType } from "../build"; + +const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); +const doc = new Document({ + title: "Title", + externalStyles: styles +}); + + +// Create a table and pass the XML Style +const table = new Table({ + style: 'MyCustomTableStyle', + width: { + size: 9070, + type: WidthType.DXA + }, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Header Colum 1")], + }), + new TableCell({ + children: [new Paragraph("Header Colum 2")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Column Content 3")], + }), + new TableCell({ + children: [new Paragraph("Column Content 2")], + }), + ], + }), + ], +}); + +doc.addSection({ + children: [table], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/assets/custom-styles.xml b/demo/assets/custom-styles.xml index 76159f2985..33becc2d37 100644 --- a/demo/assets/custom-styles.xml +++ b/demo/assets/custom-styles.xml @@ -1,2 +1,2 @@ - \ No newline at end of file + From 2100b7b7d21ff4667c9ef3df2659428574b3947f Mon Sep 17 00:00:00 2001 From: ramonmata Date: Thu, 24 Oct 2019 20:18:52 -0500 Subject: [PATCH 5/8] Updated custom styles --- demo/assets/custom-styles.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/assets/custom-styles.xml b/demo/assets/custom-styles.xml index 33becc2d37..2b6f2508a3 100644 --- a/demo/assets/custom-styles.xml +++ b/demo/assets/custom-styles.xml @@ -1,2 +1,2 @@ - + From b972f1789e09d2e32f553dfde01d041b91e1ef93 Mon Sep 17 00:00:00 2001 From: Dolan Date: Sun, 7 Mar 2021 21:43:30 +0000 Subject: [PATCH 6/8] Fix tests --- src/file/table/table-properties/table-properties.spec.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/file/table/table-properties/table-properties.spec.ts b/src/file/table/table-properties/table-properties.spec.ts index 49ff9f740e..a6a9b5879f 100644 --- a/src/file/table/table-properties/table-properties.spec.ts +++ b/src/file/table/table-properties/table-properties.spec.ts @@ -22,7 +22,9 @@ describe("TableProperties", () => { describe("#setStyle", () => { it("should add a table style property", () => { - const tp = new TableProperties().setStyle("TableNormal"); + const tp = new TableProperties({ + style: "TableNormal", + }); const tree = new Formatter().format(tp); expect(tree).to.deep.equal({ "w:tblPr": [{ "w:tblStyle": { _attr: { "w:val": "TableNormal" } } }], From 0ec3b64ad7db0c1ca7bd3f9303df6c9e8f944905 Mon Sep 17 00:00:00 2001 From: Dolan Date: Sun, 7 Mar 2021 21:45:59 +0000 Subject: [PATCH 7/8] Add tests --- .../table-properties/table-style.spec.ts | 22 +++++++++++++++++++ .../table/table-properties/table-style.ts | 4 +--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/file/table/table-properties/table-style.spec.ts diff --git a/src/file/table/table-properties/table-style.spec.ts b/src/file/table/table-properties/table-style.spec.ts new file mode 100644 index 0000000000..1b9b0e9748 --- /dev/null +++ b/src/file/table/table-properties/table-style.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { TableStyle } from "./table-style"; + +describe("TableStyle", () => { + describe("#constructor", () => { + it("should create", () => { + const tableStyle = new TableStyle("test-id"); + const tree = new Formatter().format(tableStyle); + + expect(tree).to.deep.equal({ + "w:tblStyle": { + _attr: { + "w:val": "test-id", + }, + }, + }); + }); + }); +}); diff --git a/src/file/table/table-properties/table-style.ts b/src/file/table/table-properties/table-style.ts index b46f853802..22b4dc5e89 100644 --- a/src/file/table/table-properties/table-style.ts +++ b/src/file/table/table-properties/table-style.ts @@ -1,11 +1,9 @@ import { Attributes, XmlComponent } from "file/xml-components"; export class TableStyle extends XmlComponent { - public readonly styleId: string; - constructor(styleId: string) { super("w:tblStyle"); - this.styleId = styleId; + this.root.push( new Attributes({ val: styleId, From 283c42cb55a08fc31028e5868489adc702ccb2d1 Mon Sep 17 00:00:00 2001 From: Dolan Date: Sun, 7 Mar 2021 21:53:16 +0000 Subject: [PATCH 8/8] Remove unused method --- src/file/table/table-properties/table-cell-margin.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/file/table/table-properties/table-cell-margin.ts b/src/file/table/table-properties/table-cell-margin.ts index 6889b6f580..9d94e7c84a 100644 --- a/src/file/table/table-properties/table-cell-margin.ts +++ b/src/file/table/table-properties/table-cell-margin.ts @@ -22,15 +22,6 @@ class BaseTableCellMargin extends XmlComponent { }), ); } - - public setProperties(value: number, type: WidthType = WidthType.DXA): void { - this.root.push( - new TableCellMarginAttributes({ - type: type, - value: value, - }), - ); - } } export interface ITableCellMarginOptions {