Add tests and margain

This commit is contained in:
Dolan
2019-03-18 23:50:21 +00:00
parent 639842332f
commit e67fd9cb2b
19 changed files with 611 additions and 107 deletions

View File

@ -106,7 +106,10 @@ doc.createParagraph("Sir,").style("normalPara");
doc.createParagraph("BRIEF DESCRIPTION").style("normalPara"); doc.createParagraph("BRIEF DESCRIPTION").style("normalPara");
const table = new Table(4, 4); const table = new Table({
rows: 4,
columns: 4,
});
table table
.getRow(0) .getRow(0)
.getCell(0) .getCell(0)

View File

@ -5,7 +5,10 @@ import { BorderStyle, Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
rows: 4,
columns: 4,
});
table table
.getCell(2, 2) .getCell(2, 2)
.addParagraph(new Paragraph("Hello")) .addParagraph(new Paragraph("Hello"))

View File

@ -5,7 +5,10 @@ import { Document, Media, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));

View File

@ -5,7 +5,10 @@ import { Document, Packer, Paragraph, VerticalAlign } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(2, 2); const table = doc.createTable({
rows: 2,
columns: 2,
});
table table
.getCell(1, 1) .getCell(1, 1)
.addParagraph(new Paragraph("This text should be in the middle of the cell")) .addParagraph(new Paragraph("This text should be in the middle of the cell"))

View File

@ -1,33 +1,68 @@
// Example of how you would merge cells together // Example of how you would merge cells together
// 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 { Document, Packer, Paragraph } from "../build"; import { Document, Packer, Paragraph, WidthType } from "../build";
const doc = new Document(); const doc = new Document();
let table = doc.createTable(2, 2); let table = doc.createTable({
rows: 2,
columns: 2,
});
table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 3); table = doc.createTable({
table.getCell(0, 0).addParagraph(new Paragraph("World")); rows: 2,
columns: 3,
width: 100,
widthUnitType: WidthType.AUTO,
columnWidths: [1000, 1000, 1000],
});
table.getCell(0, 0).addParagraph(new Paragraph("World")).setMargains({
top: 1000,
bottom: 1000,
left: 1000,
right: 1000,
});
table.getRow(0).mergeCells(0, 2); table.getRow(0).mergeCells(0, 2);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 4); table = doc.createTable({
rows: 2,
columns: 4,
width: 7000,
widthUnitType: WidthType.DXA,
margains: {
top: 400,
bottom: 400,
right: 400,
left: 400,
},
});
table.getCell(0, 0).addParagraph(new Paragraph("Foo")); table.getCell(0, 0).addParagraph(new Paragraph("Foo"));
table.getCell(0, 1).addParagraph(new Paragraph("v"));
// table.getCell(1, 0).addParagraph(new Paragraph("Bar1")); table.getCell(1, 0).addParagraph(new Paragraph("Bar1"));
// table.getCell(1, 1).addParagraph(new Paragraph("Bar2")); // table.getCell(1, 1).addParagraph(new Paragraph("Bar2"));
// table.getCell(1, 2).addParagraph(new Paragraph("Bar3")); // table.getCell(1, 2).addParagraph(new Paragraph("Bar3"));
// table.getCell(1, 3).addParagraph(new Paragraph("Bar4")); // table.getCell(1, 3).addParagraph(new Paragraph("Bar4"));
// table.getRow(0).mergeCells(0, 3); // table.getRow(0).mergeCells(0, 3);
doc.createParagraph("hi");
doc.createTable({
rows: 2,
columns: 2,
width: 100,
widthUnitType: WidthType.PERCENTAGE,
});
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -5,14 +5,19 @@ import { Document, Packer, Paragraph, RelativeHorizontalPosition, RelativeVertic
const doc = new Document(); const doc = new Document();
const table = doc.createTable(2, 2).float({ const table = doc.createTable({
rows: 2,
columns: 2,
float: {
horizontalAnchor: TableAnchorType.MARGIN, horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN, verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
},
width: 4535,
widthUnitType: WidthType.DXA,
}); });
table.setFixedWidthLayout(); table.setFixedWidthLayout();
table.setWidth(4535, WidthType.DXA);
table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);

View File

@ -6,7 +6,10 @@ import { Document, Media, Packer, Table } from "../build";
const doc = new Document(); const doc = new Document();
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
const table = new Table(2, 2); const table = new Table({
rows: 2,
columns: 2,
});
table.getCell(1, 1).addParagraph(image.Paragraph); table.getCell(1, 1).addParagraph(image.Paragraph);
// doc.createParagraph("Hello World"); // doc.createParagraph("Hello World");

View File

@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const packer = new Packer(); const packer = new Packer();

View File

@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(13, 6); const table = doc.createTable({
rows: 13,
columns: 6,
});
let row = 0; let row = 0;
table.getCell(row, 0).addContent(new Paragraph("0,0")); table.getCell(row, 0).addContent(new Paragraph("0,0"));
table.getCell(row, 1).addContent(new Paragraph("0,1")); table.getCell(row, 1).addContent(new Paragraph("0,1"));

View File

@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable({
rows: 4,
columns: 4,
});
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
table.getColumn(3).mergeCells(1, 2); table.getColumn(3).mergeCells(1, 2);
// table.getCell(3, 2).addParagraph(new Paragraph("Hello")); // table.getCell(3, 2).addParagraph(new Paragraph("Hello"));

View File

@ -0,0 +1,81 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain";
describe("TopCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new TopCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("BottomCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new BottomCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("LeftCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new LeftCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("RightCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new RightCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});

View File

@ -0,0 +1,63 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface ICellMargainProperties {
readonly type: string;
readonly width: number;
}
class CellMargainAttributes extends XmlAttributeComponent<ICellMargainProperties> {
protected readonly xmlKeys = { width: "w:w", type: "w:type" };
}
export class TopCellMargain extends XmlComponent {
constructor(value: number) {
super("w:top");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class BottomCellMargain extends XmlComponent {
constructor(value: number) {
super("w:bottom");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class LeftCellMargain extends XmlComponent {
constructor(value: number) {
super("w:start");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class RightCellMargain extends XmlComponent {
constructor(value: number) {
super("w:end");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}

View File

@ -0,0 +1,112 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableCellMargain } from "./table-cell-margains";
describe("TableCellMargain", () => {
describe("#constructor", () => {
it("should create with default values", () => {
const cellMargain = new TableCellMargain({});
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
],
});
});
it("should create with values", () => {
const cellMargain = new TableCellMargain({
top: 5,
bottom: 5,
left: 5,
right: 5,
});
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,21 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlComponent } from "file/xml-components";
import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain";
export interface ITableCellMargainOptions {
readonly top?: number;
readonly left?: number;
readonly bottom?: number;
readonly right?: number;
}
export class TableCellMargain extends XmlComponent {
constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMargainOptions) {
super("w:tcMar");
this.root.push(new TopCellMargain(top));
this.root.push(new BottomCellMargain(bottom));
this.root.push(new RightCellMargain(right));
this.root.push(new LeftCellMargain(left));
}
}

View File

@ -1,5 +1,6 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { ITableCellMargainOptions, TableCellMargain } from "./cell-margain/table-cell-margains";
import { import {
GridSpan, GridSpan,
ITableCellShadingAttributesProperties, ITableCellShadingAttributesProperties,
@ -55,4 +56,10 @@ export class TableCellProperties extends XmlComponent {
return this; return this;
} }
public addMargains(options: ITableCellMargainOptions): TableCellProperties {
this.root.push(new TableCellMargain(options));
return this;
}
} }

View File

@ -3,6 +3,7 @@ import { Paragraph } from "file/paragraph";
import { IXmlableObject, XmlComponent } from "file/xml-components"; import { IXmlableObject, XmlComponent } from "file/xml-components";
import { Table } from "../table"; import { Table } from "../table";
import { ITableCellMargainOptions } from "./cell-margain/table-cell-margains";
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties"; import { TableCellProperties } from "./table-cell-properties";
@ -11,6 +12,7 @@ export class TableCell extends XmlComponent {
constructor() { constructor() {
super("w:tc"); super("w:tc");
this.properties = new TableCellProperties(); this.properties = new TableCellProperties();
this.root.push(this.properties); this.root.push(this.properties);
} }
@ -64,6 +66,12 @@ export class TableCell extends XmlComponent {
return this; return this;
} }
public setMargains(margains: ITableCellMargainOptions): TableCell {
this.properties.addMargains(margains);
return this;
}
public get Borders(): TableCellBorders { public get Borders(): TableCellBorders {
return this.properties.Borders; return this.properties.Borders;
} }

View File

@ -0,0 +1,82 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableCell } from "../table-cell";
import { TableRow } from "./table-row";
describe("TableRow", () => {
describe("#constructor", () => {
it("should create with no cells", () => {
const tableRow = new TableRow([]);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
{
"w:trPr": [],
},
],
});
});
it("should create with one cell", () => {
const tableRow = new TableRow([new TableCell()]);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
{
"w:trPr": [],
},
{
"w:tc": [
{
"w:tcPr": [],
},
{
"w:p": [
{
"w:pPr": [],
},
],
},
],
},
],
});
});
});
describe("#getCell", () => {
it("should get the cell", () => {
const cell = new TableCell();
const tableRow = new TableRow([cell]);
expect(tableRow.getCell(0)).to.equal(cell);
});
it("should throw an error if index is out of bounds", () => {
const cell = new TableCell();
const tableRow = new TableRow([cell]);
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#addGridSpan", () => {
it("should merge the cell", () => {
const tableRow = new TableRow([new TableCell(), new TableCell()]);
tableRow.addGridSpan(0, 2);
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#mergeCells", () => {
it("should merge the cell", () => {
const tableRow = new TableRow([new TableCell(), new TableCell()]);
tableRow.mergeCells(0, 1);
expect(() => tableRow.getCell(1)).to.throw();
});
});
});

View File

@ -9,15 +9,23 @@ import { Table } from "./table";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties"; import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
const DEFAULT_TABLE_PROPERTIES = { const DEFAULT_TABLE_PROPERTIES = {
"w:tblBorders": [ "w:tblCellMar": [
{
"w:bottom": [
{
_attr: {
"w:sz": "auto",
"w:w": 0,
},
},
],
},
{ {
"w:top": [ "w:top": [
{ {
_attr: { _attr: {
"w:color": "auto", "w:sz": "auto",
"w:space": 0, "w:w": 0,
"w:sz": 4,
"w:val": "single",
}, },
}, },
], ],
@ -26,22 +34,8 @@ const DEFAULT_TABLE_PROPERTIES = {
"w:left": [ "w:left": [
{ {
_attr: { _attr: {
"w:color": "auto", "w:sz": "auto",
"w:space": 0, "w:w": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
}, },
}, },
], ],
@ -50,34 +44,8 @@ const DEFAULT_TABLE_PROPERTIES = {
"w:right": [ "w:right": [
{ {
_attr: { _attr: {
"w:color": "auto", "w:sz": "auto",
"w:space": 0, "w:w": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:insideH": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:insideV": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
}, },
}, },
], ],
@ -85,15 +53,88 @@ const DEFAULT_TABLE_PROPERTIES = {
], ],
}; };
const BORDERS = {
"w:tblBorders": [
{ "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
{ "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
{ "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
{ "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
{ "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
{ "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
],
};
const WIDTHS = {
"w:tblW": [
{
_attr: {
"w:type": "auto",
"w:w": 100,
},
},
],
};
// const f = {
// "w:tbl": [
// {
// "w:tblPr": [
// {
// "w:tblCellMar": [
// { "w:bottom": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] },
// { "w:top": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] },
// { "w:left": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] },
// { "w:right": [{ _attr: { "w:sz": "auto", "w:w": 0 } }] },
// ],
// },
// {
// "w:tblBorders": [
// { "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// { "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// { "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// { "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// { "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// { "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
// ],
// },
// { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 100 } }] },
// {
// "w:tblpPr": [
// {
// _attr: {
// "w:horzAnchor": "margin",
// "w:vertAnchor": "page",
// "w:tblpX": 10,
// "w:tblpXSpec": "center",
// "w:tblpY": 20,
// "w:tblpYSpec": "bottom",
// "w:bottomFromText": 30,
// "w:topFromText": 40,
// "w:leftFromText": 50,
// "w:rightFromText": 60,
// },
// },
// ],
// },
// ],
// },
// { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }] },
// { "w:tr": [{ "w:trPr": [] }, { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] }] },
// ],
// };
describe("Table", () => { describe("Table", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("creates a table with the correct number of rows and columns", () => { it("creates a table with the correct number of rows and columns", () => {
const table = new Table(3, 2); const table = new Table({
rows: 3,
columns: 2,
});
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
const cell = { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] }; const cell = { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] };
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:tbl": [ "w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
{ {
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
}, },
@ -106,7 +147,10 @@ describe("Table", () => {
}); });
describe("#getRow and Row#getCell", () => { describe("#getRow and Row#getCell", () => {
const table = new Table(2, 2); const table = new Table({
rows: 2,
columns: 2,
});
it("should return the correct row", () => { it("should return the correct row", () => {
table table
@ -136,7 +180,7 @@ describe("Table", () => {
}); });
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:tbl": [ "w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
{ {
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
}, },
@ -152,9 +196,12 @@ describe("Table", () => {
}); });
describe("#getColumn", () => { describe("#getColumn", () => {
const table = new Table(2, 2); const table = new Table({
rows: 2,
columns: 2,
});
it("should get correct row", () => { it("should get correct cell", () => {
const column = table.getColumn(0); const column = table.getColumn(0);
expect(column.getCell(0)).to.equal(table.getCell(0, 0)); expect(column.getCell(0)).to.equal(table.getCell(0, 0));
@ -164,7 +211,10 @@ describe("Table", () => {
describe("#getCell", () => { describe("#getCell", () => {
it("should returns the correct cell", () => { it("should returns the correct cell", () => {
const table = new Table(2, 2); const table = new Table({
rows: 2,
columns: 2,
});
table.getCell(0, 0).addParagraph(new Paragraph("A1")); table.getCell(0, 0).addParagraph(new Paragraph("A1"));
table.getCell(0, 1).addParagraph(new Paragraph("B1")); table.getCell(0, 1).addParagraph(new Paragraph("B1"));
table.getCell(1, 0).addParagraph(new Paragraph("A2")); table.getCell(1, 0).addParagraph(new Paragraph("A2"));
@ -180,7 +230,7 @@ describe("Table", () => {
}); });
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:tbl": [ "w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] }, { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
{ {
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }], "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
}, },
@ -193,7 +243,7 @@ describe("Table", () => {
// describe("#setWidth", () => { // describe("#setWidth", () => {
// it("should set the preferred width on the table", () => { // it("should set the preferred width on the table", () => {
// const table = new Table(2, 2).setWidth(1000, WidthType.PERCENTAGE); // const table = new Table({rows: 1,columns: 1,}).setWidth(1000, WidthType.PERCENTAGE);
// const tree = new Formatter().format(table); // const tree = new Formatter().format(table);
// expect(tree) // expect(tree)
// .to.have.property("w:tbl") // .to.have.property("w:tbl")
@ -205,7 +255,7 @@ describe("Table", () => {
// }); // });
// it("sets the preferred width on the table with a default of AUTO", () => { // it("sets the preferred width on the table with a default of AUTO", () => {
// const table = new Table(2, 2).setWidth(1000); // const table = new Table({rows: 1,columns: 1,}).setWidth(1000);
// const tree = new Formatter().format(table); // const tree = new Formatter().format(table);
// expect(tree["w:tbl"][0]).to.deep.equal({ // expect(tree["w:tbl"][0]).to.deep.equal({
@ -216,14 +266,17 @@ describe("Table", () => {
describe("#setFixedWidthLayout", () => { describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => { it("sets the table to fixed width layout", () => {
const table = new Table(2, 2).setFixedWidthLayout(); const table = new Table({
rows: 1,
columns: 1,
}).setFixedWidthLayout();
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
expect(tree) expect(tree)
.to.have.property("w:tbl") .to.have.property("w:tbl")
.which.is.an("array") .which.is.an("array")
.with.has.length.at.least(1); .with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({ expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }], "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }],
}); });
}); });
}); });
@ -231,7 +284,10 @@ describe("Table", () => {
describe("Cell", () => { describe("Cell", () => {
describe("#prepForXml", () => { describe("#prepForXml", () => {
it("inserts a paragraph at the end of the cell if it is empty", () => { it("inserts a paragraph at the end of the cell if it is empty", () => {
const table = new Table(1, 1); const table = new Table({
rows: 1,
columns: 1,
});
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
expect(tree) expect(tree)
.to.have.property("w:tbl") .to.have.property("w:tbl")
@ -247,8 +303,16 @@ describe("Table", () => {
}); });
it("inserts a paragraph at the end of the cell even if it has a child table", () => { it("inserts a paragraph at the end of the cell even if it has a child table", () => {
const parentTable = new Table(1, 1); const parentTable = new Table({
parentTable.getCell(0, 0).addTable(new Table(1, 1)); rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addTable(
new Table({
rows: 1,
columns: 1,
}),
);
const tree = new Formatter().format(parentTable); const tree = new Formatter().format(parentTable);
expect(tree) expect(tree)
.to.have.property("w:tbl") .to.have.property("w:tbl")
@ -266,7 +330,10 @@ describe("Table", () => {
}); });
it("does not insert a paragraph if it already ends with one", () => { it("does not insert a paragraph if it already ends with one", () => {
const parentTable = new Table(1, 1); const parentTable = new Table({
rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addParagraph(new Paragraph("Hello")); parentTable.getCell(0, 0).addParagraph(new Paragraph("Hello"));
const tree = new Formatter().format(parentTable); const tree = new Formatter().format(parentTable);
expect(tree) expect(tree)
@ -293,7 +360,10 @@ describe("Table", () => {
describe("#createParagraph", () => { describe("#createParagraph", () => {
it("inserts a new paragraph in the cell", () => { it("inserts a new paragraph in the cell", () => {
const table = new Table(1, 1); const table = new Table({
rows: 1,
columns: 1,
});
const para = table.getCell(0, 0).createParagraph("Test paragraph"); const para = table.getCell(0, 0).createParagraph("Test paragraph");
expect(para).to.be.an.instanceof(Paragraph); expect(para).to.be.an.instanceof(Paragraph);
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
@ -324,7 +394,10 @@ describe("Table", () => {
describe("#float", () => { describe("#float", () => {
it("sets the table float properties", () => { it("sets the table float properties", () => {
const table = new Table(1, 1).float({ const table = new Table({
rows: 1,
columns: 1,
float: {
horizontalAnchor: TableAnchorType.MARGIN, horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.PAGE, verticalAnchor: TableAnchorType.PAGE,
absoluteHorizontalPosition: 10, absoluteHorizontalPosition: 10,
@ -335,6 +408,7 @@ describe("Table", () => {
topFromText: 40, topFromText: 40,
leftFromText: 50, leftFromText: 50,
rightFromText: 60, rightFromText: 60,
},
}); });
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
expect(tree) expect(tree)
@ -344,6 +418,8 @@ describe("Table", () => {
expect(tree["w:tbl"][0]).to.deep.equal({ expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [ "w:tblPr": [
DEFAULT_TABLE_PROPERTIES, DEFAULT_TABLE_PROPERTIES,
BORDERS,
WIDTHS,
{ {
"w:tblpPr": [ "w:tblpPr": [
{ {

View File

@ -16,11 +16,6 @@ import { TableRow } from "./table-row";
table will make it look reasonable, as the layout table will make it look reasonable, as the layout
algorithm will expand columns to fit its content algorithm will expand columns to fit its content
*/ */
export interface IWidthOptions {
readonly width: number;
readonly type?: WidthType;
}
export interface ITableOptions { export interface ITableOptions {
readonly rows: number; readonly rows: number;
readonly columns: number; readonly columns: number;
@ -104,9 +99,4 @@ export class Table extends XmlComponent {
this.properties.setFixedWidthLayout(); this.properties.setFixedWidthLayout();
return this; return this;
} }
public float(tableFloatOptions: ITableFloatOptions): Table {
this.properties.setTableFloatProperties(tableFloatOptions);
return this;
}
} }