Files
docx-js/demo/32-merge-table-cells.ts

116 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-12-29 01:57:20 +00:00
// Example of how you would merge cells together
2018-09-12 21:03:06 +01:00
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2019-06-25 01:21:28 +01:00
import { Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, WidthType } from "../build";
2018-09-12 21:03:06 +01:00
const doc = new Document();
2019-07-31 08:48:02 +01:00
const table = new Table({
2019-03-18 23:50:21 +00:00
rows: 2,
columns: 2,
});
2018-10-15 21:54:33 +01:00
2019-06-25 23:17:56 +01:00
table.getCell(0, 0).add(new Paragraph("Hello"));
2018-10-15 21:54:33 +01:00
table.getRow(0).mergeCells(0, 1);
2018-09-12 21:03:06 +01:00
2019-07-31 08:48:02 +01:00
const table2 = new Table({
2019-03-18 23:50:21 +00:00
rows: 2,
columns: 3,
width: 100,
widthUnitType: WidthType.AUTO,
columnWidths: [1000, 1000, 1000],
});
2019-06-25 01:21:28 +01:00
2019-07-31 08:48:02 +01:00
table2
2019-03-21 01:06:07 +00:00
.getCell(0, 0)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("World"))
2019-04-18 13:55:18 +10:00
.setMargins({
2019-03-21 01:06:07 +00:00
top: 1000,
bottom: 1000,
left: 1000,
right: 1000,
});
2018-10-15 21:54:33 +01:00
table.getRow(0).mergeCells(0, 2);
2018-09-12 21:03:06 +01:00
2019-07-31 08:48:02 +01:00
const table3 = new Table({
2019-03-18 23:50:21 +00:00
rows: 2,
columns: 4,
width: 7000,
widthUnitType: WidthType.DXA,
2019-04-18 13:55:18 +10:00
margins: {
2019-03-18 23:50:21 +00:00
top: 400,
bottom: 400,
right: 400,
left: 400,
},
});
2019-06-25 01:21:28 +01:00
2019-07-31 08:48:02 +01:00
table3.getCell(0, 0).add(new Paragraph("Foo"));
table3.getCell(0, 1).add(new Paragraph("v"));
2019-06-25 01:21:28 +01:00
2019-07-31 08:48:02 +01:00
table3
2019-03-21 01:06:07 +00:00
.getCell(1, 0)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("Bar1"))
2019-03-21 01:06:07 +00:00
.setShading({
fill: "b79c2f",
val: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "auto",
});
2019-07-31 08:48:02 +01:00
table3
2019-03-21 01:06:07 +00:00
.getCell(1, 1)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("Bar2"))
2019-03-21 01:06:07 +00:00
.setShading({
fill: "42c5f4",
val: ShadingType.PERCENT_95,
color: "auto",
});
2019-07-31 08:48:02 +01:00
table3
2019-03-21 01:06:07 +00:00
.getCell(1, 2)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("Bar3"))
2019-03-21 01:06:07 +00:00
.setShading({
fill: "880aa8",
val: ShadingType.PERCENT_10,
color: "e2df0b",
});
2019-07-31 08:48:02 +01:00
table3
2019-03-21 01:06:07 +00:00
.getCell(1, 3)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("Bar4"))
2019-03-21 01:06:07 +00:00
.setShading({
fill: "FF0000",
val: ShadingType.CLEAR,
color: "auto",
});
2018-09-13 01:54:37 +01:00
2019-07-31 08:48:02 +01:00
table3.getRow(0).mergeCells(0, 3);
2019-03-18 23:50:21 +00:00
2019-07-31 08:48:02 +01:00
const table4 = new Table({
2019-03-18 23:50:21 +00:00
rows: 2,
columns: 2,
width: 100,
widthUnitType: WidthType.PERCENTAGE,
});
2019-07-31 08:48:02 +01:00
doc.addSection({
children: [
table,
new Paragraph({
text: "Another table",
heading: HeadingLevel.HEADING_2,
}),
table2,
new Paragraph({
text: "Another table",
heading: HeadingLevel.HEADING_2,
}),
table3,
new Paragraph("hi"),
table4,
],
});
2019-06-25 01:21:28 +01:00
2018-09-12 21:03:06 +01:00
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});