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

113 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-10-23 23:44:50 +01:00
// http://officeopenxml.com/WPtableGrid.php
import { Paragraph } from "file/paragraph";
2019-09-13 00:51:20 +01:00
import { BorderStyle } from "file/styles";
2018-10-23 23:44:50 +01:00
import { IXmlableObject, XmlComponent } from "file/xml-components";
2019-03-21 01:06:07 +00:00
import { ITableShadingAttributesProperties } from "../shading";
2018-10-23 23:44:50 +01:00
import { Table } from "../table";
2019-04-18 13:55:18 +10:00
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
2019-09-13 00:51:20 +01:00
import { VerticalAlign, VMergeType } from "./table-cell-components";
2018-10-23 23:44:50 +01:00
import { TableCellProperties } from "./table-cell-properties";
2019-06-25 01:21:28 +01:00
export interface ITableCellOptions {
readonly shading?: ITableShadingAttributesProperties;
2019-09-13 00:51:20 +01:00
readonly margins?: ITableCellMarginOptions;
readonly verticalAlign?: VerticalAlign;
readonly verticalMerge?: VMergeType;
readonly columnSpan?: number;
2019-09-22 02:39:38 +01:00
readonly rowSpan?: number;
2019-09-13 00:51:20 +01:00
readonly borders?: {
readonly top?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly bottom?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly left?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly right?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
};
readonly children: Array<Paragraph | Table>;
2019-06-25 01:21:28 +01:00
}
2018-10-23 23:44:50 +01:00
export class TableCell extends XmlComponent {
private readonly properties: TableCellProperties;
2019-09-13 00:51:20 +01:00
constructor(readonly options: ITableCellOptions) {
2018-10-23 23:44:50 +01:00
super("w:tc");
2019-03-18 23:50:21 +00:00
2018-10-23 23:44:50 +01:00
this.properties = new TableCellProperties();
this.root.push(this.properties);
2019-09-13 00:51:20 +01:00
for (const child of options.children) {
this.root.push(child);
2018-10-23 23:44:50 +01:00
}
2019-09-13 00:51:20 +01:00
if (options.verticalAlign) {
this.properties.setVerticalAlign(options.verticalAlign);
}
2019-03-04 22:50:04 +00:00
2019-09-13 00:51:20 +01:00
if (options.verticalMerge) {
this.properties.addVerticalMerge(options.verticalMerge);
}
2019-03-18 23:50:21 +00:00
2019-09-13 00:51:20 +01:00
if (options.margins) {
this.properties.addMargins(options.margins);
}
2019-03-18 23:50:21 +00:00
2019-09-13 00:51:20 +01:00
if (options.shading) {
this.properties.setShading(options.shading);
}
2019-03-21 01:06:07 +00:00
2019-09-13 00:51:20 +01:00
if (options.columnSpan) {
this.properties.addGridSpan(options.columnSpan);
}
2019-03-21 01:06:07 +00:00
2019-09-25 00:57:24 +01:00
if (options.rowSpan && options.rowSpan > 1) {
this.properties.addVerticalMerge(VMergeType.RESTART);
}
2019-09-13 00:51:20 +01:00
if (options.borders) {
if (options.borders.top) {
this.properties.Borders.addTopBorder(options.borders.top.style, options.borders.top.size, options.borders.top.color);
}
if (options.borders.bottom) {
this.properties.Borders.addBottomBorder(
options.borders.bottom.style,
options.borders.bottom.size,
options.borders.bottom.color,
);
}
if (options.borders.left) {
this.properties.Borders.addLeftBorder(options.borders.left.style, options.borders.left.size, options.borders.left.color);
}
if (options.borders.right) {
this.properties.Borders.addRightBorder(
options.borders.right.style,
options.borders.right.size,
options.borders.right.color,
);
}
}
2018-10-23 23:44:50 +01:00
}
2019-03-05 11:38:21 +01:00
2019-09-13 00:51:20 +01:00
public prepForXml(): IXmlableObject | undefined {
// Cells must end with a paragraph
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
this.root.push(new Paragraph({}));
}
return super.prepForXml();
2019-03-05 11:38:21 +01:00
}
2018-10-23 23:44:50 +01:00
}