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

410 lines
11 KiB
TypeScript
Raw Normal View History

2019-09-25 00:57:24 +01:00
// Example of how you would merge cells together (Rows and Columns) and apply shading
2019-11-24 01:22:17 +00:00
// Also includes an example on how to center tables
2023-06-05 00:33:43 +01:00
2018-09-12 21:03:06 +01:00
import * as fs from "fs";
2020-12-24 03:37:43 +00:00
import {
AlignmentType,
BorderStyle,
convertInchesToTwip,
Document,
HeadingLevel,
Packer,
Paragraph,
ShadingType,
Table,
TableCell,
TableRow,
WidthType,
2023-06-05 00:33:43 +01:00
} from "docx";
2018-09-12 21:03:06 +01:00
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-11-24 01:22:17 +00:00
alignment: AlignmentType.CENTER,
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("World")],
margins: {
2020-12-24 03:37:43 +00:00
top: convertInchesToTwip(0.69),
bottom: convertInchesToTwip(0.69),
left: convertInchesToTwip(0.69),
right: convertInchesToTwip(0.69),
2019-09-13 00:51:20 +01:00
},
columnSpan: 3,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
2019-09-29 04:38:07 +01:00
width: {
size: 100,
type: WidthType.AUTO,
},
2020-12-24 03:37:43 +00:00
columnWidths: [convertInchesToTwip(0.69), convertInchesToTwip(0.69), convertInchesToTwip(0.69)],
2019-03-18 23:50:21 +00:00
});
2019-06-25 01:21:28 +01:00
2019-07-31 08:48:02 +01:00
const table3 = new Table({
2019-11-24 01:22:17 +00:00
alignment: AlignmentType.CENTER,
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",
2021-05-29 05:51:06 +03:00
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
2019-09-13 00:51:20 +01:00
color: "auto",
},
}),
new TableCell({
children: [new Paragraph("Bar2")],
shading: {
fill: "42c5f4",
2021-05-29 05:51:06 +03:00
type: ShadingType.PERCENT_95,
2019-09-13 00:51:20 +01:00
color: "auto",
},
}),
new TableCell({
children: [new Paragraph("Bar3")],
shading: {
fill: "880aa8",
2021-05-29 05:51:06 +03:00
type: ShadingType.PERCENT_10,
2019-09-13 00:51:20 +01:00
color: "e2df0b",
},
}),
new TableCell({
children: [new Paragraph("Bar4")],
shading: {
fill: "FF0000",
2021-05-29 05:51:06 +03:00
type: ShadingType.CLEAR,
2019-09-13 00:51:20 +01:00
color: "auto",
},
}),
],
}),
],
2019-09-29 04:38:07 +01:00
width: {
2020-12-24 03:37:43 +00:00
size: convertInchesToTwip(4.86),
2019-09-29 04:38:07 +01:00
type: WidthType.DXA,
},
2019-04-18 13:55:18 +10:00
margins: {
2020-12-24 03:37:43 +00:00
top: convertInchesToTwip(0.27),
bottom: convertInchesToTwip(0.27),
right: convertInchesToTwip(0.27),
left: convertInchesToTwip(0.27),
2019-03-18 23:50:21 +00:00
},
});
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({
2019-09-25 00:57:24 +01:00
children: [new Paragraph("0,0")],
columnSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("1,0")],
}),
new TableCell({
children: [new Paragraph("1,1")],
2019-09-13 00:51:20 +01:00
}),
2019-09-25 00:57:24 +01:00
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("2,0")],
columnSpan: 2,
}),
],
}),
],
2019-09-29 04:38:07 +01:00
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
2019-09-25 00:57:24 +01:00
});
const table5 = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("0,0")],
}),
new TableCell({
children: [new Paragraph("0,1")],
rowSpan: 2,
}),
new TableCell({
children: [new Paragraph("0,2")],
}),
],
}),
new TableRow({
children: [
2019-09-13 00:51:20 +01:00
new TableCell({
children: [new Paragraph("1,0")],
2019-09-13 00:51:20 +01:00
}),
2019-09-25 00:57:24 +01:00
new TableCell({
children: [new Paragraph("1,2")],
rowSpan: 2,
}),
2019-09-13 00:51:20 +01:00
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("2,0")],
2019-09-13 00:51:20 +01:00
}),
new TableCell({
children: [new Paragraph("2,1")],
2019-09-13 00:51:20 +01:00
}),
],
}),
],
2019-09-29 04:38:07 +01:00
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
2019-03-18 23:50:21 +00:00
});
const borders = {
top: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "FF0000",
},
bottom: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "FF0000",
},
left: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "FF0000",
},
right: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "FF0000",
},
};
const table6 = new Table({
rows: [
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("0,0")],
rowSpan: 2,
}),
new TableCell({
borders,
children: [new Paragraph("0,1")],
}),
],
}),
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("1,1")],
rowSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("2,0")],
}),
],
}),
],
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
});
const table7 = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("0,0")],
}),
new TableCell({
children: [new Paragraph("0,1")],
}),
new TableCell({
children: [new Paragraph("0,2")],
rowSpan: 2,
}),
new TableCell({
children: [new Paragraph("0,3")],
rowSpan: 3,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("1,0")],
columnSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("2,0")],
columnSpan: 2,
}),
new TableCell({
children: [new Paragraph("2,2")],
rowSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("3,0")],
}),
new TableCell({
children: [new Paragraph("3,1")],
}),
new TableCell({
children: [new Paragraph("3,3")],
}),
],
}),
],
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
});
const table8 = new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph("1,1")] }),
new TableCell({ children: [new Paragraph("1,2")] }),
new TableCell({ children: [new Paragraph("1,3")] }),
new TableCell({ children: [new Paragraph("1,4")], rowSpan: 4, borders }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("2,1")] }),
new TableCell({ children: [new Paragraph("2,2")] }),
new TableCell({ children: [new Paragraph("2,3")], rowSpan: 3 }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("3,1")] }),
new TableCell({ children: [new Paragraph("3,2")], rowSpan: 2 }),
],
}),
new TableRow({
2020-12-24 03:37:43 +00:00
children: [new TableCell({ children: [new Paragraph("4,1")] })],
}),
],
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
});
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [
{
children: [
table,
new Paragraph({
text: "Another table",
heading: HeadingLevel.HEADING_2,
}),
table2,
new Paragraph({
text: "Another table",
heading: HeadingLevel.HEADING_2,
}),
table3,
new Paragraph("Merging columns 1"),
table4,
new Paragraph("Merging columns 2"),
table5,
new Paragraph("Merging columns 3"),
table6,
new Paragraph("Merging columns 4"),
table7,
new Paragraph("Merging columns 5"),
table8,
],
},
2019-07-31 08:48:02 +01:00
],
});
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);
});