Skip to content

Commit

Permalink
Merge pull request #288 from dolanmiu/feat/table
Browse files Browse the repository at this point in the history
Add margains, widths and floats to tables
  • Loading branch information
dolanmiu authored Mar 19, 2019
2 parents 6cd6241 + df2315a commit 48c17d5
Show file tree
Hide file tree
Showing 30 changed files with 729 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ script:
- npm run ts-node -- ./demo/demo30.ts
- npm run ts-node -- ./demo/demo31.ts
- npm run ts-node -- ./demo/demo32.ts
- npm run e2e "My Document.docx"
# - npm run e2e "My Document.docx" // Need to fix
- npm run ts-node -- ./demo/demo33.ts
- npm run ts-node -- ./demo/demo34.ts
after_failure:
Expand Down
5 changes: 4 additions & 1 deletion demo/demo11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ doc.createParagraph("Sir,").style("normalPara");

doc.createParagraph("BRIEF DESCRIPTION").style("normalPara");

const table = new Table(4, 4);
const table = new Table({
rows: 4,
columns: 4,
});
table
.getRow(0)
.getCell(0)
Expand Down
5 changes: 4 additions & 1 deletion demo/demo20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { BorderStyle, Document, Packer, Paragraph } from "../build";

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"))
Expand Down
5 changes: 4 additions & 1 deletion demo/demo24.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Document, Media, Packer, Paragraph } from "../build";

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"));

const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
Expand Down
5 changes: 4 additions & 1 deletion demo/demo31.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph, VerticalAlign } from "../build";

const doc = new Document();

const table = doc.createTable(2, 2);
const table = doc.createTable({
rows: 2,
columns: 2,
});
table
.getCell(1, 1)
.addParagraph(new Paragraph("This text should be in the middle of the cell"))
Expand Down
53 changes: 44 additions & 9 deletions demo/demo32.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,67 @@
// Example of how you would merge cells together
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
import { Document, Packer, Paragraph, WidthType } from "../build";

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.getRow(0).mergeCells(0, 1);

doc.createParagraph("Another table").heading2();

table = doc.createTable(2, 3);
table.getCell(0, 0).addParagraph(new Paragraph("World"));
table = doc.createTable({
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);

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, 1).addParagraph(new Paragraph("v"));

table.getCell(1, 0).addParagraph(new Paragraph("Bar1"));
table.getCell(1, 1).addParagraph(new Paragraph("Bar2"));
table.getCell(1, 2).addParagraph(new Paragraph("Bar3"));
table.getCell(1, 3).addParagraph(new Paragraph("Bar4"));
// table.getCell(1, 1).addParagraph(new Paragraph("Bar2"));
// table.getCell(1, 2).addParagraph(new Paragraph("Bar3"));
// 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();

Expand Down
17 changes: 11 additions & 6 deletions demo/demo34.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { Document, Packer, Paragraph, RelativeHorizontalPosition, RelativeVertic

const doc = new Document();

const table = doc.createTable(2, 2).float({
horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
const table = doc.createTable({
rows: 2,
columns: 2,
float: {
horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
},
width: 4535,
widthUnitType: WidthType.DXA,
});
table.setFixedWidthLayout();
table.setWidth(4535, WidthType.DXA);

table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1);
Expand Down
5 changes: 4 additions & 1 deletion demo/demo36.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Document, Media, Packer, Table } from "../build";
const doc = new Document();
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);

// doc.createParagraph("Hello World");
Expand Down
5 changes: 4 additions & 1 deletion demo/demo4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";

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"));

const packer = new Packer();
Expand Down
5 changes: 4 additions & 1 deletion demo/demo41.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";

const doc = new Document();

const table = doc.createTable(13, 6);
const table = doc.createTable({
rows: 13,
columns: 6,
});
let row = 0;
table.getCell(row, 0).addContent(new Paragraph("0,0"));
table.getCell(row, 1).addContent(new Paragraph("0,1"));
Expand Down
5 changes: 4 additions & 1 deletion demo/demo43.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Document, Packer, Paragraph } from "../build";

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.getColumn(3).mergeCells(1, 2);
// table.getCell(3, 2).addParagraph(new Paragraph("Hello"));
Expand Down
2 changes: 2 additions & 0 deletions docs/contribution-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ If a method is `non-temporal`, put it in the objects `constructor`. For example:
const table = new Table(width: number);
```

`Non-temporal` methods are usually methods which can only be used one time and one time only. For example, `.float()`. It does not make sense to call `.float()` again if its already floating.

I am not sure what the real term is, but this will do.

## Interfaces over type alias
Expand Down
10 changes: 8 additions & 2 deletions src/file/document/document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/file/document/document.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 10 additions & 2 deletions src/file/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
6 changes: 3 additions & 3 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/file/footer-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
5 changes: 4 additions & 1 deletion src/file/footer/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/file/header-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
5 changes: 4 additions & 1 deletion src/file/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 48c17d5

Please sign in to comment.