diff --git a/ROADMAP.md b/ROADMAP.md index 3466cdcfc..548cf7138 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -202,6 +202,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-13 | XLSX export writes a picture used on several sheets once, as one `xl/media` part every drawing points to (it wrote a copy per drawing: a 16-sheet workbook with one 112 KB logo grew from 0.4 to 1.9 MB) (#985) | | 2026-10-13 | XLSX: `[Content_Types].xml`, `_rels/.rels`, the workbook rels and `xl/workbook.xml` are the first zip entries (document properties last), so libmagic and PHP `finfo` detect the file type even with large properties (#1090) | | 2026-10-13 | Conditional formatting: a rule gathers range statistics only when it reads them (top/bottom, above average, duplicates, scales, bars, icons) and borrows them instead of copying them per cell, so the per-frame lookup of an expression or cell-value rule no longer visits its whole range (#1098, part 1) | +| 2026-10-13 | Spills: an array that grows into cells another array's spill already holds is `#SPILL!`, in automatic calculation and after Calculate Now, as in Excel; a workbook reopened from XLSX keeps the spill layout it was saved with (#964, found by the command-sequence fuzz) | | 2026-10-13 | Grid: resizing several selected columns or rows previews on the dragged one and applies the size to all on release; Esc cancels (#1082) | | 2026-10-12 | Next/previous sheet navigation stops at the first/last visible worksheet instead of activating a hidden boundary sheet; the current selection is retained | | 2026-10-12 | GETPIVOTDATA reads PivotTable values by field and items (#284) | diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index ab1b5bd03..f38ed8d91 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -59,26 +59,29 @@ pub struct Calc { /// Who gets the cells when two arrays want the same ones. Excel (measured by a reviewer on /// PR #2483) lets the spill that already exists keep its cells, whichever anchor comes first; -/// the other formula shows `#SPILL!`, and clearing the winner lets it spill. The rule here: +/// the other formula shows `#SPILL!`, and clearing the winner lets it spill. The rule here, for +/// each cell both want: /// -/// 1. an anchor that held a spill before this calculation outranks one that did not; -/// 2. otherwise the anchor first in reading order (row, then column) wins. +/// 1. the anchor whose spill held that cell before this calculation keeps it, so an array that +/// grows into another's area is `#SPILL!` even when it held a spill of its own; +/// 2. otherwise an anchor that held a spill before this calculation outranks one that did not; +/// 3. otherwise the anchor first in reading order (row, then column) wins. /// -/// Rule 2 alone decides when nothing is remembered, so the result never depends on hash order +/// Rule 3 alone decides when nothing is remembered, so the result never depends on hash order /// or evaluation order. A full recalculation remembers the layout the sheet held before it -/// started (rule 1), so it agrees with the incremental one; a workbook that is calculated for -/// the first time has no such layout (rule 2 only). -/// -/// Known limits: (a) the xlsx reader does not restore spill layouts, so a workbook opened from a -/// file is laid out by rule 2 alone, although Excel keeps the layout the file records; (b) when -/// both anchors held a spill and one grows into the other, rule 2 decides, where Excel keeps -/// the cells the other already owns. +/// started, so it agrees with the incremental one; a workbook that is calculated for the first +/// time has no such layout (rule 3 only). The xlsx reader restores the area each dynamic array +/// was saved spilling over, so a workbook opened from a file keeps the layout it was saved with. #[derive(Clone, Debug, Default)] struct Standing { /// `true`: `keys` are the anchors that held a spill (full recalculation, which lays every /// spill out afresh). `false`: `keys` are the formulas that held none (incremental). held: bool, keys: HashSet, + /// The area each anchor that held a spill filled before this calculation, for the anchors the + /// calculation lays out again (full: all of them; incremental: the formulas it recalculates). + /// Any other anchor still fills the area the sheet shows. + areas: HashMap, } impl Standing { @@ -86,7 +89,7 @@ impl Standing { self.keys.contains(&k) == self.held } - /// Whether `a` takes cells from `b` when both want them. + /// Whether `a` takes cells from `b` when both want them and neither held them (rules 2, 3). fn outranks(&self, a: Key, b: Key) -> bool { match (self.had_spill(a), self.had_spill(b)) { (true, false) => true, @@ -94,6 +97,22 @@ impl Standing { _ => (a.1.row, a.1.col) < (b.1.row, b.1.col), } } + + /// Whether `k`'s spill held `cell` before this calculation; `now` is the area the sheet + /// shows for `k`. + fn held_cell(&self, k: Key, now: Option, cell: CellRef) -> bool { + self.had_spill(k) && self.areas.get(&k).copied().or(now).is_some_and(|r| r.contains(cell)) + } + + /// Whether `a` takes `cell` from `b` when both want it (rule 1, then [`Self::outranks`]). Each + /// anchor comes with the area the sheet shows for it. + fn outranks_at(&self, (a, a_now): (Key, Option), (b, b_now): (Key, Option), cell: CellRef) -> bool { + match (self.held_cell(a, a_now, cell), self.held_cell(b, b_now, cell)) { + (true, false) => true, + (false, true) => false, + _ => self.outranks(a, b), + } + } } /// A set of cells by row, each row's columns in their own set: a window of rows and columns is @@ -313,12 +332,18 @@ impl Calc { self.rebuild(wb); // The layout the sheets hold now decides who keeps cells two arrays both want. // (A Python cell that holds a spill counts too, as it does in the incremental pass.) - let mut held = HashSet::default(); + let mut areas: HashMap = HashMap::default(); for i in 0..wb.sheets.len() { if let Some(sh) = wb.sheet(i) { - held.extend(sh.spill_ranges.keys().filter(|a| sh.cell(**a).is_some_and(|c| c.formula.is_some() || c.py.is_some())).map(|a| (i, *a))); + areas.extend( + sh.spill_ranges + .iter() + .filter(|(a, _)| sh.cell(**a).is_some_and(|c| c.formula.is_some() || c.py.is_some())) + .map(|(a, r)| ((i, *a), *r)), + ); } } + let held = areas.keys().copied().collect(); // Spills are laid out again from the formulas where they are now (a structural edit moves // or deletes the formulas, not the values they spilled). for i in 0..wb.sheets.len() { @@ -330,7 +355,7 @@ impl Calc { } } let all: Vec = self.graph.nodes.keys().copied().collect(); - self.standing = Standing { held: true, keys: held }; + self.standing = Standing { held: true, keys: held, areas }; self.run(wb, all, true); self.standing = Standing::default(); } @@ -542,8 +567,15 @@ impl Calc { if !full { // What each formula held before this calculation touched it. for &k in &dirty { - if seen.insert(k) && wb.sheet(k.0).is_some_and(|s| !s.spill_ranges.contains_key(&k.1)) { - self.standing.keys.insert(k); + if seen.insert(k) { + match wb.sheet(k.0).and_then(|s| s.spill_ranges.get(&k.1)) { + Some(r) => { + self.standing.areas.insert(k, *r); + } + None => { + self.standing.keys.insert(k); + } + } } } } @@ -664,7 +696,7 @@ impl Calc { let end = CellRef::new(key.1.row.saturating_add(array.rows as u32 - 1), key.1.col.saturating_add(array.cols as u32 - 1)); let range = RangeRef::new(key.1, end); // Two arrays that want the same cells (see [`Standing`]): a spill already laid out - // that this anchor outranks is taken over, and recalculated. + // that this anchor outranks on every cell they share is taken over, and recalculated. let standing = &self.standing; // A spill can only be in the way where it has cells besides its anchor, and an // anchor itself blocks the array. When no spilled cell lies in the range, the owners @@ -675,13 +707,18 @@ impl Calc { sheet .spill_ranges .iter() - .filter(|(o, r)| **o != key.1 && r.intersects(&range) && standing.outranks(*key, (key.0, **o))) + .filter(|(o, r)| { + **o != key.1 + && r.intersection(&range).is_some_and(|shared| { + shared.iter().all(|cell| standing.outranks_at((*key, old), ((key.0, **o), Some(**r)), cell)) + }) + }) .map(|(o, r)| (*o, *r)) .collect() } else { Vec::new() }; - let blocked = spill_is_blocked(sheet, *key, range, end, standing); + let blocked = spill_is_blocked(sheet, (*key, old), range, end, standing); if !blocked { for (o, r) in outranked { for c in r.iter() { @@ -739,11 +776,22 @@ thread_local! { pub(crate) static OWNER_SCANS: std::cell::Cell = const { std::cell::Cell::new(0) }; } -/// Whether the array at `anchor` can't be laid out over `range`. A spill already laid out that -/// `anchor` outranks does not block it: it is taken over when written back. -fn spill_is_blocked(sheet: &gridcraft_model::Sheet, anchor: Key, range: RangeRef, end: CellRef, standing: &Standing) -> bool { - let outranked = - |cell: CellRef| sheet.spill_ranges.iter().any(|(o, r)| *o != anchor.1 && r.contains(cell) && standing.outranks(anchor, (anchor.0, *o))); +/// Whether the array at `anchor` (with the area the sheet showed for it) can't be laid out over +/// `range`. A spill already laid out that `anchor` outranks does not block it: it is taken over +/// when written back. +fn spill_is_blocked( + sheet: &gridcraft_model::Sheet, + (anchor, now): (Key, Option), + range: RangeRef, + end: CellRef, + standing: &Standing, +) -> bool { + let outranked = |cell: CellRef| { + sheet + .spill_ranges + .iter() + .any(|(o, r)| *o != anchor.1 && r.contains(cell) && standing.outranks_at((anchor, now), ((anchor.0, *o), Some(*r)), cell)) + }; end.row >= gridcraft_core::MAX_ROWS || end.col >= gridcraft_core::MAX_COLS || range.iter().any(|cell| cell != anchor.1 && (sheet.cells.has(cell) || (sheet.spill.contains_key(&cell) && !outranked(cell)))) diff --git a/crates/calc/src/recalc/pass_host.rs b/crates/calc/src/recalc/pass_host.rs index 78bd5d9ef..7f42c3644 100644 --- a/crates/calc/src/recalc/pass_host.rs +++ b/crates/calc/src/recalc/pass_host.rs @@ -163,9 +163,9 @@ fn spill_hits_overlap(index: &SpillIndex, sheet: usize, hits: &[CellRef], range: } /// Whether an array result at `k` can't spill: it is in a table, or its area runs off the sheet, -/// overlaps merged cells, holds other cells, or the spill of another formula that outranks it -/// (see `Standing`; unless that formula is recalculated in this pass, and so may spill elsewhere -/// now). A spill it outranks gives way to it when written back. +/// overlaps merged cells, holds other cells, or the spill of another formula that outranks it on +/// a cell they share (see `Standing`; unless that formula is recalculated in this pass, and so +/// may spill elsewhere now). A spill it outranks gives way to it when written back. fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { let Some(sh) = host.wb.sheet(sheet) else { return false }; let end = @@ -182,7 +182,7 @@ fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { && r.contains(c) && !host.exprs.contains_key(&(sheet, *o)) && !host.python.contains_key(&(sheet, *o)) - && standing.outranks((sheet, *o), (sheet, anchor)) + && standing.outranks_at(((sheet, *o), Some(*r)), ((sheet, anchor), own.copied()), c) }) }; end.row >= gridcraft_core::MAX_ROWS diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 695b33f72..b1a06db35 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -706,6 +706,53 @@ fn spill_into_the_old_area_of_a_blocked_neighbour() { assert_eq!([t.num("C8"), t.num("D8"), t.num("C9"), t.num("D9")], [1.0, 2.0, 3.0, 4.0]); } +#[test] +fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { + // Both arrays spill; in manual calculation F13 then grows over F14:G14, which C14's array + // fills. A full recalculation gives what an automatic one would: C14 keeps its cells and F13 + // is #SPILL!, although F13 comes first in reading order. + let mut t = T::new(); + t.set("J1", "1"); + t.set("C14", "=SEQUENCE(1,7)"); + t.set("F13", "=SEQUENCE(J1,2)"); + assert_eq!([t.num("F13"), t.num("G13")], [1.0, 2.0]); + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + t.set("J1", "5"); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); + assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); +} + +#[test] +fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() { + // C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves. + // Automatic recalculation lays them out in reading order with each former area still filled: + // C1 is #SPILL! and A3 takes A3:E3. A full recalculation after the same edit in manual + // calculation gives the same sheet. + let build = |manual: bool| { + let mut t = T::new(); + t.set("J1", "2"); + t.set("C1", "=SEQUENCE(J1)"); + t.set("A3", "=SEQUENCE(1,J1+1)"); + t.set("E1", "=SEQUENCE(5-J1)"); + assert_eq!([t.num("C2"), t.num("C3"), t.num("E3")], [2.0, 3.0, 3.0]); + if manual { + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + } + t.set("J1", "4"); + if manual { + t.calc.recalc_all(&mut t.wb); + } + t + }; + let (automatic, full) = (build(false), build(true)); + assert_eq!(automatic.get("C1"), Value::Error(CellError::Spill)); + assert_eq!([automatic.num("A3"), automatic.num("C3"), automatic.num("E3")], [1.0, 3.0, 5.0]); + for cell in ["A1", "C1", "C2", "C3", "C4", "E1", "E2", "A3", "B3", "C3", "D3", "E3"] { + assert_eq!(full.get(cell), automatic.get(cell), "{cell}"); + } +} + #[test] fn formulas_reading_an_array_that_loses_a_spill_conflict_read_spill_errors() { // Two arrays of one recalculation want the same empty cell: the one laid out second is diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 5d236fe67..0a64e87a6 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3078,9 +3078,10 @@ fn overlapping_spills_keep_the_one_that_exists_whichever_anchor_is_first() { #[test] fn a_spill_growing_into_another_and_back_matches_a_full_recalc_at_every_step() { use gridcraft_core::CellError::Spill; - // Both arrays already spill; F8 (first in reading order) grows down over E10's spill, takes - // its cells, shrinks back and lets it spill again, then the steps are undone, redone, edited - // inside the contested area and cleared. Every state must match a full recalc. + // Both arrays already spill; F8 (first in reading order) grows down over E10's spill, is + // #SPILL! because E10 holds those cells (as in Excel), shrinks back and spills again, then the + // steps are undone, redone, edited inside the contested area and cleared. Every state must + // match a full recalc. let mut s = s(); s.execute("cell.set", json!({"cell": "H1", "input": "1"})).unwrap(); s.execute("cell.set", json!({"cell": "F8", "input": "=SEQUENCE(H1)"})).unwrap(); @@ -3102,18 +3103,18 @@ fn a_spill_growing_into_another_and_back_matches_a_full_recalc_at_every_step() { s.execute(id, params.clone()).unwrap(); assert_matches_full_recalc(&s); if i == 0 { - // Both held a spill: reading order gives F8 the cells. - assert_eq!(v(&s, "F12"), Value::Number(5.0)); - assert_eq!(v(&s, "E10"), Value::Error(Spill)); + // Both held a spill: E10 holds F10, so it keeps it and F8 is blocked. + assert_eq!(v(&s, "F8"), Value::Error(Spill)); + assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 2 { assert_eq!(v(&s, "E10"), Value::Number(10.0)); assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 3 { - // Undone back to the grown F8. - assert_eq!(v(&s, "F12"), Value::Number(5.0)); - assert_eq!(v(&s, "E10"), Value::Error(Spill)); + // Undone back to the grown F8, blocked again. + assert_eq!(v(&s, "F8"), Value::Error(Spill)); + assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 8 { assert_eq!((v(&s, "F8"), v(&s, "E10"), v(&s, "F9")), (Value::Empty, Value::Empty, Value::Empty)); @@ -3355,6 +3356,21 @@ fn a_spill_that_goes_away_unblocks_another() { assert_eq!((v(&s, "E10"), v(&s, "G10")), (Value::Number(1.0), Value::Number(3.0))); } +#[test] +fn a_contested_spill_area_stays_with_its_array_after_reopening() { + // F13's array wants F14:G14, which C14's array already fills, so F13 is #SPILL!. The file + // can only cache #VALUE! for it; reopening (which recalculates) used to hand the area to F13 + // for coming first in reading order. + let mut s = s(); + s.execute("cell.set", json!({"cell": "C14", "input": "=SEQUENCE(1,7)"})).unwrap(); + s.execute("cell.set", json!({"cell": "F13", "input": "=SEQUENCE(5,2)"})).unwrap(); + assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); + let saved = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap(); + s.execute("file.open", json!({"name": "contested.xlsx", "base64": saved["base64"]})).unwrap(); + assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); + assert_eq!((v(&s, "C14"), v(&s, "G14"), v(&s, "I14")), (Value::Number(1.0), Value::Number(5.0), Value::Number(7.0))); +} + #[test] fn an_array_reading_another_arrays_spill_is_updated() { // G5 is evaluated before D11 in the same pass, so it first reads D11's old spill. @@ -3379,6 +3395,19 @@ fn indirect_reads_a_spill_laid_out_in_the_same_pass() { assert_eq!(v(&s, "E5"), Value::Number(9.0)); } +#[test] +fn inserting_rows_lays_out_spills_where_the_formulas_moved() { + // D1's array grows with the inserted rows to D1:D10 and C10's array moves down to C12:E14: + // the area C10:E12 it filled before the insertion blocks neither of them. + let mut s = s(); + s.execute("cell.set", json!({"cell": "D1", "input": "=SEQUENCE(ROWS(A2:A9))"})).unwrap(); + s.execute("cell.set", json!({"cell": "C8", "input": "=1"})).unwrap(); + s.execute("cell.set", json!({"cell": "C10", "input": "=SEQUENCE(3,3)"})).unwrap(); + s.execute("home.insertRows", json!({"rows": "3:4"})).unwrap(); + assert_eq!((v(&s, "D1"), v(&s, "D10")), (Value::Number(1.0), Value::Number(10.0))); + assert_eq!((v(&s, "C12"), v(&s, "D12"), v(&s, "E14")), (Value::Number(1.0), Value::Number(2.0), Value::Number(9.0))); +} + #[test] fn every_command_survives_empty_params() { let mut s = Session::new(); diff --git a/crates/xlsx/src/sheet_read.rs b/crates/xlsx/src/sheet_read.rs index eedf13120..a3089cab8 100644 --- a/crates/xlsx/src/sheet_read.rs +++ b/crates/xlsx/src/sheet_read.rs @@ -657,6 +657,14 @@ fn read_cell(cx: &mut Ctx<'_>, sheet: &mut Sheet, st: &mut RowState, c: &El, pos Some(_) => Value::Empty, None => value, }; + // A dynamic array keeps the area it was saved spilling over (`ref`): when two arrays want the + // same cells, recalculating gives them to this one again, as before saving. + if dynamic + && formula.as_ref().is_some_and(|f| f.array.is_none()) + && let Some(r) = st.dynamic.last().filter(|r| r.start == pos) + { + sheet.spill_ranges.insert(pos, *r); + } sheet.cells.set(pos, Cell { value, formula: formula.map(Arc::new), style, py }); } diff --git a/crates/xlsx/src/tests/roundtrip.rs b/crates/xlsx/src/tests/roundtrip.rs index d03c8cca5..f561645d6 100644 --- a/crates/xlsx/src/tests/roundtrip.rs +++ b/crates/xlsx/src/tests/roundtrip.rs @@ -1059,12 +1059,14 @@ fn legacy_and_dynamic_formulas_keep_their_kind() { let (xml, _) = sheet1_xml(&bytes); assert!(xml.contains("TANH(A1:A3)+A1:A3"), "{xml}"); assert!(xml.contains("SUM(A1:A3)"), "{xml}"); - // Uncalculated, the spill range is not known yet; the formula stays dynamic. - assert!(xml.contains("TANH(A1:A3)"), "{xml}"); + // Uncalculated, the dynamic array keeps the area the file says it spilled over. + assert!(xml.contains("TANH(A1:A3)"), "{xml}"); assert!(xml.contains("TANH(A1:A3)"), "{xml}"); let (back, _) = read_xlsx(&bytes).unwrap(); - for a in ["B2", "B3", "C2", "D2"] { + for (a, dynamic) in [("B2", false), ("B3", false), ("C2", true), ("D2", false)] { assert_eq!(text(&back, a), text(&wb, a), "{a}"); + let formula = back.sheet(0).unwrap().cell(at(a)).unwrap().formula.as_ref().unwrap(); + assert_eq!(formula.dynamic, dynamic, "{a}: dynamic-array metadata survives saving"); } }