diff --git a/demo/demo11.ts b/demo/demo11.ts index a4f0b5f949..2b2e183c32 100644 --- a/demo/demo11.ts +++ b/demo/demo11.ts @@ -106,7 +106,10 @@ doc.createParagraph("Sir,").style("normalPara"); doc.createParagraph("BRIEF DESCRIPTION").style("normalPara"); -const table = new Table(4, 4); +const table = new Table({ + rows: 4, + columns: 4, +}); table .getRow(0) .getCell(0) diff --git a/demo/demo20.ts b/demo/demo20.ts index c0158d8dce..d80d23913b 100644 --- a/demo/demo20.ts +++ b/demo/demo20.ts @@ -5,7 +5,10 @@ import { BorderStyle, Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(4, 4); +const table = doc.createTable({ + rows: 4, + columns: 4, +}); table .getCell(2, 2) .addParagraph(new Paragraph("Hello")) diff --git a/demo/demo24.ts b/demo/demo24.ts index 62d80592a4..831a4c29a8 100644 --- a/demo/demo24.ts +++ b/demo/demo24.ts @@ -5,7 +5,10 @@ import { Document, Media, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(4, 4); +const table = doc.createTable({ + rows: 4, + columns: 4, +}); table.getCell(2, 2).addParagraph(new Paragraph("Hello")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); diff --git a/demo/demo31.ts b/demo/demo31.ts index 349dd7e393..e05a20f454 100644 --- a/demo/demo31.ts +++ b/demo/demo31.ts @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph, VerticalAlign } from "../build"; const doc = new Document(); -const table = doc.createTable(2, 2); +const table = doc.createTable({ + rows: 2, + columns: 2, +}); table .getCell(1, 1) .addParagraph(new Paragraph("This text should be in the middle of the cell")) diff --git a/demo/demo32.ts b/demo/demo32.ts index b7c75593ee..7b13855bf2 100644 --- a/demo/demo32.ts +++ b/demo/demo32.ts @@ -1,33 +1,68 @@ // Example of how you would merge cells together // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, WidthType } from "../build"; const doc = new Document(); -let table = doc.createTable(2, 2); +let table = doc.createTable({ + rows: 2, + columns: 2, +}); table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getRow(0).mergeCells(0, 1); doc.createParagraph("Another table").heading2(); -table = doc.createTable(2, 3); -table.getCell(0, 0).addParagraph(new Paragraph("World")); +table = doc.createTable({ + rows: 2, + columns: 3, + width: 100, + widthUnitType: WidthType.AUTO, + columnWidths: [1000, 1000, 1000], +}); +table.getCell(0, 0).addParagraph(new Paragraph("World")).setMargains({ + top: 1000, + bottom: 1000, + left: 1000, + right: 1000, +}); table.getRow(0).mergeCells(0, 2); doc.createParagraph("Another table").heading2(); -table = doc.createTable(2, 4); +table = doc.createTable({ + rows: 2, + columns: 4, + width: 7000, + widthUnitType: WidthType.DXA, + margains: { + top: 400, + bottom: 400, + right: 400, + left: 400, + }, +}); table.getCell(0, 0).addParagraph(new Paragraph("Foo")); +table.getCell(0, 1).addParagraph(new Paragraph("v")); -// table.getCell(1, 0).addParagraph(new Paragraph("Bar1")); +table.getCell(1, 0).addParagraph(new Paragraph("Bar1")); // table.getCell(1, 1).addParagraph(new Paragraph("Bar2")); // table.getCell(1, 2).addParagraph(new Paragraph("Bar3")); // table.getCell(1, 3).addParagraph(new Paragraph("Bar4")); // table.getRow(0).mergeCells(0, 3); +doc.createParagraph("hi"); + +doc.createTable({ + rows: 2, + columns: 2, + width: 100, + widthUnitType: WidthType.PERCENTAGE, +}); + const packer = new Packer(); packer.toBuffer(doc).then((buffer) => { diff --git a/demo/demo34.ts b/demo/demo34.ts index 3b0630a2bf..0899f26bfe 100644 --- a/demo/demo34.ts +++ b/demo/demo34.ts @@ -5,14 +5,19 @@ import { Document, Packer, Paragraph, RelativeHorizontalPosition, RelativeVertic const doc = new Document(); -const table = doc.createTable(2, 2).float({ - horizontalAnchor: TableAnchorType.MARGIN, - verticalAnchor: TableAnchorType.MARGIN, - relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, - relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, +const table = doc.createTable({ + rows: 2, + columns: 2, + float: { + horizontalAnchor: TableAnchorType.MARGIN, + verticalAnchor: TableAnchorType.MARGIN, + relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, + relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, + }, + width: 4535, + widthUnitType: WidthType.DXA, }); table.setFixedWidthLayout(); -table.setWidth(4535, WidthType.DXA); table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getRow(0).mergeCells(0, 1); diff --git a/demo/demo36.ts b/demo/demo36.ts index 1066c2a4d9..5baa5086c3 100644 --- a/demo/demo36.ts +++ b/demo/demo36.ts @@ -6,7 +6,10 @@ import { Document, Media, Packer, Table } from "../build"; const doc = new Document(); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); -const table = new Table(2, 2); +const table = new Table({ + rows: 2, + columns: 2, +}); table.getCell(1, 1).addParagraph(image.Paragraph); // doc.createParagraph("Hello World"); diff --git a/demo/demo4.ts b/demo/demo4.ts index 87c3f418e6..805e7ed33f 100644 --- a/demo/demo4.ts +++ b/demo/demo4.ts @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(4, 4); +const table = doc.createTable({ + rows: 4, + columns: 4, +}); table.getCell(2, 2).addParagraph(new Paragraph("Hello")); const packer = new Packer(); diff --git a/demo/demo41.ts b/demo/demo41.ts index fa573b674b..a7094784ca 100644 --- a/demo/demo41.ts +++ b/demo/demo41.ts @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(13, 6); +const table = doc.createTable({ + rows: 13, + columns: 6, +}); let row = 0; table.getCell(row, 0).addContent(new Paragraph("0,0")); table.getCell(row, 1).addContent(new Paragraph("0,1")); diff --git a/demo/demo43.ts b/demo/demo43.ts index 41c0747164..65d34355aa 100644 --- a/demo/demo43.ts +++ b/demo/demo43.ts @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const table = doc.createTable(4, 4); +const table = doc.createTable({ + rows: 4, + columns: 4, +}); table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getColumn(3).mergeCells(1, 2); // table.getCell(3, 2).addParagraph(new Paragraph("Hello")); diff --git a/src/file/table/table-cell/cell-margain/cell-margain.spec.ts b/src/file/table/table-cell/cell-margain/cell-margain.spec.ts new file mode 100644 index 0000000000..1d60cedabd --- /dev/null +++ b/src/file/table/table-cell/cell-margain/cell-margain.spec.ts @@ -0,0 +1,81 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain"; + +describe("TopCellMargain", () => { + describe("#constructor", () => { + it("should create", () => { + const cellMargain = new TopCellMargain(1); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:top": [ + { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + ], + }); + }); + }); +}); + +describe("BottomCellMargain", () => { + describe("#constructor", () => { + it("should create", () => { + const cellMargain = new BottomCellMargain(1); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:bottom": [ + { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + ], + }); + }); + }); +}); + +describe("LeftCellMargain", () => { + describe("#constructor", () => { + it("should create", () => { + const cellMargain = new LeftCellMargain(1); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:start": [ + { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + ], + }); + }); + }); +}); + +describe("RightCellMargain", () => { + describe("#constructor", () => { + it("should create", () => { + const cellMargain = new RightCellMargain(1); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:end": [ + { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + ], + }); + }); + }); +}); diff --git a/src/file/table/table-cell/cell-margain/cell-margain.ts b/src/file/table/table-cell/cell-margain/cell-margain.ts new file mode 100644 index 0000000000..ccfd072f2d --- /dev/null +++ b/src/file/table/table-cell/cell-margain/cell-margain.ts @@ -0,0 +1,63 @@ +// http://officeopenxml.com/WPtableCellProperties-Margins.php +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +export interface ICellMargainProperties { + readonly type: string; + readonly width: number; +} + +class CellMargainAttributes extends XmlAttributeComponent { + protected readonly xmlKeys = { width: "w:w", type: "w:type" }; +} + +export class TopCellMargain extends XmlComponent { + constructor(value: number) { + super("w:top"); + + this.root.push( + new CellMargainAttributes({ + width: value, + type: "dxa", + }), + ); + } +} + +export class BottomCellMargain extends XmlComponent { + constructor(value: number) { + super("w:bottom"); + + this.root.push( + new CellMargainAttributes({ + width: value, + type: "dxa", + }), + ); + } +} + +export class LeftCellMargain extends XmlComponent { + constructor(value: number) { + super("w:start"); + + this.root.push( + new CellMargainAttributes({ + width: value, + type: "dxa", + }), + ); + } +} + +export class RightCellMargain extends XmlComponent { + constructor(value: number) { + super("w:end"); + + this.root.push( + new CellMargainAttributes({ + width: value, + type: "dxa", + }), + ); + } +} diff --git a/src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts b/src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts new file mode 100644 index 0000000000..f90f837f30 --- /dev/null +++ b/src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts @@ -0,0 +1,112 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { TableCellMargain } from "./table-cell-margains"; + +describe("TableCellMargain", () => { + describe("#constructor", () => { + it("should create with default values", () => { + const cellMargain = new TableCellMargain({}); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:tcMar": [ + { + "w:top": [ + { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + ], + }, + { + "w:bottom": [ + { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + ], + }, + { + "w:end": [ + { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + ], + }, + { + "w:start": [ + { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + ], + }, + ], + }); + }); + + it("should create with values", () => { + const cellMargain = new TableCellMargain({ + top: 5, + bottom: 5, + left: 5, + right: 5, + }); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ + "w:tcMar": [ + { + "w:top": [ + { + _attr: { + "w:type": "dxa", + "w:w": 5, + }, + }, + ], + }, + { + "w:bottom": [ + { + _attr: { + "w:type": "dxa", + "w:w": 5, + }, + }, + ], + }, + { + "w:end": [ + { + _attr: { + "w:type": "dxa", + "w:w": 5, + }, + }, + ], + }, + { + "w:start": [ + { + _attr: { + "w:type": "dxa", + "w:w": 5, + }, + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/table/table-cell/cell-margain/table-cell-margains.ts b/src/file/table/table-cell/cell-margain/table-cell-margains.ts new file mode 100644 index 0000000000..634b38af5d --- /dev/null +++ b/src/file/table/table-cell/cell-margain/table-cell-margains.ts @@ -0,0 +1,21 @@ +// http://officeopenxml.com/WPtableCellProperties-Margins.php +import { XmlComponent } from "file/xml-components"; + +import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain"; + +export interface ITableCellMargainOptions { + readonly top?: number; + readonly left?: number; + readonly bottom?: number; + readonly right?: number; +} + +export class TableCellMargain extends XmlComponent { + constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMargainOptions) { + super("w:tcMar"); + this.root.push(new TopCellMargain(top)); + this.root.push(new BottomCellMargain(bottom)); + this.root.push(new RightCellMargain(right)); + this.root.push(new LeftCellMargain(left)); + } +} diff --git a/src/file/table/table-cell/table-cell-properties.ts b/src/file/table/table-cell/table-cell-properties.ts index 2d8d45ed2d..accec2d0ca 100644 --- a/src/file/table/table-cell/table-cell-properties.ts +++ b/src/file/table/table-cell/table-cell-properties.ts @@ -1,5 +1,6 @@ import { XmlComponent } from "file/xml-components"; +import { ITableCellMargainOptions, TableCellMargain } from "./cell-margain/table-cell-margains"; import { GridSpan, ITableCellShadingAttributesProperties, @@ -55,4 +56,10 @@ export class TableCellProperties extends XmlComponent { return this; } + + public addMargains(options: ITableCellMargainOptions): TableCellProperties { + this.root.push(new TableCellMargain(options)); + + return this; + } } diff --git a/src/file/table/table-cell/table-cell.ts b/src/file/table/table-cell/table-cell.ts index 8e78eaac23..dee03fcff0 100644 --- a/src/file/table/table-cell/table-cell.ts +++ b/src/file/table/table-cell/table-cell.ts @@ -3,6 +3,7 @@ import { Paragraph } from "file/paragraph"; import { IXmlableObject, XmlComponent } from "file/xml-components"; import { Table } from "../table"; +import { ITableCellMargainOptions } from "./cell-margain/table-cell-margains"; import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellProperties } from "./table-cell-properties"; @@ -11,6 +12,7 @@ export class TableCell extends XmlComponent { constructor() { super("w:tc"); + this.properties = new TableCellProperties(); this.root.push(this.properties); } @@ -64,6 +66,12 @@ export class TableCell extends XmlComponent { return this; } + public setMargains(margains: ITableCellMargainOptions): TableCell { + this.properties.addMargains(margains); + + return this; + } + public get Borders(): TableCellBorders { return this.properties.Borders; } diff --git a/src/file/table/table-row/table-row.spec.ts b/src/file/table/table-row/table-row.spec.ts new file mode 100644 index 0000000000..9cc2d3e478 --- /dev/null +++ b/src/file/table/table-row/table-row.spec.ts @@ -0,0 +1,82 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { TableCell } from "../table-cell"; +import { TableRow } from "./table-row"; + +describe("TableRow", () => { + describe("#constructor", () => { + it("should create with no cells", () => { + const tableRow = new TableRow([]); + const tree = new Formatter().format(tableRow); + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [], + }, + ], + }); + }); + + it("should create with one cell", () => { + const tableRow = new TableRow([new TableCell()]); + const tree = new Formatter().format(tableRow); + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [], + }, + { + "w:tc": [ + { + "w:tcPr": [], + }, + { + "w:p": [ + { + "w:pPr": [], + }, + ], + }, + ], + }, + ], + }); + }); + }); + + describe("#getCell", () => { + it("should get the cell", () => { + const cell = new TableCell(); + const tableRow = new TableRow([cell]); + + expect(tableRow.getCell(0)).to.equal(cell); + }); + + it("should throw an error if index is out of bounds", () => { + const cell = new TableCell(); + const tableRow = new TableRow([cell]); + + expect(() => tableRow.getCell(1)).to.throw(); + }); + }); + + describe("#addGridSpan", () => { + it("should merge the cell", () => { + const tableRow = new TableRow([new TableCell(), new TableCell()]); + + tableRow.addGridSpan(0, 2); + expect(() => tableRow.getCell(1)).to.throw(); + }); + }); + + describe("#mergeCells", () => { + it("should merge the cell", () => { + const tableRow = new TableRow([new TableCell(), new TableCell()]); + + tableRow.mergeCells(0, 1); + expect(() => tableRow.getCell(1)).to.throw(); + }); + }); +}); diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index 15177b402b..c960496dcb 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -9,15 +9,23 @@ import { Table } from "./table"; import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties"; const DEFAULT_TABLE_PROPERTIES = { - "w:tblBorders": [ + "w:tblCellMar": [ + { + "w:bottom": [ + { + _attr: { + "w:sz": "auto", + "w:w": 0, + }, + }, + ], + }, { "w:top": [ { _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", + "w:sz": "auto", + "w:w": 0, }, }, ], @@ -26,22 +34,8 @@ const DEFAULT_TABLE_PROPERTIES = { "w:left": [ { _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", - }, - }, - ], - }, - { - "w:bottom": [ - { - _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", + "w:sz": "auto", + "w:w": 0, }, }, ], @@ -50,34 +44,8 @@ const DEFAULT_TABLE_PROPERTIES = { "w:right": [ { _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", - }, - }, - ], - }, - { - "w:insideH": [ - { - _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", - }, - }, - ], - }, - { - "w:insideV": [ - { - _attr: { - "w:color": "auto", - "w:space": 0, - "w:sz": 4, - "w:val": "single", + "w:sz": "auto", + "w:w": 0, }, }, ], @@ -85,15 +53,88 @@ const DEFAULT_TABLE_PROPERTIES = { ], }; +const BORDERS = { + "w:tblBorders": [ + { "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + { "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + { "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + { "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + { "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + { "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, + ], +}; + +const WIDTHS = { + "w:tblW": [ + { + _attr: { + "w:type": "auto", + "w:w": 100, + }, + }, + ], +}; + +// const f = { +// "w:tbl": [ +// { +// "w:tblPr": [ +// { +// "w:tblCellMar": [ +// { "w:bottom": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] }, +// { "w:top": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] }, +// { "w:left": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] }, +// { "w:right": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] }, +// ], +// }, +// { +// "w:tblBorders": [ +// { "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// { "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// { "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// { "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// { "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// { "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] }, +// ], +// }, +// { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 100 } }] }, +// { +// "w:tblpPr": [ +// { +// _attr: { +// "w:horzAnchor": "margin", +// "w:vertAnchor": "page", +// "w:tblpX": 10, +// "w:tblpXSpec": "center", +// "w:tblpY": 20, +// "w:tblpYSpec": "bottom", +// "w:bottomFromText": 30, +// "w:topFromText": 40, +// "w:leftFromText": 50, +// "w:rightFromText": 60, +// }, +// }, +// ], +// }, +// ], +// }, +// { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }] }, +// { "w:tr": [{ "w:trPr": [] }, { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] }] }, +// ], +// }; + describe("Table", () => { describe("#constructor", () => { it("creates a table with the correct number of rows and columns", () => { - const table = new Table(3, 2); + const table = new Table({ + rows: 3, + columns: 2, + }); const tree = new Formatter().format(table); const cell = { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] }; expect(tree).to.deep.equal({ "w:tbl": [ - { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, + { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] }, { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, @@ -106,7 +147,10 @@ describe("Table", () => { }); describe("#getRow and Row#getCell", () => { - const table = new Table(2, 2); + const table = new Table({ + rows: 2, + columns: 2, + }); it("should return the correct row", () => { table @@ -136,7 +180,7 @@ describe("Table", () => { }); expect(tree).to.deep.equal({ "w:tbl": [ - { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, + { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] }, { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, @@ -152,9 +196,12 @@ describe("Table", () => { }); describe("#getColumn", () => { - const table = new Table(2, 2); + const table = new Table({ + rows: 2, + columns: 2, + }); - it("should get correct row", () => { + it("should get correct cell", () => { const column = table.getColumn(0); expect(column.getCell(0)).to.equal(table.getCell(0, 0)); @@ -164,7 +211,10 @@ describe("Table", () => { describe("#getCell", () => { it("should returns the correct cell", () => { - const table = new Table(2, 2); + const table = new Table({ + rows: 2, + columns: 2, + }); table.getCell(0, 0).addParagraph(new Paragraph("A1")); table.getCell(0, 1).addParagraph(new Paragraph("B1")); table.getCell(1, 0).addParagraph(new Paragraph("A2")); @@ -180,7 +230,7 @@ describe("Table", () => { }); expect(tree).to.deep.equal({ "w:tbl": [ - { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, + { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] }, { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, @@ -193,7 +243,7 @@ describe("Table", () => { // describe("#setWidth", () => { // it("should set the preferred width on the table", () => { - // const table = new Table(2, 2).setWidth(1000, WidthType.PERCENTAGE); + // const table = new Table({rows: 1,columns: 1,}).setWidth(1000, WidthType.PERCENTAGE); // const tree = new Formatter().format(table); // expect(tree) // .to.have.property("w:tbl") @@ -205,7 +255,7 @@ describe("Table", () => { // }); // it("sets the preferred width on the table with a default of AUTO", () => { - // const table = new Table(2, 2).setWidth(1000); + // const table = new Table({rows: 1,columns: 1,}).setWidth(1000); // const tree = new Formatter().format(table); // expect(tree["w:tbl"][0]).to.deep.equal({ @@ -216,14 +266,17 @@ describe("Table", () => { describe("#setFixedWidthLayout", () => { it("sets the table to fixed width layout", () => { - const table = new Table(2, 2).setFixedWidthLayout(); + const table = new Table({ + rows: 1, + columns: 1, + }).setFixedWidthLayout(); const tree = new Formatter().format(table); expect(tree) .to.have.property("w:tbl") .which.is.an("array") .with.has.length.at.least(1); expect(tree["w:tbl"][0]).to.deep.equal({ - "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }], + "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }], }); }); }); @@ -231,7 +284,10 @@ describe("Table", () => { describe("Cell", () => { describe("#prepForXml", () => { it("inserts a paragraph at the end of the cell if it is empty", () => { - const table = new Table(1, 1); + const table = new Table({ + rows: 1, + columns: 1, + }); const tree = new Formatter().format(table); expect(tree) .to.have.property("w:tbl") @@ -247,8 +303,16 @@ describe("Table", () => { }); it("inserts a paragraph at the end of the cell even if it has a child table", () => { - const parentTable = new Table(1, 1); - parentTable.getCell(0, 0).addTable(new Table(1, 1)); + const parentTable = new Table({ + rows: 1, + columns: 1, + }); + parentTable.getCell(0, 0).addTable( + new Table({ + rows: 1, + columns: 1, + }), + ); const tree = new Formatter().format(parentTable); expect(tree) .to.have.property("w:tbl") @@ -266,7 +330,10 @@ describe("Table", () => { }); it("does not insert a paragraph if it already ends with one", () => { - const parentTable = new Table(1, 1); + const parentTable = new Table({ + rows: 1, + columns: 1, + }); parentTable.getCell(0, 0).addParagraph(new Paragraph("Hello")); const tree = new Formatter().format(parentTable); expect(tree) @@ -293,7 +360,10 @@ describe("Table", () => { describe("#createParagraph", () => { it("inserts a new paragraph in the cell", () => { - const table = new Table(1, 1); + const table = new Table({ + rows: 1, + columns: 1, + }); const para = table.getCell(0, 0).createParagraph("Test paragraph"); expect(para).to.be.an.instanceof(Paragraph); const tree = new Formatter().format(table); @@ -324,17 +394,21 @@ describe("Table", () => { describe("#float", () => { it("sets the table float properties", () => { - const table = new Table(1, 1).float({ - horizontalAnchor: TableAnchorType.MARGIN, - verticalAnchor: TableAnchorType.PAGE, - absoluteHorizontalPosition: 10, - relativeHorizontalPosition: RelativeHorizontalPosition.CENTER, - absoluteVerticalPosition: 20, - relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, - bottomFromText: 30, - topFromText: 40, - leftFromText: 50, - rightFromText: 60, + const table = new Table({ + rows: 1, + columns: 1, + float: { + horizontalAnchor: TableAnchorType.MARGIN, + verticalAnchor: TableAnchorType.PAGE, + absoluteHorizontalPosition: 10, + relativeHorizontalPosition: RelativeHorizontalPosition.CENTER, + absoluteVerticalPosition: 20, + relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, + bottomFromText: 30, + topFromText: 40, + leftFromText: 50, + rightFromText: 60, + }, }); const tree = new Formatter().format(table); expect(tree) @@ -344,6 +418,8 @@ describe("Table", () => { expect(tree["w:tbl"][0]).to.deep.equal({ "w:tblPr": [ DEFAULT_TABLE_PROPERTIES, + BORDERS, + WIDTHS, { "w:tblpPr": [ { diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 6c7de2e950..06cfd7c960 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -16,11 +16,6 @@ import { TableRow } from "./table-row"; table will make it look reasonable, as the layout algorithm will expand columns to fit its content */ -export interface IWidthOptions { - readonly width: number; - readonly type?: WidthType; -} - export interface ITableOptions { readonly rows: number; readonly columns: number; @@ -104,9 +99,4 @@ export class Table extends XmlComponent { this.properties.setFixedWidthLayout(); return this; } - - public float(tableFloatOptions: ITableFloatOptions): Table { - this.properties.setTableFloatProperties(tableFloatOptions); - return this; - } }