diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..4e90ca830 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -84,12 +84,17 @@ The parts this crate writes use the transitional namespaces; a strict file's kep 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 cell formulas are decoded from their tokens into the +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`; so is a name whose formula can't be read, with a +warning. 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 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: user defined names, table references, +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. diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..1941ea61e 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,5 +1,5 @@ -//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an +//! Binary workbooks ([MS-XLSB]): the workbook part with its defined names, shared strings and +//! worksheet cells, from their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an //! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. //! Styles are not read. A record that does not parse fails the read. @@ -15,7 +15,7 @@ use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; -use crate::workbook::{SheetEntry, SheetKind, Workbook}; +use crate::workbook::{DefinedName, SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; pub(crate) use formula::Links; use formula::{Book, Mode, Name, Xti}; @@ -266,6 +266,9 @@ pub(crate) fn workbook( records.first(131, warnings)?; let mut wb = Workbook::default(); let mut links = Some(Links::default()); + let mut pending = vec![]; + // Each BrtBundleSh's place in `wb.sheets`, `None` for a skipped one. + let mut bundle_sheets: Vec> = vec![]; let mut bundle = false; let mut bundle_seen = false; while let Some((id, payload)) = records.next(warnings)? { @@ -279,14 +282,15 @@ pub(crate) fn workbook( if wb.sheets.is_empty() { return Err(invalid("no supported worksheets")); } - if links.is_none() { - warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"); + match links.as_mut() { + Some(l) => wb.defined_names = defined_names(pending, &bundle_sheets, l, limits, budget, warnings), + None => warnings.warn("XLSB: the workbook's sheet links or names could not be read; formulas keep their saved results"), } return Ok((wb, links)); } 39 | 355 | 357 | 358 | 362 | 667 => { if let Some(l) = links.as_mut() - && link_record(id, payload, l).is_err() + && link_record(id, payload, l, &mut pending).is_err() { links = None; } @@ -341,6 +345,7 @@ pub(crate) fn workbook( if let Some(l) = links.as_mut() { l.sheets.push(None); } + bundle_sheets.push(None); continue; }; let rel = rels.by_id(&rel_id).ok_or_else(|| invalid("worksheet relationship is missing"))?; @@ -355,6 +360,7 @@ pub(crate) fn workbook( if let Some(l) = links.as_mut() { l.sheets.push((kind == SheetKind::Worksheet).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()) }); } _ => {} @@ -363,9 +369,110 @@ pub(crate) fn workbook( Err(invalid("missing workbook end record")) } +/// A `BrtName`'s parts that only its [`DefinedName`] needs, until the workbook part is read. +struct PendingName { + /// Its place in [`Links`]'s names. + index: usize, + flags: u32, + /// `chKey`: a macro's shortcut key. + key: u8, + rgce: Vec, + rgcb: Vec, + comment: Option, + menu: Option, + description: Option, +} + +/// The workbook's defined names, as `.xlsx` has them in `definedNames`: every `BrtName` but the +/// hidden ones for newer functions and LET parameters, which Excel doesn't write there. `sheets` +/// maps a name's `itab` (its sheet's place in `BrtBundleSh` order) to [`Workbook::sheets`]. +/// A name whose formula can't be read, or that belongs to a sheet the reader skips, is left out +/// with a warning, and a cell formula using it keeps its saved result. +fn defined_names( + pending: Vec, + sheets: &[Option], + links: &mut Links, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Vec { + let mut out = vec![]; + for p in pending { + let Some(name) = links.names.get(p.index) else { continue }; + if name.future || formula::is_param(&name.text) { + continue; + } + let local_sheet_id = match name.scope { + None => None, + Some(itab) => match usize::try_from(itab).ok().and_then(|i| sheets.get(i)) { + Some(Some(i)) => Some(*i), + _ => { + warnings.warn("XLSB: some defined names were left out (names of sheets the reader skips)"); + continue; + } + }, + }; + let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) { + Ok(e) => expr::print(&e), + Err(formula::Unsupported(why)) => { + warnings.warn(format!("XLSB: some defined names were left out ({why})")); + continue; + } + }; + if !budget.try_formula_text(formula.len(), limits) { + warnings.warn("XLSB: some defined names were left out (more formula text than the limits allow)"); + continue; + } + 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); + } + let (func, vba, proc) = (p.flags & 0x02 != 0, p.flags & 0x04 != 0, p.flags & 0x08 != 0); + if func { + put("function", "1".into()); + } + if vba { + put("vbProcedure", "1".into()); + } else if proc { + put("xlm", "1".into()); + } + let group = (p.flags >> 6) & 0x1FF; + if proc && group != 0 { + put("functionGroupId", group.to_string()); + } + if p.key != 0 { + put("shortcutKey", char::from(p.key).to_string()); + } + if p.flags & (1 << 15) != 0 { + put("publishToServer", "1".into()); + } + if p.flags & (1 << 16) != 0 { + put("workbookParameter", "1".into()); + } + out.push(DefinedName { + name: name.text.clone(), + local_sheet_id, + formula, + comment: p.comment.unwrap_or_default(), + hidden: p.flags & 1 != 0, + attrs, + }); + if let Some(n) = links.names.get_mut(p.index) { + n.read = true; + } + } + out +} + /// A supporting link (`BrtSupSelf`, `BrtSupSame`, `BrtSupAddin`, `BrtSupBookSrc`), the sheet links -/// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`. -fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> { +/// (`BrtExternSheet`) or a defined name (`BrtName`), added to `links`; a name's other parts go to +/// `pending`. +fn link_record(id: u16, payload: &[u8], links: &mut Links, pending: &mut Vec) -> Result<(), Error> { let mut b = Bytes::new(payload); match id { 357 | 358 | 667 => { @@ -391,15 +498,37 @@ fn link_record(id: u16, payload: &[u8], links: &mut Links) -> Result<(), Error> b.finish()?; } _ => { - // BrtName ([MS-XLSB] 2.4.718): flags, chKey, itab, then the name; the formula and the - // rest aren't needed to call a function or tell a name from one. + // BrtName ([MS-XLSB] 2.4.718). Its formula is decoded once the workbook part is read. if links.names.len() >= MAX_NAMES { return Err(invalid("too many defined names")); } let flags = b.u32()?; - b.take(5)?; + let key = b.u8()?; + let itab = b.u32()?; let text = b.text(255)?; - links.names.push(Name { text, future: flags & (1 << 17) != 0 }); + let cce = b.u32()? as usize; + let rgce = b.take(cce)?.to_vec(); + let cb = b.u32()? as usize; + let rgcb = b.take(cb)?.to_vec(); + let comment = b.string(255, true)?; + // A macro's name (fProc) has four more strings: one unused, its custom menu, its + // description and another unused. + let (menu, description) = if flags & 0x08 != 0 { + b.string(32767, true)?; + let menu = b.string(32767, true)?; + let description = b.string(32767, true)?; + b.string(32767, true)?; + (menu, description) + } else { + (None, None) + }; + b.finish()?; + let scope = (itab != u32::MAX).then_some(itab); + // A built-in name (fBuiltin) is stored bare; `.xlsx` spells it `_xlnm.Print_Area`, in + // `definedNames` and in formulas alike. + let text = if flags & 0x20 != 0 { format!("_xlnm.{text}") } else { text }; + pending.push(PendingName { index: links.names.len(), flags, key, rgce, rgcb, comment, menu, description }); + links.names.push(Name { text, future: flags & (1 << 17) != 0, scope, read: false }); } } Ok(()) diff --git a/crates/xlsx/src/xlsb/expr.rs b/crates/xlsx/src/xlsb/expr.rs index 3dd643a6a..17f5086ae 100644 --- a/crates/xlsx/src/xlsb/expr.rs +++ b/crates/xlsx/src/xlsb/expr.rs @@ -24,6 +24,8 @@ pub(crate) enum RefKind { Rows(u32, bool, u32, bool), /// Whole columns `A:C`. Cols(u32, bool, u32, bool), + /// A deleted cell or range on a named sheet: `Sheet2!#REF!`. + Deleted, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -173,6 +175,7 @@ fn push_reference(s: &mut String, r: &Reference) { s.push_str(dollar(*a1)); push_col(s, *c1); } + RefKind::Deleted => s.push_str(CellError::Ref.as_str()), } } diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index 327ffcc25..2e7f7cc96 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -4,8 +4,9 @@ //! called through hidden names), so this is its own decoder, with its own function table //! ([`super::ftab`]) and its own copies of craft-xls's depth guards and limits. Anything outside the //! token set it knows is reported as unsupported, and the caller keeps the cell's cached value. -//! So is a formula that needs something the reader doesn't read: a user's defined name (XLSB -//! names are not read yet) or Python code stored outside the cell. +//! So is a formula that needs something the reader doesn't read: a defined name whose own +//! formula could not be read, or Python code stored outside the cell. Defined names' formulas +//! are decoded here too ([`Mode::Name`]). //! //! Names keep their file spelling (`_xlfn.CONCAT`, `_xlpm.x`), as `.xlsx` formula text has them. @@ -92,13 +93,26 @@ pub(super) struct Xti { pub last: i32, } -/// A `BrtName` as far as cell formulas need it. +/// A `BrtName` as far as formulas need it. #[derive(Clone, Debug)] pub(super) struct Name { - /// The name as written (`_xlfn.CONCAT`, `_xlpm.x`, `Rate`). + /// The name as `.xlsx` spells it (`_xlfn.CONCAT`, `_xlpm.x`, `_xlnm.Print_Area`, `Rate`). pub text: String, /// `fFutureFunction`: a hidden name standing for a function newer than the file format. pub future: bool, + /// `itab`: the sheet a sheet-scoped name belongs to, by its place in `BrtBundleSh` order. + pub scope: Option, + /// Its own formula was read, so it is one of the workbook's defined names. + pub read: bool, +} + +impl Name { + /// Whether a formula may use the name: one the formula text itself defines (a newer + /// function, a LET parameter), or a defined name the reader gives. A name's formula may use + /// any other name: its text is the same either way. + fn usable(&self, mode: Mode) -> bool { + self.future || is_param(&self.text) || self.read || mode == Mode::Name + } } /// Workbook-level tables formulas refer to. @@ -114,20 +128,37 @@ pub(crate) struct Links { } /// Whether a name stands for a LET parameter (`_xlpm.x`), which only the formula itself defines. -fn is_param(name: &str) -> bool { +pub(super) fn is_param(name: &str) -> bool { name.get(..6).is_some_and(|p| p.eq_ignore_ascii_case("_xlpm.")) } impl Links { - /// The sheets a 3-D reference names, or `None` for a deleted sheet (the reference is `#REF!`). - fn sheet_sel(&self, ixti: u16) -> Result, Unsupported> { + /// `PtgName`'s and `PtgNameX`'s name: a 1-based index into every `BrtName`. A cell formula + /// using a defined name the reader doesn't give would calculate to `#NAME?`, so it keeps its + /// saved result instead. + fn name(&self, index: u32, mode: Mode) -> Result<&Name, Unsupported> { + let i = usize::try_from(index).ok().and_then(|i| i.checked_sub(1)); + let name = i.and_then(|i| self.names.get(i)).ok_or(Unsupported("reference to a missing defined name"))?; + if !name.usable(mode) { + return Err(Unsupported("defined names whose formula could not be read")); + } + Ok(name) + } + + /// Sheet link `ixti`, which must be through this workbook's own supporting link. + fn this_book(&self, ixti: u16) -> Result<&Xti, Unsupported> { let x = self.xti.get(usize::from(ixti)).ok_or(Unsupported("reference to a missing sheet link"))?; match usize::try_from(x.book).ok().and_then(|b| self.books.get(b)) { - Some(Book::This) => {} - Some(Book::External) => return Err(Unsupported("references to other workbooks (external links)")), - Some(_) => return Err(Unsupported("references through add-in or same-sheet links")), - None => return Err(Unsupported("reference to a missing workbook link")), + Some(Book::This) => Ok(x), + Some(Book::External) => Err(Unsupported("references to other workbooks (external links)")), + Some(_) => Err(Unsupported("references through add-in or same-sheet links")), + None => Err(Unsupported("reference to a missing workbook link")), } + } + + /// The sheets a 3-D reference names, or `None` for a deleted sheet (the reference is `#REF!`). + fn sheet_sel(&self, ixti: u16) -> Result, Unsupported> { + let x = self.this_book(ixti)?; // -1: "the first sheet in the reference cannot be found" (a deleted sheet). if x.first == -1 || x.last == -1 { return Ok(None); @@ -194,10 +225,15 @@ const LAST_COL: u32 = MAX_COLS - 1; /// Where a formula lives: a cell formula's references hold coordinates; a shared formula's /// relative tokens (`PtgRefN`, `PtgAreaN`) hold offsets from the shared formula's first cell. +/// +/// A defined name's relative references are offsets from A1 that wrap around the grid +/// (`RgceLocRel`), in its 3-D references too: `.xlsx` spells them as the cell they reach from A1 +/// (one column left is `Data!XFD1`). #[derive(Clone, Copy, PartialEq)] pub(super) enum Mode { Cell, Shared(CellRef), + Name, } /// RgceLoc ([MS-XLSB] 2.5.98.91): a row and a `ColRelShort` (14-bit column, then the @@ -225,9 +261,15 @@ fn loc(row: u32, col: u16, mode: Mode) -> Result { match mode { Mode::Cell => anchor(row, col), Mode::Shared(base) => relative_anchor(row, col, base), + Mode::Name => relative_anchor(row, col, CellRef::new(0, 0)), } } +/// A 3-D reference's cell: a coordinate, except in a defined name. +fn loc3d(row: u32, col: u16, mode: Mode) -> Result { + if mode == Mode::Name { loc(row, col, mode) } else { anchor(row, col) } +} + /// RgceArea: a range spanning every row (or column) is a whole-column (row) reference. fn area(r0: u32, r1: u32, c0: u16, c1: u16) -> Result { area_of(anchor(r0, c0)?, anchor(r1, c1)?) @@ -380,14 +422,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res } 0x23 => { // PtgName: a 1-based index into every BrtName, hidden ones included. - let i = usize::try_from(t.u32()?).ok().and_then(|i| i.checked_sub(1)); - let name = i.and_then(|i| links.names.get(i)).ok_or(Unsupported("reference to a missing defined name"))?; - // Defined names are not imported from XLSB yet: a cell using one would calculate - // to #NAME?, so it keeps its saved result. Newer functions and LET parameters are - // hidden names that the formula text itself defines. - if !name.future && !is_param(&name.text) { - return Err(Unsupported("defined names")); - } + let name = links.name(t.u32()?, mode)?; push(&mut stack, kids, Expr::Name(name.text.clone()))?; } 0x24 => { @@ -420,36 +455,52 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res t.take(12)?; push(&mut stack, kids, Expr::Error(CellError::Ref))?; } - 0x39 => return Err(Unsupported("names in other workbooks or on other sheets")), + 0x39 => { + // PtgNameX: a name through a supporting link. Through this workbook's own link + // it is a sheet-scoped name used from another sheet, which `.xlsx` spells + // `'Other Sheet'!Local`: Excel's files show it, [MS-XLSB] doesn't say. + let ixti = t.u16()?; + let index = t.u32()?; + links.this_book(ixti)?; + let name = links.name(index, mode)?; + let sheet = name.scope.and_then(|s| links.sheets.get(usize::try_from(s).ok()?).cloned().flatten()); + let sheet = sheet.ok_or(Unsupported("names in other workbooks or on other sheets"))?; + push(&mut stack, kids, Expr::Name(format!("{sheet}!{}", name.text)))?; + } 0x3A => { let ixti = t.u16()?; let (r, c) = (t.u32()?, t.u16()?); - let kind = RefKind::Cell(anchor(r, c)?); + let kind = RefKind::Cell(loc3d(r, c, mode)?); push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; } 0x3B => { let ixti = t.u16()?; let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); - let kind = area(r0, r1, c0, c1)?; + let kind = area_of(loc3d(r0, c0, mode)?, loc3d(r1, c1, mode)?)?; push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?; } 0x3C | 0x3D => { + // PtgRefErr3d, PtgAreaErr3d: a deleted cell or range, which `.xlsx` spells + // `Sheet2!#REF!` (`#REF!` when the sheet is gone too). The sheet link must still + // be one the reader follows: an external #REF! stays cached. let ixti = t.u16()?; t.take(if id == 0x3C { 6 } else { 12 })?; - // The sheet link must still be one the reader follows: an external #REF! stays cached. - links.sheet_sel(ixti)?; - push(&mut stack, kids, Expr::Error(CellError::Ref))?; + push(&mut stack, kids, reference(links.sheet_sel(ixti)?, RefKind::Deleted))?; } 0x01 => return Err(Unsupported("the shared or array formula form")), 0x02 => return Err(Unsupported("what-if data tables")), 0x18 => return Err(Unsupported("structured table references")), 0x2C => { - let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + if mode == Mode::Cell { + return Err(Unsupported("relative references in a cell formula")); + } let (r, c) = (t.u32()?, t.u16()?); push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: RefKind::Cell(loc(r, c, mode)?) }))?; } 0x2D => { - let Mode::Shared(_) = mode else { return Err(Unsupported("relative references in a cell formula")) }; + if mode == Mode::Cell { + return Err(Unsupported("relative references in a cell formula")); + } let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?); let kind = area_of(loc(r0, c0, mode)?, loc(r1, c1, mode)?)?; push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind }))?; diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..73bd23218 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, SheetKind, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -463,6 +463,9 @@ fn name(flags: u32, text: &str) -> Vec { data.extend(wide(text)); data.extend([2, 0, 0, 0, 0x1c, 0x1d, 0, 0, 0, 0]); // Formula `#NAME?`, no extra data. data.extend(u32::MAX.to_le_bytes()); // No comment. + if flags & 0x08 != 0 { + data.extend(u32::MAX.to_le_bytes().repeat(4)); // A macro's four strings, all null. + } record(39, &data) } @@ -543,8 +546,8 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { ), (9, ref3d(3, 0, 0), vec![]), (10, [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &REL.to_le_bytes(), &REL.to_le_bytes(), &[0x41, 20, 0]].concat(), vec![]), - // Kept as their saved results: (11, vec![0x43, 4, 0, 0, 0, 0x1E, 2, 0, 0x05], vec![]), + // Kept as their saved results: (12, ref3d(4, 0, REL), vec![]), (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), // Part of a shared formula at A1 that isn't one. ]; @@ -572,7 +575,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { Some("_xlfn.LET(_xlpm.x,1,_xlpm.x+1)"), Some("#REF!"), Some("SQRT(A1:A2)"), - None, + Some("Rate*2"), None, None, // A BrtCellMeta before a cell gives it cell metadata, as `cm` does in .xlsx. @@ -584,7 +587,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); // Every formula cell keeps its saved result as its value. assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); - for why in ["defined names", "other workbooks", "missing from the file", "structured table references"] { + for why in ["other workbooks", "missing from the file", "structured table references"] { assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); } } @@ -930,3 +933,127 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +// Defined names ([MS-XLSB] 2.4.718): `BrtName` records, and the `definedNames` Excel writes for +// the same names when it saves the workbook as `.xlsx`. + +/// `BrtName`. A macro's name (`fProc`, 0x08) has four more strings, null here. +fn brt_name(flags: u32, key: u8, itab: u32, text: &str, rgce: &[u8], comment: Option<&str>) -> Vec { + let mut data = flags.to_le_bytes().to_vec(); + data.push(key); + data.extend(itab.to_le_bytes()); + data.extend(wide(text)); + data.extend((rgce.len() as u32).to_le_bytes()); + data.extend(rgce); + data.extend(0u32.to_le_bytes()); + data.extend(comment.map_or_else(|| u32::MAX.to_le_bytes().to_vec(), wide)); + if flags & 0x08 != 0 { + data.extend(u32::MAX.to_le_bytes().repeat(4)); + } + record(39, &data) +} +fn ref3d_name(ixti: u16, row: u32, col: u16) -> Vec { + [&[0x3Au8][..], &ixti.to_le_bytes(), &loc(row, col)].concat() +} +const WORKBOOK: u32 = u32::MAX; + +/// `book(false)` with the supporting links of `book_with_links` (sheet links Data, Hidden, +/// Data:Hidden, a deleted sheet, the other workbook, and this workbook as a whole, which +/// `PtgNameX` uses), and `names`. +fn book_with_names(names: &[Vec]) -> Vec { + let plain = book(false); + let end = record(132, &[]); + let mut xti = 6u32.to_le_bytes().to_vec(); + for (book, first, last) in [(0u32, 0i32, 0i32), (0, 1, 1), (0, 0, 1), (0, -1, -1), (1, 0, 0), (0, -2, -2)] { + xti.extend([book.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat()); + } + let links = [record(353, &[]), record(357, &[]), record(355, &wide("rId9")), record(362, &xti), record(354, &[])]; + [&plain[..plain.len() - end.len()], &links.concat(), &names.concat(), &end].concat() +} + +/// The names below, in record order, as `PtgName` numbers them from 1. +fn names() -> Vec> { + vec![ + // 1: a newer function's hidden name: not a defined name in `.xlsx`. + brt_name(0x0002_000b, 0, WORKBOOK, "_xlfn.CONCAT", &[0x1C, 0x1D], None), + // 2: built-in and hidden, on Data. + brt_name(0x21, 0, 0, "_FilterDatabase", &[&[0x3Bu8, 0, 0][..], &0u32.to_le_bytes(), &9u32.to_le_bytes(), &[0, 0, 2, 0]].concat(), None), + // 3: refers to another workbook: left out. + brt_name(0, 0, WORKBOOK, "External", &ref3d_name(4, 0, 0), None), + // 4: a deleted cell. + brt_name(0, 0, WORKBOOK, "Gone", &[0x3C, 0, 0, 0, 0, 0, 0, 0, 0], None), + brt_name(1, 0, WORKBOOK, "Hid", &[0x1E, 42, 0], None), + // 6, 7: the same name on two sheets. + brt_name(0, 0, 0, "Local", &ref3d_name(0, 1, 1), None), + brt_name(0, 0, 1, "Local", &ref3d_name(1, 0, 1), None), + // 8: an XLM macro with a shortcut key, in the "User Defined" group (14). + brt_name(0x388, b'k', WORKBOOK, "MacroName", &ref3d_name(0, 0, 0), None), + // 9 + brt_name(0, 0, WORKBOOK, "Rate", &[&[0x1Fu8][..], &0.05f64.to_le_bytes()].concat(), Some("a note")), + // 10, 11: relative references are offsets from A1, wrapping around the grid. + brt_name(0, 0, WORKBOOK, "Rel", &ref3d_name(0, 0, 0xFFFF), None), + brt_name(0, 0, WORKBOOK, "RelN", &[&[0x2Cu8][..], &loc(u32::MAX, REL)].concat(), None), + // 12: on a sheet the workbook doesn't have: left out. + brt_name(0, 0, 5, "Skipped", &[0x1E, 1, 0], None), + // 13: a name of a name. + brt_name(0, 0, WORKBOOK, "Twice", &[0x23, 9, 0, 0, 0, 0x1E, 2, 0, 0x05], None), + ] +} + +const NAMES_XML: &str = r#"Data!#REF!Data!$B$2Hidden!$B$1Data!$A$10.05Data!XFD1A1048576Rate*2"#; + +fn xml_names(xml: &str) -> Vec { + let types = r#""#; + let root = r#""#; + let bytes = zip(&[("[Content_Types].xml", types.as_bytes()), ("_rels/.rels", root.as_bytes()), ("xl/workbook.xml", xml.as_bytes())]); + Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap().defined_names +} + +#[test] +fn defined_names_are_read_as_their_xlsx_spelling() { + let cells: Vec> = vec![ + vec![0x23, 9, 0, 0, 0, 0x1E, 100, 0, 0x05], + vec![0x23, 6, 0, 0, 0], + // PtgNameX through this workbook's own link: a sheet-scoped name used from another sheet. + vec![0x39, 5, 0, 7, 0, 0, 0], + vec![0x23, 2, 0, 0, 0], + vec![0x3C, 1, 0, 0, 0, 0, 0, 0, 0], + // Kept as their saved results: a name left out, a name in another workbook, and a + // workbook name through PtgNameX. + vec![0x23, 3, 0, 0, 0], + vec![0x39, 4, 0, 9, 0, 0, 0], + vec![0x39, 5, 0, 9, 0, 0, 0], + ]; + let mut data = row(0); + for (i, rgce) in cells.iter().enumerate() { + data.extend(formula_cell(i as u32 + 1, rgce, &[])); + } + let bytes = package(&book_with_names(&names()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + assert_eq!(wb.defined_names, xml_names(NAMES_XML)); + let book = read(&bytes).unwrap(); + let got: Vec<_> = (1..=8).map(|c| book.sheets[0].formula(CellRef::new(0, c))).collect(); + let want = [Some("Rate*100"), Some("Local"), Some("Hidden!Local"), Some("_xlnm._FilterDatabase"), Some("Hidden!#REF!"), None, None, None]; + assert_eq!(got, want); + for why in ["defined names whose formula could not be read", "other workbooks", "names in other workbooks or on other sheets"] { + assert!(warned(&book, why), "{why}: {:?}", book.warnings); + } + for why in ["(references to other workbooks (external links))", "(names of sheets the reader skips)"] { + assert!(book.warnings.iter().any(|w| w.contains("defined names were left out") && w.contains(why)), "{why}: {:?}", book.warnings); + } +} + +#[test] +fn a_damaged_name_leaves_names_and_formulas_as_values() { + // A truncated BrtName: the workbook opens with no names, and every formula keeps its value. + let mut list = names(); + let full = brt_name(0, 0, WORKBOOK, "Rate", &[0x1E, 1, 0], None); + list[8] = record(39, &full[2..full.len() - 2]); // The payload without its record header and last two bytes. + let data = [row(0), formula_cell(1, &[0x1E, 1, 0], &[])].concat(); + let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES); + let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap(); + assert!(wb.defined_names.is_empty()); + let book = read(&bytes).unwrap(); + 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); +}