diff --git a/src/file/table/table-width.ts b/src/file/table/table-width.ts index ff203506eb..554654a583 100644 --- a/src/file/table/table-width.ts +++ b/src/file/table/table-width.ts @@ -38,6 +38,10 @@ export class TableWidthElement extends XmlComponent { constructor(name: string, { type = WidthType.AUTO, size }: ITableWidthProperties) { super(name); // super("w:tblW"); - this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(size) })); + let tableWidthValue = size; + if (type === WidthType.PERCENTAGE && typeof size === "number") { + tableWidthValue = `${size}%`; + } + this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(tableWidthValue) })); } } diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index d3e703decd..78a273e4e1 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -257,7 +257,7 @@ describe("Table", () => { }); }); - it("should set the table to provided width", () => { + it("should set the table to provided 100% width", () => { const table = new Table({ rows: [ new TableRow({ @@ -282,7 +282,42 @@ describe("Table", () => { "w:tblW": { _attr: { "w:type": "pct", - "w:w": 100, + "w:w": "100%", + }, + }, + }, + BORDERS, + { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, + ], + }); + }); + + it("should set the table to provided 1000 DXA", () => { + const table = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("hello")], + }), + ], + }), + ], + width: { + size: 1000, + type: WidthType.DXA, + }, + layout: TableLayoutType.FIXED, + }); + 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": [ + { + "w:tblW": { + _attr: { + "w:type": "dxa", + "w:w": 1000, }, }, },