Data Bars and Color Scales
dataBar draws a bar in each cell of a numeric column, sized by the cell's value. colorScale shades each cell from a low color to a high one, like a heat map. Both are native Excel conditional formats: the file holds one rule per column, not a style per cell, and the bars and colors follow the values when someone edits the sheet. They need numbers: a column whose accessor or formula holds something else gets a type error on the option, and one whose transform returns a non-number throws when its rows are planned, naming the column.
import { createExcelSchema } from "typed-xlsx";
type RegionSales = { region: string; revenue: number; margin: number; growth: number };
const sales = createExcelSchema<RegionSales>()
.column("region", { header: "Region", accessor: "region" })
.column("revenue", { header: "Revenue", accessor: "revenue", format: "#,##0", dataBar: {} })
.column("margin", {
header: "Margin",
accessor: "margin",
format: "0%",
colorScale: { colors: "white-green", min: 0, max: 0.5 },
})
.column("growth", {
header: "Growth",
accessor: "growth",
format: "0%",
colorScale: { colors: "red-yellow-green", mid: 0 },
})
.build();
They cover the column's data rows. Title, header, summary and totals rows stay out of the range, so a total does not flatten the scale.
Data bars
dataBar: {} uses Excel's defaults: a solid blue bar, automatic ends, and negative values drawn in red on the other side of an axis.
import { createExcelSchema } from "typed-xlsx";
type Account = { name: string; balance: number; usage: number };
const accounts = createExcelSchema<Account>()
.column("name", { header: "Account", accessor: "name" })
.column("balance", {
header: "Balance",
accessor: "balance",
dataBar: { color: { rgb: "16A34A" }, negativeColor: { rgb: "DC2626" } },
})
.column("usage", {
header: "Usage",
accessor: "usage",
// A percentage: the bar is full at 100, whatever the highest value is.
dataBar: { min: 0, max: 100, barOnly: true },
})
.build();
| Option | Default | What it does |
|---|---|---|
color | theme dataBar (638EC6) | bar fill and border |
negativeColor | theme dataBarNegative (FF0000) | bars of negative values |
min | automatic: 0, or the lowest negative | value drawn as an empty bar |
max | automatic: the highest value | value drawn as a full bar |
gradient | false | true fades the fill to white, as in Excel 2007 |
barOnly | false | true hides the number and shows only the bar |
Excel 2010 and later draw the solid bar with its negative color and axis. Excel 2007, and readers that skip Excel 2010 extensions, draw a gradient bar in color instead.
Color scales
A color scale takes two colors (low and high) or three (low, middle and high). Without colors, it uses the theme's three colors, Excel's red, yellow and green.
import { createExcelSchema } from "typed-xlsx";
type Store = { store: string; returns: number; nps: number; stock: number };
const stores = createExcelSchema<Store>()
.column("store", { header: "Store", accessor: "store" })
// Fewer returns is better, so low values get green.
.column("returns", { header: "Returns", accessor: "returns", colorScale: { colors: "green-yellow-red" } })
// Net promoter score: red below 0, green above, yellow at 0.
.column("nps", { header: "NPS", accessor: "nps", colorScale: { mid: 0 } })
.column("stock", {
header: "Stock",
accessor: "stock",
colorScale: { colors: [{ rgb: "FFFFFF" }, { rgb: "2563EB" }], min: 0 },
})
.build();
| Option | Default | What it does |
|---|---|---|
colors | theme colorScaleLow, colorScaleMid, colorScaleHigh | a preset, or two or three { rgb } colors |
min | the lowest value in the column | value that gets the low color |
mid | the 50th percentile (three colors only) | value that gets the middle color |
max | the highest value in the column | value that gets the high color |
The presets are Excel's colors: "red-yellow-green", "green-yellow-red", "white-green", "white-red", "green-white" and "red-white". mid with two colors, min not below max, or a value that is not a finite number throws when the column is declared, naming it.
Empty cells and text get no color, so a column with gaps or merged sub-rows is shaded by its numbers only.
Numeric columns only
Bars and scales compare numbers, so TypeScript accepts them only on columns whose cells hold numbers: an accessor or transform that returns a number (null allowed), or a formula column. Other columns get the error on the key:
import { createExcelSchema } from "typed-xlsx";
type Invoice = { customer: string; amount: number; due: Date };
const invoices = createExcelSchema<Invoice>()
.column("customer", { header: "Customer", accessor: "customer", dataBar: {} })
.column("due", { header: "Due", accessor: "due", colorScale: {} })
.build();
Badge, checkbox, hyperlink, image and sparkline columns do not take them at all.
With conditional styles
A column can have conditional styles, a color scale and a data bar together. They are written in that order, so a conditional style's fill wins over the color scale, and the bar draws over either fill.
import { createExcelSchema } from "typed-xlsx";
const schema = createExcelSchema<{ amount: number }>()
.column("amount", {
accessor: "amount",
conditionalStyle: (rules) =>
rules.when(({ refs }) => refs.column("amount").lt(0), { font: { color: { rgb: "991B1B" } } }),
colorScale: { colors: "white-green" },
dataBar: {},
})
.build();
Theme colors
Data bars and color scales that set no color use six theme tokens: dataBar, dataBarNegative, dataBarAxis, colorScaleLow, colorScaleMid and colorScaleHigh. Their defaults are Excel's, so a file looks like one made in Excel unless a theme changes them:
import { createExcelSchema, spreadsheetThemes } from "typed-xlsx";
const brand = spreadsheetThemes.ocean.extend({
tokens: { colors: { dataBar: "0EA5E9", colorScaleHigh: "0EA5E9", colorScaleLow: "FFFFFF" } },
});
const schema = createExcelSchema<{ visits: number }>()
.column("visits", { accessor: "visits", dataBar: {} })
.theme(brand)
.build();
In Excel tables, streams and dynamic columns
- Excel tables (
mode: "excel-table") take the same options. The range is the table's data rows, not its totals row. - The streaming builder writes the same rules; the range ends at the last committed row.
- Each column of a dynamic block gets its own rule, so each month of a region × month grid is scaled on its own.