Skip to content

Implement conditional formatting#628

Open
ddimaria wants to merge 4 commits intotafia:masterfrom
ddimaria:implement-conditional-formatting
Open

Implement conditional formatting#628
ddimaria wants to merge 4 commits intotafia:masterfrom
ddimaria:implement-conditional-formatting

Conversation

@ddimaria
Copy link
Copy Markdown

Add conditional formatting support for XLSX

Adds read support for conditional formatting rules from XLSX worksheets. This parses <conditionalFormatting> elements and their child <cfRule> nodes, resolving differential format styles (dxf) into the existing Style type.

Supported rule types

Rule type Variant
Cell value comparison CellIs (all operators: equal, between, greaterThan, etc.)
Color scales ColorScale2, ColorScale3
Data bars DataBar (with fill and border colors)
Icon sets IconSet (all 20 built-in icon set types)
Top/Bottom N Top10 (rank, percent, bottom)
Above/below average AboveAverage (with std deviation support)
Text rules Text (contains, notContains, beginsWith, endsWith)
Time period TimePeriod (yesterday, today, last7Days, etc.)
Formula-based Expression
Blanks/errors ContainsBlanks, NotContainsBlanks, ContainsErrors, NotContainsErrors
Duplicates/uniques DuplicateValues, UniqueValues
Fallback Unknown (preserves raw type string)

API

Two new public methods on Xlsx<RS>:

  • worksheet_conditional_formatting(&mut self, name: &str) -- by sheet name
  • worksheet_conditional_formatting_at(&mut self, index: usize) -- by sheet index

Both return Vec<ConditionalFormatting>, where each block contains the target sqref range and a list of prioritized rules with resolved Style formats.

Changes

  • src/conditional_format.rs (new): Public data model -- ConditionalFormatting, ConditionalFormatRule, ConditionalFormatRuleType, and supporting enums (CfOperator, CfTextOperator, CfValueObject, IconSetType, TimePeriodType, etc.)
  • src/xlsx/mod.rs: Parses <dxfs> from styles.xml into differential format styles; adds parse_conditional_formattings and helpers (parse_cf_rules, parse_single_cf_rule, parse_color_scale, parse_data_bar, parse_icon_set, parse_cfvo) to walk the worksheet XML
  • src/lib.rs: Re-exports all new public types
  • tests/test.rs: 29 new tests covering every rule type variant, multi-rule blocks, stop_if_true, reversed icon sets, bottom-percent, std-dev below-average, unknown types, and dxf style resolution
  • tests/conditional_formatting.xlsx (new): Test fixture with 25 conditional formatting blocks

Example & Benchmarks

  • examples/conditional_formatting.rs: Demonstrates reading and printing all conditional formatting rules from a workbook
  • benches/conditional_formatting.rs: Criterion benchmarks for three scenarios:
    • cf_small_25_blocks -- real-world file with 25 CF blocks
    • cf_large_200_blocks_1000_rules -- synthetic file with 200 blocks / 1,000 rules
    • cf_large_with_dxf_access -- large file with dxf style resolution

Run with:

cargo bench --bench conditional_formatting

Test plan

  • All 29 new conditional formatting tests pass
  • Existing test suite unaffected
  • Example runs against test fixture: cargo run --example conditional_formatting
  • Benchmarks compile and run: cargo bench --bench conditional_formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant