From add4ebf5d8d70f3dd61e5147d09bf30a07b3a4a2 Mon Sep 17 00:00:00 2001 From: neilcawse <8085959+neilcawse@users.noreply.github.com> Date: Mon, 12 Oct 2026 19:13:33 -0400 Subject: [PATCH] PivotTables: custom subtotals on the first axis field keep the table's subtotal default on --- crates/engine/src/cmd/pivot.rs | 28 ++++++++++++++++++++++++++++ crates/xlsx/src/pivot.rs | 19 ++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/pivot.rs b/crates/engine/src/cmd/pivot.rs index 61e315608..ed1dadb6e 100644 --- a/crates/engine/src/cmd/pivot.rs +++ b/crates/engine/src/cmd/pivot.rs @@ -2666,6 +2666,34 @@ mod tests { assert!(warnings.iter().any(|w| w.contains("subtotal")), "{warnings:?}"); } + #[test] + fn custom_subtotals_on_the_first_field_keep_the_table_default() { + use std::io::Read; + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region", "Product"], "values": ["Units"]})).unwrap(); + s.execute("pivot.layout", json!({"layout": "tabular"})).unwrap(); + // Custom Sum subtotal on Region (cache field 0, the first axis field), no xpdl extension. + let (wb, _) = reopen_patched(&s, |x| { + assert!(!x.contains("pivotTableDefinition16"), "{x}"); + let start = x.find("").unwrap(); + let field = x[start..end] + .replacen("", ""); + assert!(field.contains("t=\"sum\""), "{field}"); + format!("{}{field}{}", &x[..start], &x[end..]) + }); + let p = wb.sheet(wb.active_sheet).unwrap().pivots.last().unwrap(); + assert!(p.subtotals); + assert_eq!([p.rows[0].subtotal, p.rows[1].subtotal], [None, None]); + // Saving doesn't add a table default of "off" the file never had. + let bytes = crate::io::save_bytes(&wb, "t.xlsx").unwrap(); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut xml = String::new(); + z.by_name("xl/pivotTables/pivotTable1.xml").unwrap().read_to_string(&mut xml).unwrap(); + assert!(!xml.contains("EnabledSubtotalsDefault=\"0\""), "{xml}"); + } + #[test] fn sort_scope_on_a_vanished_item_sorts_as_after_saving() { let mut s = sales(); diff --git a/crates/xlsx/src/pivot.rs b/crates/xlsx/src/pivot.rs index 5cd764d4b..06df26384 100644 --- a/crates/xlsx/src/pivot.rs +++ b/crates/xlsx/src/pivot.rs @@ -1327,6 +1327,17 @@ fn items_of(e: &El) -> Vec<&El> { e.child("items").map(|it| it.kids("item").filter(|i| i.attr("t").is_none_or(|t| t == "data")).collect()).unwrap_or_default() } +/// Whether a `` shows subtotals: custom subtotal functions (Field Settings › +/// Subtotals › Custom, `sumSubtotal` and the like, ECMA-376 §18.10.1.69) turn them on as +/// well as `defaultSubtotal`. +fn field_subtotals(e: &El) -> bool { + has_custom_subtotals(e) || e.flag("defaultSubtotal", true) +} + +fn has_custom_subtotals(e: &El) -> bool { + CUSTOM_SUBTOTALS.iter().any(|a| e.flag(a, false)) +} + /// The x14 element of an `` (e.g. `x14:pivotField`), if present. fn x14_ext<'a>(e: &'a El, uri: &str) -> Option<&'a El> { let ext = e.child("extLst")?.kids("ext").find(|x| x.attr("uri").is_some_and(|u| u.eq_ignore_ascii_case(uri)))?; @@ -1421,7 +1432,7 @@ fn read_table(cx: &mut Ctx<'_>, x: &El, cache: &CacheIn, id: u32) -> Option, x: &El, cache: &CacheIn, id: u32) -> Option