# Conditional Formatting and Cell Styles

> Highlight values with data bars, color scales, icon sets and formula rules from the ribbon or addConditionalRule, manage rule priority and reuse cell styles.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Documents > Conditional Formatting and Cell Styles
- **2650 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/conditional-formatting/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/conditional-formatting/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/conditional-formatting/llms-full.txt

---

Data bars, color scales, icon sets and highlights are conditional formatting: they change the effective appearance of a cell when its value, or a supported formula, meets a rule. The rule is recalculated as the data changes. Conditional formatting is useful for finding exceptions, comparing values and showing progress without manually coloring each cell.

Named cell styles and workbook themes add the reusable base formatting that a rule draws on top of. The examples use the Performance worksheet of the sales sample, first in the ribbon and then in code.

### What the Rules Show

The **Performance** sheet in the [sales and dashboard sample](https://docs.textcontrol.com/tx-sheet-control/overview/sample-documents/llms-full.txt) compares six accounts:

![The Performance worksheet of six accounts with blue revenue data bars, green high-revenue highlights for Cedar Partners and Northstar Labs, green, yellow and red progress arrows and red-to-green margin color scales](https://docs.textcontrol.com/assets/img/tx-sheet-control/performance-sheet-rules.dbee5952.webp)

| Visual cue | What it means in this example |
|---|---|
| Blue bars in Revenue | Longer bars identify higher revenue in `B4:B9` |
| Green revenue highlights | Revenue is greater than $120,000 |
| Arrows in Progress | Green is at least 100% of target, yellow is at least 80%, red is below 80% |
| Red-to-green Margin scale | Low margins are red, intermediate margins yellow and high margins green within this range |

The values and formulas remain in the cells. Progress is Revenue divided by Target. The visual rules help a reader identify Northstar's high revenue and Pacific's low margin without changing either amount.

### Pick a Rule From the Home Ribbon

1. Select the cells the rule should evaluate, such as `B4:B9`.
2. Open **Home → Conditional Formatting**. If the ribbon is narrow, open the **Styles** group first.
3. Choose a value rule, data bar, color scale or icon set appropriate to the data.
4. Review its settings and apply it to the selection.

![The Conditional Formatting menu and its Data Bars submenu with gradient and solid fill presets over the account-performance worksheet, with the Revenue cells B4:B9 selected](https://docs.textcontrol.com/assets/img/tx-sheet-control/conditional-formatting-menu.1746ff09.webp)

The menu groups the rules by what they show:

- **Highlight Cells Rules** identifies values matching a criterion.
- **Top/Bottom Rules** identifies relative extremes.
- **Data Bars** encodes magnitude with length.
- **Color Scales** encodes the position of a value in a range.
- **Icon Sets** divides values into threshold bands.

Choose the boundaries deliberately. A built-in icon preset may use relative or percentage thresholds. For a business target, explicit numeric thresholds such as `0.8` and `1` are clearer than assuming that the green icon of a preset always means 100% of target.

### Create a High-Revenue Highlight

For the Performance example, highlight the accounts with more than $120,000 revenue:

1. Select the Revenue cells and choose **Highlight Cells Rules → Greater Than…**.
2. Confirm **Applies To**.
3. Enter `120000` as the rule value. The numeric threshold has no currency symbol: the cells already contain numeric amounts and have a currency display format.
4. Choose the green format.
5. Select **Apply Rule**.

The green highlights now identify Cedar Partners and Northstar Labs. Changing the revenue of an account below the threshold removes its matching conditional highlight, and its base formatting remains.

> Important
>
> **Strict Comparison.** Values equal to 120000 do not match a strict greater-than condition.

### Preview a Rule Before Applying

The **New Formatting Rule** dialog keeps **Rule Type** and **Applies To** above its collapsible settings. **Rule Criteria**, **Appearance** and **Thresholds** show the controls that apply to the chosen rule. Opening one section closes the previous section while the dialog header, preview and actions stay in place.

![New Formatting Rule with data-bar thresholds and a live three-value formatting preview](https://docs.textcontrol.com/assets/img/tx-sheet-control/conditional-formatting-rule.8f7a079d.webp)

The **Live Preview** renders representative data bars, color scales or icons using the draft settings; value and expression formats preview their text appearance. The samples help compare colors and thresholds before **Apply Rule** writes the rule. **Cancel** discards the draft. Preview samples do not replace testing actual boundary values in the target range.

### Manage Overlapping Rules

Choose **Conditional Formatting → Manage Rules…** to review the rules and their target ranges:

![Conditional Formatting Rules with scope selection, a scrollable priority list, per-rule format samples and edit, delete and reorder actions](https://docs.textcontrol.com/assets/img/tx-sheet-control/conditional-rules-manager.4e7808ca.webp)

- **Scope.** Use the scope selector to inspect the selection or the worksheet.
- **Applies To.** Check **Applies To** before editing a rule.
- **Edit and delete.** The edit and delete actions target a particular rule.
- **Priority.** The up and down actions change the priority.
- **Stop If True.** Where available, **Stop If True** prevents lower-priority rules from contributing when that rule matches.

The manager shares the fixed dialog heading and footer. Its rule list scrolls while the scope selector and **New Rule…** / **Close** actions remain available. Each row supplies a format sample, so the manager does not need a separate preview panel. Commands update the worksheet rules; **Close** finishes the review.

Overlapping rules can supply different aspects of the appearance. In the example, Revenue can have a data bar and a green highlight together. If the result is unexpected, inspect the rule order and scope before changing the direct fill of the cell. Clearing a conditional rule and clearing the content of a cell are different operations.

### Define the Same Rules in Code

The following code adds a data bar to the existing Performance sheet. Select the owning worksheet before you use these workbook-level rule methods. `editor` is the [`Spreadsheet`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/spreadsheet/llms-full.txt) of the browser editor:

```javascript
const sheet = editor.workbook.sheets.get('Performance');
editor.ui.selectSheet(sheet);
editor.workbook.addConditionalRule({
    type: 'dataBar', ranges: ['B4:B9'],
    color: '#638ec6', gradient: false
});
```

The code works as follows:

- [`Workbook.sheets`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/sheets-property/llms-full.txt) and [`SheetCollection.get`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/sheetcollection/get-method/llms-full.txt) find the worksheet `Performance` by its case-insensitive name. `get` throws when the sheet is absent.
- [`editor.ui.selectSheet`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/editorui/selectsheet-method/llms-full.txt) selects the worksheet, because the rule methods of the workbook work on the active sheet.
- [`Workbook.addConditionalRule`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/addconditionalrule-method/llms-full.txt) adds a rule of the type `dataBar` to `B4:B9`, with the bar color `#638ec6` and no gradient, and returns the stable ID of the rule.

[`listConditionalRules(range)`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/listconditionalrules-method/llms-full.txt) returns detached rule definitions. Use [`updateConditionalRule`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/updateconditionalrule-method/llms-full.txt) and [`deleteConditionalRule`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/deleteconditionalrule-method/llms-full.txt) with the stable ID of a rule to maintain it. Consult [`ConditionalRule`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/conditionalrule/llms-full.txt) for the payload of each kind: data bars, color scales, icons, expression rules and value rules have different fields.

### Reuse Formatting With Named Styles and Themes

[`listNamedStyles()`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/listnamedstyles-method/llms-full.txt) enumerates the workbook styles. [`createCellStyle`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/createcellstyle-method/llms-full.txt), [`updateCellStyle`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/updatecellstyle-method/llms-full.txt) and [`applyCellStyle`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/applycellstyle-method/llms-full.txt) provide reusable base formatting. Updating a shared named style affects its users, so choose meaningful names and review the affected cells.

Workbook theme colors, set with [`Workbook.setTheme`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/settheme-method/llms-full.txt), coordinate cells, charts and supported document formatting. Changing a workbook theme is separate from switching the light or dark interface of the application, which [Appearance and Localization](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/appearance/llms-full.txt) describes.

Table styles remain attached to their tables. A conditional rule supplies its calculated appearance on top of the applicable base formatting.

### Which Formatting Mechanism to Use

| Mechanism | What it does |
|---|---|
| Named cell style | Supplies reusable base formatting |
| Table style | Formats an operational table |
| Conditional formatting | Calculates an effective appearance from a rule |

Choose the mechanism that matches the requirement rather than replacing a dynamic rule with manual colors. A shared named style is useful for a report-wide heading change. It does not replace a rule that must react when an amount crosses a threshold.

### Review and Test Rules

- **Review rules as a whole.** Keep the target range, thresholds, priority and expected result together when you review rules.
- **Test the boundaries.** Test exact boundary values as well as typical values, and test blanks, errors and overlapping rules.
- **Check the output.** Review the exported PDF as well as the grid.
- **XLSX.** XLSX carries supported native conditional definitions. Specialized imported rules may require preservation rather than editing.

### Formula Rules and Pattern Fills

Excel expression rules use the shared calculation engine and retain rule priority and Stop If True. Within a conditional expression, relative named formulas are resolved from the current cell; ordinary API-defined names retain their fixed-reference behavior. Conditional formatting takes precedence over direct and table formatting.

Rule `formatting` accepts text and fill colors, font family and point size, emphasis, border edges, numeric format codes and `fillPattern`. A solid conditional fill replaces a lower-priority hatch. The cell's stored content and base formatting stay unchanged. Canvas and PDF use the same resolved styles.

Choose **Custom Format** in the rule's **Appearance** section to edit **Pattern Style** and **Pattern Color**. Leave **Use Fill Color** off to draw the pattern over the cell's existing background, or enable it to supply a background as well. **None** explicitly removes the matching cell's pattern when selected. The live preview and rule-manager samples show the pattern; the cell's base formatting stays unchanged.

For example, the following expression rule marks positive amounts with a diagonal hatch:

```javascript
editor.workbook.addConditionalRule({
    type: 'expression',
    ranges: ['B4:B9'],
    formula: '=B4>0',
    formatting: { fillPattern: { type: 'lightUp', color: '#735773' } }
});
```

### Next Steps

- [Format Cells Dialog](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/format-cells/llms-full.txt): set the base formatting of a selection, section by section.
- [Tables](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/tables/llms-full.txt): format an operational table with a table style.
- [Formatting and Number Display](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formatting/llms-full.txt): format ranges in code and choose number formats.
- [XLSX Compatibility](https://docs.textcontrol.com/tx-sheet-control/document-processing/xlsx-compatibility/llms-full.txt): validate how a workbook with rules survives the exchange as XLSX.

---

## Nearby

- **Previous:** [Formatting and Number Display](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formatting/llms-full.txt)
- **Next:** [Formulas](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formulas/llms-full.txt)
