PivotTables: custom subtotals on the first axis field keep the table's subtotal default on

This commit is contained in:
neilcawse
2026-10-13 13:42:46 -04:00
parent 8fc6aa7082
commit add4ebf5d8
2 changed files with 42 additions and 5 deletions
+28
View File
@@ -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("<pivotField ").unwrap();
let end = start + x[start..].find("</pivotField>").unwrap();
let field = x[start..end]
.replacen("<pivotField ", "<pivotField defaultSubtotal=\"0\" sumSubtotal=\"1\" ", 1)
.replace("<item t=\"default\"/>", "<item t=\"sum\"/>");
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();
+14 -5
View File
@@ -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 `<pivotField>` 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 `<extLst><ext uri>` (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<Pivo
if let Some(f) = first_axis {
pt.subtotals = x14_ext(x, EXT_XPDL_TABLE)
.and_then(|e| e.attr("EnabledSubtotalsDefault"))
.map_or_else(|| f.flag("defaultSubtotal", true), |v| v == "1" || v.eq_ignore_ascii_case("true"));
.map_or_else(|| field_subtotals(f), |v| v == "1" || v.eq_ignore_ascii_case("true"));
pt.subtotals_top = f.flag("subtotalTop", true);
}
pt.grand_totals_rows = x.flag("colGrandTotals", true);
@@ -1471,13 +1482,11 @@ fn read_table(cx: &mut Ctx<'_>, x: &El, cache: &CacheIn, id: u32) -> Option<Pivo
};
by = auto_sort_scope(e);
f.caption = e.attr("name").filter(|n| !n.is_empty()).map(str::to_string);
// Custom subtotal functions (Field Settings › Subtotals › Custom, `sumSubtotal`
// and the like, ECMA-376 §18.10.1.69) also turn subtotals on.
let custom = CUSTOM_SUBTOTALS.iter().any(|a| e.flag(a, false));
let custom = has_custom_subtotals(e);
if custom {
cx.warn(format!("PivotTable field \"{name}\": custom subtotal functions are not supported; it shows the automatic subtotal"));
}
let own = custom || e.flag("defaultSubtotal", true);
let own = field_subtotals(e);
f.subtotal = (own != table_subtotals).then_some(own);
f.repeat_labels = x14_ext(e, EXT_X14_FIELD).is_some_and(|x| x.flag("fillDownLabels", false));
if group == PivotDateGroup::None {