Tables
Create tables, set options, merge cells, borders, rich cells and conditional formatting.
Tables#
Table Creation Methods#
| Method | Parameters | Description |
|---|---|---|
addTable() | data, options | Full-featured table with all formatting options |
addSimpleTable() | data | Plain table with no header styling |
addStyledTable() | data, headerBg, evenBg | Quick styled table with two colour parameters |
Table Options#
| Option | Type | Description |
|---|---|---|
:headerRow | true/false | First row is a header: bold, coloured background |
:headerBgColor | String | Header row background colour hex |
:headerTextColor | String | Header text colour. Default: FFFFFF |
:evenRowBgColor | String | Alternating even-row background colour |
:borderStyle | String | "single", "double", "dashed", "dotted", "none" |
:borderColor | String | Border colour hex. Default: 000000 |
:colWidths | List | Column widths in cm, e.g. [2, 6, 3, 5.5] |
:headerVAlign | String | Header vertical alignment: "top", "center", "bottom" |
:cellVAlign | String | Body cell vertical alignment |
:repeatHeader | true/false | Repeat header row on each page when the table spans pages |
:rowHeights | List | Per-row heights in cm — 0 means auto-size, e.g. [1.5, 0, 2.0] |
:rowHRule | String | Height rule for :rowHeights: "atLeast" (default) or "exact" |
:fontSize | Number | Font size for all cells in points |
:conditionalRules | List | Conditional cell formatting rules (see §6.5) |
doc.addTable([
["ID", "Description", "Status", "Owner" ],
["1", "Design new dashboard", "In Progress", "Ahmed" ],
["2", "Fix login timeout on mobile", "Done", "Sara" ],
["3", "Write API documentation v2", "Pending", "Omar" ]
], [
:headerRow = true,
:borderStyle = "single",
:headerBgColor = "1565C0",
:evenRowBgColor = "E3F2FD",
:headerVAlign = "center",
:cellVAlign = "center",
:colWidths = [2, 6, 3, 5.5]
])
# Per-row heights — header 1.5 cm tall, rows 2 & 4 auto-height, row 3 is 2.5 cm
doc.addTable(data, [
:headerRow = true,
:colWidths = [5, 10],
:rowHeights = [1.5, 0, 2.5, 0],
:rowHRule = "atLeast"
])Rich Cell Content — WordCell#
For cells needing mixed formatting, images, lists, or cell spanning:
# Styled cell spanning 3 columns
titleCell = new WordCell()
titleCell.setText("Status Report", [:bold=true, :color="1E6BB5"])
titleCell.setBgColor("DEEAF1")
titleCell.setColSpan(3)
titleCell.setVerticalAlign("center")
# Row-spanning category cell
cat = new WordCell()
cat.addRun("Electronics", [:bold=true, :color="1565C0"])
cat.setRowSpan(3)
cat.setVerticalAlign("center")
cat.setAlign("center")
cat.setBgColor("E3F2FD")
# Convenience function: wordCell(text, options)
hdr = wordCell("Category", [:bold=true, :color="white", :bgColor="37474F"])WordCell method | Parameters | Description |
|---|---|---|
setText() | text, options | Set main text with formatting options |
addRun() | text, options | Append an additional text run |
addText() | text, options | Alias for addRun() |
addCellBulletList() | items, listId | Add a bullet list inside the cell |
addCellNumberedList() | items, listId | Add a numbered list inside the cell |
addCellImage() | path, widthCm, heightCm | Embed an image in the cell |
addCellHyperlink() | text, relId | Add a hyperlink inside the cell |
setBgColor() | color | Cell background colour hex |
setColSpan() | n | Merge across n columns |
setRowSpan() | n | Merge down n rows |
setVerticalAlign() | align | "top", "center", "bottom" |
setAlign() | align | Text alignment within the cell |
setBorder() | style, color | All-sides border (legacy — prefer setBorderSide) |
setBorderSide() | side, style, color, size | Per-side border — see §6.4 |
setTopBorder() | style, color, size | Top edge only |
setBottomBorder() | style, color, size | Bottom edge only |
setLeftBorder() | style, color, size | Left edge only |
setRightBorder() | style, color, size | Right edge only |
setNoBorder() | — | Remove all borders from this cell |
setTextDir() | dir | Rotate cell text — see §6.4 |
setPadding() | top, bot, left, right | Cell padding in cm |
setWidth() | widthCm | Override cell width in cm |
Tip: Use wordCell(text, options) as a shorthand to create a WordCell with a single text run in one line.
Per-Side Cell Borders and Text Direction#
Per-side borders give each cell edge an independent style, colour, and weight. This makes it easy to build accent-left sidebars, coloured header underlines, and borderless inner cells with only outer rules showing.
# Left accent sidebar — only a thick teal left edge, everything else removed
sidebar = wordCell("Note", [])
sidebar.setLeftBorder("thick", "1B998B", 16)
sidebar.setTopBorder("none", NULL, NULL)
sidebar.setBottomBorder("none", NULL, NULL)
sidebar.setRightBorder("none", NULL, NULL)
# Double outer box
boxed = wordCell("Total", [:bold=true])
boxed.setTopBorder("double", "000000", 8)
boxed.setBottomBorder("double", "000000", 8)
boxed.setLeftBorder("double", "000000", 8)
boxed.setRightBorder("double", "000000", 8)
# Header cell with a thick coloured bottom accent only
hdr = wordCell("January", [:bold=true, :align="center"])
hdr.setBottomBorder("thick", "E94F37", 12)setBorderSide(side, style, color, size) parameter values:
| Parameter | Values |
|---|---|
side | "top", "bottom", "left", "right", "insideH", "insideV" |
style | "single", "double", "dashed", "dotted", "thick", "none" |
color | Hex string, or NULL for auto |
size | Eighths-of-a-point: 4=½pt, 8=1pt, 16=2pt, 24=3pt — or NULL for default |
Text direction rotates the cell's text. Most commonly used for compact column headers in data tables where many narrow columns need rotated labels.
# Text reads upward (BTL) — saves horizontal space for many columns
colHdr = wordCell("January", [:bold=true, :align="center"])
colHdr.setTextDir("btLr")
# Text reads downward (TBR)
colHdr2 = wordCell("Q1 Target", [:bold=true])
colHdr2.setTextDir("tbRl")setTextDir value | Description |
|---|---|
"btLr" | Bottom-to-top: text rotated 90° CCW (reads upward) |
"tbRl" | Top-to-bottom: text rotated 90° CW (reads downward) |
"lrTb" | Normal horizontal (default — also clears a previous direction) |
Conditional Table Formatting#
Conditional rules apply per-cell background colour, text colour, and bold weight based on the cell's value. Rules are evaluated in order; a later rule overrides an earlier one for the same cell. The header row is never subject to conditional formatting.
Pass rules via the :conditionalRules option in addTable():
| Rule key | Type | Description |
|---|---|---|
:col | Number | 1-based column index to test; 0 = test all columns |
:condition | String | "lt", "lte", "gt", "gte", "eq", "neq", "between", "contains" |
:value | Number / String | Threshold or comparison value |
:value2 | Number | Upper bound for "between" |
:bgColor | String | Background colour hex applied when rule matches |
:textColor | String | Text colour hex applied when rule matches (optional) |
:bold | true/false | Bold weight applied when rule matches (optional) |
doc.addTable(data, [
:headerRow = true,
:borderStyle = "single",
:colWidths = [4, 3, 3],
:conditionalRules = [
# Column 2 negative values → red background
[:col=2, :condition="lt", :value=0, :bgColor="FDECEA", :textColor="C00000"],
# Column 2 high values → green background
[:col=2, :condition="gte", :value=90, :bgColor="E2EFDA", :textColor="375623"],
# Any column containing "FAIL" → yellow + bold
[:col=0, :condition="contains", :value="FAIL", :bgColor="FFF2CC", :bold=true]
]
])