Skip to content

Add INDIRECT function (HF-27) - #1843

Open
Tobiadefami wants to merge 17 commits into
developfrom
feature/hf-27-phase2
Open

Tobiadefami wants to merge 17 commits into
developfrom
feature/hf-27-phase2

Conversation

@Tobiadefami

@Tobiadefami Tobiadefami commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Context

The PR adds the INDIRECT function, without making it volatile.

  • Why: INDIRECT reads an address known only at evaluation time. The parser cannot add that dependency to the graph.

  • Approach: each formula keeps the cells and ranges INDIRECT read in its last evaluation as dependencies in the graph, so an edit re-runs only the formulas that read what changed. If a target isn't computed yet in the current pass, the formula stops, the sort computes the target first, then runs the formula again from the start. I built and measured two alternatives and dropped them:

    • a volatile INDIRECT, as in Excel: every edit re-runs every INDIRECT formula;
    • computing the target in the middle of the formula's own evaluation: it nests one evaluation inside another, so long chains of INDIRECT need a depth limit to stay within the JavaScript stack.
  • What changed:

    • IndirectPlugin.ts: resolves A1 and R1C1 text on any sheet:
      • cells, ranges, whole columns and rows ("B:B", "2:3", "C2" and "R2" in R1C1), and named expressions;
      • the corners of a range can come in any order;
      • text past the sheet limits returns #REF!, including a row or column number too large to be a number;
      • trailing spaces and line feeds in the text are ignored.
    • DependencyGraph.ts, FormulaVertex.ts, Graph.ts: store each cell or range that INDIRECT read as a runtime dependency of the formula. Reading a range again doesn't revisit its cells.
    • DependencyGraph.ts, dependencyTransformers/: row, column, cell and sheet-merge changes move the recorded addresses with the cells, so an empty cell that loses its last reader is removed.
    • TopSort.ts, Evaluator.ts, EvaluationPass.ts, EvaluationDeferred.ts:
      • If the target is not computed yet, the formula defers. The sort computes the target first, then retries the formula.
      • An evaluation that requested a target is retried even if a function caught the deferral and returned a value, such as a custom function with a try/catch.
      • A range's cells are checked for values not computed yet once per evaluation pass, not on every read.
    • SimpleRangeValue.ts: an INDIRECT range records its dependency on first use of its values or address.
    • InformationPlugin.ts, FormulaTextPlugin.ts, Interpreter.ts, FunctionPlugin.ts: ROW, COLUMN, ROWS, COLUMNS, SHEET, SHEETS, FORMULATEXT and ISFORMULA take a reference returned by INDIRECT like the same reference typed into the formula, without reading its cells or recording them as dependencies, so a formula inside the range it measures is no cycle. Other functions return values: a range returned by ARRAYFORMULA is not a reference, as in Google Sheets.
    • namedReference.ts: resolves a named cell or range from the name's formula, for INDIRECT and for ROW and COLUMN.
    • Operations.ts, GraphBuilder.ts: sheet, name and structure changes mark INDIRECT formulas dirty.
    • lookup-and-reference.ts: the catalogue entry for INDIRECT.
    • functionCapabilities.ts: INDIRECT in the fun:lookup.c license group, with the other lookup and reference functions.
    • i18n/languages/: the name of INDIRECT in every language pack, from Microsoft's function pages for each language.
    • Docs: an INDIRECT section in the known limitations, INDIRECT among the functions that depend on the sheet structure, and the dependency-collection row of the list of differences. Developer docs: runtime dependencies and the retry in DEPENDENCY-GRAPH.md; referenceExpected and the structure flag in INTERPRETER.md.
  • Result: =INDIRECT("B"&A1) recalculates when A1 or the target changes, and not on unrelated edits.

  • Side effect on existing functions: ROW and COLUMN of a named cell or range, and ROWS and COLUMNS of a named whole column or row or of a whole column or row returned by ARRAYFORMULA, now give the same result as the literal reference:

    Formula (myRange = Sheet1!$B$2:$C$3, myCell = Sheet1!$B$5, myCol = Sheet1!$A:$A) Before Now Literal reference
    =ROW(myRange) #VALUE! 2 2
    =ROW(myCell) #N/A 5 5
    =ROWS(myCol) rows holding data maxRows maxRows
    =ROWS(ARRAYFORMULA(A:A)) rows holding data maxRows maxRows

How did you test your changes?

Paired branch in hyperformula-tests: feature/hf-27-phase2 (handsontable/hyperformula-tests#104).

  • npx jest on the INDIRECT specs:
    • specs: function-indirect.spec.ts, function-indirect-whole-columns-rows.spec.ts, indirect-dependency-graph.spec.ts, graph.spec.ts, address-representation-converters.spec.ts;
    • neighbouring specs: the ROW, COLUMN, ROWS and COLUMNS specs;
    • result: 392 passed.
  • Full Jest suite with the private tests: 591 suites, 17,248 passed, 3 skipped.
  • Browser run (npm run test:browser): 17,199 of 17,248 run in Chrome and in Firefox, none failed (49 skipped).
  • Mutation testing of the changes for whole columns and rows: 83% of non-equivalent mutants killed by the INDIRECT and neighbouring specs. Tests were then added for the surviving cases.
  • npm run lint (no errors), npm run verify:typings, npm run docs:generate-function-docs (INDIRECT listed), git diff --check.

AI assistance

Claude Code wrote the implementation, docs and tests, and ran the Excel and Google Sheets checks.

Compatibility with Excel and Google Sheets

Excel: Microsoft Excel Online. Google Sheets: checked only for ranges returned by ARRAYFORMULA (below the table).

Data on the formula's sheet: B1 = 5, C1 = 11, B2 = 7, C2 = 13, B8 = 1.333, and nothing else in column C or row 1. Sheet2!A1 = 42, 'My Sheet'!A1 = 7. Names: George refers to B8, Konst to =5.

Formula Excel Google Sheets HyperFormula
=INDIRECT("B1") 5 – 5
=INDIRECT(F21), where F21 holds the text B1 5 – 5
=INDIRECT("'My Sheet'!A1") 7 – 7
=SUM(INDIRECT("B1:C2")) 36 – 36
=SUM(INDIRECT("C2:B1")) 36 – 36
=INDIRECT("George") 1.333 – 1.333
=INDIRECT("Konst") #REF! – #REF!
=SUM(INDIRECT("C:C")) 24 – 24
=SUM(INDIRECT("1:1")) 16 – 16
=ROWS(INDIRECT("C:C")) 1048576 – 40000
=SUM(INDIRECT("R1C2:R2C3", FALSE())) 36 – 36
=INDIRECT("B1 ") 5 – 5
=INDIRECT("B0") #REF! – #REF!
=INDIRECT(1/0) #DIV/0! – #DIV/0!
=SUM(INDIRECT("A:A")), entered in column A 0 – #CYCLE!
  • Excel was measured in Polish, where R1C1 text uses W for the row and K for the column, so the R1C1 row's Excel value is for W1K2:W2K3. HyperFormula reads R and C in every language (see the known limitations).
  • ROWS: HyperFormula's sheet has 40,000 rows by default (maxRows).
  • A formula that reads its own column is a cycle in HyperFormula, as it is for the same reference typed into the formula (see the known limitations and the list of differences).

Functions that take a reference, with A1 = =1+1, A2 = x and a sheet named Sheet2:

Formula Excel HyperFormula
=SHEET(INDIRECT("Sheet2!A1")) 2 2
=SHEETS(INDIRECT("A1")) 1 1
=FORMULATEXT(INDIRECT("A1")) =1+1 =1+1
=FORMULATEXT(INDIRECT("A2")) #N/A #N/A
=ISFORMULA(INDIRECT("A1")) TRUE TRUE
=ISFORMULA(INDIRECT("A0")) #REF! #REF!
=FORMULATEXT(INDIRECT("A1")), entered in A1 its own formula its own formula

Google Sheets treats a range returned by ARRAYFORMULA as a value: ROW, SHEET, SHEETS, FORMULATEXT and ISFORMULA of ARRAYFORMULA(range) return an error there and in HyperFormula, and ROWS and COLUMNS of ARRAYFORMULA(A:A) and ARRAYFORMULA(1:1) count the whole sheet in both.

Public API changes

  • The new INDIRECT function.
  • FunctionPlugin.runFunctionWithReferenceArgument takes an optional last argument, referenceExpected, default false. ROW, COLUMN, SHEET, SHEETS, FORMULATEXT and ISFORMULA set it. A custom function that takes a reference keeps reading an INDIRECT range's cells and recording them as dependencies.

Out of scope and follow-ups

Types of changes

  • Breaking change (existing functionality no longer works as before) – agreed with the maintainers
  • New feature or improvement (a non-breaking change that adds functionality)
  • Bug fix (a non-breaking change that fixes an issue)
  • Additional language file, or a change to an existing language file (translations)
  • Change to the documentation

Related issues

  1. Addresses Add support for INDIRECT #1260 (task HF-27).

Checklist

🤖 Generated with Claude Code

Tobiadefami and others added 3 commits October 24, 2026 15:40
INDIRECT resolves its text at evaluation time and records the cell it reads as a
runtime dependency in the graph, so it is not volatile. When the target is not
current yet in the running pass, the evaluation is deferred: the sort computes the
target first, with an explicit stack, and then retries the formula.

Covers A1 references to a single cell, on the same or another sheet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
INDIRECT now accepts an A1 range, a named expression and R1C1 text (absolute,
relative, ranges, sheet-qualified). A range is read lazily: its cells become a
runtime dependency of the formula, through the range vertex, the first time the
range's values or address are used, so ROWS and COLUMNS read only its size.
Changes to named expressions mark INDIRECT formulas for recalculation.

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

- Moving a cell that an INDIRECT formula read marks that formula dirty: the
  address in its text does not move with the cell, so it reads the address again.
- A formula whose result is an INDIRECT range records the range as its dependency
  while it is evaluated, like a literal range; a cell that later reads the stored
  range no longer reads cells that are not computed yet.
- Range and empty cell vertices that lose their last runtime dependent are removed
  after the evaluation, not during it, while the sort may still hold them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
hyperformula-docs ff9828f Commit Preview URL

Branch Preview URL
Nov 28 2026, 01:06 PM

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Performance comparison of head (ff9828f) vs base (ce5e52f)

                                     testName |    base |    head |  change
---------------------------------------------------------------------------
                                      Sheet A |  406.35 |  398.46 |  -1.94%
                                      Sheet B |  132.64 |  130.73 |  -1.44%
                                      Sheet T |  113.04 |  113.78 |  +0.65%
                                Column ranges |   419.9 |  412.67 |  -1.72%
                                Sorted lookup | 12465.5 | 12891.6 |  +3.42%
Sheet A:  change value, add/remove row/column |   15.65 |   13.86 | -11.44%
 Sheet B: change value, add/remove row/column |  114.29 |  115.54 |  +1.09%
                   Column ranges - add column |  127.55 |  128.98 |  +1.12%
                Column ranges - without batch |  405.43 |   436.6 |  +7.69%
                        Column ranges - batch |  101.51 |  110.52 |  +8.88%

Tobiadefami and others added 14 commits October 26, 2026 20:59
INDIRECT strips a trailing run of spaces and line feeds from ref_text before
parsing it, in A1 and R1C1 notation and for names. Other whitespace still
makes the text invalid: a trailing tab, carriage return or no-break space,
and any leading or inner space give #REF!.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…f references

- INDIRECT reads whole columns and rows, in A1 notation ("B:B", "B:D", "2:3",
  "Sheet2!A:A") and in R1C1 notation ("C2", "C[-1]:C2", "R2"), with the
  corners in any order. Past maxRows or maxColumns, or with a row or column
  number too large to be a number, it returns #REF!.
- ROW, COLUMN, ROWS and COLUMNS use the position or the size of a reference
  returned by INDIRECT without reading its cells or recording them as
  dependencies, so a formula inside the range it measures is no cycle. A named
  range, a named whole column or row, and a range returned by ARRAYFORMULA now
  give the same result as the literal range, e.g., ROW of a named range gives
  its top row instead of #VALUE!.
- FunctionPlugin.runFunctionWithReferenceArgument takes an optional
  referenceExpected argument. Only ROW and COLUMN set it, so a custom function
  that takes a reference still reads an INDIRECT range's cells and records
  them as dependencies.
- A range returned by INDIRECT checks its cells for values not computed yet
  once per evaluation pass, not on every read.
- Trailing spaces and line feeds are removed with a loop instead of a regular
  expression, which took quadratic time on a long run of spaces followed by
  another character.
- The catalogue entry for INDIRECT describes ranges, whole columns and rows,
  named expressions and R1C1 notation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Names from Microsoft's function pages and the lookup and reference function
list for each language.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Known limitations: what INDIRECT can't do (R1C1 letters, spill and
  external references, ranges that don't spill, own-range cycles).
- List of differences: INDIRECT's dependencies are collected at evaluation.
- Volatile functions: INDIRECT depends on the sheet structure, which now
  includes adding or renaming a sheet and changing named expressions.
- Developer docs: runtime dependencies in the dependency graph, the
  referenceExpected argument, and why plugins must not catch
  EvaluationDeferred.
- Changelog: INDIRECT, and ROW, COLUMN, ROWS and COLUMNS of named ranges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The edge is removed during evaluation; unused vertices are removed later,
by removeUnusedRuntimeVertices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tion caught the signal

A custom function that wrapped its work in a try/catch could catch
EvaluationDeferred and return a value, which the cell then kept until a
rebuild. The evaluator now discards the value of an evaluation that
requested a cell, and retries the formula once that cell is computed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It was in the ungrouped list, so only fun:all or fun:INDIRECT granted it.
It now comes with fun:lookup.c, like ADDRESS and INDEX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	CHANGELOG.md
#	hyperformula/src/interpreter/plugin/InformationPlugin.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	CHANGELOG.md
#	hyperformula/src/interpreter/plugin/InformationPlugin.ts
Marking a volatile array formula as volatile when the sheet is built is a
separate engine fix, not part of INDIRECT. The structure-change flag stays:
INDIRECT needs it for array formulas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Don't revisit the cells of a range vertex the graph already has when a
  formula reads the range again.
- Keep the recorded addresses of runtime dependencies current through row,
  column, cell and sheet-merge changes, and forget a formula once it
  leaves the graph, so an empty cell vertex that loses its last reader is
  removed.
- Point runtime dependencies to the range vertex that a removed range is
  merged into.
- SHEET, SHEETS, FORMULATEXT and ISFORMULA take INDIRECT's reference like a
  reference typed into the formula. Only INDIRECT's result counts as a
  reference; a range returned by ARRAYFORMULA is a value, as in Google
  Sheets. ROW and COLUMN read a named cell or range from the name's
  formula (namedReference.ts, shared with INDIRECT).
- Docs: the license-key placeholder in the INDIRECT snippets;
  DEPENDENCY-GRAPH.md and INTERPRETER.md updated; changelog.
- Remove runtimeEdgesAdded, TopSortResult.deferred and the TOP_SORT stat,
  which nothing used, and move two JSDoc blocks back to their
  declarations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Tobiadefami
Tobiadefami marked this pull request as ready for review November 28, 2026 13:45
@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.27586% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.78%. Comparing base (ce5e52f) to head (ff9828f).

Files with missing lines Patch % Lines
...yperformula/src/DependencyGraph/DependencyGraph.ts 97.09% 5 Missing ⚠️
hyperformula/src/EvaluationPass.ts 96.49% 2 Missing ⚠️
...erformula/src/interpreter/plugin/IndirectPlugin.ts 98.36% 2 Missing ⚠️
hyperformula/src/DependencyGraph/Graph.ts 93.75% 1 Missing ⚠️
hyperformula/src/DependencyGraph/TopSort.ts 99.01% 1 Missing ⚠️
...c/dependencyTransformers/RenameSheetTransformer.ts 80.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1843      +/-   ##
===========================================
+ Coverage    97.76%   97.78%   +0.02%     
===========================================
  Files          212      216       +4     
  Lines        19159    19728     +569     
  Branches      4540     4658     +118     
===========================================
+ Hits         18730    19292     +562     
  Misses         429      429              
- Partials         0        7       +7     
Files with missing lines Coverage Δ
hyperformula/src/DependencyGraph/FormulaVertex.ts 87.59% <100.00%> (+2.85%) ⬆️
hyperformula/src/Evaluator.ts 100.00% <100.00%> (ø)
hyperformula/src/GraphBuilder.ts 100.00% <100.00%> (ø)
hyperformula/src/Operations.ts 99.12% <100.00%> (+<0.01%) ⬆️
hyperformula/src/SimpleRangeValue.ts 100.00% <100.00%> (ø)
...src/dependencyTransformers/MoveCellsTransformer.ts 100.00% <100.00%> (ø)
...rformula/src/dependencyTransformers/Transformer.ts 97.46% <100.00%> (+0.09%) ⬆️
hyperformula/src/i18n/languages/csCZ.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/daDK.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/deDE.ts 100.00% <ø> (ø)
... and 33 more

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@handsontable-pr-review handsontable-pr-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

High risk: Rewrites the core recalculation engine (TopSort, Evaluator, DependencyGraph) to support runtime dependencies, so any defect affects every formula recalculation, cycle handling, and structural-change propagation across the whole library.

Full review: no issues found.

Want a deeper review? Comment /review improve on its own line: a full review of the latest commit with an extra fix-oriented pass (about 2x the cost).

This branch has not been deployed

No deployments
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