Adding TableBorders.NONE convenience object
This commit is contained in:
@ -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);
|
||||
|
@ -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",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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");
|
||||
|
||||
|
Reference in New Issue
Block a user