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-13 01:07:00 +01:00
|
|
|
import { Document, HeadingLevel, Packer, Paragraph, ShadingType, WidthType } from "../build";
|
2018-09-12 21:03:06 +01:00
|
|
|
|
|
|
|
const doc = new Document();
|
|
|
|
|
2019-03-18 23:50:21 +00:00
|
|
|
let table = doc.createTable({
|
|
|
|
rows: 2,
|
|
|
|
columns: 2,
|
|
|
|
});
|
2018-10-15 21:54:33 +01:00
|
|
|
|
2018-12-29 01:57:20 +00:00
|
|
|
table.getCell(0, 0).addParagraph(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-06-13 01:07:00 +01:00
|
|
|
doc.addParagraph(
|
|
|
|
new Paragraph({
|
|
|
|
text: "Another table",
|
|
|
|
heading: HeadingLevel.HEADING_2,
|
|
|
|
}),
|
|
|
|
);
|
2018-09-12 21:03:06 +01:00
|
|
|
|
2019-03-18 23:50:21 +00:00
|
|
|
table = doc.createTable({
|
|
|
|
rows: 2,
|
|
|
|
columns: 3,
|
|
|
|
width: 100,
|
|
|
|
widthUnitType: WidthType.AUTO,
|
|
|
|
columnWidths: [1000, 1000, 1000],
|
|
|
|
});
|
2019-03-21 01:06:07 +00:00
|
|
|
table
|
|
|
|
.getCell(0, 0)
|
|
|
|
.addParagraph(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-06-13 01:07:00 +01:00
|
|
|
doc.addParagraph(
|
|
|
|
new Paragraph({
|
|
|
|
text: "Another table",
|
|
|
|
heading: HeadingLevel.HEADING_2,
|
|
|
|
}),
|
|
|
|
);
|
2018-09-13 01:54:37 +01:00
|
|
|
|
2019-03-18 23:50:21 +00:00
|
|
|
table = doc.createTable({
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
2018-12-29 01:57:20 +00:00
|
|
|
table.getCell(0, 0).addParagraph(new Paragraph("Foo"));
|
2019-03-18 23:50:21 +00:00
|
|
|
table.getCell(0, 1).addParagraph(new Paragraph("v"));
|
2018-09-13 01:54:37 +01:00
|
|
|
|
2019-03-21 01:06:07 +00:00
|
|
|
table
|
|
|
|
.getCell(1, 0)
|
|
|
|
.addParagraph(new Paragraph("Bar1"))
|
|
|
|
.setShading({
|
|
|
|
fill: "b79c2f",
|
|
|
|
val: ShadingType.REVERSE_DIAGONAL_STRIPE,
|
|
|
|
color: "auto",
|
|
|
|
});
|
|
|
|
table
|
|
|
|
.getCell(1, 1)
|
|
|
|
.addParagraph(new Paragraph("Bar2"))
|
|
|
|
.setShading({
|
|
|
|
fill: "42c5f4",
|
|
|
|
val: ShadingType.PERCENT_95,
|
|
|
|
color: "auto",
|
|
|
|
});
|
|
|
|
table
|
|
|
|
.getCell(1, 2)
|
|
|
|
.addParagraph(new Paragraph("Bar3"))
|
|
|
|
.setShading({
|
|
|
|
fill: "880aa8",
|
|
|
|
val: ShadingType.PERCENT_10,
|
|
|
|
color: "e2df0b",
|
|
|
|
});
|
|
|
|
table
|
|
|
|
.getCell(1, 3)
|
|
|
|
.addParagraph(new Paragraph("Bar4"))
|
|
|
|
.setShading({
|
|
|
|
fill: "FF0000",
|
|
|
|
val: ShadingType.CLEAR,
|
|
|
|
color: "auto",
|
|
|
|
});
|
2018-09-13 01:54:37 +01:00
|
|
|
|
2019-03-21 01:06:07 +00:00
|
|
|
table.getRow(0).mergeCells(0, 3);
|
2018-10-15 21:54:33 +01:00
|
|
|
|
2019-06-13 01:07:00 +01:00
|
|
|
doc.addParagraph(new Paragraph("hi"));
|
2019-03-18 23:50:21 +00:00
|
|
|
|
|
|
|
doc.createTable({
|
|
|
|
rows: 2,
|
|
|
|
columns: 2,
|
|
|
|
width: 100,
|
|
|
|
widthUnitType: WidthType.PERCENTAGE,
|
|
|
|
});
|
|
|
|
|
2018-09-12 21:03:06 +01:00
|
|
|
const packer = new Packer();
|
|
|
|
|
|
|
|
packer.toBuffer(doc).then((buffer) => {
|
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|