Skip to content

Functions: MODE/VALUE/SUMPRODUCT text args, ADDRESS [Book]Sheet, Excel-style lookup bisection, SUBTOTAL skips filtered rows - #419

Merged
h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
neilcawse:fix/function-fixes
Oct 11, 2026
Merged

h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
neilcawse:fix/function-fixes

Conversation

@neilcawse

@neilcawse neilcawse commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Function fixes found by comparing GridCraft against Excel-cached formula results in test workbooks. One PR because they came out of the same comparison run; the parts are independent and grouped below.

MODE / MODE.SNGL / MODE.MULT (crates/functions/src/stat.rs)

  • New mode_numbers: inside multi-cell ranges and arrays only numbers count (as before), but direct text (even numeric text like "1") and a single-cell reference to text or a blank cell give #VALUE!. Arguments are read left to right, so the first error wins (MODE(1,1,#DIV/0!,"1") is #DIV/0!, MODE("1",1,#DIV/0!) is #VALUE!).

VALUE (crates/functions/src/text.rs)

  • VALUE("") and whitespace-only text are now #VALUE! (they returned 0). A blank cell still reads as 0.

SUMPRODUCT (crates/functions/src/math.rs)

  • Text in a scalar argument (a literal or a single-cell reference) is #VALUE!. Text inside arrays and multi-cell ranges still counts as 0.

ADDRESS (crates/functions/src/lookup.rs)

  • A [Book]Sheet sheet_text is judged by its parts, so ADDRESS(2,3,3,TRUE,"[Book1]Sheet1") gives [Book1]Sheet1!$C2 instead of '[Book1]Sheet1'!$C2. Names with spaces are still quoted ('[My Book]Sheet1'!$A$1, 'EXCEL SHEET'!R2C3).

Approximate match: VLOOKUP / HLOOKUP / LOOKUP / MATCH (crates/functions/src/lookup.rs)

  • bsearch_last now bisects the whole vector the way Excel does, instead of first filtering to entries of the needle's type and searching those. A probe of a different type moves to the nearest same-type entry below it in the window (or the window moves up past it). An equal probe ends the search, then the run of equal entries after it is taken, so sorted duplicates give the last one. On unsorted, mixed-type data the answer now matches Excel's (e.g. VLOOKUP(534, {"INTEGRAL";0;1;2;534;9999999999;-9999999999}, …) finds 534). No allocation; linear work in the worst case, including long runs of trailing blanks.

XLOOKUP / XMATCH binary search, search_mode ±2 (crates/functions/src/lookup.rs)

  • Binary search compares entries across types in Excel's sort order (numbers < text < logicals < errors, reversed for descending data, blanks always last) instead of skipping entries of other types. New binary_order, bsearch_first and bsearch_upto use partition_point. Observed case: XLOOKUP(46523, {9700;"xyz";"xyz";160726;204100;510300}, rates, 0, 1, 2) returns the row of the first "xyz" (0.22).

SUBTOTAL leaves out filtered rows (crates/calc, crates/model, crates/xlsx, crates/engine)

  • SUBTOTAL with function numbers 1–11 now leaves out rows hidden by a filter (rows hidden by hand still count). 101–111 behave as before (all hidden rows left out). AGGREGATE is unchanged.
  • New Sheet::is_row_filtered: a hidden row is "filtered" when it is inside the sheet AutoFilter while that has criteria, or inside the data range of a table whose AutoFilter has criteria.
  • To tell filtered table rows apart, Table gets filter: Vec<(u32, FilterCriterion)> (#[serde(default)]). XLSX reads and writes the table <autoFilter> criteria through the existing read_autofilter / autofilter_xml. Before this they were dropped on load, leaving only the filter button. Tables created by Insert ▸ Table and the Forecast Sheet start with no criteria.
  • New FilterCriterion::Icon { set, id } for <iconFilter>, read and written on table and sheet AutoFilters. apply_filter does not evaluate icons: rows keep their current visibility.
  • table.header off and toggling a table's filter button off now clear the table's criteria.

Tests

  • functions::stat::tests::mode_scalar_arguments: 8 cases × MODE and MODE.SNGL, plus MODE.MULT.
  • functions::text::tests::value_observed: 5 cases.
  • functions::math::tests::sumproduct_scalar_arguments: 7 cases.
  • functions::lookup::tests::shape_and_address: 4 new ADDRESS cases.
  • functions::lookup::tests::approximate_match_follows_bisection: VLOOKUP/HLOOKUP/LOOKUP on the mixed-type test data, sorted duplicates, descending MATCH -1, text-only data, 10,000 trailing blanks.
  • functions::lookup::tests::xlookup_binary_search_observed: 5 XLOOKUP cases with search_mode 2 / -2.
  • calc::tests::subtotal_skips_filtered_rows: filtered table, filtered sheet AutoFilter, a row hidden by hand outside both, SUBTOTAL 9/109/2 and AGGREGATE.
  • xlsx::tests::roundtrip::full_roundtrip: the sample table now has a values criterion and an icon criterion, and they must survive save and reload.

Expected values are Excel results cached in the test workbooks, or come from Microsoft's documentation examples (ADDRESS) where the test says so.

Commands run:

cargo fmt --all -- --check
cargo check --workspace --all-targets
cargo clippy -p gridcraft-functions -p gridcraft-calc -p gridcraft-model -p gridcraft-xlsx -p gridcraft-engine --all-targets -- -D warnings
cargo test -p gridcraft-functions -p gridcraft-calc -p gridcraft-model -p gridcraft-xlsx -p gridcraft-engine
cargo xtask parity --check

All pass.

The mismatches were found with the gridcraft-cli check oracle subcommand (#416, separate PR). This PR does not need that code.

Docs

  • docs/function-parity.md: header Change: line and a history row. No function counts change.
  • docs/file-format-parity.md: header Change: line and a history row for table AutoFilter criteria and icon filters.

The header lines and top history rows of both docs will conflict with sibling PRs that touch them (#418, #421, #417 for function-parity.md; #423, #422, #417 for file-format-parity.md), so a rebase will be needed. Any PR that adds a Table { … } literal also needs filter: vec![] once this lands (for example the table test in #423).

Not done

  • Icon filters are kept but not evaluated: re-applying a filter leaves rows under an icon criterion as they are.
  • is_row_filtered treats every hidden row inside a filtered range as filtered. A row hidden by hand inside an active filter range counts as filtered.

Unverified: needs confirmation in Excel

  • XLOOKUP/XMATCH binary search with logicals, errors or blanks mixed into the data follows the sort order above but was only checked against Excel with numbers and text.
  • The ADDRESS [My Book]Sheet1 case (quoted) follows the sheet-name quoting rule. No Excel result was observed for it.

…ook]Sheet, Excel-style lookup bisection, SUBTOTAL skips filtered rows (table filter criteria kept)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Merged — thank you for the contribution! If you haven't already, please join our Discord: https://discord.gg/artcraft

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.

3 participants