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

299 lines
7.5 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
2018-09-12 21:03:06 +01:00
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { AlignmentType, BorderStyle, 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-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: {
top: 1000,
bottom: 1000,
left: 1000,
right: 1000,
},
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,
},
2019-03-18 23:50:21 +00:00
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-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",
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-09-29 04:38:07 +01:00
width: {
size: 7000,
type: 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({
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: [],
}),
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 TableCell({
children: [],
}),
],
}),
],
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: "red",
},
bottom: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "red",
},
left: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "red",
},
right: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "red",
},
};
const table6 = new Table({
rows: [
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("11"), new Paragraph("21")],
rowSpan: 2,
}),
new TableCell({
borders,
children: [new Paragraph("12")],
}),
],
}),
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("22"), new Paragraph("32")],
rowSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
borders,
children: [new Paragraph("31")],
}),
],
}),
],
width: {
size: 100,
type: 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,
2019-09-25 00:57:24 +01:00
new Paragraph("Merging columns"),
2019-07-31 08:48:02 +01:00
table4,
2019-09-25 00:57:24 +01:00
new Paragraph("More Merging columns"),
table5,
new Paragraph("Another Merging columns"),
table6,
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);
});