diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index de9e3902a..e2add5284 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -184,9 +184,9 @@ struct PassHost<'a> { depth: usize, } -/// Whether an array result at `k` can't spill: its area runs off the sheet, overlaps merged cells, -/// holds other cells, or another formula's spill (unless that formula is recalculated in this -/// pass, and so may spill elsewhere now). +/// 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 another formula's spill (unless that formula is +/// recalculated in this pass, and so may spill elsewhere now). fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { let Some(sh) = host.wb.sheet(sheet) else { return false }; let end = @@ -200,6 +200,7 @@ fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { end.row >= gridcraft_core::MAX_ROWS || end.col >= gridcraft_core::MAX_COLS || sh.merges.iter().any(|m| m.intersects(&range)) + || sh.tables.iter().any(|t| t.range.contains(anchor)) || sh.cells.iter_range(range).any(|(c, cell)| c != anchor && (!cell.value.is_empty() || cell.formula.is_some())) || sh.spill.range(range.start..=range.end).any(|(c, _)| range.contains(*c) && spilled_over(*c)) } @@ -542,7 +543,8 @@ impl Calc { let blocked = end.row >= gridcraft_core::MAX_ROWS || end.col >= gridcraft_core::MAX_COLS || range.iter().any(|c| c != k.1 && (sheet.cells.has(c) || sheet.spill.contains_key(&c))) - || sheet.merges.iter().any(|m| m.intersects(&range)); + || sheet.merges.iter().any(|m| m.intersects(&range)) + || sheet.tables.iter().any(|t| t.range.contains(k.1)); if blocked { if let Some(cell) = sheet.cells.get_mut(k.1) { cell.value = Value::Error(CellError::Spill); diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index e911ef989..daebfd23f 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -144,6 +144,33 @@ 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 arrays_do_not_spill_in_tables() { + // Excel: spilled array formulas aren't supported in tables. + let mut t = T::new(); + t.wb.sheet_mut(0).unwrap().tables.push(gridcraft_model::Table { + id: 1, + name: "Table1".into(), + range: gridcraft_core::RangeRef::parse("B3:C6").unwrap(), + header_row: true, + totals_row: false, + columns: vec![], + style: String::new(), + banded_rows: false, + banded_cols: false, + first_col: false, + last_col: false, + filter_button: false, + }); + t.set("C4", "=SEQUENCE(2)"); + t.set("E1", "=SEQUENCE(4)"); + t.set("H1", "=SUM(C4:C5)"); + assert_eq!(t.get("C4"), Value::Error(CellError::Spill)); + assert_eq!(t.get("C5"), Value::Empty); + assert_eq!(t.get("H1"), Value::Error(CellError::Spill)); + assert_eq!(t.num("E4"), 4.0); +} + #[test] fn ifs_and_lazy() { let mut t = T::new();