|
| 1 | +# Conditional Formatting |
| 2 | + |
| 3 | +Apply formatting rules that change cell appearance based on values. |
| 4 | + |
| 5 | +## Reading Conditional Formats |
| 6 | + |
| 7 | +```python |
| 8 | +from excelbench_rust import UmyaBook |
| 9 | + |
| 10 | +book = UmyaBook.open("dashboard.xlsx") |
| 11 | +rules = book.read_conditional_formats("Sheet1") |
| 12 | +for r in rules: |
| 13 | + print(f"{r['ranges']}: {r['type']}") |
| 14 | +# ['A1:A100']: cellIs |
| 15 | +# ['B1:B100']: colorScale |
| 16 | +``` |
| 17 | + |
| 18 | +## Writing Conditional Formats |
| 19 | + |
| 20 | +### Cell Value Rules |
| 21 | + |
| 22 | +```python |
| 23 | +book = UmyaBook() |
| 24 | +book.add_sheet("Sales") |
| 25 | + |
| 26 | +# Highlight cells greater than 1000 |
| 27 | +book.add_conditional_format("Sales", { |
| 28 | + "ranges": ["B2:B50"], |
| 29 | + "type": "cellIs", |
| 30 | + "operator": "greaterThan", |
| 31 | + "formula": "1000", |
| 32 | + "format": {"bg_color": "#C6EFCE", "font_color": "#006100"}, # green |
| 33 | +}) |
| 34 | + |
| 35 | +# Highlight cells below target |
| 36 | +book.add_conditional_format("Sales", { |
| 37 | + "ranges": ["B2:B50"], |
| 38 | + "type": "cellIs", |
| 39 | + "operator": "lessThan", |
| 40 | + "formula": "500", |
| 41 | + "format": {"bg_color": "#FFC7CE", "font_color": "#9C0006"}, # red |
| 42 | +}) |
| 43 | + |
| 44 | +book.save("output.xlsx") |
| 45 | +``` |
| 46 | + |
| 47 | +### Color Scales |
| 48 | + |
| 49 | +```python |
| 50 | +# 2-color scale (red to green) |
| 51 | +book.add_conditional_format("Sales", { |
| 52 | + "ranges": ["C2:C50"], |
| 53 | + "type": "colorScale", |
| 54 | + "color_scale": { |
| 55 | + "min_color": "#FF0000", |
| 56 | + "max_color": "#00FF00", |
| 57 | + }, |
| 58 | +}) |
| 59 | + |
| 60 | +# 3-color scale (red / yellow / green) |
| 61 | +book.add_conditional_format("Sales", { |
| 62 | + "ranges": ["D2:D50"], |
| 63 | + "type": "colorScale", |
| 64 | + "color_scale": { |
| 65 | + "min_color": "#FF0000", |
| 66 | + "mid_color": "#FFFF00", |
| 67 | + "max_color": "#00FF00", |
| 68 | + }, |
| 69 | +}) |
| 70 | +``` |
| 71 | + |
| 72 | +### Data Bars |
| 73 | + |
| 74 | +```python |
| 75 | +book.add_conditional_format("Sales", { |
| 76 | + "ranges": ["E2:E50"], |
| 77 | + "type": "dataBar", |
| 78 | + "data_bar": {"color": "#638EC6"}, |
| 79 | +}) |
| 80 | +``` |
| 81 | + |
| 82 | +## Rule Types |
| 83 | + |
| 84 | +| Type | Description | Key fields | |
| 85 | +|------|-------------|-----------| |
| 86 | +| `cellIs` | Compare cell value | `operator`, `formula`, `format` | |
| 87 | +| `colorScale` | Gradient fill | `color_scale` with 2-3 colors | |
| 88 | +| `dataBar` | In-cell bar chart | `data_bar` with color | |
| 89 | +| `top10` | Top/bottom N | `rank`, `percent`, `bottom` | |
| 90 | +| `containsText` | Text matching | `text`, `format` | |
| 91 | + |
| 92 | +## Rule Priority |
| 93 | + |
| 94 | +!!! info "Evaluation order" |
| 95 | + When multiple rules apply to the same cell, rules are evaluated |
| 96 | + in the order they were added. The first matching rule determines |
| 97 | + the formatting. This matches Excel's "Stop if True" default behavior. |
0 commit comments