Add table column and vertical merging
This commit is contained in:
17
demo/demo43.ts
Normal file
17
demo/demo43.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// Add image to table cell
|
||||
// Import from 'docx' rather than '../build' if you install from npm
|
||||
import * as fs from "fs";
|
||||
import { Document, Packer, Paragraph } from "../build";
|
||||
|
||||
const doc = new Document();
|
||||
|
||||
const table = doc.createTable(4, 4);
|
||||
table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
|
||||
table.getColumn(3).mergeCells(1, 2);
|
||||
// table.getCell(3, 2).addParagraph(new Paragraph("Hello"));
|
||||
|
||||
const packer = new Packer();
|
||||
|
||||
packer.toBuffer(doc).then((buffer) => {
|
||||
fs.writeFileSync("My Document.docx", buffer);
|
||||
});
|
@ -3,7 +3,7 @@ import { Paragraph } from "file/paragraph";
|
||||
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { Table } from "../table";
|
||||
import { TableCellBorders, VerticalAlign } from "./table-cell-components";
|
||||
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
|
||||
import { TableCellProperties } from "./table-cell-properties";
|
||||
|
||||
export class TableCell extends XmlComponent {
|
||||
@ -58,6 +58,12 @@ export class TableCell extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public addVerticalMerge(type: VMergeType): TableCell {
|
||||
this.properties.addVerticalMerge(type);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public get Borders(): TableCellBorders {
|
||||
return this.properties.Borders;
|
||||
}
|
||||
|
22
src/file/table/table-column.ts
Normal file
22
src/file/table/table-column.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { TableCell, VMergeType } from "./table-cell";
|
||||
|
||||
export class TableColumn {
|
||||
constructor(private readonly cells: TableCell[]) {}
|
||||
|
||||
public getCell(index: number): TableCell {
|
||||
const cell = this.cells[index];
|
||||
|
||||
if (!cell) {
|
||||
throw Error("Index out of bounds when trying to get cell on column");
|
||||
}
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
public mergeCells(startIndex: number, endIndex: number): TableCell {
|
||||
this.cells[startIndex].addVerticalMerge(VMergeType.RESTART);
|
||||
this.cells[endIndex].addVerticalMerge(VMergeType.CONTINUE);
|
||||
|
||||
return this.cells[startIndex];
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ export class TableProperties extends XmlComponent {
|
||||
this.root.push(this.cellMargin);
|
||||
}
|
||||
|
||||
public setWidth(width: number | string, type: WidthType = WidthType.AUTO): TableProperties {
|
||||
public setWidth(width: number, type: WidthType = WidthType.AUTO): TableProperties {
|
||||
this.root.push(new PreferredTableWidth(type, width));
|
||||
return this;
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// http://officeopenxml.com/WPtableWidth.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { WidthType } from "../table-cell";
|
||||
@ -12,7 +13,7 @@ class TableWidthAttributes extends XmlAttributeComponent<ITableWidth> {
|
||||
}
|
||||
|
||||
export class PreferredTableWidth extends XmlComponent {
|
||||
constructor(type: WidthType, w: number | string) {
|
||||
constructor(type: WidthType, w: number) {
|
||||
super("w:tblW");
|
||||
this.root.push(new TableWidthAttributes({ type, w }));
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { TableGrid } from "./grid";
|
||||
import { TableCell, WidthType } from "./table-cell";
|
||||
import { TableColumn } from "./table-column";
|
||||
import { ITableFloatOptions, TableProperties } from "./table-properties";
|
||||
import { TableRow } from "./table-row";
|
||||
|
||||
@ -51,8 +52,8 @@ export class Table extends XmlComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public getRow(ix: number): TableRow {
|
||||
const row = this.rows[ix];
|
||||
public getRow(index: number): TableRow {
|
||||
const row = this.rows[index];
|
||||
|
||||
if (!row) {
|
||||
throw Error("Index out of bounds when trying to get row on table");
|
||||
@ -61,11 +62,17 @@ export class Table extends XmlComponent {
|
||||
return row;
|
||||
}
|
||||
|
||||
public getColumn(index: number): TableColumn {
|
||||
// This is a convinence method for people who like to work with columns
|
||||
const cells = this.rows.map((row) => row.getCell(index));
|
||||
return new TableColumn(cells);
|
||||
}
|
||||
|
||||
public getCell(row: number, col: number): TableCell {
|
||||
return this.getRow(row).getCell(col);
|
||||
}
|
||||
|
||||
public setWidth(width: number | string, type: WidthType = WidthType.AUTO): Table {
|
||||
public setWidth(width: number, type: WidthType = WidthType.AUTO): Table {
|
||||
this.properties.setWidth(width, type);
|
||||
return this;
|
||||
}
|
||||
|
Reference in New Issue
Block a user