Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/engine/src/cmd/data/grouping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,11 @@ pub(super) fn subtotal(s: &mut Session, p: &Json) -> Result<Json> {
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
Expand Down
28 changes: 28 additions & 0 deletions crates/engine/src/cmd/data/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading