diff --git a/demo/49-table-borders.ts b/demo/49-table-borders.ts index 2aebf1feb1..75dd89c49f 100644 --- a/demo/49-table-borders.ts +++ b/demo/49-table-borders.ts @@ -1,7 +1,19 @@ -// Add custom borders to the table itself +// Add custom borders and no-borders to the table itself // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; +import { + BorderStyle, + Document, + HeadingLevel, + Packer, + Paragraph, + Table, + TableBorders, + TableCell, + TableRow, + TextDirection, + VerticalAlign, +} from "../build"; const doc = new Document(); @@ -10,6 +22,28 @@ const table = new Table({ new TableRow({ children: [ new TableCell({ + 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", + }, + }, children: [new Paragraph("Hello")], }), new TableCell({ @@ -30,7 +64,103 @@ const table = new Table({ ], }); -doc.addSection({ children: [table] }); +// Using the no-border convenience object. It is the same as writing this manually: +// const borders = { +// top: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// bottom: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// left: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// right: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// insideHorizontal: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// insideVertical: { +// style: BorderStyle.NONE, +// size: 0, +// color: "auto", +// }, +// }; +const noBorderTable = new Table({ + borders: TableBorders.NONE, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph({}), new Paragraph({})], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [new Paragraph({}), new Paragraph({})], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})], + textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT, + }), + new TableCell({ + children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})], + textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [ + new Paragraph({ + text: + "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", + heading: HeadingLevel.HEADING_1, + }), + ], + }), + new TableCell({ + children: [ + new Paragraph({ + text: "This text should be in the middle of the cell", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [ + new Paragraph({ + text: "Text above should be vertical from bottom to top", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [ + new Paragraph({ + text: "Text above should be vertical from top to bottom", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + ], + }), + ], +}); + +doc.addSection({ children: [table, new Paragraph("Hello"), noBorderTable] }); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); diff --git a/src/file/table/table-properties/table-borders.spec.ts b/src/file/table/table-properties/table-borders.spec.ts index 679192f120..4a3212feb1 100644 --- a/src/file/table/table-properties/table-borders.spec.ts +++ b/src/file/table/table-properties/table-borders.spec.ts @@ -546,5 +546,77 @@ describe("TableBorders", () => { }); }); }); + + describe("TableBorders.NONE convenience object", () => { + it("should add no borders", () => { + const tableBorders = new TableBorders(TableBorders.NONE); + const tree = new Formatter().format(tableBorders); + + expect(tree).to.deep.equal({ + "w:tblBorders": [ + { + "w:top": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + { + "w:left": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + { + "w:bottom": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + { + "w:right": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + { + "w:insideH": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + { + "w:insideV": { + _attr: { + "w:color": "auto", + "w:space": 0, + "w:sz": 0, + "w:val": "none", + }, + }, + }, + ], + }); + }); + }); }); }); diff --git a/src/file/table/table-properties/table-borders.ts b/src/file/table/table-properties/table-borders.ts index 7c7ac96ca9..8de354fc36 100644 --- a/src/file/table/table-properties/table-borders.ts +++ b/src/file/table/table-properties/table-borders.ts @@ -36,6 +36,39 @@ export interface ITableBordersOptions { } export class TableBorders extends XmlComponent { + public static readonly NONE = { + top: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + bottom: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + left: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + right: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + insideHorizontal: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + insideVertical: { + style: BorderStyle.NONE, + size: 0, + color: "auto", + }, + }; + constructor(options: ITableBordersOptions) { super("w:tblBorders");