mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-18 16:14:57 +00:00
Calc: a spill keeps the cells it held when another array grows into them (#964)
#2483 lets a spill that already exists keep its cells, but when both anchors held a spill, reading order decided: an array growing into a neighbour's spill took its cells, in automatic calculation and after a full recalculation. Excel keeps the cells the neighbour owns. Standing now also remembers the area each anchor that held a spill filled before the calculation. For each contested cell the anchor whose spill held it keeps it; otherwise #2483's rules apply as before. With the xlsx reader restoring each dynamic array's saved area, a reopened workbook keeps its layout too, so both known limits documented on Standing are gone. Tests now covered by #2483 are dropped, and its grow-and-back test asserts the blocked grower.
This commit is contained in:
1 parent
e03780704e
commit
8a04139ef2
6 files changed
+91
-84
No files matched your search
+1
-1
@@ -200,7 +200,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: when two dynamic arrays want the same cells, the one already spilling there keeps them after Calculate Now and after saving and reopening, as when the second was entered; clearing it lets the other spill at once (#964, found by the command-sequence fuzz) |
|
||||
| 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 | PDF writer: objects are written straight to the output, identical pictures are stored once, style operators are emitted only when they change, `q` nesting is limited to 28 and polyline joins no longer leak into later strokes (#816) |
|
||||
|
||||
+73
-25
@@ -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<Key>,
|
||||
/// 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<Key, RangeRef>,
|
||||
}
|
||||
|
||||
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<RangeRef>, 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<RangeRef>), (b, b_now): (Key, Option<RangeRef>), 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<Key, RangeRef> = 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<Key> = 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<usize> = 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<RangeRef>),
|
||||
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))))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -706,20 +706,6 @@ 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_with_the_formula_already_spilling_there() {
|
||||
// F13's array would spill over F14:G14, which C14's array already fills: F13 is #SPILL!, and
|
||||
// a full recalculation (Ctrl+Alt+F9) doesn't hand the area to F13 for coming first in
|
||||
// reading order.
|
||||
let mut t = T::new();
|
||||
t.set("C14", "=SEQUENCE(1,7)");
|
||||
t.set("F13", "=SEQUENCE(5,2)");
|
||||
assert_eq!(t.get("F13"), Value::Error(CellError::Spill));
|
||||
t.calc.recalc_all(&mut t.wb);
|
||||
assert_eq!(t.get("F13"), Value::Error(CellError::Spill));
|
||||
assert_eq!([t.num("C14"), t.num("G14"), t.num("I14")], [1.0, 5.0, 7.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
|
||||
@@ -737,23 +723,6 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() {
|
||||
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_keeps_a_former_area_where_it_crosses_into_far_cells() {
|
||||
// As above, with one array at BI14:BO14 and the other at BN13: the former area runs past
|
||||
// column BL, and the array grows into the part beyond it (former areas are indexed by squares
|
||||
// of cells, so the overlap must be found away from the area's first cells too).
|
||||
let mut t = T::new();
|
||||
t.set("J1", "1");
|
||||
t.set("BI14", "=SEQUENCE(1,7)");
|
||||
t.set("BN13", "=SEQUENCE(J1,2)");
|
||||
assert_eq!([t.num("BN13"), t.num("BO13")], [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("BN13"), Value::Error(CellError::Spill));
|
||||
assert_eq!([t.num("BI14"), t.num("BN14"), t.num("BO14")], [1.0, 6.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.
|
||||
|
||||
+10
-23
@@ -2993,9 +2993,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();
|
||||
@@ -3017,18 +3018,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));
|
||||
@@ -3270,20 +3271,6 @@ 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 clearing_the_array_that_holds_a_contested_area_lets_the_other_spill() {
|
||||
// F13's array wants F14:G14, inside C14's spill C14:I14. Clearing C14 empties the whole
|
||||
// area, to the right of C14 too, so F13 spills straight away, as in Excel.
|
||||
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));
|
||||
s.execute("edit.clearContents", json!({"range": "C14"})).unwrap();
|
||||
assert_eq!((v(&s, "F13"), v(&s, "G17")), (Value::Number(1.0), Value::Number(10.0)));
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!((v(&s, "F13"), v(&s, "I14")), (Value::Error(gridcraft_core::CellError::Spill), Value::Number(7.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
|
||||
|
||||
@@ -1058,6 +1058,9 @@ fn legacy_and_dynamic_formulas_keep_their_kind() {
|
||||
let (xml, _) = sheet1_xml(&bytes);
|
||||
assert!(xml.contains("<c r=\"B2\"><f>TANH(A1:A3)+A1:A3</f>"), "{xml}");
|
||||
assert!(xml.contains("<c r=\"B3\"><f>SUM(A1:A3)</f>"), "{xml}");
|
||||
// Uncalculated, the dynamic array keeps the area the file says it spilled over.
|
||||
assert!(xml.contains("<c r=\"C2\" cm=\"1\"><f t=\"array\" ref=\"C2:C4\">TANH(A1:A3)</f>"), "{xml}");
|
||||
assert!(xml.contains("<c r=\"D2\"><f t=\"array\" ref=\"D2:D4\">TANH(A1:A3)</f>"), "{xml}");
|
||||
let (back, _) = read_xlsx(&bytes).unwrap();
|
||||
for (a, dynamic) in [("B2", false), ("B3", false), ("C2", true), ("D2", false)] {
|
||||
assert_eq!(text(&back, a), text(&wb, a), "{a}");
|
||||
|
||||
Reference in new issue
Block a user