Work on new table API
This commit is contained in:
@ -59,7 +59,10 @@ describe("Document", () => {
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should create a new table and append it to body", () => {
|
||||
const table = document.createTable(2, 3);
|
||||
const table = document.createTable({
|
||||
rows: 2,
|
||||
columns: 3,
|
||||
});
|
||||
expect(table).to.be.an.instanceof(Table);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body)
|
||||
@ -69,7 +72,10 @@ describe("Document", () => {
|
||||
});
|
||||
|
||||
it("should create a table with the correct dimensions", () => {
|
||||
document.createTable(2, 3);
|
||||
document.createTable({
|
||||
rows: 2,
|
||||
columns: 3,
|
||||
});
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body)
|
||||
.to.be.an("array")
|
||||
|
@ -1,7 +1,7 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { Table } from "../table";
|
||||
import { ITableOptions, Table } from "../table";
|
||||
import { TableOfContents } from "../table-of-contents";
|
||||
import { Body } from "./body";
|
||||
import { SectionPropertiesOptions } from "./body/section-properties";
|
||||
@ -58,8 +58,8 @@ export class Document extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table(rows, cols);
|
||||
public createTable(options: ITableOptions): Table {
|
||||
const table = new Table(options);
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
|
@ -93,7 +93,12 @@ describe("File", () => {
|
||||
it("should call the underlying document's addTable", () => {
|
||||
const wrapper = new File();
|
||||
const spy = sinon.spy(wrapper.Document, "addTable");
|
||||
wrapper.addTable(new Table(1, 1));
|
||||
wrapper.addTable(
|
||||
new Table({
|
||||
rows: 1,
|
||||
columns: 1,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
@ -103,7 +108,10 @@ describe("File", () => {
|
||||
it("should call the underlying document's createTable", () => {
|
||||
const wrapper = new File();
|
||||
const spy = sinon.spy(wrapper.Document, "createTable");
|
||||
wrapper.createTable(1, 1);
|
||||
wrapper.createTable({
|
||||
rows: 1,
|
||||
columns: 1,
|
||||
});
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
|
@ -24,7 +24,7 @@ import { Settings } from "./settings";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { Table } from "./table";
|
||||
import { ITableOptions, Table } from "./table";
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
|
||||
export class File {
|
||||
@ -131,8 +131,8 @@ export class File {
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
return this.document.createTable(rows, cols);
|
||||
public createTable(options: ITableOptions): Table {
|
||||
return this.document.createTable(options);
|
||||
}
|
||||
|
||||
public addImage(image: Image): File {
|
||||
|
@ -21,7 +21,12 @@ describe("FooterWrapper", () => {
|
||||
it("should call the underlying footer's addParagraph", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Footer, "addTable");
|
||||
file.addTable(new Table(1, 1));
|
||||
file.addTable(
|
||||
new Table({
|
||||
rows: 1,
|
||||
columns: 1,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
|
@ -53,7 +53,10 @@ export class Footer extends InitializableXmlComponent {
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table(rows, cols);
|
||||
const table = new Table({
|
||||
rows: rows,
|
||||
columns: cols,
|
||||
});
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
|
@ -21,7 +21,12 @@ describe("HeaderWrapper", () => {
|
||||
it("should call the underlying header's addTable", () => {
|
||||
const wrapper = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(wrapper.Header, "addTable");
|
||||
wrapper.addTable(new Table(1, 1));
|
||||
wrapper.addTable(
|
||||
new Table({
|
||||
rows: 1,
|
||||
columns: 1,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
|
@ -64,7 +64,10 @@ export class Header extends InitializableXmlComponent {
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table(rows, cols);
|
||||
const table = new Table({
|
||||
rows: rows,
|
||||
columns: cols,
|
||||
});
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ export class TableRow extends XmlComponent {
|
||||
cells.forEach((c) => this.root.push(c));
|
||||
}
|
||||
|
||||
public getCell(ix: number): TableCell {
|
||||
const cell = this.cells[ix];
|
||||
public getCell(index: number): TableCell {
|
||||
const cell = this.cells[index];
|
||||
|
||||
if (!cell) {
|
||||
throw Error("Index out of bounds when trying to get cell on row");
|
||||
|
@ -21,31 +21,62 @@ export interface IWidthOptions {
|
||||
readonly type?: WidthType;
|
||||
}
|
||||
|
||||
export interface ITableOptions {
|
||||
readonly rows: number;
|
||||
readonly columns: number;
|
||||
readonly width?: number;
|
||||
readonly widthUnitType?: WidthType;
|
||||
readonly columnWidths?: number[];
|
||||
readonly margains?: {
|
||||
readonly margainUnitType?: WidthType;
|
||||
readonly top?: number;
|
||||
readonly bottom?: number;
|
||||
readonly right?: number;
|
||||
readonly left?: number;
|
||||
};
|
||||
readonly float?: ITableFloatOptions;
|
||||
}
|
||||
|
||||
export class Table extends XmlComponent {
|
||||
private readonly properties: TableProperties;
|
||||
private readonly rows: TableRow[];
|
||||
|
||||
constructor(
|
||||
rowCount: number,
|
||||
columnCount: number,
|
||||
widthOptions: IWidthOptions = { width: 100, type: WidthType.AUTO },
|
||||
colSizes: number[] = Array<number>(columnCount).fill(100),
|
||||
) {
|
||||
constructor({
|
||||
rows,
|
||||
columns,
|
||||
width = 100,
|
||||
widthUnitType = WidthType.AUTO,
|
||||
columnWidths = Array<number>(columns).fill(100),
|
||||
margains: { margainUnitType, top, bottom, right, left } = { margainUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
|
||||
float,
|
||||
}: ITableOptions) {
|
||||
super("w:tbl");
|
||||
this.properties = new TableProperties();
|
||||
this.root.push(this.properties);
|
||||
this.properties.setBorder();
|
||||
this.properties.setWidth(widthOptions.width, widthOptions.type);
|
||||
const grid = new TableGrid(colSizes);
|
||||
this.properties.setWidth(width, widthUnitType);
|
||||
this.properties.CellMargin.addBottomMargin(bottom || 0, margainUnitType);
|
||||
this.properties.CellMargin.addTopMargin(top || 0, margainUnitType);
|
||||
this.properties.CellMargin.addLeftMargin(left || 0, margainUnitType);
|
||||
this.properties.CellMargin.addRightMargin(right || 0, margainUnitType);
|
||||
const grid = new TableGrid(columnWidths);
|
||||
|
||||
this.root.push(grid);
|
||||
|
||||
this.rows = [];
|
||||
for (let i = 0; i < rowCount; i++) {
|
||||
const cells = Array<TableCell>(columnCount).fill(new TableCell());
|
||||
this.rows = Array(rows)
|
||||
.fill(0)
|
||||
.map(() => {
|
||||
const cells = Array(columns)
|
||||
.fill(0)
|
||||
.map(() => new TableCell());
|
||||
const row = new TableRow(cells);
|
||||
this.rows.push(row);
|
||||
this.root.push(row);
|
||||
return row;
|
||||
});
|
||||
|
||||
this.rows.forEach((x) => this.root.push(x));
|
||||
|
||||
if (float) {
|
||||
this.properties.setTableFloatProperties(float);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user