Skip to content

Add Excel-compatible nested SUBTOTAL handling#1

Draft
tomtomau wants to merge 1 commit into
developfrom
feature/nested-subtotal
Draft

Add Excel-compatible nested SUBTOTAL handling#1
tomtomau wants to merge 1 commit into
developfrom
feature/nested-subtotal

Conversation

@tomtomau

Copy link
Copy Markdown

Summary

Makes SUBTOTAL ignore cells within its reference range that themselves contain a SUBTOTAL formula, preventing double-counting of nested subtotals — matching Excel and Google Sheets behaviour.

Previously SUBTOTAL dispatched to the same aggregation helpers as SUM/AVERAGE/etc. and treated nested SUBTOTAL cells like any other value, so an outer subtotal spanning sub-group subtotals double-counted them.

Approach

  • A skipSubtotalCells flag is threaded through the shared aggregation chain (do*reduce/reduceAggregateevaluateRangegetRangeValues), defaulting to false so SUM/AVERAGE/COUNT/etc. are unchanged.
  • getRangeValues() and the single-cell-reference path skip cells where a new isSubtotalCell() helper detects a top-level SUBTOTAL call in the cell's formula AST (canonical name comparison, so i18n-safe).
  • Subtotal aggregation uses a dedicated range-cache key (SUBTOTAL_<fn>) so it never collides with a plain aggregation over the same range.

Applies to both the 1–11 and 101–111 code families. Hidden-row handling (the practical difference between those families) remains out of scope, as the engine is headless and has no row-visibility concept.

Tests

New spec test/unit/interpreter/function-subtotal.spec.ts covering: baseline parity with SUM, nested exclusion for sum/average/count, the 1xx code family, SUM-vs-SUBTOTAL cache independence, single-cell references, and recalculation on edit.

  • npx jest function-subtotal → 8/8 pass
  • smoke suite → pass; npm run lint → 0 errors; npm run compile → clean

SUBTOTAL now ignores cells within its reference range that themselves
contain a SUBTOTAL formula, preventing double-counting of nested
subtotals to match Excel and Google Sheets.

A skipSubtotalCells flag is threaded through the shared aggregation
helpers so regular SUM/AVERAGE/COUNT behaviour is unchanged, and
subtotal results use a dedicated range-cache key to avoid collisions
with plain aggregations over the same range.
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