2018-08-21 02:46:21 +01:00
|
|
|
// Example of how you would create a table and add data to it
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2021-03-05 03:03:02 +00:00
|
|
|
import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build";
|
2018-08-21 02:46:21 +01:00
|
|
|
|
|
|
|
const doc = new Document();
|
|
|
|
|
2019-06-25 01:21:28 +01:00
|
|
|
const table = new Table({
|
2021-03-05 03:03:02 +00:00
|
|
|
columnWidths: [3505, 5505],
|
|
|
|
rows: [
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 3505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [new Paragraph("Hello")],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 5505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 3505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 5505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [new Paragraph("World")],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const table2 = new Table({
|
|
|
|
columnWidths: [4505, 4505],
|
|
|
|
rows: [
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 4505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [new Paragraph("Hello")],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 4505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 4505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
width: {
|
|
|
|
size: 4505,
|
|
|
|
type: WidthType.DXA,
|
|
|
|
},
|
|
|
|
children: [new Paragraph("World")],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const table3 = new Table({
|
2019-09-13 00:51:20 +01:00
|
|
|
rows: [
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
children: [new Paragraph("Hello")],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new TableRow({
|
|
|
|
children: [
|
|
|
|
new TableCell({
|
|
|
|
children: [],
|
|
|
|
}),
|
|
|
|
new TableCell({
|
|
|
|
children: [new Paragraph("World")],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
2019-03-18 23:50:21 +00:00
|
|
|
});
|
2019-06-25 01:21:28 +01:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
doc.addSection({
|
2021-03-05 03:03:02 +00:00
|
|
|
children: [
|
|
|
|
new Paragraph({ text: "Table with skewed widths" }),
|
|
|
|
table,
|
|
|
|
new Paragraph({ text: "Table with equal widths" }),
|
|
|
|
table2,
|
|
|
|
new Paragraph({ text: "Table without setting widths" }),
|
|
|
|
table3,
|
|
|
|
],
|
2019-07-31 08:48:02 +01:00
|
|
|
});
|
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-08-21 02:46:21 +01:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|