From b5eda42a33893ec17143532cef6fc1d97accb9d5 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 12:21:06 -0500 Subject: [PATCH] craft-xlsx: VBA function names and macro descriptions in binary workbooks An Excel probe with VBA enabled showed two things the names reader got wrong: - Excel writes a VBA function a cell calls as a BrtName with fFunc, fOB and fProc, no formula, and none of the four strings fProc names otherwise carry. The reader refused it, which dropped every name and left every formula as its saved result. Such a name now ends after its comment; it stays out of the names, as .xlsm lists none, and cells calling it spell it. - The two middle strings of a macro's name are its description and help topic, as .xlsm writes them (description="Function A" help="!0"), not the custom menu and description the specification's labels say. --- crates/xlsx/README.md | 39 ++++++++++++++++----------------- crates/xlsx/src/xlsb.rs | 23 +++++++++++--------- crates/xlsx/tests/xlsb.rs | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index b873c87f6..e79fdbd55 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -83,25 +83,26 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its defined names are those of `definedNames` in the -`.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`, 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, apart from the four ISO/IEC 29500 functions -`.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in the formula, numbers as -Excel spells them), 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 -file keeps the tokens once. A formula whose decoding needs something the reader does not read -keeps only its saved result, with a warning saying why: a defined name left out, table references, -other workbooks, Python code stored outside the cell, data tables. The XLSB function table -(`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from `craft-xls`'s. +A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not read +(each cell keeps its format index). Its defined names are those of `definedNames` in the `.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` or +`vbProcedure`, `functionGroupId`, `shortcutKey`, `description` and `help`. Excel's hidden names for +newer functions 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`, and the formula-less name of a VBA function a cell calls (`.xlsm` lists none; the +cell spells it); 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, apart from the four ISO/IEC +29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks typed in the +formula, numbers as Excel spells them), 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 file keeps the tokens once. A formula whose decoding needs +something the reader does not read keeps only its saved result, with a warning saying why: a defined +name left out, table references, other workbooks, Python code stored outside the cell, data tables. +The XLSB function table (`xlsb/ftab.rs`) and the expression printer are this crate's own, apart from +`craft-xls`'s. ## Hostile input diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index dd52a55b4..e6246f4be 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -380,8 +380,8 @@ struct PendingName { rgce: Vec, rgcb: Vec, comment: Option, - menu: Option, description: Option, + help: Option, } /// The workbook's defined names, as `.xlsx` has them in `definedNames`: every `BrtName` but the @@ -435,12 +435,12 @@ fn defined_names( let mut attrs = vec![]; let mut put = |k: &str, v: String| attrs.push((k.to_string(), v)); // In `CT_DefinedName`'s order. - if let Some(m) = p.menu { - put("customMenu", m); - } if let Some(d) = p.description { put("description", d); } + if let Some(h) = p.help { + put("help", h); + } let (func, vba, proc) = (p.flags & 0x02 != 0, p.flags & 0x04 != 0, p.flags & 0x08 != 0); if func { put("function", "1".into()); @@ -520,14 +520,17 @@ fn link_record(id: u16, payload: &[u8], links: &mut Links, pending: &mut VecData!$A$1"#; + assert_eq!(wb.defined_names, xml_names(xml)); +}