mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Merge pull request #1330 from keithcsqueensu/xlsb-names
craft-xlsx: read the defined names of a binary workbook
This commit is contained in:
+19
-10
@@ -92,16 +92,25 @@ empty settings left out, a cell style format's `apply*="0"`, a number format wit
|
||||
in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not
|
||||
read; a damaged styles part is a warning and no styles, while one past a cap (65,536 entries in a
|
||||
table, 1,024 gradient stops) or past `binary_text` is too large. The descent Excel records for rows
|
||||
and the sheet (`x14ac:dyDescent`) is in alternate-content blocks, which are skipped. 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: user defined names, 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.
|
||||
and the sheet (`x14ac:dyDescent`) is in alternate-content blocks, which are skipped. 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, and so is every name
|
||||
whose formula uses a left-out name. 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
|
||||
|
||||
|
||||
+223
-16
@@ -1,15 +1,16 @@
|
||||
//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheets (rows, cells,
|
||||
//! columns, default sizes, merged 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. The styles part is read into the same
|
||||
//! [`crate::styles::Styles`] ([`styles`]). A record that does not parse fails the read.
|
||||
//! Binary workbooks ([MS-XLSB]): the workbook part with its defined names, shared strings and
|
||||
//! worksheets (rows, cells, columns, default sizes, merged 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. The styles part
|
||||
//! is read into the same [`crate::styles::Styles`] ([`styles`]). A record that does not parse
|
||||
//! fails the read.
|
||||
|
||||
mod expr;
|
||||
mod formula;
|
||||
mod ftab;
|
||||
mod styles;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_opc::{ContentTypes, Rels};
|
||||
@@ -18,7 +19,7 @@ use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility};
|
||||
use crate::features::SheetFormat;
|
||||
use crate::sheet::{Cell, CellFormula, CellValue, Column, 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};
|
||||
@@ -293,6 +294,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<Option<u32>> = vec![];
|
||||
let mut bundle = false;
|
||||
let mut bundle_seen = false;
|
||||
// Every BrtBundleSh counts toward the cap, a skipped module sheet too: each keeps a place
|
||||
@@ -309,14 +313,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;
|
||||
}
|
||||
@@ -372,6 +377,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"))?;
|
||||
@@ -387,6 +393,7 @@ pub(crate) fn workbook(
|
||||
if let Some(l) = links.as_mut() {
|
||||
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()) });
|
||||
}
|
||||
_ => {}
|
||||
@@ -395,9 +402,184 @@ 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<u8>,
|
||||
rgcb: Vec<u8>,
|
||||
comment: Option<String>,
|
||||
description: Option<String>,
|
||||
help: Option<String>,
|
||||
}
|
||||
|
||||
/// 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<PendingName>,
|
||||
sheets: &[Option<u32>],
|
||||
links: &mut Links,
|
||||
limits: &Limits,
|
||||
budget: &Budget,
|
||||
warnings: &mut Warnings,
|
||||
) -> Vec<DefinedName> {
|
||||
// Each name's formula, and the names it uses. A name whose formula uses a name that is left
|
||||
// out is left out too, whatever their order, so a cell using it keeps its saved result.
|
||||
let mut omitted = vec![false; links.names.len()];
|
||||
let mut kept = vec![];
|
||||
// PtgName identifies a record, but a bare workbook name in a sheet-scoped definition
|
||||
// would instead bind to a same-spelled local name. Without a workbook qualifier, keep
|
||||
// the cached results rather than silently changing that identity. Index once so hostile
|
||||
// files with many names and references cannot turn this check into a quadratic scan.
|
||||
// Ordinary shadows still count if their own formulas are omitted: do not infer a new
|
||||
// binding from a partially emitted name environment. Synthetic names are not definitions.
|
||||
let local_names: HashSet<_> =
|
||||
links.names.iter().filter(|n| !n.future && !formula::is_param(&n.text)).filter_map(|n| n.scope.map(|s| (s, n.text.to_lowercase()))).collect();
|
||||
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)");
|
||||
omit(&mut omitted, p.index);
|
||||
continue;
|
||||
}
|
||||
},
|
||||
};
|
||||
// Excel omits the formula for a workbook-level Database built-in and a VBA function
|
||||
// (fFunc, fOB, fProc). They remain usable by formulas without a definedNames entry.
|
||||
// Other empty names are malformed: omitting them must also omit their dependants.
|
||||
if p.rgce.is_empty() {
|
||||
let database = p.flags & 0x20 != 0 && name.scope.is_none() && name.text == "_xlnm.Database";
|
||||
let vba_function = p.flags & 0x0E == 0x0E;
|
||||
if database || vba_function {
|
||||
if let Some(n) = links.names.get_mut(p.index) {
|
||||
n.read = true;
|
||||
}
|
||||
} else {
|
||||
warnings.warn("XLSB: some defined names were left out (empty formula)");
|
||||
omit(&mut omitted, p.index);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mut used = vec![];
|
||||
let formula = match formula::decode_using(&p.rgce, &p.rgcb, links, Mode::Name, &mut used) {
|
||||
Ok(e) => expr::print(&e),
|
||||
Err(formula::Unsupported(why)) => {
|
||||
warnings.warn(format!("XLSB: some defined names were left out ({why})"));
|
||||
omit(&mut omitted, p.index);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if name.scope.is_some_and(|scope| {
|
||||
used.iter().filter_map(|&i| links.names.get(i)).any(|target| {
|
||||
target.scope.is_none()
|
||||
&& !target.future
|
||||
&& !formula::is_param(&target.text)
|
||||
&& local_names.contains(&(scope, target.text.to_lowercase()))
|
||||
})
|
||||
}) {
|
||||
warnings.warn("XLSB: some defined names were left out (workbook names shadowed by local names)");
|
||||
omit(&mut omitted, p.index);
|
||||
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)");
|
||||
omit(&mut omitted, p.index);
|
||||
continue;
|
||||
}
|
||||
kept.push((p, local_sheet_id, formula, used));
|
||||
}
|
||||
// Who uses each name, then every name reached from a left-out one: once each, so a chain
|
||||
// of any length costs what its uses do.
|
||||
let mut users: HashMap<usize, Vec<usize>> = HashMap::new();
|
||||
for (k, (.., used)) in kept.iter().enumerate() {
|
||||
for &u in used {
|
||||
users.entry(u).or_default().push(k);
|
||||
}
|
||||
}
|
||||
let mut queue: Vec<usize> = (0..omitted.len()).filter(|&i| omitted[i]).collect();
|
||||
while let Some(i) = queue.pop() {
|
||||
for &k in users.get(&i).map_or(&[][..], Vec::as_slice) {
|
||||
let Some((p, ..)) = kept.get(k) else { continue };
|
||||
if omit(&mut omitted, p.index) {
|
||||
warnings.warn("XLSB: some defined names were left out (names using a name that was left out)");
|
||||
queue.push(p.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
for (p, local_sheet_id, formula, _) in kept {
|
||||
if omitted.get(p.index).copied().unwrap_or(true) {
|
||||
continue;
|
||||
}
|
||||
let Some(name) = links.names.get(p.index) else { 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(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());
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
/// Marks name `i` left out; whether it wasn't already.
|
||||
fn omit(omitted: &mut [bool], i: usize) -> bool {
|
||||
omitted.get_mut(i).is_some_and(|o| !std::mem::replace(o, true))
|
||||
}
|
||||
|
||||
/// 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<PendingName>) -> Result<(), Error> {
|
||||
let mut b = Bytes::new(payload);
|
||||
if matches!(id, 355 | 357 | 358 | 667) && links.books.len() >= MAX_BOOKS {
|
||||
return Err(invalid("too many supporting links"));
|
||||
@@ -426,15 +608,40 @@ 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 description, its help
|
||||
// topic and another unused. ([MS-XLSB] calls the second the custom menu and the third
|
||||
// the description; Excel writes them as `.xlsx`'s `description` and `help`.) Excel
|
||||
// leaves them out of a VBA function's name (fOB), which ends after its comment.
|
||||
let vba_without_strings = flags & 0x04 != 0 && b.rest.is_empty();
|
||||
let (description, help) = if flags & 0x08 != 0 && !vba_without_strings {
|
||||
b.string(32767, true)?;
|
||||
let description = b.string(32767, true)?;
|
||||
let help = b.string(32767, true)?;
|
||||
b.string(32767, true)?;
|
||||
(description, help)
|
||||
} 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, description, help });
|
||||
links.names.push(Name { text, future: flags & (1 << 17) != 0, scope, read: false });
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -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)]
|
||||
@@ -176,6 +178,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()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,6 +283,20 @@ fn write_args(s: &mut String, args: &[Expr], sep: char) {
|
||||
}
|
||||
}
|
||||
|
||||
/// A function's name; a sheet-scoped one (`My Sheet!Fn`) is quoted as a sheet-qualified name is.
|
||||
/// Kept out of [`write_expr`], whose frame every nesting level of a formula repeats.
|
||||
#[inline(never)]
|
||||
fn push_function_name(s: &mut String, name: &str) {
|
||||
match sheet_split(name) {
|
||||
Some((sheet, name)) => {
|
||||
s.push_str("e_sheet(sheet));
|
||||
s.push('!');
|
||||
s.push_str(name);
|
||||
}
|
||||
None => s.push_str(name),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_expr(s: &mut String, e: &Expr) {
|
||||
match e {
|
||||
Expr::Number(n) => s.push_str(&number(*n)),
|
||||
@@ -328,7 +345,7 @@ fn write_expr(s: &mut String, e: &Expr) {
|
||||
write_expr(s, b);
|
||||
}
|
||||
Expr::Call(name, args) => {
|
||||
s.push_str(name);
|
||||
push_function_name(s, name);
|
||||
s.push('(');
|
||||
write_args(s, args, ',');
|
||||
s.push(')');
|
||||
@@ -374,4 +391,12 @@ mod tests {
|
||||
assert_eq!(number_to_text(1e100), "1E+100");
|
||||
assert!(number(f64::MAX).parse::<f64>().is_ok_and(f64::is_finite));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sheet_scoped_function_name_is_quoted() {
|
||||
let call = |name: &str| print(&Expr::Call(name.into(), vec![Expr::Number(1.0)]));
|
||||
assert_eq!(call("My Sheet!Fn"), "'My Sheet'!Fn(1)");
|
||||
assert_eq!(call("Data!Fn"), "Data!Fn(1)");
|
||||
assert_eq!(call("SUM"), "SUM(1)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -136,13 +137,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<u32>,
|
||||
/// 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.
|
||||
@@ -158,20 +172,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<Option<SheetSel>, 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<Option<SheetSel>, 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);
|
||||
@@ -235,10 +266,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
|
||||
@@ -266,9 +302,15 @@ fn loc(row: u32, col: u16, mode: Mode) -> Result<Anchor, Unsupported> {
|
||||
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<Anchor, Unsupported> {
|
||||
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<RefKind, Unsupported> {
|
||||
area_of(anchor(r0, c0)?, anchor(r1, c1)?)
|
||||
@@ -296,6 +338,11 @@ fn reference(sheet: Option<SheetSel>, kind: RefKind) -> Expr {
|
||||
/// represented. A shared formula's tokens are decoded once for each of its cells
|
||||
/// (`Mode::Shared` with that cell), whose relative references are offsets from it.
|
||||
pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Result<Expr, Unsupported> {
|
||||
decode_using(rgce, rgcb, links, mode, &mut Vec::new())
|
||||
}
|
||||
|
||||
/// [`decode`], adding to `used` the 0-based index of every defined name the formula uses.
|
||||
pub(super) fn decode_using(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode, used: &mut Vec<usize>) -> Result<Expr, Unsupported> {
|
||||
if rgce.len() > MAX_RGCE {
|
||||
return Err(Unsupported("formula too long"));
|
||||
}
|
||||
@@ -470,14 +517,9 @@ 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 index = t.u32()?;
|
||||
let name = links.name(index, mode)?;
|
||||
used.extend(usize::try_from(index).ok().and_then(|i| i.checked_sub(1)));
|
||||
push(&mut stack, kids, Expr::Name(name.text.clone()))?;
|
||||
}
|
||||
0x24 => {
|
||||
@@ -510,36 +552,53 @@ 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)?;
|
||||
used.extend(usize::try_from(index).ok().and_then(|i| i.checked_sub(1)));
|
||||
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 }))?;
|
||||
|
||||
+315
-3
@@ -466,6 +466,9 @@ fn name(flags: u32, text: &str) -> Vec<u8> {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -546,8 +549,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.
|
||||
];
|
||||
@@ -575,7 +578,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.
|
||||
@@ -587,7 +590,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() {
|
||||
assert_eq!(sheet.cell_metadata.iter().collect::<Vec<_>>(), [(&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);
|
||||
}
|
||||
}
|
||||
@@ -934,6 +937,176 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() {
|
||||
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<u8> {
|
||||
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<u8> {
|
||||
[&[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<u8>]) -> Vec<u8> {
|
||||
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<u8>> {
|
||||
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#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><definedNames><definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">Data!$A$1:$C$10</definedName><definedName name="Gone">Data!#REF!</definedName><definedName name="Hid" hidden="1">42</definedName><definedName name="Local" localSheetId="0">Data!$B$2</definedName><definedName name="Local" localSheetId="1">Hidden!$B$1</definedName><definedName name="MacroName" xlm="1" functionGroupId="14" shortcutKey="k">Data!$A$1</definedName><definedName name="Rate" comment="a note">0.05</definedName><definedName name="Rel">Data!XFD1</definedName><definedName name="RelN">A1048576</definedName><definedName name="Twice">Rate*2</definedName></definedNames></workbook>"#;
|
||||
|
||||
fn xml_names(xml: &str) -> Vec<craft_xlsx::DefinedName> {
|
||||
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/></Types>"#;
|
||||
let root = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="r" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#;
|
||||
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<u8>> = 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);
|
||||
}
|
||||
|
||||
#[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(
|
||||
"</Relationships>",
|
||||
r#"<Relationship Id="c" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="../data/c.bin"/><Relationship Id="m" Type="http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet" Target="../data/m.bin"/></Relationships>"#,
|
||||
);
|
||||
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#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><definedNames><definedName name="_xlnm.Database" localSheetId="0">Data!$A$1:$B$2</definedName><definedName name="Cmd" xlm="1" functionGroupId="14" shortcutKey="k">Macro1!$A$1</definedName><definedName name="Span">Data:Hidden!$A$1</definedName></definedNames></workbook>"#;
|
||||
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);
|
||||
}
|
||||
|
||||
/// `BrtRowHdr` with its style, height and three flag bytes, and no column spans.
|
||||
fn row_hdr(r: u32, ixfe: u32, twips: u16, flags: [u8; 3]) -> Vec<u8> {
|
||||
record(0, &[&r.to_le_bytes()[..], &ixfe.to_le_bytes(), &twips.to_le_bytes(), &flags, &0u32.to_le_bytes()].concat())
|
||||
@@ -1443,6 +1616,51 @@ fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() {
|
||||
assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vba_function_names_without_macro_strings_are_read() {
|
||||
// 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. `.xlsm` lists
|
||||
// no such name; a cell calling the function spells it.
|
||||
let vba = |text: &str| {
|
||||
let data = [
|
||||
&0x0Eu32.to_le_bytes()[..],
|
||||
&[0],
|
||||
&u32::MAX.to_le_bytes(),
|
||||
&wide(text),
|
||||
&0u32.to_le_bytes(),
|
||||
&0u32.to_le_bytes(),
|
||||
&u32::MAX.to_le_bytes(),
|
||||
]
|
||||
.concat();
|
||||
record(39, &data)
|
||||
};
|
||||
let mut list = names();
|
||||
list.push(vba("Twice"));
|
||||
let twice = [&[0x23u8, 14, 0, 0, 0][..], &[0x1E, 4, 0], &[0x42, 2, 0xFF, 0]].concat();
|
||||
let data = [row(0), formula_cell(1, &twice, &[]), formula_cell(2, &[0x23, 9, 0, 0, 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_eq!(wb.defined_names, xml_names(NAMES_XML));
|
||||
let book = read(&bytes).unwrap();
|
||||
assert_eq!([1, 2].map(|c| book.sheets[0].formula(CellRef::new(0, c))), [Some("Twice(4)"), Some("Rate")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_macro_names_description_and_help_topic_are_read_as_excel_writes_them() {
|
||||
// An XLM function in category 3 with the strings Excel writes after its comment: unused, the
|
||||
// description, the help topic, unused. [MS-XLSB] labels the middle two the custom menu and the
|
||||
// description; Excel's `.xlsm` has them as `description` and `help`.
|
||||
let flags: u32 = 0x02 | 0x08 | (3 << 6);
|
||||
let mut data = [&flags.to_le_bytes()[..], &[0], &u32::MAX.to_le_bytes(), &wide("FnA")].concat();
|
||||
let rgce = ref3d_name(0, 0, 0);
|
||||
data.extend([&(rgce.len() as u32).to_le_bytes()[..], &rgce, &0u32.to_le_bytes(), &u32::MAX.to_le_bytes()].concat());
|
||||
data.extend([u32::MAX.to_le_bytes().to_vec(), wide("Function A"), wide("!0"), u32::MAX.to_le_bytes().to_vec()].concat());
|
||||
let bytes = package(&book_with_names(&[record(39, &data)]), &worksheet(&row(0), &[]), &sst(), ROOT, RELS, TYPES);
|
||||
let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap();
|
||||
let xml = r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><definedNames><definedName name="FnA" description="Function A" help="!0" function="1" xlm="1" functionGroupId="3">Data!$A$1</definedName></definedNames></workbook>"#;
|
||||
assert_eq!(wb.defined_names, xml_names(xml));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_formulas_array_constants_share_one_allowance() {
|
||||
// Two arrays of 40,000 Boolean values, each within what one array may hold, added together:
|
||||
@@ -1470,6 +1688,28 @@ fn a_spent_formula_allowance_skips_decoding() {
|
||||
assert!(!warned(&wb, "unknown formula token"), "{:?}", wb.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_using_a_left_out_name_is_left_out_too() {
|
||||
// 14 Early = Later (a later name), 15 Alias = External (left out: another workbook),
|
||||
// 16 Later = Alias, 17 Fine = Rate. Early, Alias and Later are left out whatever their order,
|
||||
// and cells using them keep their saved results; Fine is read.
|
||||
let mut list = names();
|
||||
for (text, index) in [("Early", 16u32), ("Alias", 3), ("Later", 15), ("Fine", 9)] {
|
||||
list.push(brt_name(0, 0, WORKBOOK, text, &[&[0x23u8][..], &index.to_le_bytes()].concat(), None));
|
||||
}
|
||||
let cell = |i: u32| [&[0x23u8][..], &i.to_le_bytes()].concat();
|
||||
let data =
|
||||
[row(0), formula_cell(1, &cell(14), &[]), formula_cell(2, &cell(15), &[]), formula_cell(3, &cell(16), &[]), formula_cell(4, &cell(17), &[])]
|
||||
.concat();
|
||||
let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES);
|
||||
let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap();
|
||||
let xml = NAMES_XML.replace("</definedNames>", r#"<definedName name="Fine">Rate</definedName></definedNames>"#);
|
||||
assert_eq!(wb.defined_names, xml_names(&xml));
|
||||
let book = read(&bytes).unwrap();
|
||||
assert_eq!((1..=4).map(|c| book.sheets[0].formula(CellRef::new(0, c))).collect::<Vec<_>>(), [None, None, None, Some("Fine")]);
|
||||
assert!(book.warnings.iter().any(|w| w.contains("names using a name that was left out")), "{:?}", book.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn differential_format_strings_are_charged_to_the_text_allowance() {
|
||||
// A DXF's font name and number format are text the styles part keeps, charged like a font's.
|
||||
@@ -1497,3 +1737,75 @@ fn module_sheets_and_supporting_links_are_bounded() {
|
||||
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None);
|
||||
assert!(wb.warnings.iter().any(|w| w.contains("sheet links or names could not be read")), "{:?}", wb.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_ordinary_names_and_their_dependants_keep_cached_values() {
|
||||
// Forward and transitive references to an empty ordinary name must not become dangling
|
||||
// formulas. An empty sheet-scoped Database or another built-in is not the observed
|
||||
// workbook-level Database exception either.
|
||||
let mut list = names();
|
||||
let cell = |i: u32| [&[0x23u8][..], &i.to_le_bytes()].concat();
|
||||
list.push(brt_name(0, 0, WORKBOOK, "Early", &cell(16), None)); // 14
|
||||
list.push(brt_name(0, 0, WORKBOOK, "Empty", &[], None)); // 15
|
||||
list.push(brt_name(0, 0, WORKBOOK, "Alias", &cell(15), None)); // 16
|
||||
list.push(brt_name(0x20, 0, 0, "Database", &[], None)); // 17
|
||||
list.push(brt_name(0x20, 0, WORKBOOK, "Print_Area", &[], None)); // 18
|
||||
list.push(brt_name(0, 0, WORKBOOK, "Fine", &cell(9), None)); // 19
|
||||
let mut data = row(0);
|
||||
for i in 14..=19 {
|
||||
data.extend(formula_cell(i - 13, &cell(i), &[]));
|
||||
}
|
||||
let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES);
|
||||
let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap();
|
||||
let xml = NAMES_XML.replace("</definedNames>", r#"<definedName name="Fine">Rate</definedName></definedNames>"#);
|
||||
assert_eq!(wb.defined_names, xml_names(&xml));
|
||||
let book = read(&bytes).unwrap();
|
||||
for col in 1..=5 {
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, col)), None);
|
||||
assert_eq!(book.sheets[0].value(CellRef::new(0, col)), Value::Number(0.0));
|
||||
}
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, 6)), Some("Fine"));
|
||||
assert!(book.warnings.iter().any(|w| w.contains("empty formula")), "{:?}", book.warnings);
|
||||
assert!(book.warnings.iter().any(|w| w.contains("names using a name that was left out")), "{:?}", book.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_names_do_not_rebind_shadowed_workbook_names() {
|
||||
let token = |i: u32| [&[0x23u8][..], &i.to_le_bytes()].concat();
|
||||
let list = vec![
|
||||
brt_name(0, 0, WORKBOOK, "Rate", &[0x1E, 1, 0], None), // 1
|
||||
brt_name(0, 0, 1, "rAtE", &[0x1E, 2, 0], None), // 2: Hidden shadows Rate case-insensitively.
|
||||
brt_name(0, 0, 1, "Alias", &token(1), None), // 3: must not become Hidden!rAtE.
|
||||
brt_name(0, 0, 1, "Forward", &token(5), None), // 4
|
||||
brt_name(0, 0, 1, "Dependent", &token(3), None), // 5
|
||||
brt_name(0, 0, 0, "Fine", &token(1), None), // 6: no shadow on Data.
|
||||
brt_name(0, 0, 1, "LocalAlias", &token(2), None), // 7: intentionally uses Hidden!rAtE.
|
||||
brt_name(0, 0, WORKBOOK, "Other", &[0x1E, 3, 0], None), // 8
|
||||
brt_name(0, 0, 1, "Other", &[], None), // 9: malformed, but still an ordinary source shadow.
|
||||
brt_name(0, 0, 1, "Conservative", &token(8), None), // 10
|
||||
brt_name(0, 0, WORKBOOK, "Synthetic", &[0x1E, 4, 0], None), // 11
|
||||
brt_name(0x0002_0000, 0, 1, "Synthetic", &[0x1C, 0x1D], None), // 12: future-function metadata.
|
||||
brt_name(0, 0, 1, "SyntheticControl", &token(11), None), // 13: synthetic name is not a local definition.
|
||||
];
|
||||
let mut data = row(0);
|
||||
for (col, index) in [3u32, 4, 5, 6, 10, 13].into_iter().enumerate() {
|
||||
// PtgNameX qualifies these names on their own sheets.
|
||||
let formula = [&[0x39u8, 0, 0][..], &index.to_le_bytes()].concat();
|
||||
data.extend(formula_cell(col as u32 + 1, &formula, &[]));
|
||||
}
|
||||
let bytes = package(&book_with_names(&list), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES);
|
||||
let wb = Package::open(&bytes, &Limits::default()).unwrap().workbook().unwrap();
|
||||
let xml = r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><definedNames><definedName name="Rate">1</definedName><definedName name="rAtE" localSheetId="1">2</definedName><definedName name="Fine" localSheetId="0">Rate</definedName><definedName name="LocalAlias" localSheetId="1">rAtE</definedName><definedName name="Other">3</definedName><definedName name="Synthetic">4</definedName><definedName name="SyntheticControl" localSheetId="1">Synthetic</definedName></definedNames></workbook>"#;
|
||||
assert_eq!(wb.defined_names, xml_names(xml));
|
||||
let book = read(&bytes).unwrap();
|
||||
for col in 1..=3 {
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, col)), None);
|
||||
assert_eq!(book.sheets[0].value(CellRef::new(0, col)), Value::Number(0.0));
|
||||
}
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, 4)), Some("Data!Fine"));
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, 5)), None);
|
||||
assert_eq!(book.sheets[0].value(CellRef::new(0, 5)), Value::Number(0.0));
|
||||
assert_eq!(book.sheets[0].formula(CellRef::new(0, 6)), Some("Hidden!SyntheticControl"));
|
||||
assert!(book.warnings.iter().any(|w| w.contains("workbook names shadowed by local names")), "{:?}", book.warnings);
|
||||
assert!(book.warnings.iter().any(|w| w.contains("names using a name that was left out")), "{:?}", book.warnings);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user