Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
c9fb9a827d | |||
3a9fa49fbb | |||
218a08d793 | |||
416a239708 |
48
demo/demo41.ts
Normal file
48
demo/demo41.ts
Normal file
@ -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);
|
||||||
|
});
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docx",
|
"name": "docx",
|
||||||
"version": "4.7.0",
|
"version": "4.7.1",
|
||||||
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -78,9 +78,9 @@ describe("Document", () => {
|
|||||||
.to.have.property("w:tbl")
|
.to.have.property("w:tbl")
|
||||||
.which.includes({
|
.which.includes({
|
||||||
"w:tblGrid": [
|
"w:tblGrid": [
|
||||||
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
|
{ "w:gridCol": [{ _attr: { "w:w": 100 } }] },
|
||||||
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
|
{ "w:gridCol": [{ _attr: { "w:w": 100 } }] },
|
||||||
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
|
{ "w:gridCol": [{ _attr: { "w:w": 100 } }] },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
|
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
|
||||||
|
@ -95,7 +95,7 @@ describe("Table", () => {
|
|||||||
"w:tbl": [
|
"w:tbl": [
|
||||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
|
{ "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] },
|
||||||
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
|
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
|
||||||
@ -137,7 +137,7 @@ describe("Table", () => {
|
|||||||
"w:tbl": [
|
"w:tbl": [
|
||||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
|
{ "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("A1"), cell("B1")] },
|
||||||
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
||||||
@ -166,7 +166,7 @@ describe("Table", () => {
|
|||||||
"w:tbl": [
|
"w:tbl": [
|
||||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
|
{ "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("A1"), cell("B1")] },
|
||||||
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
||||||
|
@ -32,7 +32,7 @@ export class Table extends XmlComponent {
|
|||||||
table will make it look reasonable, as the layout
|
table will make it look reasonable, as the layout
|
||||||
algorithm will expand columns to fit its content
|
algorithm will expand columns to fit its content
|
||||||
*/
|
*/
|
||||||
gridCols.push(1);
|
gridCols.push(100);
|
||||||
}
|
}
|
||||||
this.grid = new TableGrid(gridCols);
|
this.grid = new TableGrid(gridCols);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user