diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index a6e8db42b4..7b0d99aaed 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -45,4 +45,11 @@ export class Document extends XmlComponent { public addTable(table: Table): void { this.body.push(table); } + + public createTable(rows: number, cols: number): Table { + const table = new Table(rows, cols); + this.addTable(table); + return table; + } + } diff --git a/ts/tests/docx/document/documentTest.ts b/ts/tests/docx/document/documentTest.ts index 98b9a9cf97..abb6ab568b 100644 --- a/ts/tests/docx/document/documentTest.ts +++ b/ts/tests/docx/document/documentTest.ts @@ -46,4 +46,28 @@ describe("Document", () => { }); }); }); + + describe("#createTable", () => { + it("should create a new table and append it to body", () => { + const table = document.createTable(2, 3); + expect(table).to.be.an.instanceof(docx.Table); + const body = new Formatter().format(document)["w:document"][1]["w:body"]; + expect(body).to.be.an("array").which.has.length.at.least(1); + expect(body[0]).to.have.property("w:tbl"); + }); + + it("should create a table with the correct dimensions", () => { + const table = document.createTable(2, 3); + const body = new Formatter().format(document)["w:document"][1]["w:body"]; + expect(body).to.be.an("array").which.has.length.at.least(1); + expect(body[0]).to.have.property("w:tbl").which.includes({ + "w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + ], + }); + expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2); + }); + }); });