From 7f277ad679db441d80cd3362486a403681e11b55 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 17 Nov 2026 13:34:34 +0500 Subject: [PATCH] Data Subtotal: the grand total ends at the last data row, as in Excel The grand total's SUBTOTAL ran to the row above it, the last group's total, as =SUBTOTAL(9,B2:B6). Excel writes =SUBTOTAL(9,B2:B5), ending at the last group's last data row. A value typed over the last group's total then reached GridCraft's grand total (160) and not Excel's (60), while one typed over any other group's total reaches both. Fixes #4268 --- crates/engine/src/cmd/data/grouping.rs | 5 ++++- crates/engine/src/cmd/data/tests.rs | 28 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/data/grouping.rs b/crates/engine/src/cmd/data/grouping.rs index 1de23a4b8c..7d069279aa 100644 --- a/crates/engine/src/cmd/data/grouping.rs +++ b/crates/engine/src/cmd/data/grouping.rs @@ -279,8 +279,11 @@ pub(super) fn subtotal(s: &mut Session, p: &Json) -> Result { let lbl = CellRef::new(last, group_col).a1(); s.execute("home.insertRows", json!({"sheet": sheet, "rows": format!("{}:{}", last + 1, last + 1)}))?; s.execute("cell.set", json!({"sheet": sheet, "cell": lbl, "input": format!("Grand {label}")}))?; + // The grand total's range ends at the last group's last data row, above that group's total, + // as Excel writes it: a value typed over that total doesn't reach the grand total, as a value + // typed over any other group's total doesn't either (SUBTOTAL skips only nested SUBTOTALs). for c in &cols { - let range = RangeRef::new(CellRef::new(first, *c), CellRef::new(last - 1, *c)).a1(); + let range = RangeRef::new(CellRef::new(first, *c), CellRef::new(last - 2, *c)).a1(); s.execute("cell.set", json!({"sheet": sheet, "cell": CellRef::new(last, *c).a1(), "input": format!("=SUBTOTAL({code},{range})")}))?; } // One outer group over every data and subtotal row, summarised by the grand total, as in diff --git a/crates/engine/src/cmd/data/tests.rs b/crates/engine/src/cmd/data/tests.rs index 524591c946..b9fa1845a3 100644 --- a/crates/engine/src/cmd/data/tests.rs +++ b/crates/engine/src/cmd/data/tests.rs @@ -31,6 +31,34 @@ fn subtotal_honours_header_false() { assert_eq!(get(&mut s, "B7")["value"], json!(100.0), "the grand total counts every data row"); } +/// #4268: the grand total's range ends at the last data row, as Excel writes it, so a value +/// typed over the last group's total doesn't change the grand total, as one typed over any other +/// group's total doesn't. +#[test] +fn subtotal_grand_total_ends_at_the_last_data_row() { + // (function, SUBTOTAL code, label, grand total, after B6 = 100, after B6 = =50+50, after B4 = 100) + for (function, code, label, steps) in + [("sum", 9, "Total", [60.0, 60.0, 60.0, 160.0]), ("count", 3, "Count", [3.0, 3.0, 3.0, 4.0]), ("max", 4, "Max", [30.0, 30.0, 30.0, 100.0])] + { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Group", "Amount"], ["A", 10], ["A", 20], ["B", 30]]})).unwrap(); + s.execute("data.subtotal", json!({"range": "A1:B4", "groupBy": "A", "columns": ["B"], "function": function})).unwrap(); + let get = |s: &mut Session, a: &str| s.execute("cell.get", json!({"cell": a})).unwrap(); + assert_eq!(get(&mut s, "A7")["text"], json!(format!("Grand {label}"))); + assert_eq!(get(&mut s, "B4")["formula"], json!(format!("=SUBTOTAL({code},B2:B3)")), "{function}"); + assert_eq!(get(&mut s, "B6")["formula"], json!(format!("=SUBTOTAL({code},B5)")), "{function}"); + assert_eq!(get(&mut s, "B7")["formula"], json!(format!("=SUBTOTAL({code},B2:B5)")), "{function}"); + assert_eq!(get(&mut s, "B7")["value"], json!(steps[0]), "{function}"); + // A value over the last group's total doesn't reach the grand total, as one over the + // first group's total does (it isn't a nested SUBTOTAL any more), in both apps. + for ((cell, input), want) in [("B6", "100"), ("B6", "=50+50"), ("B4", "100")].into_iter().zip(&steps[1..]) { + s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap(); + assert_eq!(get(&mut s, "B7")["value"], json!(want), "{function}: {cell} = {input}"); + } + } +} + #[test] fn subtotal_puts_summary_rows_below_their_detail() { let mut s = Session::new();