Skip to content

AGGREGATE: an option outside 0 to 7 is #VALUE! - #4347

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/aggregate-invalid-options
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/aggregate-invalid-options

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4270

What was wrong

subtotal in crates/calc/src/eval/reference_fns.rs read AGGREGATE's options argument as n.trunc() as u32:

  • 8 and 9 were accepted and matched none of the option rules, so AGGREGATE returned a plain result.
  • −0.5 truncated to 0.
  • −1 saturated to 0 in the cast.

Native Excel returns #VALUE! for any option outside 0 to 7, as the issue shows. That covers a typed value, text such as "8", and a value from a cell. A fraction inside the range still truncates (0.5, 6.9, 7.1 and 7.9 match in the issue).

What changed

An option is accepted when 0 ≤ n < 8 and then truncated as before; anything else returns #VALUE!. Errors in the option argument still propagate. SUBTOTAL, which has no options argument, isn't touched.

QA report

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

The cells are built with cell.set, 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 (ceec99d7). The Excel column is the issue's native Excel 16.0 result; I didn't run Excel myself.

Setup: A1:A2 = 10, 20; C1 = −1, C2 = 8, C3 = 7; D1 = =SUBTOTAL(9,A1:A2), a genuine nested subtotal.

Cell Formula main this PR Excel (issue)
E1 =AGGREGATE(9,8,A1:A2) 30 #VALUE! #VALUE!
E2 =AGGREGATE(9,7,A1:A2) 30 30 30
E3 =AGGREGATE(9,9,A1:A2) 30 #VALUE! #VALUE!
E4 =AGGREGATE(9,-1,A1:A2) 30 #VALUE! #VALUE!
E5 =AGGREGATE(9,-0.5,A1:A2) 30 #VALUE! #VALUE!
E6 =AGGREGATE(9,"8",A1:A2) 30 #VALUE! #VALUE!
E7 =AGGREGATE(9,C1,A1:A2) (−1 from a cell) 30 #VALUE! #VALUE!
E8 =AGGREGATE(9,C2,A1:A2) (8 from a cell) 30 #VALUE! #VALUE!
E9–E12 =AGGREGATE(9,0.5…), 6.9, 7.1, 7.9 30 each 30 each match (issue)
E13 =AGGREGATE(9,8,A1:A2,D1) (with a nested subtotal) 60 #VALUE! #VALUE! (issue: SUM form over a nested subtotal)
E14 =AGGREGATE(9,0,A1:D1) (option 0 skips the nested D1) 9 9
E15 =AGGREGATE(9,4,A1:D1) (option 4 keeps it) 39 39
E16 =AGGREGATE(14,8,A1:A2,1) 20 #VALUE! #VALUE! (issue: LARGE form)
E17 =AGGREGATE(14,-1,A1:A2,1) 20 #VALUE! #VALUE! (issue: LARGE form)
E18 =AGGREGATE(14,7,A1:A2,1) 20 20

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

With gridcraft-cli eval:

Formula main this PR
=AGGREGATE(9,8,{10,20}) 30 #VALUE!
=AGGREGATE(9,-0.5,{10,20}) 30 #VALUE!
=AGGREGATE(4,8,{10,20}) 20 #VALUE!
=AGGREGATE(9,7.9,{10,20}) 30 30
=AGGREGATE(9,8) / =AGGREGATE(99,8,{10,20}) #VALUE! / #VALUE! the same
=SUBTOTAL(9,{10,20}) 30 30

Test

tests::aggregate_refuses_options_outside_0_to_7 (crates/calc/src/tests.rs) checks:

  • options 8, 9, −1, −0.5 and "8", and −1 and 8 from cells, with SUM and with LARGE: all #VALUE!;
  • options 7, 7.9, 0.5, 6.9 and 0, and 7 from a cell: the plain result.

Negative control

With the range check removed (on top of ceec99d7, then restored with git checkout), the test fails at =AGGREGATE(9,8,A1:A2): left Number(30.0), right Error(Value).

Gate (macOS arm64)

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

Not covered

Summary by CodeRabbit

  • Bug Fixes
    • Corrected validation of the AGGREGATE function’s options. Values from 0 through 7 are accepted after truncation, while values outside that range now return #VALUE!. This also applies to negative fractions and invalid text or cell-referenced values.

The option was truncated and cast to an unsigned number, so 8, 9, -1 and -0.5 were accepted and a number came back. Excel answers #VALUE! for any option outside 0 to 7, while a fraction inside it (7.9) still truncates. Fixes storytold#4270.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

AGGREGATE now checks that its numeric options are at least 0 and less than 8 before truncating them. Tests cover out-of-range and text options, plus fractional options within the accepted range.

Changes: AGGREGATE option validation

Layer / File(s) Summary
Validate options and test results
crates/calc/src/eval/reference_fns.rs, crates/calc/src/tests.rs
The evaluator returns #VALUE! for numeric options outside [0, 8) before truncation. Tests cover out-of-range and text options, cell-referenced values, and in-range fractional options.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, vangel76, dylanmtaylor

Merge Risk: Minimal · up to ceec9

AGGREGATE now returns #VALUE! for options outside 0–7, as requested in the linked issue, and keeps in-range fractional behavior. The change is small, tested, and low risk to merge.

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 states the main behavior change: AGGREGATE now returns #VALUE! for an option outside the accepted range. The raw summary and objectives confirm that the change adds option range validation i…
Linked Issues check Passed [#4270] requires AGGREGATE to return #VALUE! for options outside 0 to 7, for numbers, text, and cell references, and to keep in-range fractions (0.5, 6.9, 7.1, 7.9) working. The diff to crates/calc/sr…
Out of Scope Changes check Passed The whole-PR change set touches two files: crates/calc/src/eval/reference_fns.rs (the option guard, plus an explanatory comment) and crates/calc/src/tests.rs (the new regression test). Both directly i…
Full details: Title check

Explanation

The title states the main behavior change: AGGREGATE now returns #VALUE! for an option outside the accepted range. The raw summary and objectives confirm that the change adds option range validation in Evaluator::subtotal (reference_fns.rs) and a test covering options outside 0 to 7. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

[#4270] requires AGGREGATE to return #VALUE! for options outside 0 to 7, for numbers, text, and cell references, and to keep in-range fractions (0.5, 6.9, 7.1, 7.9) working. The diff to crates/calc/src/eval/reference_fns.rs adds a guard Ok(n) if (0.0..8.0).contains(&amp;n) =&gt; n.trunc() as u32 and Ok(_) =&gt; return err(CellError::Value) before the cast. This rejects 8, 9, -1, -0.5 and NaN. Text such as "8" and cell values go through the same arg_num path, so they are also rejected. Errors from arg_num still propagate through the Err(e) arm. The guard runs before the function-code dispatch, so it covers both SUM and LARGE forms. The PR adds tests::aggregate_refuses_options_outside_0_to_7 in crates/calc/src/tests.rs, which covers these cases per the PR description and change summary. I did not run the test. The guard is the coding change the issue requires. Unresolved: the precedence of errors when function_num and options are both invalid is not tested, and the issue does not require a specific precedence.

Full details: Out of Scope Changes check

Explanation

The whole-PR change set touches two files: crates/calc/src/eval/reference_fns.rs (the option guard, plus an explanatory comment) and crates/calc/src/tests.rs (the new regression test). Both directly implement or verify the [#4270] requirement. The SUBTOTAL path has no options argument and the diff leaves it unchanged. The diff does not change the PR #2796 data-argument handling for functions 1 to 13, which the issue marks as a separate problem. No unrelated changes were found in the reviewed diff.

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 ef99f1e and ceec99d.

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; 2 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 numbers for invalid options

1 participant