From fb2b27b1f02b603d696480d03345827e8b44124a Mon Sep 17 00:00:00 2001 From: FredApps Date: Mon, 16 Nov 2026 16:50:42 +0100 Subject: [PATCH] craft-xlsx: preserve spacing in simple structured references --- crates/xlsx/src/xlsb/formula.rs | 6 +++++- crates/xlsx/tests/xlsb.rs | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 5d4cfa619..40d461db1 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -681,17 +681,22 @@ fn list(t: &mut Bytes<'_>, links: &Links) -> Result { 2 if first <= last => Some((column(first)?, Some(column(last)?))), _ => return Err(Unsupported("invalid structured reference columns")), }; + let pad = if bits & 0x80 != 0 { " " } else { "" }; let mut s = table.name.clone(); match (rows, &columns) { ([], None) => s.push_str("[]"), ([], Some((c, None))) => { s.push('['); + s.push_str(pad); s.push_str(c); + s.push_str(pad); s.push(']'); } ([row], None) => { s.push('['); + s.push_str(pad); s.push_str(row); + s.push_str(pad); s.push(']'); } _ => { @@ -701,7 +706,6 @@ fn list(t: &mut Bytes<'_>, links: &Links) -> Result { Some((a, None)) => items.push(format!("[{a}]")), None => {} } - let pad = if bits & 0x80 != 0 { " " } else { "" }; let comma = if bits & 0x100 != 0 { ", " } else { "," }; s.push('['); s.push_str(pad); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index cd5494c03..a7253df22 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1839,6 +1839,27 @@ fn a_name_using_a_structured_reference_is_decoded_after_tables_are_indexed() { assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), Some("Vals"), "{:?}", wb.warnings); } +#[test] +fn simple_table_references_preserve_square_bracket_spacing() { + let cases = [ + (1, "Sales[Val]"), + (1 | 0x80, "Sales[ Val ]"), + (rows(1), "Sales[#All]"), + (rows(1) | 0x80, "Sales[ #All ]"), + (1 | 0x100, "Sales[Val]"), + (rows(1) | 0x100, "Sales[#All]"), + ]; + let mut data = row(0); + for (i, (bits, _)) in cases.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, &ptg_list(0, *bits, 1, 1, 1), &[])); + } + let bytes = book_with_tables(&data, &[sales(&sales_filters())]); + let wb = read(&bytes).unwrap(); + for (i, (_, want)) in cases.iter().enumerate() { + assert_eq!(wb.sheets[0].formula(CellRef::new(0, i as u32 + 1)), Some(*want), "{:?}", wb.warnings); + } +} + #[test] fn tables_are_read_as_their_xlsx_spelling() { // Spacing flags: 0x80 inside the outer brackets, 0x100 after commas.