* fix: separate vertical alignment enums for table and section properties - Introduced `VerticalAlignTable` for table-cell vertical alignment with valid values: `top`, `center`, and `bottom`. - Added `VerticalAlignSection` for section properties, extending `VerticalAlignTable` with an additional value `both`. - Marked the original `VerticalAlign` as deprecated for backward compatibility, directing users to the new enums. - Updated type definitions for better clarity on valid vertical alignments. * refactor: update vertical alignment imports and types for section and table properties - Renamed `VerticalAlign` to `VerticalAlignSection` in section properties and `VerticalAlignTable` in table-cell properties for clarity. - Updated type definitions to reflect the new enum names, ensuring better organization and understanding of vertical alignment options. - Adjusted related test cases to utilize the new imports and types. * refactor: update demos to use new enums for table and section properties for vertical alignment
194 lines
6.1 KiB
TypeScript
194 lines
6.1 KiB
TypeScript
// Example of how you would create a table and add data to it from a data source
|
|
|
|
import * as fs from "fs";
|
|
import {
|
|
Document,
|
|
HeadingLevel,
|
|
Packer,
|
|
Paragraph,
|
|
Table,
|
|
TableCell,
|
|
TableRow,
|
|
VerticalAlignTable,
|
|
TextDirection,
|
|
TextRun,
|
|
WidthType,
|
|
} from "docx";
|
|
|
|
interface StockPrice {
|
|
readonly date: Date;
|
|
readonly ticker: string;
|
|
readonly price: number;
|
|
}
|
|
|
|
const DATA: StockPrice[] = [
|
|
{
|
|
date: new Date("2007-08-28"),
|
|
ticker: "Apple",
|
|
price: 18.12,
|
|
},
|
|
{
|
|
date: new Date("2007-08-29"),
|
|
ticker: "Apple",
|
|
price: 19.15,
|
|
},
|
|
{
|
|
date: new Date("2007-08-30"),
|
|
ticker: "Apple",
|
|
price: 19.46,
|
|
},
|
|
{
|
|
date: new Date("2007-08-31"),
|
|
ticker: "Apple",
|
|
price: 19.78,
|
|
},
|
|
{
|
|
date: new Date("2007-09-04"),
|
|
ticker: "Apple",
|
|
price: 20.59,
|
|
},
|
|
{
|
|
date: new Date("2007-09-05"),
|
|
ticker: "Apple",
|
|
price: 19.54,
|
|
},
|
|
{
|
|
date: new Date("2007-09-06"),
|
|
ticker: "Apple",
|
|
price: 19.29,
|
|
},
|
|
{
|
|
date: new Date("2007-09-07"),
|
|
ticker: "Apple",
|
|
price: 18.82,
|
|
},
|
|
{
|
|
date: new Date("2007-09-10"),
|
|
ticker: "Apple",
|
|
price: 19.53,
|
|
},
|
|
{
|
|
date: new Date("2007-09-11"),
|
|
ticker: "Apple",
|
|
price: 19.36,
|
|
},
|
|
{
|
|
date: new Date("2007-09-12"),
|
|
ticker: "Apple",
|
|
price: 19.55,
|
|
},
|
|
{
|
|
date: new Date("2007-09-13"),
|
|
ticker: "Apple",
|
|
price: 19.6,
|
|
},
|
|
{
|
|
date: new Date("2007-09-14"),
|
|
ticker: "Apple",
|
|
price: 19.83,
|
|
},
|
|
{
|
|
date: new Date("2007-09-17"),
|
|
ticker: "Apple",
|
|
price: 19.77,
|
|
},
|
|
];
|
|
|
|
const generateRows = (prices: StockPrice[]): TableRow[] =>
|
|
prices.map(
|
|
({ date, ticker, price }) =>
|
|
new TableRow({
|
|
children: [
|
|
new TableCell({
|
|
children: [new Paragraph(date.toString())],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
|
|
}),
|
|
new TableCell({
|
|
children: [new Paragraph(ticker)],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
|
|
}),
|
|
new TableCell({
|
|
children: [new Paragraph(price.toString())],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
children: [
|
|
new Table({
|
|
width: {
|
|
size: 9070,
|
|
type: WidthType.DXA,
|
|
},
|
|
rows: [
|
|
new TableRow({
|
|
children: [
|
|
new TableCell({
|
|
children: [
|
|
new Paragraph({
|
|
heading: HeadingLevel.HEADING_2,
|
|
children: [
|
|
new TextRun({
|
|
text: "Date",
|
|
bold: true,
|
|
size: 40,
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
|
|
}),
|
|
new TableCell({
|
|
children: [
|
|
new Paragraph({
|
|
heading: HeadingLevel.HEADING_2,
|
|
children: [
|
|
new TextRun({
|
|
text: "Ticker",
|
|
bold: true,
|
|
size: 40,
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
|
|
}),
|
|
new TableCell({
|
|
children: [
|
|
new Paragraph({
|
|
heading: HeadingLevel.HEADING_2,
|
|
children: [
|
|
new TextRun({
|
|
text: "Price",
|
|
bold: true,
|
|
size: 40,
|
|
}),
|
|
],
|
|
}),
|
|
],
|
|
verticalAlign: VerticalAlignTable.CENTER,
|
|
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
|
|
}),
|
|
],
|
|
}),
|
|
...generateRows(DATA),
|
|
],
|
|
}),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
});
|