Skip to content

AGGREGATE: an error k reached through a reference is #VALUE! - #4344

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/aggregate-k-reference-error
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/aggregate-k-reference-error

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4277

What was wrong

For AGGREGATE codes 14 to 19, subtotal in crates/calc/src/eval/reference_fns.rs read k with self.value, which dereferences a reference to the cell's value. An error in the k cell was then passed along as that error. Native Excel answers #VALUE! when k is an error reached through a reference: a cell, a defined name, INDEX or INDIRECT. It does so even when the data holds an error too. A computed error, such as NA() or E1+0, stays itself. The issue and its follow-up comment measure both kinds.

The cell-versus-value distinction only exists in calc: by the time aggregate_values_k in gridcraft-functions sees k, it's a plain value. So the check is in calc.

What changed

The k argument is evaluated with eval, and the code notes whether the result was a reference before dereferencing it. If it was a reference and the value is an error, AGGREGATE returns #VALUE! before reading the data. Everything else is unchanged: computed k values, numeric k, codes 1 to 13, SUBTOTAL, and the cell budget.

QA report

Main vs branch, end to end (gridcraft-cli run, real cells)

This is the issue's setup, plus its follow-up rows, built with cell.set and formulas.defineName, printed, saved to .xlsx, and reopened with gridcraft-cli cat. main is a gridcraft-cli built from origin/main (19f07f3a); the branch is this PR (97d8313c). The Excel column is native Excel 16.0 as reported in the issue and its comment; I didn't run Excel myself.

Setup:

  • A1:A4 = 10, 20, 30, 40
  • E1 = =NA(), E3 = =1/0, E4 = 2
  • C1:C2 = 10, =1/0
  • the name ErrorCell refers to =Sheet1!$E$1
Cell Formula main this PR Excel
G1 =AGGREGATE(14,6,A1:A4,E1) #N/A #VALUE! #VALUE! (issue)
G2 =AGGREGATE(15,6,A1:A4,E3) #DIV/0! #VALUE! #VALUE! (issue)
G3–G6 =AGGREGATE(16…19,6,A1:A4,E1) #N/A each #VALUE! each #VALUE! (issue: "all six codes 14–19")
G7 =AGGREGATE(14,6,A1:A4,ErrorCell) #N/A #VALUE! #VALUE! (comment)
G8 =AGGREGATE(15,6,A1:A4,INDEX(E3:E4,1)) #DIV/0! #VALUE! #VALUE! (comment)
G9 =AGGREGATE(14,6,A1:A4,INDIRECT("E1")) #N/A #VALUE! #VALUE! (comment)
G10 =AGGREGATE(14,4,C1:C2,E1) (data error too, errors kept) #DIV/0! #VALUE! #VALUE! (issue: "in the tested cases")
G11 =AGGREGATE(14,6,C1:C2,E1) (data error too, errors ignored) #N/A #VALUE! #VALUE! (issue)
G12 =AGGREGATE(14,6,A1:A4,E1+0) #N/A #N/A #N/A (comment)
G13 =AGGREGATE(14,6,A1:A4,NA()) #N/A #N/A #N/A (issue)
G14 =AGGREGATE(15,6,A1:A4,INDEX(E3:E4,1)+0) #DIV/0! #DIV/0! #DIV/0! (comment)
G15 =AGGREGATE(15,6,A1:A4,E4) (numeric k from a cell) 20 20 20 (comment)
G16 =AGGREGATE(14,6,A1:A4,2) 30 30
G17 =AGGREGATE(9,6,A1:A4,E1) (code 9: E1 is data, skipped) 100 100
G18 =LARGE(A1:A4,E1) (not AGGREGATE) #N/A #N/A

Both builds reopened their saved workbook with the same 18 values. gridcraft-cli check on the branch's file: 21 formula cells, 21 matched, round trip 0 of 1 files differ.

Test

tests::aggregate_k_error_through_a_reference_is_value (crates/calc/src/tests.rs) covers:

  • an error k through a cell (codes 14, 15 and 19), a defined name, INDEX and INDIRECT;
  • a data error alongside it, with errors both kept and ignored;
  • the computed-error controls E1+0, NA() and INDEX(…)+0;
  • numeric k, from a cell and typed.

The existing AGGREGATE tests in calc still pass, including the whole-column budget cases.

Negative control

With reference_fns.rs from origin/main and the new test kept (on top of 97d8313c, then restored with git checkout), the test fails at its first row: =AGGREGATE(14,6,A1:A4,E1), left Error(NA), right Error(Value).

Gate (macOS arm64)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-calc -p gridcraft-functions --all-targets -- -D warnings clean
cargo test -p gridcraft-calc -p gridcraft-functions -p gridcraft-engine 131 (+3 ignored) + 183 + 987 + 120 + 7 passed, 0 failed

Not covered

  • A multi-cell k range (AGGREGATE(14,6,A1:A4,E1:E2)) is AGGREGATE uses the first rank in a bare range instead of intersecting it with the formula cell #4276, about intersecting it with the formula's row. It isn't touched; a range that dereferences to an array goes through as before.
  • A computed error k with a data error and errors kept (AGGREGATE(14,4,C1:C2,NA())) still returns the data's error, as on main. The issue doesn't measure it.
  • Excel: the Excel column is the issue's and the comment's results; I didn't run Excel myself.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed AGGREGATE functions 14–19 so an error reached through a reference in the optional k argument returns #VALUE!. Errors produced by calculations in that argument continue to return their original error.

For codes 14 to 19, Excel answers #VALUE! when k is an error read from a cell, a name, INDEX or INDIRECT, ahead of any error in the data; a computed error such as NA() or E1+0 stays itself. GridCraft passed the cell's error through. Fixes storytold#4277.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

AGGREGATE functions 14–19 now return #VALUE! when the optional k argument resolves to an error through a reference. Computed errors remain unchanged. Regression tests cover referenced errors, computed errors, and valid k values.

Changes: AGGREGATE k error handling

Layer / File(s) Summary
Handle referenced errors in k
crates/calc/src/eval/reference_fns.rs, crates/calc/src/tests.rs
The functions evaluate and dereference the optional k argument. Referenced errors return #VALUE!, while computed errors are retained. Tests cover references through cells, defined names, INDEX, and INDIRECT, as well as computed errors and valid values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aiongg, vangel76

Merge Risk: Minimal · up to 97d83

AGGREGATE codes 14-19 now return #VALUE! when k is an error reached through a reference, which matches the linked issue. No merge-blocking risk was found, and the change is covered by a regression test.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the core behavior change: an error-valued k reached through a reference to AGGREGATE returns #VALUE!. This matches the raw summary (AGGREGATE 14-19, k dereferenced, #VALUE! for referen…
Linked Issues check Passed Issue #4277 requires AGGREGATE codes 14-19 to return #VALUE! when k is an error reached through a reference, including when the data also holds an error. The diff in crates/calc/src/eval/reference_fns…
Out of Scope Changes check Passed The whole-PR changes touch only crates/calc/src/eval/reference_fns.rs (the AGGREGATE k-argument path for codes 14-19) and crates/calc/src/tests.rs (the regression test). The reference_fns.rs hunk is l…
Full details: Title check

Explanation

The title names the core behavior change: an error-valued k reached through a reference to AGGREGATE returns #VALUE!. This matches the raw summary (AGGREGATE 14-19, k dereferenced, #VALUE! for referenced errors, computed errors unchanged) and the linked issue's described scope. The title is specific and concise, and it does not cover the test addition, which is expected.

Full details: Linked Issues check

Explanation

Issue #4277 requires AGGREGATE codes 14-19 to return #VALUE! when k is an error reached through a reference, including when the data also holds an error. The diff in crates/calc/src/eval/reference_fns.rs evaluates k with self.eval, records whether the result was a reference (Ev::R), and returns err(CellError::Value) before any data is read when that reference dereferences to an error. Computed errors (NA(), E1+0) take the non-reference path and keep their value, which matches the issue's comparison. Numeric k is passed through unchanged. The new test tests::aggregate_k_error_through_a_reference_is_value, as described in the PR, covers cell, defined name, INDEX and INDIRECT references, data-error cases with errors kept and ignored, and computed-error controls. I did not run the tests. The result rests on the diff and the PR's reported QA evidence. Multi-cell k ranges are excluded by the issue's stated scope and by the PR, so they are not required here.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes touch only crates/calc/src/eval/reference_fns.rs (the AGGREGATE k-argument path for codes 14-19) and crates/calc/src/tests.rs (the regression test). The reference_fns.rs hunk is limited to the k evaluation and its comment. Codes 1-13, SUBTOTAL, and the cell budget are not altered in the shown diff. No unrelated changes were identified.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 19f07f3 and 97d8313.

Files selected for processing: 2

  • crates/calc/src/eval/reference_fns.rs
  • crates/calc/src/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

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.

AGGREGATE returns the wrong error when k references an error cell

1 participant