2018-12-29 01:57:20 +00:00
# Tables
2019-09-19 22:49:09 +01:00
!> Paragraphs requires an understanding of [Sections ](usage/sections.md ).
2018-12-29 01:57:20 +00:00
2019-09-19 22:49:09 +01:00
## Intro
2018-12-29 01:57:20 +00:00
2019-09-19 22:49:09 +01:00
Create a simple table like so:
2018-12-29 01:57:20 +00:00
```ts
2019-09-19 22:49:09 +01:00
const table = new Table({
rows: [Array of `TableRow` s]
2019-06-25 01:21:28 +01:00
});
2018-12-29 01:57:20 +00:00
```
2019-09-19 22:49:09 +01:00
Then add the table in the `section`
2018-12-29 01:57:20 +00:00
```ts
2019-09-19 22:49:09 +01:00
doc.addSection({
children: [table],
2019-06-25 01:21:28 +01:00
});
2018-12-29 01:57:20 +00:00
```
2019-09-19 22:49:09 +01:00
## Table Row
2019-03-05 21:57:31 +00:00
2019-09-19 22:49:09 +01:00
A table consists of multiple `table rows` . Table rows have a list of `children` which accepts a list of `table cells` explained below. You can create a simple `table row` like so:
2019-03-05 21:57:31 +00:00
```ts
2019-09-19 22:49:09 +01:00
const tableRow = new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
});
2019-03-05 21:57:31 +00:00
```
2019-09-19 22:49:09 +01:00
Or preferably, add the tableRow directly into the `table` without declaring a variable:
2019-03-05 21:57:31 +00:00
```ts
2019-09-19 22:49:09 +01:00
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
});
2019-03-05 21:57:31 +00:00
```
2019-09-19 22:49:09 +01:00
### Options
2019-03-05 21:57:31 +00:00
2019-09-19 22:49:09 +01:00
Here is a list of options you can add to the `table row` :
2019-03-05 21:57:31 +00:00
2019-09-19 22:49:09 +01:00
| Property | Type | Notes |
| ----------- | ------------------------------------- | -------- |
| children | `Array<TableCell>` | Required |
| cantSplit | `boolean` | Optional |
| tableHeader | `boolean` | Optional |
| height | `{ value: number, rule: HeightRule }` | Optional |
2019-03-05 21:57:31 +00:00
2019-09-19 22:49:09 +01:00
## Cells
2019-03-05 21:57:31 +00:00
2019-09-19 22:49:09 +01:00
Cells need to be added in the `table row` , you can create a table cell like:
2019-03-05 21:57:31 +00:00
```ts
2019-09-19 22:49:09 +01:00
const tableCell = new TableCell({
children: [new Paragraph("hello")],
});
2019-03-05 21:57:31 +00:00
```
2019-09-19 22:49:09 +01:00
Or preferably, add the tableRow directly into the `table row` without declaring a variable:
2019-03-05 21:57:31 +00:00
```ts
2019-09-19 22:49:09 +01:00
const tableRow = new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
});
2018-12-29 01:57:20 +00:00
```
2019-09-19 22:49:09 +01:00
| Property | Type | Notes |
| ----------- | ------------------------------------- | -------- |
| children | `Array<TableCell>` | Required |
| cantSplit | `boolean` | Optional |
| tableHeader | `boolean` | Optional |
| height | `{ value: number, rule: HeightRule }` | Optional |
### Options
readonly shading?: ITableShadingAttributesProperties;
readonly margins?: ITableCellMarginOptions;
readonly verticalAlign?: VerticalAlign;
readonly verticalMerge?: VMergeType;
readonly columnSpan?: number;
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-01-03 02:11:04 +00:00
2019-03-05 21:57:31 +00:00
2018-12-29 01:57:20 +00:00
2019-01-03 02:11:04 +00:00
### Add paragraph to a cell
2019-06-25 23:17:56 +01:00
Once you have got the cell, you can add data to it with the `add()` method.
2019-01-03 02:11:04 +00:00
```ts
2019-09-25 01:59:30 +01:00
new TableCell({
children: [new Paragraph("Hello")],
}),
2019-01-03 02:11:04 +00:00
```
2019-04-12 13:46:05 +02:00
### Set width of a cell
You can specify the width of a cell using:
`cell.Properties.setWidth(width, format)`
format can be:
2019-06-25 01:21:28 +01:00
- WidthType.AUTO
- WidthType.DXA: value is in twentieths of a point
- WidthType.NIL: is considered as zero
- WidthType.PCT: percent of table width
2019-04-12 13:46:05 +02:00
### Example
```ts
cell.Properties.setWidth(100, WidthType.DXA);
```
```ts
2019-06-25 01:21:28 +01:00
cell.Properties.setWidth("50%", WidthType.PCT);
2019-04-12 13:46:05 +02:00
```
2018-12-29 01:57:20 +00:00
## Borders
BorderStyle can be imported from `docx` . Size determines the thickness. HTML color can be a hex code or alias such as `red` .
```ts
cell.Borders.addTopBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addBottomBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addStartBorder([[BorderStyle]], [SIZE], [HTML COLOR]);
```
```ts
cell.Borders.addEndBorder([BorderStyle], [SIZE], [HTML COLOR]);
```
### Example
```ts
import { BorderStyle } from "docx";
cell.Borders.addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green");
2019-04-12 13:46:05 +02:00
cell.Borders.addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000 ");
```
### Google DOCS
Google DOCS does not support start and end borders, instead they use left and right borders. So to set left and right borders for Google DOCS you should use:
```ts
import { BorderStyle } from "docx";
cell.Borders.addLeftBorder(BorderStyle.DOT_DOT_DASH, 3, "green");
cell.Borders.addRightBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000 ");
2018-12-29 01:57:20 +00:00
```
## Set Width
```ts
2018-12-31 01:55:15 +00:00
import { WidthType } from "docx";
table.setWidth([WIDTH], [OPTIONAL WidthType. Defaults to DXA]);
2018-12-29 01:57:20 +00:00
```
2018-12-31 01:55:15 +00:00
For example:
2018-12-29 01:57:20 +00:00
```ts
table.setWidth(4535, WidthType.DXA);
```
## Vertical Align
2018-12-31 01:55:15 +00:00
Sets the vertical alignment of the contents of the cell
```ts
import { VerticalAlign } from "docx";
cell.setVerticalAlign([VerticalAlign TYPE]);
```
For example, to center align a cell:
```ts
cell.setVerticalAlign(VerticalAlign.CENTER);
```
2018-12-29 01:57:20 +00:00
## Rows
To get a row, use the `getRow` method on a `table` . There are a handful of methods which you can apply to a row which will be explained below.
```ts
table.getRow([ROW INDEX]);
```
## Merge cells together
### Merging on a row
First obtain the row, and call `mergeCells()` . The first argument is where the merge should start. The second argument is where the merge should end.
```ts
table.getRow(0).mergeCells([FROM INDEX], [TO INDEX]);
```
#### Example
This will merge 3 cells together starting from index `0` :
```ts
table.getRow(0).mergeCells(0, 2);
```
### Merging on a column
It has not been implemented yet, but it will follow a similar structure as merging a row.
## Nested Tables
To have a table within a table
```ts
2019-06-25 23:17:56 +01:00
cell.add(new Table(1, 1));
2018-12-29 01:57:20 +00:00
```
2019-04-12 13:46:05 +02:00
## Pagination
###Prevent row pagination
To prevent breaking contents of a row across multiple pages, call `cantSplit()` :
```ts
table.getRow(0).setCantSplit();
```
###Repeat row
If a table is paginated on multiple pages, it is possible to repeat a row at the top of each new page calling `setTableHeader()` :
```ts
table.getRow(0).setTableHeader();
```
2018-12-29 01:57:20 +00:00
## Examples
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/4-basic-table.ts ':include' )
2018-12-29 01:57:20 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/4-basic-table.ts_
2019-03-05 23:15:50 +00:00
### Custom borders
Example showing how to add colourful borders to tables
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/20-table-cell-borders.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/20-table-cell-borders.ts_
2019-03-05 23:15:50 +00:00
### Adding images
Example showing how to add images to tables
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/24-images-to-table-cell.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/24-images-to-table-cell.ts_
2019-03-05 23:15:50 +00:00
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/36-image-to-table-cell.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/36-image-to-table-cell.ts_
2019-03-05 23:15:50 +00:00
### Alignment of text in a cell
Example showing how align text in a table cell
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/31-tables.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/31-tables.ts_
2019-03-05 23:15:50 +00:00
2019-09-25 01:59:30 +01:00
### Shading
2019-03-05 23:15:50 +00:00
2019-09-25 01:59:30 +01:00
Example showing merging of columns and rows and shading
2019-03-05 23:15:50 +00:00
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/32-merge-table-cells.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/32-merge-table-cells.ts_
2019-03-05 23:15:50 +00:00
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/41-merge-table-cells-2.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/41-merge-table-cells-2.ts_
2019-03-05 23:15:50 +00:00
### Merging columns
2019-09-25 01:59:30 +01:00
Example showing merging of columns and rows
2019-03-05 23:15:50 +00:00
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/43-images-to-table-cell-2.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/43-images-to-table-cell-2.ts_
2019-03-05 23:15:50 +00:00
### Floating tables
2019-08-21 00:53:49 +01:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/34-floating-tables.ts ':include' )
2019-03-05 23:15:50 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/34-floating-tables.ts_