Merge pull request #4184 from ZIDAN44/fix/3945-subtotal-grand-total-outline

engine: Subtotal groups every row under the grand total (#3945)
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 10:25:43 +01:00
commit 1476d4e7ab
3 files changed
+24 -3

No files matched your search

+3
View File
@@ -283,5 +283,8 @@ pub(super) fn subtotal(s: &mut Session, p: &Json) -> Result<Json> {
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}))
}
+2 -2
View File
@@ -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");
+19 -1
View File
@@ -1797,7 +1797,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());
@@ -1805,6 +1805,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<u8> = (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<u32> { (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();