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

174 lines
4.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-09-13 00:51:20 +01:00
import { Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, TableCell, TableRow, 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-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Hello")],
columnSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
});
2018-10-15 21:54:33 +01:00
2019-07-31 08:48:02 +01:00
const table2 = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("World")],
margins: {
top: 1000,
bottom: 1000,
left: 1000,
right: 1000,
},
columnSpan: 3,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
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
const table3 = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Foo")],
}),
new TableCell({
children: [new Paragraph("v")],
columnSpan: 3,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Bar1")],
shading: {
fill: "b79c2f",
val: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "auto",
},
}),
new TableCell({
children: [new Paragraph("Bar2")],
shading: {
fill: "42c5f4",
val: ShadingType.PERCENT_95,
color: "auto",
},
}),
new TableCell({
children: [new Paragraph("Bar3")],
shading: {
fill: "880aa8",
val: ShadingType.PERCENT_10,
color: "e2df0b",
},
}),
new TableCell({
children: [new Paragraph("Bar4")],
shading: {
fill: "FF0000",
val: ShadingType.CLEAR,
color: "auto",
},
}),
],
}),
],
2019-03-18 23:50:21 +00:00
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
const table4 = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
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
2019-08-07 22:12:14 +01:00
Packer.toBuffer(doc).then((buffer) => {
2018-09-12 21:03:06 +01:00
fs.writeFileSync("My Document.docx", buffer);
});