Merge pull request #4383 from Hamza0101/fix/table-hidden-total-row-keeps-calc

XLSX: a hidden Total Row keeps its calculations
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 19:43:58 -05:00
commit fc7698eb11
2 files changed
+89 -19

No files matched your search

+19 -19
View File
@@ -142,27 +142,27 @@ pub(super) fn table_xml(t: &Table, cols: &[String], name: &str, id: u32, out: &m
}
let _ = write!(s, " name=\"{}\"", esc_attr(&encode_xstring(name)));
let totals = col.map(|c| c.totals).unwrap_or_default();
if t.totals_row {
let f = match totals {
TotalsFn::None => None,
TotalsFn::Average => Some("average"),
TotalsFn::Count => Some("count"),
TotalsFn::CountNums => Some("countNums"),
TotalsFn::Max => Some("max"),
TotalsFn::Min => Some("min"),
TotalsFn::Sum => Some("sum"),
TotalsFn::StdDev => Some("stdDev"),
TotalsFn::Var => Some("var"),
TotalsFn::Custom => Some("custom"),
};
if let Some(f) = f {
let _ = write!(s, " totalsRowFunction=\"{f}\"");
} else if let Some(l) = col.and_then(|c| c.totals_label.as_ref()) {
let _ = write!(s, " totalsRowLabel=\"{}\"", esc_attr(l));
}
// A hidden Total Row keeps each column's calculation, as Excel's files do: turning the row
// back on restores it (#4296). The reader takes them whether the row is shown or not.
let f = match totals {
TotalsFn::None => None,
TotalsFn::Average => Some("average"),
TotalsFn::Count => Some("count"),
TotalsFn::CountNums => Some("countNums"),
TotalsFn::Max => Some("max"),
TotalsFn::Min => Some("min"),
TotalsFn::Sum => Some("sum"),
TotalsFn::StdDev => Some("stdDev"),
TotalsFn::Var => Some("var"),
TotalsFn::Custom => Some("custom"),
};
if let Some(f) = f {
let _ = write!(s, " totalsRowFunction=\"{f}\"");
} else if let Some(l) = col.and_then(|c| c.totals_label.as_ref()) {
let _ = write!(s, " totalsRowLabel=\"{}\"", esc_attr(l));
}
let calc = col.and_then(|c| c.formula.as_ref()).filter(|f| !f.is_empty());
let custom = if t.totals_row && totals == TotalsFn::Custom { col.and_then(|c| c.totals_label.as_ref()) } else { None };
let custom = if totals == TotalsFn::Custom { col.and_then(|c| c.totals_label.as_ref()) } else { None };
let xml = col.and_then(|c| c.xml.as_ref());
if calc.is_none() && custom.is_none() && xml.is_none() {
s.push_str("/>");
+70
View File
@@ -1407,6 +1407,76 @@ fn table_newline_column_and_backslash_name_survive() {
assert!(gridcraft_formula::parse("COUNTA(\\_Prime.1[Name])").is_ok(), "backslash table name parses as a structured reference");
}
/// #4296: a table whose Total Row is turned off keeps each column's calculation in the file, as
/// Excel writes it, so turning the row back on in Excel restores a custom formula or Average.
#[test]
fn hidden_total_row_keeps_its_calculations() {
let col = |name: &str, totals: TotalsFn, label: Option<&str>| TableColumn {
name: name.into(),
totals,
totals_label: label.map(str::to_string),
formula: None,
unique_name: None,
xml: None,
};
let mut wb = Workbook::new();
let s = wb.sheet_mut(0).unwrap();
s.tables.push(Table {
id: 1,
name: "Table1".into(),
range: rr("A1:C3"),
header_row: true,
totals_row: false,
columns: vec![
col("Group", TotalsFn::None, Some("Total")),
col("Amount", TotalsFn::Custom, Some("SUM(B2:B3)+10")),
col("Mean", TotalsFn::Average, None),
],
style: "TableStyleMedium2".into(),
banded_rows: true,
banded_cols: false,
first_col: false,
last_col: false,
filter_button: true,
filter: vec![],
table_type: None,
connection_id: None,
});
let bytes = write_xlsx(&wb).unwrap();
let mut xml = String::new();
zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap().by_name("xl/tables/table1.xml").unwrap().read_to_string(&mut xml).unwrap();
// Still a table without a total row: no totalsRowCount, and its range unchanged.
assert!(xml.contains(r#"ref="A1:C3""#) && xml.contains(r#"totalsRowShown="0""#) && !xml.contains("totalsRowCount"), "{xml}");
for part in [
r#"name="Group" totalsRowLabel="Total"/>"#,
r#"name="Amount" totalsRowFunction="custom"><totalsRowFormula>SUM(B2:B3)+10</totalsRowFormula></tableColumn>"#,
r#"name="Mean" totalsRowFunction="average"/>"#,
] {
assert!(xml.contains(part), "{part}: {xml}");
}
let (back, report) = read_xlsx(&bytes).unwrap();
assert!(report.warnings.is_empty(), "{:?}", report.warnings);
let t = &back.sheet(0).unwrap().tables[0];
assert!(!t.totals_row);
assert_eq!(t.range.a1(), "A1:C3");
let got: Vec<_> = t.columns.iter().map(|c| (c.totals, c.totals_label.clone())).collect();
assert_eq!(got, vec![(TotalsFn::None, Some("Total".into())), (TotalsFn::Custom, Some("SUM(B2:B3)+10".into())), (TotalsFn::Average, None)]);
// A table that never had a total row writes no calculation.
let mut plain = wb.clone();
plain.sheet_mut(0).unwrap().tables[0].columns =
vec![col("Group", TotalsFn::None, None), col("Amount", TotalsFn::None, None), col("Mean", TotalsFn::None, None)];
let mut xml = String::new();
zip::ZipArchive::new(std::io::Cursor::new(write_xlsx(&plain).unwrap()))
.unwrap()
.by_name("xl/tables/table1.xml")
.unwrap()
.read_to_string(&mut xml)
.unwrap();
for attr in ["totalsRowFunction", "totalsRowLabel", "totalsRowFormula"] {
assert!(!xml.contains(attr), "{attr}: {xml}");
}
}
/// A table that is only a header row and a totals row (Insert Table on one row, then Total
/// Row) is written like the header-only and one-data-row shapes, not dropped on save.
#[test]