From 416a239708a365fd6714d0ef9b66c815c1c67e3d Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 26 Feb 2019 21:38:54 +0000 Subject: [PATCH 1/3] Add higher width for grid col to fix merging --- demo/demo41.ts | 48 +++++++++++++++++++++++++++++++++++++++++ src/file/table/table.ts | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 demo/demo41.ts diff --git a/demo/demo41.ts b/demo/demo41.ts new file mode 100644 index 0000000000..fa573b674b --- /dev/null +++ b/demo/demo41.ts @@ -0,0 +1,48 @@ +// Multiple cells merging in the same table +// 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(13, 6); +let row = 0; +table.getCell(row, 0).addContent(new Paragraph("0,0")); +table.getCell(row, 1).addContent(new Paragraph("0,1")); +table.getCell(row, 3).addContent(new Paragraph("0,3")); +table.getCell(row, 4).addContent(new Paragraph("0,4")); +table.getRow(row).mergeCells(4, 5); +table.getRow(row).mergeCells(1, 2); +row = 1; +table.getCell(row, 0).addContent(new Paragraph("1,0")); +table.getCell(row, 2).addContent(new Paragraph("1,2")); +table.getCell(row, 4).addContent(new Paragraph("1,4")); +table.getRow(row).mergeCells(4, 5); +table.getRow(row).mergeCells(2, 3); +table.getRow(row).mergeCells(0, 1); + +row = 2; +table.getCell(row, 0).addContent(new Paragraph("2,0")); +table.getCell(row, 1).addContent(new Paragraph("2,1")); +table.getCell(row, 2).addContent(new Paragraph("2,2")); +table.getCell(row, 3).addContent(new Paragraph("2,3")); +table.getCell(row, 4).addContent(new Paragraph("2,4")); +table.getRow(row).mergeCells(4, 5); +table.getRow(row).mergeCells(1, 2); +row = 3; +table.getCell(row, 0).addContent(new Paragraph("3,0")); +table.getCell(row, 1).addContent(new Paragraph("3,1")); +table.getCell(row, 2).addContent(new Paragraph("3,2")); +table.getCell(row, 3).addContent(new Paragraph("3,3")); +table.getCell(row, 4).addContent(new Paragraph("3,4")); +table.getCell(row, 5).addContent(new Paragraph("3,5")); +row = 4; +table.getCell(row, 0).addContent(new Paragraph("4,0")); +table.getCell(row, 5).addContent(new Paragraph("4,5")); +table.getRow(row).mergeCells(0, 4); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 8311b5ecfd..8af223bc51 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -32,7 +32,7 @@ export class Table extends XmlComponent { table will make it look reasonable, as the layout algorithm will expand columns to fit its content */ - gridCols.push(1); + gridCols.push(100); } this.grid = new TableGrid(gridCols); } From 218a08d793386b843c317750fc9e078f6194b96f Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 26 Feb 2019 21:45:55 +0000 Subject: [PATCH 2/3] Fix tests --- src/file/document/document.spec.ts | 6 +++--- src/file/table/table.spec.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/file/document/document.spec.ts b/src/file/document/document.spec.ts index 16c3b29f95..fb8831c381 100644 --- a/src/file/document/document.spec.ts +++ b/src/file/document/document.spec.ts @@ -78,9 +78,9 @@ describe("Document", () => { .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 } }] }, + { "w:gridCol": [{ _attr: { "w:w": 100 } }] }, + { "w:gridCol": [{ _attr: { "w:w": 100 } }] }, + { "w:gridCol": [{ _attr: { "w:w": 100 } }] }, ], }); expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2); diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index f1ddfe0ed6..6dbdaf023b 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -95,7 +95,7 @@ describe("Table", () => { "w:tbl": [ { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { - "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }], + "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, { "w:tr": [{ "w:trPr": [] }, cell, cell] }, { "w:tr": [{ "w:trPr": [] }, cell, cell] }, @@ -137,7 +137,7 @@ describe("Table", () => { "w:tbl": [ { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { - "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }], + "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, { "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] }, { "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] }, @@ -166,7 +166,7 @@ describe("Table", () => { "w:tbl": [ { "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { - "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }], + "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], }, { "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] }, { "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] }, @@ -184,7 +184,7 @@ describe("Table", () => { .which.is.an("array") .with.has.length.at.least(1); expect(tree["w:tbl"][0]).to.deep.equal({ - "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 1000 } }] }], + "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 10000000 } }] }], }); }); }); From 3a9fa49fbb57068c0ce451440b80537a42e2a276 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 26 Feb 2019 21:46:19 +0000 Subject: [PATCH 3/3] Fix test --- src/file/table/table.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index 6dbdaf023b..f5f815d65a 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -184,7 +184,7 @@ describe("Table", () => { .which.is.an("array") .with.has.length.at.least(1); expect(tree["w:tbl"][0]).to.deep.equal({ - "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 10000000 } }] }], + "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 1000 } }] }], }); }); });