craft-xlsx: review fixes: names using a left-out name are left out; quoted sheet-scoped calls

- A name whose formula uses a name that is left out (another workbook, a
  skipped sheet, an unreadable formula) was still published, so a cell using
  it decoded and would calculate to #NAME?. Each name's formula now records
  the names it uses, and every name reached from a left-out one is left out
  too, whatever their order: once each, so a long chain costs no more than
  its uses. A cell using one keeps its saved result.
- A sheet-scoped name called as a function through PtgNameX prints its sheet
  quoted where needed ('My Sheet'!Fn(1)), as a sheet-qualified name does.
This commit is contained in:
kiwi4boy committed 2026-11-14 12:45:29 -05:00
1 parent bf4a2897e7
commit 0f944f62d5
5 files changed
+106 -16

No files matched your search

+12 -12
View File
@@ -91,18 +91,18 @@ relative references as the cell they reach from A1, macro names with their `func
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.
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
+40 -2
View File
@@ -397,7 +397,10 @@ fn defined_names(
budget: &Budget,
warnings: &mut Warnings,
) -> Vec<DefinedName> {
let mut out = vec![];
// 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![];
for p in pending {
let Some(name) = links.names.get(p.index) else { continue };
if name.future || formula::is_param(&name.text) {
@@ -409,6 +412,7 @@ fn defined_names(
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;
}
},
@@ -421,17 +425,46 @@ fn defined_names(
}
continue;
}
let formula = match formula::decode(&p.rgce, &p.rgcb, links, Mode::Name) {
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 !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.
@@ -478,6 +511,11 @@ fn defined_names(
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`; a name's other parts go to
/// `pending`.
+23 -1
View File
@@ -283,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(&quote_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)),
@@ -331,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(')');
@@ -377,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)");
}
}
+9 -1
View File
@@ -338,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"));
}
@@ -512,7 +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 name = links.name(t.u32()?, mode)?;
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 => {
@@ -553,6 +560,7 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links, mode: Mode) -> Res
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)))?;
+22
View File
@@ -1280,3 +1280,25 @@ fn a_spent_formula_allowance_skips_decoding() {
assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings);
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);
}