craft-xlsx: read cell formulas from .xlsb files (port of gridcraft#3818)

A BIFF12 decoder for cell formulas (xlsb/formula.rs), separate from craft-xls's BIFF8 one because
nearly every operand differs (32-bit rows, 14-bit columns, UTF-16 strings, its own array-constant
codes, newer functions called through hidden names). It reads references, 3-D references,
constants, array constants, functions by number (xlsb/ftab.rs) and newer functions through
Excel's hidden _xlfn. names. A formula cell's CellFormula holds the text as an .xlsx file holds it
(`_xlfn.CONCAT`, `_xlpm.x`, no implicit `@`), and a BrtCellMeta before a cell gives it
cell_metadata, as `cm` does. Reverse-Polish tokens group by order alone, so the decoded tree
gets the parentheses its text needs from operator precedence; Excel's own text is unchanged. A
chain of string constants joined with & prints as one flat chain.

What a formula refers to must be readable too, so shared, array and data-table forms, user
defined names (XLSB names aren't read yet), table references, external links and PY(index, ...)
(code stored elsewhere in the package) keep only their saved results, with a warning saying why.
So does a formula past Limits::binary_formula_text (128 MiB of text over the workbook). The
workbook's links and names are optional: if they can't be read, the file opens with values only,
as before, and a warning says so.

Ported from storytold/gridcraft#3818 (issue gridcraft#3806), with its two review fixes
(grouping by precedence, the flat & chain). Changed from the GridCraft PR:
- The tree and its A1 printer (xlsb/expr.rs) are a copy of craft-xls's expr.rs, with
  BinOp::precedence added; the decoder's stack and depth guards are copies of craft-xls's. The
  PR used GridCraft's formula crate and shared these with GridCraft's .xls decoder.
- No fmla::from_file, implicit `@`, parse check or Python-cell adoption: those are GridCraft's
  reading of the text, not the file's. A single-cell dynamic-array formula is read (with its
  cell metadata) instead of being kept as its value; the PR left dynamic arrays to the next PR.
- The links are read with the workbook part and kept by Package for its sheets.
Its tests come along in tests/xlsb.rs, with the expected text in the file's spelling.

Checked against Excel: of the formulas in a workbook Excel saved both as .xlsb and as .xlsx,
every one this reads (26) has the text of the .xlsx; the other 12 are the kinds above.
This commit is contained in:
kiwi4boy
2026-11-12 23:29:29 -05:00
parent 1bdaa7aff3
commit 787b3e44f4
8 changed files with 1329 additions and 20 deletions
+5
View File
@@ -145,6 +145,10 @@ pub struct Limits {
pub binary_cells: usize,
/// Bytes of decoded text (shared and inline strings) in a binary workbook, over all its parts.
pub binary_text: usize,
/// Bytes of decoded formula text in a binary workbook, over all its sheets. A cell formula
/// may hold 16 KB of tokens, so without a total a million cells of long formulas would cost
/// gigabytes; past it, formulas keep their saved results (the file still opens).
pub binary_formula_text: usize,
}
impl Default for Limits {
@@ -156,6 +160,7 @@ impl Default for Limits {
binary_records: 40_000_000,
binary_cells: 4_000_000,
binary_text: 256 << 20,
binary_formula_text: 128 << 20,
}
}
}
+13 -2
View File
@@ -29,6 +29,9 @@ pub struct Package<'a> {
binary: Option<ContentTypes>,
workbook_rels: Option<Rels>,
budget: xlsb::Budget,
/// What a binary workbook's formulas refer to, once its workbook part is read: `Some(None)`
/// when that couldn't be read (every formula keeps its saved result).
binary_links: Option<Option<xlsb::Links>>,
/// Shared strings in a binary workbook, once counted: a cell's index is checked against it.
binary_strings: Option<usize>,
}
@@ -67,6 +70,7 @@ impl<'a> Package<'a> {
binary,
workbook_rels: None,
budget: xlsb::Budget::default(),
binary_links: None,
binary_strings: None,
})
}
@@ -145,7 +149,9 @@ impl<'a> Package<'a> {
if self.binary.is_some() {
let bytes = self.required(&self.workbook_part.clone())?;
let rels = self.workbook_rels()?;
return xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings);
let (wb, links) = xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings)?;
self.binary_links = Some(links);
return Ok(wb);
}
let options = self.xml_options();
let root = match self.opc.read_xml(&self.workbook_part, &options)? {
@@ -234,8 +240,13 @@ impl<'a> Package<'a> {
Some(n) => n,
None => self.shared_strings()?.len(),
};
// Formulas name sheets and functions through the workbook part.
if self.binary_links.is_none() {
self.workbook()?;
}
let bytes = self.required(&part)?;
let mut sheet = xlsb::worksheet(&bytes, strings, &self.limits, &self.budget, &mut self.warnings, &mut each)?;
let links = self.binary_links.as_ref().and_then(Option::as_ref);
let mut sheet = xlsb::worksheet(&bytes, strings, links, &self.limits, &self.budget, &mut self.warnings, &mut each)?;
sheet.part = part;
return Ok(sheet);
}
+144 -14
View File
@@ -1,7 +1,10 @@
//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from
//! their records. Saved values only: formulas are token streams that are not decoded, and
//! styles are not read. A record that does not parse fails the read.
//! 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.
mod expr;
mod formula;
mod ftab;
use std::sync::Arc;
@@ -9,10 +12,12 @@ use std::sync::Arc;
use craft_opc::{ContentTypes, Rels};
use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility};
use crate::sheet::{Cell, CellValue, Row, SheetPart};
use crate::sheet::{Cell, CellFormula, CellValue, Row, SheetPart};
use crate::strings::StringItem;
use crate::workbook::{SheetEntry, SheetKind, Workbook};
use crate::{Error, Limits, Warnings};
pub(crate) use formula::Links;
use formula::{Book, Name, Xti};
pub(crate) const MAIN: &str = "application/vnd.ms-excel.main";
const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main";
@@ -22,6 +27,9 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings";
const MAX_SHEETS: usize = 1024;
/// Most merged ranges in one worksheet.
const MAX_MERGES: usize = 100_000;
/// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the
/// workbook's formulas keep their saved results.
const MAX_NAMES: usize = 65_536;
pub(crate) fn invalid(msg: impl Into<String>) -> Error {
Error::Invalid(format!("XLSB: {}", msg.into()))
@@ -43,9 +51,21 @@ pub(crate) struct Budget {
cells: std::cell::Cell<usize>,
text: std::cell::Cell<usize>,
records: std::cell::Cell<usize>,
formula_text: std::cell::Cell<usize>,
}
impl Budget {
/// Charges a decoded formula's text if it fits [`Limits::binary_formula_text`]; a formula
/// that doesn't keeps its saved result.
fn try_formula_text(&self, len: usize, limits: &Limits) -> bool {
let total = self.formula_text.get().saturating_add(len);
let fits = total <= limits.binary_formula_text;
if fits {
self.formula_text.set(total);
}
fits
}
fn text(&self, len: usize, limits: &Limits) -> Result<(), Error> {
self.text.set(self.text.get().saturating_add(len));
if self.text.get() > limits.binary_text {
@@ -97,6 +117,9 @@ impl<'a> Bytes<'a> {
fn u32(&mut self) -> Result<u32, Error> {
Ok(u32::from_le_bytes(self.take(4)?.try_into().map_err(|_| invalid("truncated u32"))?))
}
fn i32(&mut self) -> Result<i32, Error> {
Ok(i32::from_le_bytes(self.take(4)?.try_into().map_err(|_| invalid("truncated i32"))?))
}
fn f64(&mut self) -> Result<f64, Error> {
let n = f64::from_le_bytes(self.take(8)?.try_into().map_err(|_| invalid("truncated number"))?);
@@ -224,10 +247,19 @@ impl<'a, 'l> Records<'a, 'l> {
}
/// The workbook part: BrtBeginBook, BrtWbProp, the BrtBundleSh list.
pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Result<Workbook, Error> {
/// The workbook part, and the tables its formulas refer to ([`Links`]): `None` when those can't
/// be read, so that the workbook still opens, with every formula kept as its saved result.
pub(crate) fn workbook(
bytes: &[u8],
rels: &Rels,
limits: &Limits,
budget: &Budget,
warnings: &mut Warnings,
) -> Result<(Workbook, Option<Links>), Error> {
let mut records = Records::new(bytes, limits, budget);
records.first(131, warnings)?;
let mut wb = Workbook::default();
let mut links = Some(Links::default());
let mut bundle = false;
let mut bundle_seen = false;
while let Some((id, payload)) = records.next(warnings)? {
@@ -241,7 +273,17 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg
if wb.sheets.is_empty() {
return Err(invalid("no supported worksheets"));
}
return Ok(wb);
if links.is_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()
{
links = None;
}
}
143 => {
if bundle_seen {
@@ -289,6 +331,10 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg
return Err(invalid("worksheet relationship is null"));
}
warnings.warn("XLSB module sheets were skipped");
// Formulas name sheets by their place in this list, skipped sheets included.
if let Some(l) = links.as_mut() {
l.sheets.push(None);
}
continue;
};
let rel = rels.by_id(&rel_id).ok_or_else(|| invalid("worksheet relationship is missing"))?;
@@ -299,6 +345,10 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg
if let SheetKind::Other(_) = kind {
return Err(invalid("unknown sheet relationship type"));
}
// Only a worksheet has cells a formula can refer to.
if let Some(l) = links.as_mut() {
l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone()));
}
wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) });
}
_ => {}
@@ -307,6 +357,48 @@ pub(crate) fn workbook(bytes: &[u8], rels: &Rels, limits: &Limits, budget: &Budg
Err(invalid("missing workbook end record"))
}
/// 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> {
let mut b = Bytes::new(payload);
match id {
357 | 358 | 667 => {
b.finish()?;
links.books.push(match id {
357 => Book::This,
358 => Book::Same,
_ => Book::Addin,
});
}
355 => links.books.push(Book::External),
362 => {
// [MS-XLSB] 2.4.672: cXti "MUST be less than 65536" (PtgRef3d's ixti is 16 bits).
let count = b.u32()?;
if count >= 65_536 || !links.xti.is_empty() {
return Err(invalid("invalid sheet link list"));
}
for _ in 0..count {
let book = b.u32()?;
let (first, last) = (b.i32()?, b.i32()?);
links.xti.push(Xti { book, first, last });
}
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.
if links.names.len() >= MAX_NAMES {
return Err(invalid("too many defined names"));
}
let flags = b.u32()?;
b.take(5)?;
let text = b.text(255)?;
links.names.push(Name { text, future: flags & (1 << 17) != 0 });
}
}
Ok(())
}
/// A `RichStr`: the text; formatting and phonetic runs are skipped.
fn rich_string(b: &mut Bytes<'_>) -> Result<String, Error> {
let flags = b.u8()?;
@@ -397,17 +489,37 @@ fn boolean(b: &mut Bytes<'_>) -> Result<bool, Error> {
}
}
/// The formula after a cell's saved result: checked for framing, not decoded.
fn skip_formula(b: &mut Bytes<'_>) -> Result<(), Error> {
/// The `grbitFlags` and `CellParsedFormula` after a formula cell's value: its tokens (`rgce`)
/// and their extra data (`rgcb`). The flags' only bit, `fAlwaysCalc`, is the calculator's to know.
fn cell_formula<'a>(b: &mut Bytes<'a>) -> Result<(&'a [u8], &'a [u8]), Error> {
b.take(2)?;
let cce = b.u32()?;
if !(1..=16384).contains(&cce) {
return Err(invalid("invalid formula token length"));
}
b.take(cce as usize)?;
let rgce = b.take(cce as usize)?;
let extra = b.u32()?;
b.take(extra as usize)?;
Ok(())
let rgcb = b.take(extra as usize)?;
Ok((rgce, rgcb))
}
/// A formula's text, or `None` (with a warning saying why) for one that keeps its saved result:
/// the workbook's links couldn't be read, the tokens can't be decoded, or the formula text
/// allowance is spent.
fn decode_formula(rgce: &[u8], rgcb: &[u8], links: Option<&Links>, limits: &Limits, budget: &Budget, warnings: &mut Warnings) -> Option<String> {
let links = links?;
let text = match formula::decode(rgce, rgcb, links) {
Ok(e) => expr::print(&e),
Err(formula::Unsupported(why)) => {
warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})"));
return None;
}
};
if !budget.try_formula_text(text.len(), limits) {
warnings.warn("XLSB: some formulas were kept as their saved results (more formula text than the limits allow)");
return None;
}
Some(text)
}
fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &Budget) -> Result<CellValue, Error> {
@@ -444,16 +556,16 @@ fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &B
}
_ => return Err(invalid("unsupported cell record")),
};
if (8..=11).contains(&id) {
skip_formula(b)?;
}
Ok(value)
}
/// A worksheet part: rows (BrtRowHdr) and cells inside BrtBeginSheetData, merged cells after.
/// Formulas refer to the workbook's `links`; with none, each keeps its saved result.
#[allow(clippy::too_many_arguments)]
pub(crate) fn worksheet(
bytes: &[u8],
strings: usize,
links: Option<&Links>,
limits: &Limits,
budget: &Budget,
warnings: &mut Warnings,
@@ -466,9 +578,18 @@ pub(crate) fn worksheet(
let mut data_seen = false;
let mut row: Option<Row> = None;
let mut col = None;
// BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm`
// does in `.xlsx`: a one-based index into the metadata part's cell metadata.
let mut meta: Option<u32> = None;
while let Some((id, payload)) = records.next(warnings)? {
let mut b = Bytes::new(payload);
let cell_metadata = meta.take();
match id {
49 => {
let icmb = b.i32()?;
b.finish()?;
meta = u32::try_from(icmb).ok().filter(|&i| i >= 1);
}
130 => {
if data || !data_seen {
return Err(invalid("incomplete worksheet data"));
@@ -527,7 +648,14 @@ pub(crate) fn worksheet(
}
let style = b.u32()?;
let value = value(id, &mut b, strings, limits, budget)?;
b.finish()?;
let formula = if (8..=11).contains(&id) {
let (rgce, rgcb) = cell_formula(&mut b)?;
b.finish()?;
decode_formula(rgce, rgcb, links, limits, budget, warnings).map(|text| CellFormula { text, ..CellFormula::default() })
} else {
b.finish()?;
None
};
if value != CellValue::Empty {
budget.cell(limits)?;
}
@@ -536,6 +664,8 @@ pub(crate) fn worksheet(
style: style & 0x00FF_FFFF,
phonetic: style & 0x0100_0000 != 0,
value,
formula,
cell_metadata,
..Cell::default()
});
col = Some(c);
+345
View File
@@ -0,0 +1,345 @@
//! The expression tree an XLSB formula decodes to, and its A1 formula text (without `=`), in the
//! file's spelling (`_xlfn.` and `_xlpm.` names as written). A copy of craft-xls's: the two
//! readers follow separate specifications, and can share one tree when both settle.
use std::fmt::Write;
use std::sync::Arc;
use craft_sheet::{CellError, push_col, quote_sheet, sheet_needs_quotes};
/// One end of a reference, with `$` anchors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Anchor {
pub row: u32,
pub col: u32,
pub row_abs: bool,
pub col_abs: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum RefKind {
Cell(Anchor),
Range(Anchor, Anchor),
/// Whole rows `3:5` as (row, absolute) pairs.
Rows(u32, bool, u32, bool),
/// Whole columns `A:C`.
Cols(u32, bool, u32, bool),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum SheetSel {
/// Unqualified: the formula's own sheet.
Current,
Named(String),
/// 3-D span `Sheet1:Sheet3`.
Span(String, String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Reference {
pub sheet: SheetSel,
pub kind: RefKind,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum UnOp {
Neg,
Plus,
Percent,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum BinOp {
Add,
Sub,
Mul,
Div,
Pow,
Concat,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
/// `:` between two references.
Range,
/// `,` inside parentheses.
Union,
/// A space between two references.
Intersect,
}
impl BinOp {
/// How tightly the operator binds, as formula text is read: `:` 90, space 85, `,` 80, `^` 50,
/// `*` `/` 40, `+` `-` 30, `&` 20, comparisons 10.
pub(crate) fn precedence(self) -> u8 {
match self {
BinOp::Range => 90,
BinOp::Intersect => 85,
BinOp::Union => 80,
BinOp::Pow => 50,
BinOp::Mul | BinOp::Div => 40,
BinOp::Add | BinOp::Sub => 30,
BinOp::Concat => 20,
BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => 10,
}
}
fn symbol(self) -> &'static str {
match self {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Pow => "^",
BinOp::Concat => "&",
BinOp::Eq => "=",
BinOp::Ne => "<>",
BinOp::Lt => "<",
BinOp::Gt => ">",
BinOp::Le => "<=",
BinOp::Ge => ">=",
BinOp::Range => ":",
BinOp::Union => ",",
BinOp::Intersect => " ",
}
}
}
/// A formula's syntax tree. The decoder bounds its depth, so printing and dropping it recurse
/// safely.
#[derive(Debug, PartialEq)]
pub(crate) enum Expr {
Number(f64),
Text(Arc<str>),
Bool(bool),
Error(CellError),
/// Array constant `{1,2;3,4}` (rows of literals).
Array(Vec<Vec<Expr>>),
Ref(Reference),
/// A defined name.
Name(String),
Unary(UnOp, Box<Expr>),
Binary(BinOp, Box<Expr>, Box<Expr>),
Call(String, Vec<Expr>),
/// An omitted argument.
Missing,
Paren(Box<Expr>),
}
fn push_anchor(s: &mut String, a: &Anchor) {
if a.col_abs {
s.push('$');
}
push_col(s, a.col);
if a.row_abs {
s.push('$');
}
let _ = write!(s, "{}", u64::from(a.row) + 1);
}
fn push_reference(s: &mut String, r: &Reference) {
match &r.sheet {
SheetSel::Current => {}
SheetSel::Named(n) => {
s.push_str(&quote_sheet(n));
s.push('!');
}
SheetSel::Span(a, b) => {
let joined = format!("{a}:{b}");
if sheet_needs_quotes(a) || sheet_needs_quotes(b) {
let _ = write!(s, "'{}'!", joined.replace('\'', "''"));
} else {
let _ = write!(s, "{joined}!");
}
}
}
let dollar = |abs: bool| if abs { "$" } else { "" };
match &r.kind {
RefKind::Cell(a) => push_anchor(s, a),
RefKind::Range(a, b) => {
push_anchor(s, a);
s.push(':');
push_anchor(s, b);
}
RefKind::Rows(r0, a0, r1, a1) => {
let _ = write!(s, "{}{}:{}{}", dollar(*a0), u64::from(*r0) + 1, dollar(*a1), u64::from(*r1) + 1);
}
RefKind::Cols(c0, a0, c1, a1) => {
s.push_str(dollar(*a0));
push_col(s, *c0);
s.push(':');
s.push_str(dollar(*a1));
push_col(s, *c1);
}
}
}
/// Removes trailing zeros (and a trailing point) from a decimal string.
fn trim_decimal(s: &str) -> String {
if s.contains('.') {
let t = s.trim_end_matches('0').trim_end_matches('.');
if t == "-0" { "0".into() } else { t.to_string() }
} else {
s.to_string()
}
}
/// Scientific text like `1.23456789012346E+15` with up to `sig` significant digits.
fn sci_text(n: f64, sig: usize) -> String {
let s = format!("{:.*e}", sig.saturating_sub(1), n);
let Some((mant, exp)) = s.split_once('e') else { return s };
let mant = trim_decimal(mant);
let e: i32 = exp.parse().unwrap_or(0);
format!("{}E{}{:02}", mant, if e < 0 { '-' } else { '+' }, e.abs())
}
/// A number as a formula spells it: up to 15 significant digits, scientific for very large or
/// very small magnitudes.
pub(crate) fn number_to_text(n: f64) -> String {
if n == 0.0 || !n.is_finite() {
return "0".into();
}
let a = n.abs();
if !(1e-9..1e15).contains(&a) && a.round() != a || a >= 1e15 {
return sci_text(n, 15);
}
// A value just below 1e15 can round up to 1e15 at 15 significant digits.
if format!("{a:.14e}").parse::<f64>().is_ok_and(|r| r >= 1e15) {
return sci_text(n, 15);
}
let mag = a.log10().floor() as i32;
let decimals = (14 - mag).clamp(0, 30) as usize;
trim_decimal(&format!("{n:.decimals$}"))
}
fn number(n: f64) -> String {
let text = number_to_text(n);
// 15 significant digits can round past f64::MAX, which no longer parses.
if text.parse::<f64>().is_ok_and(f64::is_finite) {
return text;
}
let exact = format!("{n:E}");
match exact.split_once('E') {
Some((m, e)) if !e.starts_with('-') => format!("{m}E+{e}"),
_ => exact,
}
}
/// Splits a sheet-qualified name at its last `!` outside brackets.
fn sheet_split(n: &str) -> Option<(&str, &str)> {
let mut depth = 0usize;
let mut escaped = false;
let mut at = None;
for (i, c) in n.char_indices() {
if escaped {
escaped = false;
continue;
}
match c {
'\'' if depth > 0 => escaped = true,
'[' => depth = depth.saturating_add(1),
']' => depth = depth.saturating_sub(1),
'!' if depth == 0 => at = Some(i),
_ => {}
}
}
at.and_then(|i| Some((n.get(..i)?, n.get(i + 1..)?)))
}
/// Prints an expression in A1 notation.
pub(crate) fn print(e: &Expr) -> String {
let mut s = String::new();
write_expr(&mut s, e);
s
}
fn write_args(s: &mut String, args: &[Expr], sep: char) {
for (i, a) in args.iter().enumerate() {
if i > 0 {
s.push(sep);
}
write_expr(s, a);
}
}
fn write_expr(s: &mut String, e: &Expr) {
match e {
Expr::Number(n) => s.push_str(&number(*n)),
Expr::Text(t) => {
s.push('"');
s.push_str(&t.replace('"', "\"\""));
s.push('"');
}
Expr::Bool(b) => s.push_str(if *b { "TRUE" } else { "FALSE" }),
Expr::Error(err) => s.push_str(err.as_str()),
Expr::Array(rows) => {
s.push('{');
for (ri, row) in rows.iter().enumerate() {
if ri > 0 {
s.push(';');
}
write_args(s, row, ',');
}
s.push('}');
}
Expr::Ref(r) => push_reference(s, r),
Expr::Name(n) => match sheet_split(n) {
// A sheet-qualified name holds the bare sheet name (`My Sheet!Total`): quote it so it parses back.
Some((sheet, name)) => {
s.push_str(&quote_sheet(sheet));
s.push('!');
s.push_str(name);
}
None => s.push_str(n),
},
Expr::Unary(UnOp::Neg, x) => {
s.push('-');
write_expr(s, x);
}
Expr::Unary(UnOp::Plus, x) => {
s.push('+');
write_expr(s, x);
}
Expr::Unary(UnOp::Percent, x) => {
write_expr(s, x);
s.push('%');
}
Expr::Binary(op, a, b) => {
write_expr(s, a);
s.push_str(op.symbol());
write_expr(s, b);
}
Expr::Call(name, args) => {
s.push_str(name);
s.push('(');
write_args(s, args, ',');
s.push(')');
}
Expr::Missing => {}
Expr::Paren(x) => {
s.push('(');
write_expr(s, x);
s.push(')');
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers_print_with_fifteen_significant_digits() {
assert_eq!(number_to_text(1.0), "1");
assert_eq!(number_to_text(-2.5), "-2.5");
assert_eq!(number_to_text(0.1 + 0.2), "0.3");
assert_eq!(number_to_text(123456789012345.0), "123456789012345");
assert_eq!(number_to_text(1e15), "1E+15");
assert_eq!(number_to_text(1234567890123456.0), "1.23456789012346E+15");
assert_eq!(number_to_text(0.0001), "0.0001");
assert_eq!(number_to_text(1e-10), "1E-10");
assert!(number(f64::MAX).parse::<f64>().is_ok_and(f64::is_finite));
}
}
+538
View File
@@ -0,0 +1,538 @@
//! BIFF12 parsed formulas ([MS-XLSB] 2.2.2, 2.5.98): reverse-Polish `Ptg` tokens back to an
//! expression tree, for cell formulas. The tokens are BIFF8's, but nearly every operand differs
//! (32-bit rows, 14-bit columns, UTF-16 strings, its own array-constant codes, newer functions
//! 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.
//!
//! Names keep their file spelling (`_xlfn.CONCAT`, `_xlpm.x`), as `.xlsx` formula text has them.
use std::sync::Arc;
use craft_sheet::{CellError, MAX_COLS, MAX_ROWS};
use super::expr::{Anchor, BinOp, Expr, RefKind, Reference, SheetSel, UnOp};
use super::ftab::function;
/// Formula token bytes decoded at most (a cell formula holds at most 16,384).
pub(super) const MAX_RGCE: usize = 16 * 1024;
/// Values in one array constant at most.
const MAX_ARRAY: usize = 65_536;
/// Why a formula could not be turned back into text.
#[derive(Debug, PartialEq)]
pub(super) struct Unsupported(pub &'static str);
/// Expression nesting at most: deeper trees would overflow the stack when printed or dropped (a
/// hostile file can chain thousands of PtgParen tokens).
const MAX_DEPTH: usize = 256;
/// Operand stack entries: an expression and its nesting depth.
type Stack = Vec<(Expr, usize)>;
/// Pops an operand, recording its depth in `kids` (the deepest operand this token consumed).
fn pop(stack: &mut Stack, kids: &mut usize) -> Result<Expr, Unsupported> {
let (e, d) = stack.pop().ok_or(Unsupported("malformed formula (operand missing)"))?;
*kids = (*kids).max(d);
Ok(e)
}
fn pop_n(stack: &mut Stack, n: usize, kids: &mut usize) -> Result<Vec<Expr>, Unsupported> {
let at = stack.len().checked_sub(n).ok_or(Unsupported("malformed formula (argument missing)"))?;
let popped = stack.split_off(at);
*kids = popped.iter().map(|(_, d)| *d).fold(*kids, usize::max);
Ok(popped.into_iter().map(|(e, _)| e).collect())
}
/// Pushes the result of a token one level above the operands it consumed.
fn push(stack: &mut Stack, kids: usize, e: Expr) -> Result<(), Unsupported> {
let depth = kids.saturating_add(1);
if depth > MAX_DEPTH {
return Err(Unsupported("formula nested too deeply"));
}
stack.push((e, depth));
Ok(())
}
/// An error constant's code ([MS-XLSB] 2.5.98.21, `BErr`).
fn error_code(code: u8) -> Option<CellError> {
Some(match code {
0x00 => CellError::Null,
0x07 => CellError::Div0,
0x0F => CellError::Value,
0x17 => CellError::Ref,
0x1D => CellError::Name,
0x24 => CellError::Num,
0x2A => CellError::NA,
0x2B => CellError::GettingData,
_ => return None,
})
}
/// A supporting link record ([MS-XLSB] 2.2.7.3), in the order of the workbook's externals block.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Book {
/// `BrtSupSelf`: this workbook.
This,
/// `BrtSupSame`: the caller's sheet (macro sheets and names only).
Same,
/// `BrtSupAddin`: add-in functions.
Addin,
/// `BrtSupBookSrc`: another workbook, DDE or OLE. Never followed.
External,
}
/// Xti ([MS-XLSB] 2.5.173): a supporting link and a range of its sheets.
#[derive(Clone, Copy, Debug)]
pub(super) struct Xti {
pub book: u32,
pub first: i32,
pub last: i32,
}
/// A `BrtName` as far as cell formulas need it.
#[derive(Clone, Debug)]
pub(super) struct Name {
/// The name as written (`_xlfn.CONCAT`, `_xlpm.x`, `Rate`).
pub text: String,
/// `fFutureFunction`: a hidden name standing for a function newer than the file format.
pub future: bool,
}
/// Workbook-level tables formulas refer to.
#[derive(Default)]
pub(crate) struct Links {
/// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold
/// no cells (chart, dialog and macro sheets).
pub(super) sheets: Vec<Option<String>>,
/// Every `BrtName`, in order: `PtgName` indexes them from 1.
pub(super) names: Vec<Name>,
pub(super) books: Vec<Book>,
pub(super) xti: Vec<Xti>,
}
/// Whether a name stands for a LET parameter (`_xlpm.x`), which only the formula itself defines.
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> {
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")),
}
// -1: "the first sheet in the reference cannot be found" (a deleted sheet).
if x.first == -1 || x.last == -1 {
return Ok(None);
}
// -2 (the whole workbook) is for names, not cell references.
let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else {
return Err(Unsupported("reference to a missing sheet"));
};
// A span takes every sheet between its ends, so a skipped one anywhere in it changes
// what the reference means.
let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?;
if span.iter().any(Option::is_none) {
return Err(Unsupported("references to sheets the reader skips"));
}
let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else {
return Err(Unsupported("reference to a missing sheet"));
};
Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) }))
}
}
struct Bytes<'a> {
b: &'a [u8],
pos: usize,
}
const TRUNC: Unsupported = Unsupported("truncated formula");
impl Bytes<'_> {
fn take(&mut self, n: usize) -> Result<&[u8], Unsupported> {
let s = self.b.get(self.pos..self.pos.checked_add(n).ok_or(TRUNC)?).ok_or(TRUNC)?;
self.pos += n;
Ok(s)
}
fn u8(&mut self) -> Result<u8, Unsupported> {
self.take(1)?.first().copied().ok_or(TRUNC)
}
fn u16(&mut self) -> Result<u16, Unsupported> {
let s: [u8; 2] = self.take(2)?.try_into().map_err(|_| TRUNC)?;
Ok(u16::from_le_bytes(s))
}
fn u32(&mut self) -> Result<u32, Unsupported> {
let s: [u8; 4] = self.take(4)?.try_into().map_err(|_| TRUNC)?;
Ok(u32::from_le_bytes(s))
}
fn f64(&mut self) -> Result<f64, Unsupported> {
let s: [u8; 8] = self.take(8)?.try_into().map_err(|_| TRUNC)?;
let n = f64::from_le_bytes(s);
if !n.is_finite() {
return Err(Unsupported("invalid number constant"));
}
Ok(n)
}
/// `cch` UTF-16 code units; unpaired surrogates are refused, not replaced.
fn chars(&mut self, cch: usize) -> Result<String, Unsupported> {
let units = self.take(cch.checked_mul(2).ok_or(TRUNC)?)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c));
char::decode_utf16(units).collect::<Result<String, _>>().map_err(|_| Unsupported("invalid text constant"))
}
}
/// The last row and column of the grid: a range from 0 to these is a whole column or row.
const LAST_ROW: u32 = MAX_ROWS - 1;
const LAST_COL: u32 = MAX_COLS - 1;
/// RgceLoc ([MS-XLSB] 2.5.98.91): a row and a `ColRelShort` (14-bit column, then the
/// column-relative and row-relative flags). In a cell formula the coordinates are absolute
/// whatever the flags say; the flags only decide where `$` goes.
fn anchor(row: u32, col: u16) -> Result<Anchor, Unsupported> {
let c = u32::from(col & 0x3FFF);
if row > LAST_ROW || c > LAST_COL {
return Err(Unsupported("reference outside the grid"));
}
Ok(Anchor { row, col: c, row_abs: col & 0x8000 == 0, col_abs: col & 0x4000 == 0 })
}
/// 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> {
let (a, b) = (anchor(r0, c0)?, anchor(r1, c1)?);
Ok(if a.row == 0 && b.row == LAST_ROW {
RefKind::Cols(a.col, a.col_abs, b.col, b.col_abs)
} else if a.col == 0 && b.col == LAST_COL {
RefKind::Rows(a.row, a.row_abs, b.row, b.row_abs)
} else {
RefKind::Range(a, b)
})
}
fn reference(sheet: Option<SheetSel>, kind: RefKind) -> Expr {
match sheet {
Some(sheet) => Expr::Ref(Reference { sheet, kind }),
None => Expr::Error(CellError::Ref),
}
}
/// Decodes a cell formula's `rgce` (tokens) with its `rgcb` (extra data for array constants and
/// cached areas). Returns the expression, with names in their file spelling, or why it can't be
/// represented.
pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links) -> Result<Expr, Unsupported> {
if rgce.len() > MAX_RGCE {
return Err(Unsupported("formula too long"));
}
let mut t = Bytes { b: rgce, pos: 0 };
let mut extra = Bytes { b: rgcb, pos: 0 };
let mut stack: Stack = Vec::new();
while t.pos < rgce.len() {
let ptg = t.u8()?;
let mut kids = 0usize;
// Operand classes (reference / value / array) share one token number.
let id = if ptg & 0x60 != 0 { 0x20 | (ptg & 0x1F) } else { ptg };
match id {
0x03..=0x11 => {
let op = match id {
0x03 => BinOp::Add,
0x04 => BinOp::Sub,
0x05 => BinOp::Mul,
0x06 => BinOp::Div,
0x07 => BinOp::Pow,
0x08 => BinOp::Concat,
0x09 => BinOp::Lt,
0x0A => BinOp::Le,
0x0B => BinOp::Eq,
0x0C => BinOp::Ge,
0x0D => BinOp::Gt,
0x0E => BinOp::Ne,
0x0F => BinOp::Intersect,
0x10 => BinOp::Union,
_ => BinOp::Range,
};
let b = pop(&mut stack, &mut kids)?;
let a = pop(&mut stack, &mut kids)?;
push(&mut stack, kids, Expr::Binary(op, Box::new(a), Box::new(b)))?;
}
0x12..=0x14 => {
let op = match id {
0x12 => UnOp::Plus,
0x13 => UnOp::Neg,
_ => UnOp::Percent,
};
let x = pop(&mut stack, &mut kids)?;
push(&mut stack, kids, Expr::Unary(op, Box::new(x)))?;
}
0x15 => {
let x = pop(&mut stack, &mut kids)?;
push(&mut stack, kids, Expr::Paren(Box::new(x)))?;
}
0x16 => push(&mut stack, kids, Expr::Missing)?,
// PtgStr: cch (u16, at most 255) and always UTF-16, unlike BIFF8.
0x17 => {
let cch = usize::from(t.u16()?);
if cch > 255 {
return Err(Unsupported("text constant too long"));
}
push(&mut stack, kids, Expr::Text(Arc::from(t.chars(cch)?)))?;
}
0x19 => {
let kind = t.u8()?;
if kind & 0x04 != 0 {
// PtgAttrChoose: jump table of cOffset + 1 entries.
let n = usize::from(t.u16()?) + 1;
t.take(n * 2)?;
} else {
t.take(2)?;
}
if kind & 0x10 != 0 {
// PtgAttrSum: SUM of the single operand.
let x = pop(&mut stack, &mut kids)?;
push(&mut stack, kids, Expr::Call("SUM".into(), vec![x]))?;
}
// Semi (volatile), If, Goto, Space, Baxcel: no effect on the expression.
}
0x1C => {
let e = t.u8()?;
push(&mut stack, kids, Expr::Error(error_code(e).ok_or(Unsupported("unknown error constant"))?))?;
}
0x1D => push(&mut stack, kids, Expr::Bool(t.u8()? != 0))?,
0x1E => push(&mut stack, kids, Expr::Number(f64::from(t.u16()?)))?,
0x1F => {
let n = t.f64()?;
push(&mut stack, kids, Expr::Number(n))?;
}
0x20 => {
// PtgArray: 14 unused bytes (Excel leaves junk in them); the values are in rgcb.
t.take(14)?;
push(&mut stack, kids, array_constant(&mut extra)?)?;
}
0x21 => {
let iftab = t.u16()?;
let (name, args) = function(iftab).ok_or(Unsupported("unknown function"))?;
let n = usize::try_from(args).map_err(|_| Unsupported("unknown function argument count"))?;
let args = pop_n(&mut stack, n, &mut kids)?;
push(&mut stack, kids, Expr::Call(name.to_string(), args))?;
}
0x22 => {
let n = usize::from(t.u8()?);
let tab = t.u16()?;
if tab & 0x8000 != 0 {
return Err(Unsupported("macro sheet commands"));
}
let mut args = pop_n(&mut stack, n, &mut kids)?;
if tab & 0x7FFF == 0x00FF {
// A function called by name (a newer function, add-in or LAMBDA): the name
// comes first, as a PtgName to a hidden `_xlfn.` name for a newer function.
if args.is_empty() {
return Err(Unsupported("malformed function call"));
}
let name = match args.remove(0) {
Expr::Name(n) => n,
_ => return Err(Unsupported("function call through an expression")),
};
// `PY(index, …)` runs a script stored elsewhere in the package, which this
// import doesn't read. `PY("code", …)` carries its code and becomes a Python cell.
let py = name.rsplit('.').next().is_some_and(|n| n.eq_ignore_ascii_case("PY"));
if py && !args.first().is_some_and(literal_text) {
return Err(Unsupported("Python code stored outside the cell"));
}
push(&mut stack, kids, Expr::Call(name, args))?;
} else {
let (name, _) = function(tab & 0x7FFF).ok_or(Unsupported("unknown function"))?;
push(&mut stack, kids, Expr::Call(name.to_string(), args))?;
}
}
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"));
}
push(&mut stack, kids, Expr::Name(name.text.clone()))?;
}
0x24 => {
let (r, c) = (t.u32()?, t.u16()?);
push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: RefKind::Cell(anchor(r, c)?) }))?;
}
0x25 => {
let (r0, r1, c0, c1) = (t.u32()?, t.u32()?, t.u16()?, t.u16()?);
push(&mut stack, kids, Expr::Ref(Reference { sheet: SheetSel::Current, kind: area(r0, r1, c0, c1)? }))?;
}
0x26 => {
// PtgMemArea: a cache of the sub-expression that follows. Its rgcb entry
// (PtgExtraMem) is a count and 16 bytes per cached area: skip both.
t.take(6)?;
let n = usize::try_from(extra.u32()?).map_err(|_| TRUNC)?;
extra.take(n.checked_mul(16).ok_or(TRUNC)?)?;
}
// PtgMemErr, PtgMemNoMem: 6 bytes; PtgMemFunc: 2. The sub-expression follows them.
0x27 | 0x28 => {
t.take(6)?;
}
0x29 => {
t.take(2)?;
}
0x2A => {
t.take(6)?;
push(&mut stack, kids, Expr::Error(CellError::Ref))?;
}
0x2B => {
t.take(12)?;
push(&mut stack, kids, Expr::Error(CellError::Ref))?;
}
0x39 => return Err(Unsupported("names in other workbooks or on other sheets")),
0x3A => {
let ixti = t.u16()?;
let (r, c) = (t.u32()?, t.u16()?);
let kind = RefKind::Cell(anchor(r, c)?);
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)?;
push(&mut stack, kids, reference(links.sheet_sel(ixti)?, kind))?;
}
0x3C | 0x3D => {
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))?;
}
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 | 0x2D => return Err(Unsupported("relative references in a cell formula")),
_ => return Err(Unsupported("unknown formula token")),
}
if stack.len() > MAX_RGCE {
return Err(Unsupported("formula too complex"));
}
}
let (e, _) = stack.pop().ok_or(Unsupported("malformed formula (operand missing)"))?;
if !stack.is_empty() {
return Err(Unsupported("malformed formula (unused operands)"));
}
Ok(grouped(e, false))
}
/// Text written out in the formula: a string, or strings joined with `&` (how Python code
/// longer than a string constant is written).
fn literal_text(e: &Expr) -> bool {
match e {
Expr::Text(_) => true,
Expr::Binary(BinOp::Concat, a, b) => literal_text(a) && literal_text(b),
_ => false,
}
}
/// How tightly an expression binds when printed, as the formula parser reads it: binary
/// operators by [`BinOp::precedence`], prefix `+`/`-` at 60 and `%` at 70 (`-2^2` is `(-2)^2`);
/// anything else is one operand.
fn binding(e: &Expr) -> u8 {
match e {
Expr::Binary(op, ..) => op.precedence(),
Expr::Unary(UnOp::Neg | UnOp::Plus, _) => 60,
Expr::Unary(UnOp::Percent, _) => 70,
_ => u8::MAX,
}
}
/// The strings of a [`literal_text`] chain, left to right.
fn flatten_texts(e: Expr, out: &mut Vec<Expr>) {
match e {
Expr::Binary(BinOp::Concat, a, b) => {
flatten_texts(*a, out);
flatten_texts(*b, out);
}
text => out.push(text),
}
}
fn paren(e: Expr) -> Expr {
Expr::Paren(Box::new(e))
}
/// The tree with the parentheses its text needs to mean the same. Tokens are reverse Polish, so
/// grouping lives in their order: Excel adds `PtgParen` wherever its text has parentheses, but
/// `1 2 + 3 *` (another writer's, or a crafted file's) would print as `1+2*3`. Binary operators
/// are left-associative; a union in a function's arguments needs parentheses of its own.
/// Recursion follows the decoded tree, which `push` bounds at `MAX_DEPTH` levels.
fn grouped(e: Expr, in_args: bool) -> Expr {
match e {
// Strings joined with `&` are written as one flat chain, however their tokens nest: `&`
// is associative, and `PY("…"&"…")` is read as Python code only in that form.
e @ Expr::Binary(BinOp::Concat, ..) if literal_text(&e) => {
let mut texts = vec![];
flatten_texts(e, &mut texts);
let mut texts = texts.into_iter();
let first = texts.next().unwrap_or(Expr::Missing);
texts.fold(first, |a, b| Expr::Binary(BinOp::Concat, Box::new(a), Box::new(b)))
}
Expr::Binary(op, a, b) => {
let (a, b) = (grouped(*a, in_args), grouped(*b, in_args));
let a = if binding(&a) < op.precedence() { paren(a) } else { a };
let b = if binding(&b) <= op.precedence() { paren(b) } else { b };
let e = Expr::Binary(op, Box::new(a), Box::new(b));
if in_args && op == BinOp::Union { paren(e) } else { e }
}
Expr::Unary(op, x) => {
let x = grouped(*x, in_args);
// Each of `+`, `-` and `%` takes an operand that binds at least as tightly as a sign. The
// parser reads `-1%` as `-(1%)`, Excel's tokens as `(-1)%`: the same value, so a sign
// under `%` keeps Excel's spelling.
let needs = binding(&x) < 60;
Expr::Unary(op, Box::new(if needs { paren(x) } else { x }))
}
Expr::Paren(x) => paren(grouped(*x, false)),
Expr::Call(name, args) => Expr::Call(name, args.into_iter().map(|a| grouped(a, true)).collect()),
other => other,
}
}
/// PtgExtraArray ([MS-XLSB] 2.5.98.41): row and column counts, then `SerAr` values in row-major
/// order. The type codes are not BIFF8's: 0x00 is a number here, not a missing value.
fn array_constant(b: &mut Bytes<'_>) -> Result<Expr, Unsupported> {
let rows = usize::try_from(b.u32()?).map_err(|_| TRUNC)?;
let cols = usize::try_from(b.u32()?).map_err(|_| TRUNC)?;
if rows == 0 || cols == 0 || rows.saturating_mul(cols) > MAX_ARRAY {
return Err(Unsupported("array constant too large or empty"));
}
let mut out = Vec::with_capacity(rows);
for _ in 0..rows {
let mut row = Vec::with_capacity(cols);
for _ in 0..cols {
row.push(match b.u8()? {
0x00 => Expr::Number(b.f64()?),
0x01 => {
let cch = usize::from(b.u16()?);
if cch > 255 {
return Err(Unsupported("text constant too long"));
}
Expr::Text(Arc::from(b.chars(cch)?))
}
0x02 => Expr::Bool(b.u8()? != 0),
0x04 => {
let e = b.u8()?;
b.take(3)?;
Expr::Error(error_code(e).ok_or(Unsupported("unknown error constant"))?)
}
_ => return Err(Unsupported("unknown array constant value")),
});
}
out.push(row);
}
Ok(Expr::Array(out))
}
-2
View File
@@ -487,8 +487,6 @@ const FTAB: &[(u16, &str, i8)] = &[
/// The function a `PtgFunc` or `PtgFuncVar` calls: its name and fixed argument count (-1 when it
/// takes a variable number). `None` for a number the table doesn't assign.
// The formula decoder calls it.
#[allow(dead_code)]
pub(super) fn function(iftab: u16) -> Option<(&'static str, i8)> {
FTAB.binary_search_by_key(&iftab, |(i, _, _)| *i).ok().and_then(|i| FTAB.get(i)).map(|(_, n, a)| (*n, *a))
}
+17 -2
View File
@@ -3,12 +3,15 @@
use std::collections::BTreeMap;
use craft_xlsx::{CellRef, Error, Limits, Package, Range, SheetKind, Value, Visibility};
use craft_xlsx::{CellFormula, CellRef, Error, Limits, Package, Range, SheetKind, Value, Visibility};
pub struct Sheet {
pub name: String,
pub visibility: Visibility,
pub cells: BTreeMap<CellRef, Value>,
/// Every cell's formula, and its cell metadata index (`cm`), where it has one.
pub formulas: BTreeMap<CellRef, CellFormula>,
pub cell_metadata: BTreeMap<CellRef, u32>,
pub merges: Vec<Range>,
}
@@ -16,6 +19,11 @@ impl Sheet {
pub fn value(&self, at: CellRef) -> Value {
self.cells.get(&at).cloned().unwrap_or_default()
}
/// The formula text at `at`, if the cell has a formula.
pub fn formula(&self, at: CellRef) -> Option<&str> {
self.formulas.get(&at).map(|f| f.text.as_str())
}
}
pub struct Book {
@@ -37,8 +45,15 @@ pub fn read_with(bytes: &[u8], limits: &Limits) -> Result<Book, Error> {
continue;
}
let mut cells = BTreeMap::new();
let (mut formulas, mut cell_metadata) = (BTreeMap::new(), BTreeMap::new());
let part = p.sheet(e, |row| {
for c in row.cells {
if let Some(f) = c.formula {
formulas.insert(c.at, f);
}
if let Some(m) = c.cell_metadata {
cell_metadata.insert(c.at, m);
}
let v = c.value.resolve(&strings, wb.date_1904);
if !v.is_empty() {
cells.insert(c.at, v);
@@ -46,7 +61,7 @@ pub fn read_with(bytes: &[u8], limits: &Limits) -> Result<Book, Error> {
}
Ok(())
})?;
book.sheets.push(Sheet { name: e.name.clone(), visibility: e.visibility, cells, merges: part.merges });
book.sheets.push(Sheet { name: e.name.clone(), visibility: e.visibility, cells, formulas, cell_metadata, merges: part.merges });
}
book.warnings = p.warnings().to_vec();
Ok(book)
+267
View File
@@ -442,3 +442,270 @@ fn phonetic_runs_use_documented_size_and_tolerate_padding() {
}
assert!(read(&file(&worksheet(&[row(0), cell(62, 0, &phonetic(2, 12))].concat(), &[]))).is_err());
}
/// A formula cell (`BrtFmlaNum`, cached 0) with its own tokens and extra data.
fn formula_cell(col: u32, rgce: &[u8], rgcb: &[u8]) -> Vec<u8> {
let mut data = col.to_le_bytes().to_vec();
data.extend([0; 4]);
data.extend(0f64.to_le_bytes());
data.extend([0; 2]);
data.extend((rgce.len() as u32).to_le_bytes());
data.extend(rgce);
data.extend((rgcb.len() as u32).to_le_bytes());
data.extend(rgcb);
record(9, &data)
}
fn name(flags: u32, text: &str) -> Vec<u8> {
let mut data = flags.to_le_bytes().to_vec();
data.push(0);
data.extend(u32::MAX.to_le_bytes()); // Workbook scope.
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.
record(39, &data)
}
/// `book(false)` with a supporting-link block and defined names, as Excel writes them: this
/// workbook, then another; sheet links Data, Hidden, Data:Hidden, a deleted sheet and the other
/// workbook; hidden names for CONCAT, LET and a LET parameter, then the user name Rate.
fn book_with_links(extern_sheet: &[u8]) -> Vec<u8> {
let plain = book(false);
let end = record(132, &[]);
let mut out = plain[..plain.len() - end.len()].to_vec();
out.extend(record(353, &[]));
out.extend(record(357, &[]));
out.extend(record(355, &wide("rId9")));
out.extend(record(362, extern_sheet));
out.extend(record(354, &[]));
const FUTURE: u32 = 0x0002_000b;
out.extend(name(FUTURE, "_xlfn.CONCAT"));
out.extend(name(FUTURE, "_xlfn.LET"));
out.extend(name(0x0002_0019, "_xlpm.x"));
out.extend(name(0, "Rate"));
out.extend(end);
out
}
fn xti() -> Vec<u8> {
let mut out = 5u32.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)] {
out.extend(book.to_le_bytes());
out.extend(first.to_le_bytes());
out.extend(last.to_le_bytes());
}
out
}
fn loc(row: u32, col: u16) -> Vec<u8> {
[&row.to_le_bytes()[..], &col.to_le_bytes()].concat()
}
/// Relative row and column flags of a `ColRelShort`.
const REL: u16 = 0xC000;
fn warned(wb: &common::Book, why: &str) -> bool {
wb.warnings.iter().any(|w| w.contains("kept as their saved results") && w.contains(why))
}
#[test]
fn cell_formulas_are_read_in_their_xlsx_spelling() {
let ref3d = |ixti: u16, row: u32, col: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(row, col)].concat();
let cells: Vec<(u32, Vec<u8>, Vec<u8>)> = vec![
(1, [&[0x44u8][..], &loc(0, REL), &[0x1E, 2, 0, 0x05]].concat(), vec![]),
(2, [&[0x25u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &0u16.to_le_bytes(), &REL.to_le_bytes(), &[0x19, 0x10, 0, 0]].concat(), vec![]),
(3, ref3d(1, 1, 1 | REL), vec![]),
(
4,
[
&[0x3Bu8][..],
&2u16.to_le_bytes(),
&0u32.to_le_bytes(),
&0xFFFFFu32.to_le_bytes(),
&REL.to_le_bytes(),
&REL.to_le_bytes(),
&[0x19, 0x10, 0, 0],
]
.concat(),
vec![],
),
(5, [&[0x23u8, 1, 0, 0, 0][..], &[0x17, 1, 0, b'a', 0], &[0x17, 1, 0, b'b', 0], &[0x42, 3, 0xFF, 0]].concat(), vec![]),
(6, vec![0x1E, 1, 0, 0x1E, 0, 0, 0x06, 0x1E, 2, 0, 0x41, 0xE0, 0x01], vec![]),
(
7,
[&[0x60u8][..], &[0xAB; 14]].concat(),
[&2u32.to_le_bytes()[..], &2u32.to_le_bytes(), &[0], &1f64.to_le_bytes(), &[1, 1, 0, b'x', 0], &[2, 1], &[4, 0x2A, 0, 0, 0]].concat(),
),
(
8,
[&[0x23u8, 2, 0, 0, 0][..], &[0x23, 3, 0, 0, 0, 0x1E, 1, 0], &[0x23, 3, 0, 0, 0, 0x1E, 1, 0, 0x03], &[0x42, 4, 0xFF, 0]].concat(),
vec![],
),
(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![]),
(12, ref3d(4, 0, REL), vec![]),
(13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]),
];
let mut data = row(0);
data.extend(cell(5, 0, &3.0f64.to_le_bytes()));
for (col, rgce, rgcb) in &cells {
data.extend(formula_cell(*col, rgce, rgcb));
}
data.extend(record(49, &1u32.to_le_bytes())); // BrtCellMeta: the next formula is a dynamic array.
data.extend(formula_cell(14, &[0x1E, 1, 0], &[]));
data.extend(formula_cell(15, &[0x18, 0x19, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], &[])); // A table reference.
let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
let sheet = &wb.sheets[0];
let got: Vec<_> = (1..=15).map(|c| sheet.formula(CellRef::new(0, c))).collect();
// As an `.xlsx` file holds them: names in their file spelling (`_xlfn.`, `_xlpm.`), and no
// implicit `@`, which is how a calculator reads a legacy formula, not how the file spells it.
let want = [
Some("A1*2"),
Some("SUM($A$1:A2)"),
Some("Hidden!B2"),
Some("SUM(Data:Hidden!A:A)"),
Some("_xlfn.CONCAT(\"a\",\"b\")"),
Some("IFERROR(1/0,2)"),
Some("{1,\"x\";TRUE,#N/A}"),
Some("_xlfn.LET(_xlpm.x,1,_xlpm.x+1)"),
Some("#REF!"),
Some("SQRT(A1:A2)"),
None,
None,
None,
// A BrtCellMeta before a cell gives it cell metadata, as `cm` does in .xlsx.
Some("1"),
None,
];
assert_eq!(got, want);
assert!(sheet.formulas.values().all(|f| f.kind == craft_xlsx::FormulaKind::Normal));
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", "the shared or array formula form", "structured table references"] {
assert!(warned(&wb, why), "{why}: {:?}", wb.warnings);
}
}
#[test]
fn unreadable_links_keep_every_formula_as_its_value() {
// A sheet-link count past the spec's limit, and a truncated list: the file opens as it did
// before formulas were read, with values only.
let mut truncated = xti();
truncated.truncate(20);
for extern_sheet in [70_000u32.to_le_bytes().to_vec(), truncated] {
let data = [row(0), formula_cell(0, &[0x1E, 1, 0], &[])].concat();
let wb = read(&package(&book_with_links(&extern_sheet), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None);
assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0));
assert!(wb.warnings.iter().any(|w| w.contains("could not be read")), "{:?}", wb.warnings);
}
}
#[test]
fn formula_grouping_follows_token_order() {
// Reverse-Polish tokens group by their order alone. Excel writes PtgParen wherever its text
// has parentheses, but another writer need not: the text gets the ones it needs to mean the
// same, and no more.
let a = |row: u32| [&[0x24u8][..], &loc(row, REL)].concat();
let int = |n: u8| vec![0x1E, n, 0];
let cases: Vec<(Vec<u8>, &str)> = vec![
([int(1), int(2), vec![0x03], int(3), vec![0x05]].concat(), "(1+2)*3"),
([int(1), int(2), int(3), vec![0x04, 0x04]].concat(), "1-(2-3)"),
([int(1), int(2), vec![0x04], int(3), vec![0x04]].concat(), "1-2-3"),
([int(2), vec![0x13], int(2), vec![0x07]].concat(), "-2^2"),
([int(2), int(2), vec![0x07, 0x13]].concat(), "-(2^2)"),
// Excel's tokens for its own `-1%`: the parser groups it the other way, to the same value.
([int(1), vec![0x13, 0x14]].concat(), "-1%"),
([int(1), int(2), vec![0x03, 0x14]].concat(), "(1+2)%"),
([a(0), a(1), vec![0x10], vec![0x19, 0x10, 0, 0]].concat(), "SUM((A1,A2))"),
([int(1), int(2), vec![0x03, 0x15], int(3), vec![0x05]].concat(), "(1+2)*3"),
];
let data: Vec<u8> =
[row(0)].into_iter().chain(cases.iter().enumerate().map(|(i, (rgce, _))| formula_cell(i as u32, rgce, &[]))).flatten().collect();
let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
for (i, (_, want)) in cases.iter().enumerate() {
assert_eq!(wb.sheets[0].formula(CellRef::new(0, i as u32)), Some(*want));
}
}
#[test]
fn functions_past_0x17b_are_called_by_number() {
// [MS-XLSB] 2.5.98.10's numbers past the .xls table: IFERROR (PtgFunc) and SUMIFS (PtgFuncVar).
let sumifs = [&[0x1Eu8, 1, 0][..], &[0x1E, 2, 0], &[0x17, 2, 0, b'>', 0, b'0', 0], &[0x42, 3, 0xE2, 0x01]].concat();
let data = [row(0), formula_cell(0, &[0x1E, 1, 0, 0x1E, 2, 0, 0x41, 0xE0, 0x01], &[]), formula_cell(1, &sumifs, &[])].concat();
let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), Some("IFERROR(1,2)"));
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), Some("SUMIFS(1,2,\">0\")"));
}
#[test]
fn hostile_formula_tokens_keep_the_saved_result() {
let mut deep = vec![0x1E, 1, 0];
deep.resize(16384, 0x15); // Thousands of parentheses: refused by the depth guard.
let empty_array = [&[0x60u8][..], &[0; 14]].concat();
let bad: Vec<(Vec<u8>, Vec<u8>)> = vec![
(vec![0x1E, 1], vec![]), // Truncated PtgInt.
(vec![0x17, 1, 0, 0x00, 0xD8], vec![]), // Unpaired surrogate.
(vec![0x17, 0, 1], vec![]), // Text constant over 255.
([&[0x24u8][..], &loc(1_048_576, 0)].concat(), vec![]), // Row past the grid.
(empty_array.clone(), vec![0; 8]), // Empty array constant.
(empty_array, vec![0xFF; 8]), // Huge one.
(vec![0x1E, 1, 0, 0x1E, 2, 0], vec![]), // Unused operand.
(vec![0x21, 0xD4, 0x01], vec![]), // Unassigned function number.
(vec![0x2C, 0, 0, 0, 0, 0, 0], vec![]), // PtgRefN outside a shared formula.
(vec![0x23, 0xFF, 0xFF, 0xFF, 0xFF], vec![]), // Name index past the list.
([&[0x5Au8][..], &9u16.to_le_bytes(), &loc(0, 0)].concat(), vec![]), // Missing sheet link.
(deep, vec![]),
];
for (rgce, rgcb) in bad {
let data = [row(0), formula_cell(0, &rgce, &rgcb)].concat();
let wb = read(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), None, "{rgce:?}");
assert_eq!(wb.sheets[0].value(CellRef::new(0, 0)), Value::Number(0.0));
}
}
#[test]
fn python_formulas_need_their_code_in_the_cell() {
// `PY("code", 0)` carries its code; `PY(0, 0)` names a script elsewhere in the package, which
// the reader doesn't read, and keeps its saved result.
let mut book = book_with_links(&xti());
let end = record(132, &[]);
book.truncate(book.len() - end.len());
book.extend(name(0x0002_000b, "_xlfn._xlws.PY"));
book.extend(end);
let py = |arg: &[u8]| [&[0x23u8, 5, 0, 0, 0][..], arg, &[0x1E, 0, 0, 0x42, 3, 0xFF, 0]].concat();
// Code longer than one string constant is written as constants joined with `&`.
let joined = [&[0x17u8, 1, 0, b'1', 0][..], &[0x17, 2, 0, b'+', 0, b'1', 0], &[0x08]].concat();
// The same strings nested to the right, `"1"&("+"&"1")` by token order: still one flat chain.
let nested = [&[0x17u8, 1, 0, b'1', 0][..], &[0x17, 1, 0, b'+', 0], &[0x17, 1, 0, b'1', 0], &[0x08, 0x08]].concat();
let data = [
row(0),
formula_cell(0, &py(&[0x17, 1, 0, b'1', 0]), &[]),
formula_cell(1, &py(&[0x1E, 0, 0]), &[]),
formula_cell(2, &py(&joined), &[]),
formula_cell(3, &py(&nested), &[]),
]
.concat();
let wb = read(&package(&book, &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES)).unwrap();
let formula = |c| wb.sheets[0].formula(CellRef::new(0, c));
assert_eq!(formula(0), Some("_xlfn._xlws.PY(\"1\",0)"));
assert_eq!(formula(1), None);
assert_eq!(formula(2), Some("_xlfn._xlws.PY(\"1\"&\"+1\",0)"));
assert_eq!(formula(3), Some("_xlfn._xlws.PY(\"1\"&\"+\"&\"1\",0)"));
assert!(warned(&wb, "Python code stored outside"), "{:?}", wb.warnings);
}
#[test]
fn formula_text_past_the_allowance_keeps_the_saved_result() {
// `1` takes the last byte of a one-byte allowance; the next formula doesn't fit.
let data = [row(0), formula_cell(0, &[0x1E, 1, 0], &[]), formula_cell(1, &[0x1E, 1, 0], &[])].concat();
let limits = Limits { binary_formula_text: 1, ..Limits::default() };
let wb = read_with(&package(&book_with_links(&xti()), &worksheet(&data, &[]), &sst(), ROOT, RELS, TYPES), &limits).unwrap();
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 0)), Some("1"));
assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), None);
assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings);
}