deno.land / x / docx@8.5.0 / demo / 32-merge-and-shade-table-cells.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410// Example of how you would merge cells together (Rows and Columns) and apply shading// Also includes an example on how to center tables
import * as fs from "fs";import { AlignmentType, BorderStyle, convertInchesToTwip, Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, TableCell, TableRow, WidthType,} from "docx";
const table = new Table({ rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph("Hello")], columnSpan: 2, }), ], }), new TableRow({ children: [ new TableCell({ children: [], }), new TableCell({ children: [], }), ], }), ],});
const table2 = new Table({ alignment: AlignmentType.CENTER, rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph("World")], margins: { top: convertInchesToTwip(0.69), bottom: convertInchesToTwip(0.69), left: convertInchesToTwip(0.69), right: convertInchesToTwip(0.69), }, columnSpan: 3, }), ], }), new TableRow({ children: [ new TableCell({ children: [], }), new TableCell({ children: [], }), new TableCell({ children: [], }), ], }), ], width: { size: 100, type: WidthType.AUTO, }, columnWidths: [convertInchesToTwip(0.69), convertInchesToTwip(0.69), convertInchesToTwip(0.69)],});
const table3 = new Table({ alignment: AlignmentType.CENTER, 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", type: ShadingType.REVERSE_DIAGONAL_STRIPE, color: "auto", }, }), new TableCell({ children: [new Paragraph("Bar2")], shading: { fill: "42c5f4", type: ShadingType.PERCENT_95, color: "auto", }, }), new TableCell({ children: [new Paragraph("Bar3")], shading: { fill: "880aa8", type: ShadingType.PERCENT_10, color: "e2df0b", }, }), new TableCell({ children: [new Paragraph("Bar4")], shading: { fill: "FF0000", type: ShadingType.CLEAR, color: "auto", }, }), ], }), ], width: { size: convertInchesToTwip(4.86), type: WidthType.DXA, }, margins: { top: convertInchesToTwip(0.27), bottom: convertInchesToTwip(0.27), right: convertInchesToTwip(0.27), left: convertInchesToTwip(0.27), },});
const table4 = new Table({ rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph("0,0")], columnSpan: 2, }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("1,0")], }), new TableCell({ children: [new Paragraph("1,1")], }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("2,0")], columnSpan: 2, }), ], }), ], width: { size: 100, type: WidthType.PERCENTAGE, },});
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: [ new TableCell({ children: [new Paragraph("1,0")], }), new TableCell({ children: [new Paragraph("1,2")], rowSpan: 2, }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("2,0")], }), new TableCell({ children: [new Paragraph("2,1")], }), ], }), ], width: { size: 100, type: WidthType.PERCENTAGE, },});
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({ children: [new TableCell({ children: [new Paragraph("4,1")] })], }), ], width: { size: 100, type: WidthType.PERCENTAGE, },});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, ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info