mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 07:44:40 +00:00
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.
73 lines
2.4 KiB
Rust
73 lines
2.4 KiB
Rust
//! A whole workbook read through the part layer, for assertions.
|
|
#![allow(dead_code)]
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
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>,
|
|
}
|
|
|
|
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 {
|
|
pub date_1904: bool,
|
|
pub sheets: Vec<Sheet>,
|
|
pub skipped: Vec<(String, SheetKind)>,
|
|
pub warnings: Vec<String>,
|
|
}
|
|
|
|
/// Reads every worksheet; cells with no value are left out.
|
|
pub fn read_with(bytes: &[u8], limits: &Limits) -> Result<Book, Error> {
|
|
let mut p = Package::open(bytes, limits)?;
|
|
let wb = p.workbook()?;
|
|
let strings = p.shared_strings()?;
|
|
let mut book = Book { date_1904: wb.date_1904, sheets: vec![], skipped: vec![], warnings: vec![] };
|
|
for e in &wb.sheets {
|
|
if e.kind != SheetKind::Worksheet {
|
|
book.skipped.push((e.name.clone(), e.kind.clone()));
|
|
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);
|
|
}
|
|
}
|
|
Ok(())
|
|
})?;
|
|
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)
|
|
}
|
|
|
|
pub fn read(bytes: &[u8]) -> Result<Book, Error> {
|
|
read_with(bytes, &Limits::default())
|
|
}
|