diff --git a/crates/engine/src/cmd/data/grouping.rs b/crates/engine/src/cmd/data/grouping.rs index 03761b454..1de23a4b8 100644 --- a/crates/engine/src/cmd/data/grouping.rs +++ b/crates/engine/src/cmd/data/grouping.rs @@ -283,5 +283,8 @@ pub(super) fn subtotal(s: &mut Session, p: &Json) -> Result { let range = RangeRef::new(CellRef::new(first, *c), CellRef::new(last - 1, *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 + // Excel: outline level 1 then shows only the grand total, and level 2 the subtotals. + s.execute("data.group", json!({"sheet": sheet, "rows": format!("{}:{}", first + 1, last)}))?; Ok(json!({"groups": inserted})) } diff --git a/crates/engine/src/cmd/data/tests.rs b/crates/engine/src/cmd/data/tests.rs index e073e23a5..524591c94 100644 --- a/crates/engine/src/cmd/data/tests.rs +++ b/crates/engine/src/cmd/data/tests.rs @@ -40,8 +40,8 @@ fn subtotal_puts_summary_rows_below_their_detail() { s.execute("data.subtotal", json!({"range": "A1:B4", "groupBy": "A", "columns": ["B"]})).unwrap(); let sh = s.doc().unwrap().wb.active().unwrap().clone(); assert!(sh.summary_below, "the totals sit below their groups"); - // East's detail is rows 2–3 and its total row 4: collapsing level 1 keeps the total showing. - s.execute("data.showLevels", json!({"rows": 1})).unwrap(); + // East's detail is rows 2–3 and its total row 4: showing level 2 keeps the total showing. + s.execute("data.showLevels", json!({"rows": 2})).unwrap(); let hidden = |s: &Session, row: u32| s.doc().unwrap().wb.active().unwrap().is_row_hidden(row); assert!(hidden(&s, 1) && hidden(&s, 2), "East's detail rows are hidden"); assert!(!hidden(&s, 3), "East Total stays visible"); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f000eb9fe..33f3de875 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1759,7 +1759,7 @@ fn subtotal_with_a_sheet_prefix_works_on_the_named_sheet() { assert_eq!(col(prices, 0), ["Item", "a", "a", "a Total", "b", "b Total", "Grand Total"].map(Value::from)); assert_eq!(col(prices, 1)[1..], [1.0, 2.0, 3.0, 4.0, 4.0, 7.0].map(Value::from)); assert!(wb.styles.get(prices.style_id(CellRef::new(3, 0))).font.bold); - assert_eq!(prices.rows.get(&1).map(|l| l.outline), Some(1)); + assert_eq!(prices.rows.get(&1).map(|l| l.outline), Some(2)); let sheet1 = wb.sheet(wb.sheet_index("Sheet1").unwrap()).unwrap(); assert_eq!(col(sheet1, 0)[..4], ["Name", "x", "x", "y"].map(Value::from)); assert!(sheet1.value(CellRef::new(4, 0)).is_empty()); @@ -1767,6 +1767,24 @@ fn subtotal_with_a_sheet_prefix_works_on_the_named_sheet() { assert!(s.execute("data.group", json!({"sheet": "Prices"})).is_err()); } +/// #3945: Excel groups every data and subtotal row under the grand total, so the details are at +/// level 2, the subtotals at level 1, and showing level 1 leaves only the grand total. +#[test] +fn subtotal_groups_everything_under_the_grand_total() { + let mut s = s(); + 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": "sum"})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + let levels: Vec = (0..7).map(|r| sh.rows.get(&r).map_or(0, |l| l.outline)).collect(); + assert_eq!(levels, [0, 2, 2, 1, 2, 1, 0]); + let shown = |s: &Session| -> Vec { (0..7).filter(|&r| !s.doc().unwrap().wb.active().unwrap().is_row_hidden(r)).map(|r| r + 1).collect() }; + s.execute("data.showLevels", json!({"rows": 1})).unwrap(); + assert_eq!(shown(&s), [1, 7]); + s.execute("data.showLevels", json!({"rows": 2})).unwrap(); + assert_eq!(shown(&s), [1, 4, 6, 7]); + assert_eq!(v(&s, "B7"), Value::Number(60.0)); +} + #[test] fn formatting_and_styles() { let mut s = s();