Merge pull request #729 from dolanmiu/feat/strong-style

Adding TableBorders.NONE convenience object
This commit is contained in:
Dolan
2020-12-23 00:28:34 +00:00
committed by GitHub
3 changed files with 238 additions and 3 deletions

View File

@ -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 from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; 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(); const doc = new Document();
@ -10,6 +22,28 @@ const table = new Table({
new TableRow({ new TableRow({
children: [ children: [
new TableCell({ 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")], children: [new Paragraph("Hello")],
}), }),
new TableCell({ 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) => { Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);

View File

@ -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",
},
},
},
],
});
});
});
}); });
}); });

View File

@ -36,6 +36,39 @@ export interface ITableBordersOptions {
} }
export class TableBorders extends XmlComponent { 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) { constructor(options: ITableBordersOptions) {
super("w:tblBorders"); super("w:tblBorders");