diff --git a/crates/xlsx/src/sheet.rs b/crates/xlsx/src/sheet.rs index 243430f8e..2691764d5 100644 --- a/crates/xlsx/src/sheet.rs +++ b/crates/xlsx/src/sheet.rs @@ -113,8 +113,10 @@ pub enum FormulaKind { #[derive(Clone, Debug, Default, PartialEq)] pub struct CellFormula { pub kind: FormulaKind, - /// The formula as the file spells it (`_xlfn.` prefixes and all), trimmed. Empty for a - /// cell that shares the formula of its group's first cell. + /// The formula as the file spells it (`_xlfn.` prefixes and all), trimmed. In `.xlsx`, empty + /// for a cell that shares the formula of its group's first cell. A binary workbook (`.xlsb`) + /// stores a shared formula's tokens once, so every cell of the group gets its own text, + /// decoded at the cell. pub text: String, /// `si`: the shared formula group. pub shared_index: Option, diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index aedbae5ab..dfea974b6 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -7,17 +7,18 @@ mod expr; mod formula; mod ftab; +use std::collections::HashMap; use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellFormula, CellValue, Row, SheetPart}; +use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, 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}; +use formula::{Book, Mode, Name, Xti}; pub(crate) const MAIN: &str = "application/vnd.ms-excel.main"; const BINARY_MAIN: &str = "application/vnd.ms-excel.sheet.binary.macroEnabled.main"; @@ -57,6 +58,11 @@ pub(crate) struct Budget { impl Budget { /// Charges a decoded formula's text if it fits [`Limits::binary_formula_text`]; a formula /// that doesn't keeps its saved result. + /// Whether formula text of `len` bytes would still fit, without charging it. + fn formula_text_fits(&self, len: usize, limits: &Limits) -> bool { + self.formula_text.get().saturating_add(len) <= limits.binary_formula_text + } + 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; @@ -506,22 +512,178 @@ fn cell_formula<'a>(b: &mut Bytes<'a>) -> Result<(&'a [u8], &'a [u8]), Error> { /// 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 { +fn decode_formula( + rgce: &[u8], + rgcb: &[u8], + mode: Mode, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, +) -> Option { let links = links?; - let text = match formula::decode(rgce, rgcb, links) { + let text = match formula::decode(rgce, rgcb, links, mode) { Ok(e) => expr::print(&e), Err(formula::Unsupported(why)) => { - warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})")); + kept(warnings, 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)"); + kept(warnings, "more formula text than the limits allow"); return None; } Some(text) } +/// Warns, once per reason, that formulas were kept as their saved results. +fn kept(warnings: &mut Warnings, why: &str) { + warnings.warn(format!("XLSB: some formulas were kept as their saved results ({why})")); +} + +/// A `BrtShrFmla`: the cells it may cover, its tokens, its first cell's text length (what a copy +/// is expected to cost) and the group's index (`si`). +struct SharedFormula { + range: Range, + rgce: Vec, + rgcb: Vec, + len: usize, + si: u32, +} + +/// A formula cell whose tokens are only `PtgExp`: part of a shared or array formula. +enum Part { + /// The first cell: its formula is the `BrtShrFmla` or `BrtArrFmla` record that follows. + First, + /// Another cell of the formula whose first cell is this. + Of(CellRef), +} + +/// `PtgExp` ([MS-XLSB] 2.5.98.40) with its `PtgExtraCol`: the first cell of the shared or array +/// formula this cell belongs to. BIFF12 keeps only the row in the token and the column in rgcb. +fn part_of(rgce: &[u8], rgcb: &[u8], at: CellRef) -> Option> { + let (&ptg, row) = rgce.split_first()?; + if ptg != 0x01 { + return None; + } + let (Ok(row), Ok(col)) = (<[u8; 4]>::try_from(row), <[u8; 4]>::try_from(rgcb)) else { + return Some(Err("malformed shared or array formula references")); + }; + let first = CellRef::new(u32::from_le_bytes(row), u32::from_le_bytes(col)); + Some(Ok(if first == at { Part::First } else { Part::Of(first) })) +} + +/// One sheet's shared and array formulas, by first cell, as the reader meets them. `None` when +/// the formula keeps its values (so its other cells keep theirs). +#[derive(Default)] +struct RangeFormulas { + shared: HashMap>>, + arrays: HashMap>, +} + +impl RangeFormulas { + /// The formula of another cell `at` of the shared or array formula starting at `first`. A + /// shared formula's copy is decoded from the shared tokens at its own cell, as [MS-XLSB] + /// defines it (relative offsets wrap around the grid), and checked like any formula: its text + /// may be longer than the first cell's. That length estimates its cost, and a copy that can't + /// fit isn't decoded at all, so a long formula shared by a million cells stops costing once + /// the allowance is spent. An array formula's other cells have none, as in `.xlsx`. + fn follower( + &self, + at: CellRef, + first: CellRef, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, + ) -> Option { + if let Some(shared) = self.shared.get(&first) { + let sh = shared.as_ref()?; + if !sh.range.contains(at) { + kept(warnings, "malformed shared or array formula references"); + return None; + } + if !budget.formula_text_fits(sh.len, limits) { + kept(warnings, "more formula text than the limits allow"); + return None; + } + let text = decode_formula(&sh.rgce, &sh.rgcb, Mode::Shared(at), links, limits, budget, warnings)?; + return Some(CellFormula { kind: FormulaKind::Shared, text, shared_index: Some(sh.si), ..CellFormula::default() }); + } + match self.arrays.get(&first) { + Some(Some(range)) if range.contains(at) => {} + Some(Some(_)) => kept(warnings, "malformed shared or array formula references"), + Some(None) => {} + None => kept(warnings, "a shared or array formula that is missing from the file"), + } + None + } + + /// `BrtShrFmla` (427) or `BrtArrFmla` (426) right after the first cell `first` of its range: + /// the formula that cell keeps, recorded for the range's other cells. A record that can't be + /// read leaves the whole range as values; it never fails the file, as these records were + /// skipped before formulas were read. + #[allow(clippy::too_many_arguments)] + fn range_formula( + &mut self, + id: u16, + payload: &[u8], + first: CellRef, + links: Option<&Links>, + limits: &Limits, + budget: &Budget, + warnings: &mut Warnings, + ) -> Option { + let read = |b: &mut Bytes<'_>| -> Result<(Range, Vec, Vec), Error> { + let (r0, r1, c0, c1) = (b.u32()?, b.u32()?, b.u32()?, b.u32()?); + if id == 426 { + b.u8()?; // Only fAlwaysCalc, the calculator's to know. + } + let cce = b.u32()?; + if !(1..=16384).contains(&cce) { + return Err(invalid("invalid formula token length")); + } + let rgce = b.take(cce as usize)?.to_vec(); + let cb = b.u32()?; + let rgcb = b.take(cb as usize)?.to_vec(); + if r0 > r1 || c0 > c1 || r1 >= MAX_ROWS || c1 >= MAX_COLS { + return Err(invalid("invalid formula range")); + } + let range = Range::new(CellRef::new(r0, c0), CellRef::new(r1, c1)); + // An array formula's first cell is its range's top-left one. A shared formula's is the + // first cell that uses it, which need not be: the top-left cell may not share it. + let start_ok = if id == 427 { range.contains(first) } else { range.first == first }; + if !start_ok { + return Err(invalid("invalid formula range")); + } + Ok((range, rgce, rgcb)) + }; + let mut b = Bytes::new(payload); + let Ok((range, rgce, rgcb)) = read(&mut b).and_then(|r| b.finish().map(|()| r)) else { + kept(warnings, "malformed shared or array formulas"); + self.shared.insert(first, None); + self.arrays.insert(first, None); + return None; + }; + if id == 427 { + let text = decode_formula(&rgce, &rgcb, Mode::Shared(first), links, limits, budget, warnings); + let si = u32::try_from(self.shared.len()).unwrap_or(u32::MAX); + let shared = text.as_ref().map(|t| Arc::new(SharedFormula { range, rgce, rgcb, len: t.len(), si })); + self.shared.insert(first, shared); + return text.map(|text| CellFormula { + kind: FormulaKind::Shared, + text, + shared_index: Some(si), + range: Some(range), + ..CellFormula::default() + }); + } + let text = decode_formula(&rgce, &rgcb, Mode::Cell, links, limits, budget, warnings); + self.arrays.insert(first, text.as_ref().map(|_| range)); + text.map(|text| CellFormula { kind: FormulaKind::Array, text, range: Some(range), ..CellFormula::default() }) + } +} + fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &Budget) -> Result { let value = match id { 1 => CellValue::Empty, @@ -579,11 +741,25 @@ pub(crate) fn worksheet( let mut row: Option = 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. + // does in `.xlsx`: a one-based index into the metadata part's cell metadata. Excel may write + // the cell's BrtValueMeta (a rich value, such as a spill error) in between. let mut meta: Option = None; + let mut ranges = RangeFormulas::default(); + // The first cell of a shared or array formula, waiting for the record holding the formula. + let mut first: Option = None; while let Some((id, payload)) = records.next(warnings)? { let mut b = Bytes::new(payload); - let cell_metadata = meta.take(); + let cell_metadata = if id == 50 { meta } else { meta.take() }; + if let Some(at) = first.take() { + if matches!(id, 426 | 427) { + let f = ranges.range_formula(id, payload, at, links, limits, budget, warnings); + if let Some(cell) = row.as_mut().and_then(|r| r.cells.last_mut()).filter(|c| c.at == at) { + cell.formula = f; + } + continue; + } + kept(warnings, "a shared or array formula that is missing from the file"); + } match id { 49 => { let icmb = b.i32()?; @@ -648,10 +824,24 @@ pub(crate) fn worksheet( } let style = b.u32()?; let value = value(id, &mut b, strings, limits, budget)?; + let at = CellRef::new(current.index, c); 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() }) + match part_of(rgce, rgcb, at) { + None => decode_formula(rgce, rgcb, Mode::Cell, links, limits, budget, warnings) + .map(|text| CellFormula { text, ..CellFormula::default() }), + Some(Ok(Part::First)) => { + first = links.map(|_| at); + None + } + Some(Ok(Part::Of(start))) if links.is_some() => ranges.follower(at, start, links, limits, budget, warnings), + Some(Ok(Part::Of(_))) => None, + Some(Err(why)) => { + kept(warnings, why); + None + } + } } else { b.finish()?; None @@ -660,7 +850,7 @@ pub(crate) fn worksheet( budget.cell(limits)?; } current.cells.push(Cell { - at: CellRef::new(current.index, c), + at, style: style & 0x00FF_FFFF, phonetic: style & 0x0100_0000 != 0, value, diff --git a/crates/xlsx/src/xlsb/formula.rs b/crates/xlsx/src/xlsb/formula.rs index bed0adcd0..53b02f8a1 100644 --- a/crates/xlsx/src/xlsb/formula.rs +++ b/crates/xlsx/src/xlsb/formula.rs @@ -11,7 +11,7 @@ use std::sync::Arc; -use craft_sheet::{CellError, MAX_COLS, MAX_ROWS}; +use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS}; use super::expr::{Anchor, BinOp, Expr, RefKind, Reference, SheetSel, UnOp}; use super::ftab::function; @@ -192,6 +192,14 @@ impl Bytes<'_> { const LAST_ROW: u32 = MAX_ROWS - 1; 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. +#[derive(Clone, Copy, PartialEq)] +pub(super) enum Mode { + Cell, + Shared(CellRef), +} + /// 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. @@ -203,9 +211,29 @@ fn anchor(row: u32, col: u16) -> Result { Ok(Anchor { row, col: c, row_abs: col & 0x8000 == 0, col_abs: col & 0x4000 == 0 }) } +/// RgceLocRel ([MS-XLSB] 2.5.98.92), in a shared formula: a relative row or column is an offset +/// from `base`, wrapping around the grid (1,048,576 rows, 16,384 columns), as Excel adds them. +/// An absolute one is a coordinate. +fn relative_anchor(row: u32, col: u16, base: CellRef) -> Result { + let (row_rel, col_rel) = (col & 0x8000 != 0, col & 0x4000 != 0); + let r = if row_rel { base.row.wrapping_add(row) % MAX_ROWS } else { row }; + let c = if col_rel { (base.col + u32::from(col & 0x3FFF)) % MAX_COLS } else { u32::from(col & 0x3FFF) }; + anchor(r, (c as u16) | (col & 0xC000)) +} + +fn loc(row: u32, col: u16, mode: Mode) -> Result { + match mode { + Mode::Cell => anchor(row, col), + Mode::Shared(base) => relative_anchor(row, col, base), + } +} + /// 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 { - let (a, b) = (anchor(r0, c0)?, anchor(r1, c1)?); + area_of(anchor(r0, c0)?, anchor(r1, c1)?) +} + +fn area_of(a: Anchor, b: Anchor) -> Result { 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 { @@ -222,10 +250,11 @@ fn reference(sheet: Option, kind: RefKind) -> Expr { } } -/// Decodes a cell formula's `rgce` (tokens) with its `rgcb` (extra data for array constants and +/// Decodes a 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 { +/// 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 { if rgce.len() > MAX_RGCE { return Err(Unsupported("formula too long")); } @@ -414,7 +443,17 @@ pub(super) fn decode(rgce: &[u8], rgcb: &[u8], links: &Links) -> Result 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")), + 0x2C => { + let Mode::Shared(_) = mode else { 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")) }; + 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 }))?; + } _ => return Err(Unsupported("unknown formula token")), } if stack.len() > MAX_RGCE { diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index 42c27158b..d2b03dbbd 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -546,7 +546,7 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { // 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]), + (13, vec![0x01, 0, 0, 0, 0], vec![0, 0, 0, 0]), // Part of a shared formula at A1 that isn't one. ]; let mut data = row(0); data.extend(cell(5, 0, &3.0f64.to_le_bytes())); @@ -580,11 +580,11 @@ fn cell_formulas_are_read_in_their_xlsx_spelling() { None, ]; assert_eq!(got, want); - assert!(sheet.formulas.values().all(|f| f.kind == craft_xlsx::FormulaKind::Normal)); + assert!(sheet.formulas.values().all(|f| f.kind == FormulaKind::Normal)); assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&CellRef::new(0, 14), &1)]); // Every formula cell keeps its saved result as its value. assert!((1..=15).all(|c| sheet.value(CellRef::new(0, c)) == Value::Number(0.0))); - for why in ["defined names", "other workbooks", "the shared or array formula form", "structured table references"] { + for why in ["defined names", "other workbooks", "missing from the file", "structured table references"] { assert!(warned(&wb, why), "{why}: {:?}", wb.warnings); } } @@ -709,3 +709,165 @@ fn formula_text_past_the_allowance_keeps_the_saved_result() { assert_eq!(wb.sheets[0].formula(CellRef::new(0, 1)), None); assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); } + +fn range_formula(id: u16, (r0, c0, r1, c1): (u32, u32, u32, u32), rgce: &[u8]) -> Vec { + let mut data = vec![]; + for n in [r0, r1, c0, c1] { + data.extend(n.to_le_bytes()); + } + if id == 426 { + data.push(0x08); // The flags Excel writes for an ordinary array. + } + data.extend((rgce.len() as u32).to_le_bytes()); + data.extend(rgce); + data.extend(0u32.to_le_bytes()); + record(id, &data) +} + +/// A cell of the shared or array formula whose first cell is `row`, `col` (PtgExp + PtgExtraCol). +fn part(col: u32, (row, first_col): (u32, u32)) -> Vec { + formula_cell(col, &[&[0x01u8][..], &row.to_le_bytes()].concat(), &first_col.to_le_bytes()) +} + +fn ref_n(row: i32, col: u16) -> Vec { + [&[0x4Cu8][..], &row.to_le_bytes(), &col.to_le_bytes()].concat() +} + +fn at(a1: &str) -> CellRef { + CellRef::parse(a1).unwrap() +} + +#[test] +fn shared_array_and_dynamic_formulas_read_as_from_xlsx() { + // As Excel writes them: C1:C3 share `A1*10` (PtgRefN, column offset -2 as 16382); D1:E2 + // share `$A1+A$1`; F1:F2 hold the Ctrl+Shift+Enter array {=A1:A2*2}; G1 spills `=A1:A2`. + let area = [&[0x45u8][..], &0u32.to_le_bytes(), &1u32.to_le_bytes(), &REL.to_le_bytes(), &REL.to_le_bytes()].concat(); + let mut data = row(0); + data.extend(cell(5, 0, &1.0f64.to_le_bytes())); + data.extend(part(2, (0, 2))); + data.extend(range_formula(427, (0, 2, 2, 2), &[ref_n(0, 16382 | REL), vec![0x1E, 10, 0, 0x05]].concat())); + data.extend(part(3, (0, 3))); + data.extend(range_formula(427, (0, 3, 1, 4), &[ref_n(0, 0x8000), ref_n(0, 16381 | 0x4000), vec![0x03]].concat())); + data.extend(part(4, (0, 3))); + data.extend(part(5, (0, 5))); + data.extend(range_formula(426, (0, 5, 1, 5), &[area.clone(), vec![0x1E, 2, 0, 0x05]].concat())); + data.extend(record(49, &1u32.to_le_bytes())); + data.extend(record(50, &1u32.to_le_bytes())); // BrtValueMeta in between, as Excel writes for a spill error. + data.extend(part(6, (0, 6))); + data.extend(range_formula(426, (0, 6, 1, 6), &area)); + data.extend(row(1)); + data.extend(cell(5, 0, &2.0f64.to_le_bytes())); + for (col, first) in [(2, (0, 2)), (3, (0, 3)), (4, (0, 3)), (5, (0, 5)), (6, (0, 6))] { + data.extend(part(col, first)); + } + data.extend(row(2)); + data.extend(part(2, (0, 2))); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let sheet = &wb.sheets[0]; + let formula = |a: &str| sheet.formulas.get(&at(a)); + // Each cell of a shared formula has its own text, decoded at the cell: the first with the + // group's range, all with its index. + for (a, want, si) in [ + ("C1", "A1*10", 0), + ("C2", "A2*10", 0), + ("C3", "A3*10", 0), + ("D1", "$A1+A$1", 1), + ("E1", "$A1+B$1", 1), + ("D2", "$A2+A$1", 1), + ("E2", "$A2+B$1", 1), + ] { + let f = formula(a).unwrap_or_else(|| panic!("{a}")); + assert_eq!((f.kind, f.text.as_str(), f.shared_index), (FormulaKind::Shared, want, Some(si)), "{a}"); + } + assert_eq!(formula("C1").and_then(|f| f.range), Range::parse("C1:C3")); + assert_eq!(formula("D1").and_then(|f| f.range), Range::parse("D1:E2")); + assert_eq!(formula("C2").and_then(|f| f.range), None); + // An array formula's first cell has it, with its range; a dynamic array's has its cell + // metadata too, as `cm` in .xlsx, though Excel wrote a BrtValueMeta in between. + let f1 = formula("F1").unwrap(); + assert_eq!((f1.kind, f1.text.as_str(), f1.range), (FormulaKind::Array, "A1:A2*2", Range::parse("F1:F2"))); + let g1 = formula("G1").unwrap(); + assert_eq!((g1.kind, g1.text.as_str(), g1.range), (FormulaKind::Array, "A1:A2", Range::parse("G1:G2"))); + assert_eq!(sheet.cell_metadata.get(&at("G1")), Some(&1)); + assert_eq!(sheet.cell_metadata.get(&at("F1")), None); + // Their other cells have no formula and keep their saved results, as in .xlsx. + for a in ["F2", "G2"] { + assert!(formula(a).is_none(), "{a}"); + assert_eq!(sheet.value(at(a)), Value::Number(0.0), "{a}"); + } + assert!(!wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); +} + +#[test] +fn unreadable_shared_formulas_keep_their_values() { + // A shared formula with an unsupported token, a range that doesn't start at its first cell, + // a truncated record, and a first cell with no formula record after it: every cell of each + // keeps its saved result, and the file opens. + let unknown = range_formula(427, (0, 0, 1, 0), &[0xFF]); + let wrong_start = range_formula(427, (0, 1, 1, 1), &[0x1E, 1, 0]); + // A well-framed record whose payload stops before rgcb's length. + let short: Vec = [&[0u8; 4][..], &1u32.to_le_bytes(), &[0; 8], &3u32.to_le_bytes(), &[0x1E, 1, 0]].concat(); + let truncated = record(427, &short); + for after_first in [unknown, wrong_start, truncated, cell(5, 1, &1.0f64.to_le_bytes())] { + let data = [row(0), part(0, (0, 0)), after_first, row(1), part(0, (0, 0))].concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + for r in [0, 1] { + assert_eq!(wb.sheets[0].formula(CellRef::new(r, 0)), None); + assert_eq!(wb.sheets[0].value(CellRef::new(r, 0)), Value::Number(0.0)); + } + assert!(wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); + } +} + +#[test] +fn shared_formulas_wrap_and_may_start_inside_their_range() { + // `=` the cell to the left (column offset -1, written 16383), shared over A1:B2 from B1: A1 + // isn't part of it. In column A the offset wraps around the grid, as [MS-XLSB] defines it. + let left = ref_n(0, 16383 | REL); + let data = [ + row(0), + cell(5, 0, &1.0f64.to_le_bytes()), + part(1, (0, 1)), + range_formula(427, (0, 0, 1, 1), &left), + row(1), + part(0, (0, 1)), + part(1, (0, 1)), + ] + .concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let text = |a: &str| wb.sheets[0].formula(at(a)); + assert_eq!((text("A1"), text("B1"), text("A2"), text("B2")), (None, Some("A1"), Some("XFD2"), Some("A2"))); + assert!(!wb.warnings.iter().any(|w| w.contains("kept as their saved results")), "{:?}", wb.warnings); +} + +#[test] +fn a_shared_formula_copy_may_be_longer_than_its_first_cell() { + // SUM of nine SUMs of 255 references to the cell on the left: about 6,900 characters at B9, + // over 18,000 at B999999, where each `A9` is `A999999`. Each copy has its own text. + let mut rgce = vec![]; + for _ in 0..9 { + for _ in 0..255 { + rgce.extend(ref_n(0, 16383 | REL)); + } + rgce.extend([0x22, 255, 4, 0]); + } + rgce.extend([0x22, 9, 4, 0]); + let data = [row(8), part(1, (8, 1)), range_formula(427, (8, 1, 999_998, 1), &rgce), row(999_998), part(1, (8, 1))].concat(); + let wb = read(&file(&worksheet(&data, &[]))).unwrap(); + let (b9, last) = (wb.sheets[0].formula(at("B9")).unwrap(), wb.sheets[0].formula(at("B999999")).unwrap()); + assert!(b9.len() < 8192 && last.len() > 18_000, "{} {}", b9.len(), last.len()); + assert!(last.starts_with("SUM(SUM(A999999,A999999"), "{}", &last[..40]); +} + +#[test] +fn shared_formula_copies_are_charged_before_they_are_made() { + // A1:A3 share `1+1` (3 bytes); with 6 bytes allowed, the first cell and one copy fit, and the + // last copy keeps its value without being decoded. + let one_plus_one = [0x1E, 1, 0, 0x1E, 1, 0, 0x03]; + let data = [row(0), part(0, (0, 0)), range_formula(427, (0, 0, 2, 0), &one_plus_one), row(1), part(0, (0, 0)), row(2), part(0, (0, 0))].concat(); + let limits = Limits { binary_formula_text: 6, ..Limits::default() }; + let wb = read_with(&file(&worksheet(&data, &[])), &limits).unwrap(); + let got: Vec<_> = (0..3).map(|r| wb.sheets[0].formula(CellRef::new(r, 0))).collect(); + assert_eq!(got, [Some("1+1"), Some("1+1"), None]); + assert!(warned(&wb, "more formula text than the limits allow"), "{:?}", wb.warnings); +}