Files
docx-js/src/file/table/table.ts

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-09-12 21:01:52 +01:00
// http://officeopenxml.com/WPtableGrid.php
2018-10-23 23:44:50 +01:00
import { XmlComponent } from "file/xml-components";
2017-09-19 15:49:27 +01:00
import { TableGrid } from "./grid";
2018-10-23 23:44:50 +01:00
import { TableCell, WidthType } from "./table-cell";
import { ITableFloatOptions, TableProperties } from "./table-properties";
import { TableRow } from "./table-row";
2017-09-19 15:49:27 +01:00
export class Table extends XmlComponent {
2018-01-29 01:55:25 +00:00
private readonly properties: TableProperties;
private readonly rows: TableRow[];
private readonly grid: TableGrid;
2017-09-19 15:49:27 +01:00
constructor(rows: number, cols: number, colSizes?: number[]) {
2017-09-19 15:49:27 +01:00
super("w:tbl");
this.properties = new TableProperties();
this.root.push(this.properties);
2018-03-22 23:04:46 +00:00
this.properties.setBorder();
2017-09-19 15:49:27 +01:00
if (colSizes && colSizes.length > 0) {
this.grid = new TableGrid(colSizes);
} else {
const gridCols: number[] = [];
for (let i = 0; i < cols; i++) {
/*
0-width columns don't get rendered correctly, so we need
to give them some value. A reasonable default would be
~6in / numCols, but if we do that it becomes very hard
to resize the table using setWidth, unless the layout
algorithm is set to 'fixed'. Instead, the approach here
means even in 'auto' layout, setting a width on the
table will make it look reasonable, as the layout
algorithm will expand columns to fit its content
*/
gridCols.push(100);
}
this.grid = new TableGrid(gridCols);
2017-09-19 15:49:27 +01:00
}
2017-09-19 15:49:27 +01:00
this.root.push(this.grid);
this.rows = [];
for (let i = 0; i < rows; i++) {
const cells: TableCell[] = [];
for (let j = 0; j < cols; j++) {
2018-10-15 21:54:33 +01:00
cells.push(new TableCell());
2017-09-19 15:49:27 +01:00
}
const row = new TableRow(cells);
this.rows.push(row);
this.root.push(row);
}
}
public getRow(ix: number): TableRow {
2018-09-12 21:01:52 +01:00
const row = this.rows[ix];
if (!row) {
throw Error("Index out of bounds when trying to get row on table");
}
return row;
2017-09-19 15:49:27 +01:00
}
public getCell(row: number, col: number): TableCell {
return this.getRow(row).getCell(col);
}
public setWidth(width: number | string, type: WidthType = WidthType.AUTO): Table {
this.properties.setWidth(width, type);
2017-09-19 15:49:27 +01:00
return this;
}
2018-08-07 01:25:28 +01:00
public setFixedWidthLayout(): Table {
this.properties.setFixedWidthLayout();
2017-09-19 15:49:27 +01:00
return this;
}
2018-08-21 19:27:07 +01:00
2018-10-23 23:44:50 +01:00
public float(tableFloatOptions: ITableFloatOptions): Table {
this.properties.setTableFloatProperties(tableFloatOptions);
2018-10-23 20:03:29 +01:00
return this;
2018-08-21 19:27:07 +01:00
}
2018-10-23 09:08:43 -02:00
2018-10-23 20:03:29 +01:00
public get Properties(): TableProperties {
return this.properties;
2018-10-23 09:08:43 -02:00
}
2017-09-19 15:49:27 +01:00
}