Loading documentation…
Loading documentation…
A conditional format is an ordered rule on a sheet: when a cell in the rule's range matches the predicate, the rule's CellStyle paints over the cell's own style.
{ }, { range: scores, when: { kind: "formula", source: "=B1>=90" }, style: { backgroundColor: "#dcfce7" } }, { },]);| A | B | |
|---|---|---|
| 1 | Ana | 92 |
| 2 | Ben (late) | 48 |
| 3 | Chen | 75 |
| 4 | Dee |
import {} from "@sheetwrite/core";import "@sheetwrite/core/styles.css";
if (host === null) throw new Error("Missing #grid host");
// First rule wins, and a match stops the later rules for that cell. { when: { kind: "lessThan", value: 50 }, }, // Relative references move with each cell of the range: B1, B2, B3, B4. { range: scores, when: { kind: "formula", source: "=B1>=90" }, style: { backgroundColor: "#dcfce7" } }, { },];
{ ], }, ],}; score: [92, 48, 75, null], },};
The score 92 paints on a green fill, 48 paints in bold red, 75 has no rule style, and the blank score paints on a yellow fill. "Ben (late)" is italic.
To replace the rules of the active sheet at runtime, call grid.setConditionalFormats(rules). It commits one setSheetMeta operation with the new list:
{ when: { kind: "greaterThan", value: 100 }, },]);ConditionalFormatRule:
| Field | Type | Effect |
|---|---|---|
range | Range | The cells the rule checks. It must be on the sheet that holds the rule and inside the sheet's rows and columns. |
when | ConditionalFormatPredicate | The condition. |
style | CellStyle | The style that paints when the condition matches. |
stopIfTrue | boolean | When the rule matches a cell, later rules are not checked for that cell. |
Rules live in Sheet.conditionalFormats (and SheetSnapshot.conditionalFormats). A setSheetMeta operation with patch.conditionalFormats replaces the list.
ConditionalFormatPredicate:
kind | Fields | Matches when |
|---|---|---|
"greaterThan" | value: number | The cell holds a number greater than value. |
"lessThan" | value: number | The cell holds a number less than value. A blank cell does not match. |
"equal" | value: number | string | null | The cell holds the same number, or exactly the same text (case-sensitive). value: null matches blank cells. |
"contains" | text: string, matchCase?: boolean | The cell holds text that contains text. The match ignores case unless matchCase is true. An empty text matches nothing. |
"formula" | source: string | The formula is true for the cell. source starts with =. |
A formula predicate is anchored at the top-left cell of the rule's range. Relative A1 references move with each target cell; $-absolute parts stay fixed. Formula rules are calculated again when the cells they depend on change.
Sheetwrite checks rules in array order.
stopIfTrue: true keeps every later rule out of that cell's match set.For example, if rule 1 sets { color: "#111111", bold: true } and rule 2 sets { color: "#222222", italic: true }, and both match, the cell paints #111111, bold and italic. With stopIfTrue on rule 1, the cell paints #111111 and bold only.
Export writes each rule as a native conditional-format record with a differential style (dxf). The array position sets the rule priority, and stopIfTrue writes stopIfTrue="1".
| Sheetwrite predicate | XLSX rule |
|---|---|
greaterThan, lessThan, equal | cellIs with the same operator |
equal with value: null | containsBlanks |
contains | containsText |
contains with matchCase: true | expression: ISNUMBER(FIND("text", anchor)) |
formula | expression (without the leading =) |
Import reads the same rule types back, in priority order. An expression rule of the form ISNUMBER(FIND("text", …)) imports as a case-sensitive contains.
Vertical alignment in a differential style maps as described in Fonts and alignment.
MAX_CONDITIONAL_FORMAT_RULES, the size of the renderer's u32 match mask). Snapshot validation reports more as a resource-limit error at sheets[n].conditionalFormats.MAX_CONDITIONAL_FORMAT_FORMULA_LENGTH).=, or stopIfTrue is not a boolean.format-loss warning: color scales, data bars, icon sets, extended conditional formatting, rules with an invalid priority or differential style, formulas longer than 8,192 characters, formulas with external references, and rules past the 32-rule limit. For color scales, data bars, icon sets and other unsupported rule types, the warning names the rule type. Unrepresentable differential styles do not enter the live model or the renderer.format-loss warning.stopIfTrue. A file saved by LibreOffice loses it; this is a recorded LibreOffice difference.CellStyle.ConditionalFormatRule, ConditionalFormatPredicate and MAX_CONDITIONAL_FORMAT_RULES API reference.