From 2a786e27a2826e9a67cfe0af167410409717b0d3 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 11:30:32 -0500 Subject: [PATCH] craft-xlsx: names on macro sheets, spans over chart sheets, formula-less built-ins A second Excel probe showed three cases where the .xlsb reader left out what .xlsx has: - Macro names point at an XLM macro sheet (Macro1!$A$1), which the sheet links treated as a sheet with no cells, so every such name was left out. Macro sheets have cells; references to them are now named. - A 3-D span over a chart sheet ('Data:After Sheet'!$A$1) was refused. A span is spelled by its ends; a sheet with no cells between them adds nothing, and .xlsx spells it the same. - Excel writes a workbook-level built-in with no formula (Database) beside a sheet's own. .xlsx leaves it out of definedNames, but formulas using it spell _xlnm.Database; they now do instead of keeping their saved results. --- crates/xlsx/README.md | 7 +++-- crates/xlsx/src/xlsb.rs | 13 ++++++++-- crates/xlsx/src/xlsb/formula.rs | 13 ++++------ crates/xlsx/tests/xlsb.rs | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 4e90ca830..3b636a2fa 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -88,8 +88,11 @@ read (each cell keeps its format index). Its defined names are those of `defined `.xlsx` Excel writes: built-in names as `_xlnm.Print_Area`, sheet-scoped names with their `localSheetId`, relative references as the cell they reach from A1, macro names with their `function`, `xlm`, `functionGroupId` and `shortcutKey`. Excel's hidden names for newer functions -and LET parameters are left out, as in `.xlsx`; so is a name whose formula can't be read, with a -warning. Its cell formulas are decoded from their tokens into the +and LET parameters are left out, as in `.xlsx`, and so is the formula-less workbook-level built-in +Excel writes beside a sheet's (`Database`), which formulas still spell `_xlnm.Database`; a name +whose formula can't be read is left out with a warning. A reference may name an XLM macro sheet +(`Macro1!$A$1`, where macro names point), and a 3-D span is spelled by its ends whatever chart +sheets lie between them. Its cell formulas are decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 1941ea61e..dd52a55b4 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -356,9 +356,10 @@ pub(crate) fn workbook( if let SheetKind::Other(_) = kind { return Err(invalid("unknown sheet relationship type")); } - // Only a worksheet has cells a formula can refer to. + // Only worksheets and macro sheets have cells a formula can refer to (a macro's + // name refers to its macro sheet: `Macro1!$A$1`). if let Some(l) = links.as_mut() { - l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone())); + l.sheets.push(matches!(kind, SheetKind::Worksheet | SheetKind::Macrosheet).then(|| name.clone())); } bundle_sheets.push(u32::try_from(wb.sheets.len()).ok()); wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) }); @@ -412,6 +413,14 @@ fn defined_names( } }, }; + // Excel writes a workbook-level built-in with no formula (`Database`) beside a sheet's + // own; `.xlsx` leaves it out of `definedNames`, and formulas using it spell its name. + if p.rgce.is_empty() { + if let Some(n) = links.names.get_mut(p.index) { + n.read = true; + } + continue; + } let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 2e7f7cc96..be6f47bfb 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -118,8 +118,8 @@ impl Name { /// Workbook-level tables formulas refer to. #[derive(Default)] pub(crate) struct Links { - /// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold - /// no cells (chart, dialog and macro sheets). + /// Every sheet's name in `BrtBundleSh` order, `None` for the sheets that hold no cells (chart, + /// dialog and module sheets). pub(super) sheets: Vec>, /// Every `BrtName`, in order: `PtgName` indexes them from 1. pub(super) names: Vec, @@ -167,14 +167,11 @@ impl Links { let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else { return Err(Unsupported("reference to a missing sheet")); }; - // A span takes every sheet between its ends, so a skipped one anywhere in it changes - // what the reference means. + // A span is spelled by its ends. A sheet between them with no cells (a chart sheet) adds + // nothing to it, and `.xlsx` spells the span the same; an end with no cells can't be named. let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?; - if span.iter().any(Option::is_none) { - return Err(Unsupported("references to sheets the reader skips")); - } let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else { - return Err(Unsupported("reference to a missing sheet")); + return Err(Unsupported("references to sheets with no cells")); }; Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) })) } diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 73bd23218..78e2d89b1 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -1057,3 +1057,49 @@ fn a_damaged_name_leaves_names_and_formulas_as_values() { assert_eq!(book.sheets[0].formula(CellRef::new(0, 1)), None); assert!(book.warnings.iter().any(|w| w.contains("names could not be read")), "{:?}", book.warnings); } + +#[test] +fn names_on_macro_sheets_spans_over_chart_sheets_and_empty_built_ins() { + // Sheets Data, a chart sheet, an XLM macro sheet, Hidden; sheet links Macro1, Data:Hidden + // (over both) and Data. + let mut book = record(131, &[]); + book.extend(record(153, &[[0u8; 8].as_slice(), &wide("")].concat())); + book.extend(record(143, &[])); + for (id, rel, name) in [(1u32, "one", "Data"), (3, "c", "Chart1"), (4, "m", "Macro1"), (2, "two", "Hidden")] { + book.extend(record(156, &[&0u32.to_le_bytes()[..], &id.to_le_bytes(), &wide(rel), &wide(name)].concat())); + } + book.extend(record(144, &[])); + let mut xti = 3u32.to_le_bytes().to_vec(); + for (first, last) in [(2i32, 2i32), (0, 3), (0, 0)] { + xti.extend([0u32.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat()); + } + book.extend([record(353, &[]), record(357, &[]), record(362, &xti), record(354, &[])].concat()); + let area = [&[0x3Bu8][..], &2u16.to_le_bytes(), &0u32.to_le_bytes(), &1u32.to_le_bytes(), &0u16.to_le_bytes(), &1u16.to_le_bytes()].concat(); + // A sheet's built-in Database, then the workbook-level one Excel writes with no formula. + book.extend(brt_name(0x20, 0, 0, "Database", &area, None)); + book.extend(brt_name(0x20, 0, WORKBOOK, "Database", &[], None)); + book.extend(brt_name(0x388, b'k', WORKBOOK, "Cmd", &ref3d_name(0, 0, 0), None)); + book.extend(brt_name(0, 0, WORKBOOK, "Span", &ref3d_name(1, 0, 0), None)); + book.extend(record(132, &[])); + let rels = RELS.replace( + "", + r#""#, + ); + let ref3d_cell = |ixti: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(0, 0)].concat(); + let data = [ + row(0), + formula_cell(1, &[0x23, 2, 0, 0, 0], &[]), + formula_cell(2, &[0x23, 4, 0, 0, 0], &[]), + formula_cell(3, &ref3d_cell(0), &[]), + formula_cell(4, &ref3d_cell(1), &[]), + ] + .concat(); + let bytes = package(&book, &worksheet(&data, &[]), &sst(), ROOT, &rels, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + let xml = r#"Data!$A$1:$B$2Macro1!$A$1Data:Hidden!$A$1"#; + assert_eq!(wb.defined_names, xml_names(xml)); + let read = read(&bytes).unwrap(); + let got: Vec<_> = (1..=4).map(|c| read.sheets[0].formula(CellRef::new(0, c))).collect(); + assert_eq!(got, [Some("_xlnm.Database"), Some("Span"), Some("Macro1!$A$1"), Some("Data:Hidden!$A$1")]); + assert!(read.warnings.iter().all(|w| !w.contains("left out") && !w.contains("saved results")), "{:?}", read.warnings); +}