Files
docx-js/docs/usage/tables.md

394 lines
9.9 KiB
Markdown
Raw Normal View History

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
2021-03-15 00:11:39 +00:00
- `Tables` contain a list of `Rows`
- `Rows` contain a list of `TableCells`
2021-03-25 19:21:27 +03:00
- `TableCells` contain a list of `Paragraphs` and/or `Tables`. You can add `Tables` as tables can be nested inside each other
2019-09-26 02:03:17 +01: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
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [{
children: [table],
}];
2019-06-25 01:21:28 +01:00
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
## Table
### Set Width
```ts
const table = new Table({
...,
width: {
size: [TABLE_WIDTH],
type: WidthType,
}
});
```
For example:
```ts
const table = new Table({
...,
width: {
size: 4535,
type: WidthType.DXA,
}
});
```
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
2021-03-15 00:11:39 +00:00
| Property | Type | Notes |
| ----------- | -------------------------------------- | -------- |
| children | `Array<TableCell>` | Required |
| cantSplit | `boolean` | Optional |
| tableHeader | `boolean` | Optional |
2020-09-11 21:10:10 -05:00
| height | `{ height: number, rule: HeightRule }` | Optional |
2019-03-05 21:57:31 +00:00
2019-09-26 02:03:17 +01:00
### Repeat row
If a table is paginated on multiple pages, it is possible to repeat a row at the top of each new page by setting `tableHeader` to `true`:
```ts
const row = new TableRow({
...,
tableHeader: true,
});
```
2020-10-24 20:24:10 +01:00
### Pagination
#### Prevent row pagination
To prevent breaking contents of a row across multiple pages, call `cantSplit`:
```ts
const row = new Row({
...,
cantSplit: true,
});
```
2019-09-26 02:03:17 +01:00
## Table 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
### Options
2019-09-26 02:03:17 +01:00
| Property | Type | Notes |
| ------------- | ----------------------------------- | ----------------------------------------------------------- |
2021-03-15 00:11:39 +00:00
| children | `Array<Paragraph or Table>` | Required. You can nest tables by adding a table into a cell |
2019-09-26 02:03:17 +01:00
| shading | `ITableShadingAttributesProperties` | Optional |
| margins | `ITableCellMarginOptions` | Optional |
| verticalAlign | `VerticalAlign` | Optional |
| columnSpan | `number` | Optional |
| rowSpan | `number` | Optional |
| borders | `BorderOptions` | Optional |
| width | `{ size: number type: WidthType }` | Optional |
2019-03-05 21:57:31 +00:00
2019-09-26 02:03:17 +01:00
#### Border Options
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
| Property | Type | Notes |
| -------- | ----------------------------------------------------- | -------- |
| top | `{ style: BorderStyle, size: number, color: string }` | Optional |
| bottom | `{ style: BorderStyle, size: number, color: string }` | Optional |
| left | `{ style: BorderStyle, size: number, color: string }` | Optional |
| right | `{ style: BorderStyle, size: number, color: string }` | Optional |
2019-09-26 02:03:17 +01:00
##### Example
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
borders: {
top: {
style: BorderStyle.DASH_DOT_STROKED,
size: 1,
color: "red",
},
bottom: {
style: BorderStyle.THICK_THIN_MEDIUM_GAP,
size: 5,
color: "889900",
},
},
});
```
2019-09-26 02:03:17 +01:00
##### Google DOCS
2019-04-12 13:46:05 +02:00
2019-09-26 02:03:17 +01:00
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:
2019-04-12 13:46:05 +02:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
borders: {
top: {
style: BorderStyle.DOT_DOT_DASH,
size: 3,
color: "green",
},
bottom: {
style: BorderStyle.DOT_DOT_DASH,
size: 3,
color: "ff8000",
},
},
});
2019-04-12 13:46:05 +02:00
```
2019-09-26 02:03:17 +01:00
### Add paragraph to a cell
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
Once you have got the cell, you can add data to it:
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
children: [new Paragraph("Hello")],
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
### Set width of a cell
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
You can specify the width of a cell using:
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
width: {
size: number,
type: WidthType,
},
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
`WidthType` values can be:
2019-04-12 13:46:05 +02:00
2019-09-26 02:03:17 +01:00
| Property | Notes |
| -------- | --------------------------------- |
| AUTO | |
| DXA | value is in twentieths of a point |
| NIL | is considered as zero |
| PCT | percent of table width |
2019-04-12 13:46:05 +02:00
2019-09-26 02:03:17 +01:00
### Nested Tables
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
To have a table within a table, simply add it in the `children` block of a `table cell`:
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
children: [new Table(...)],
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
### Vertical Align
2018-12-29 01:57:20 +00:00
Sets the vertical alignment of the contents of the cell
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
verticalAlign: VerticalAlign,
});
```
2019-09-26 02:03:17 +01:00
`VerticalAlign` values can be:
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
| Property | Notes |
| -------- | ------------------------------------------ |
| BOTTOM | Align the contents on the bottom |
| CENTER | Align the contents on the center |
| TOP | Align the contents on the top. The default |
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
For example, to center align a cell:
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
verticalAlign: VerticalAlign.CENTER,
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
## Merging cells together
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
### Row Merge
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
When cell rows are merged, it counts as multiple rows, so be sure to remove excess cells. It is similar to how HTML's `rowspan` works.
https://www.w3schools.com/tags/att_td_rowspan.asp
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
rowSpan: [NUMBER_OF_CELLS_TO_MERGE],
});
2018-12-29 01:57:20 +00:00
```
#### Example
2019-09-26 02:03:17 +01:00
The example will merge three rows together.
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
rowSpan: 3,
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
### Column Merge
2018-12-29 01:57:20 +00:00
2019-09-26 02:03:17 +01:00
When cell columns are merged, it counts as multiple columns, so be sure to remove excess cells. It is similar to how HTML's `colspan` works.
https://www.w3schools.com/tags/att_td_colspan.asp
2018-12-29 01:57:20 +00:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
columnSpan: [NUMBER_OF_CELLS_TO_MERGE],
});
2018-12-29 01:57:20 +00:00
```
2019-09-26 02:03:17 +01:00
#### Example
2019-04-12 13:46:05 +02:00
2019-09-26 02:03:17 +01:00
The example will merge three columns together.
2019-04-12 13:46:05 +02:00
```ts
2019-09-26 02:03:17 +01:00
const cell = new TableCell({
...,
columnSpan: 3,
});
2019-04-12 13:46:05 +02:00
```
2021-03-04 02:08:09 +00:00
### Visual Right to Left Table
It is possible to reverse how the cells of the table are displayed. The table direction. More info here: https://superuser.com/questions/996912/how-to-change-a-table-direction-in-microsoft-word
```ts
const table = new Table({
visuallyRightToLeft: true,
});
```
2018-12-29 01:57:20 +00:00
## Examples
2019-09-30 20:58:18 +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
2021-03-25 19:21:27 +03:00
Example showing how to add colorful borders to tables
2019-03-05 23:15:50 +00:00
2019-09-30 20:58:18 +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-09-30 20:58:18 +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-09-30 20:58:18 +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-09-30 20:58:18 +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-26 02:03:17 +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-09-30 20:58:18 +01:00
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/32-merge-and-shade-table-cells.ts ':include')
2019-03-05 23:15:50 +00:00
2019-09-30 20:58:18 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/32-merge-and-shade-table-cells.ts_
2019-03-05 23:15:50 +00:00
2019-09-30 20:58:18 +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-09-30 20:58:18 +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-09-30 20:58:18 +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_