diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 92ea595f1..0b753821f 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -718,7 +718,7 @@ impl Calc { } else { Vec::new() }; - let blocked = spill_is_blocked(sheet, (*key, old), range, end, standing); + let blocked = spill_is_blocked(sheet, (*key, old), range, end, standing, array.data.len()); if !blocked { for (o, r) in outranked { for c in r.iter() { @@ -785,6 +785,7 @@ fn spill_is_blocked( range: RangeRef, end: CellRef, standing: &Standing, + array_len: usize, ) -> bool { let outranked = |cell: CellRef| { sheet @@ -796,7 +797,7 @@ fn spill_is_blocked( || end.col >= gridcraft_core::MAX_COLS || range.iter().any(|cell| cell != anchor.1 && (sheet.cells.has(cell) || (sheet.spill.contains_key(&cell) && !outranked(cell)))) || sheet.merges.iter().any(|merge| merge.intersects(&range)) - || sheet.tables.iter().any(|table| table.range.contains(anchor.1)) + || (array_len > 1 && sheet.tables.iter().any(|table| table.range.contains(anchor.1))) } fn fill_spill(sheet: &mut gridcraft_model::Sheet, anchor: CellRef, range: RangeRef, array: &Array) { diff --git a/crates/calc/src/recalc/pass_host.rs b/crates/calc/src/recalc/pass_host.rs index 7f42c3644..5733546e5 100644 --- a/crates/calc/src/recalc/pass_host.rs +++ b/crates/calc/src/recalc/pass_host.rs @@ -162,7 +162,8 @@ 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, +/// Whether an array result at `k` can't spill: it is in a table (a one-value result is shown +/// there, as in Excel), or its area runs off the sheet, /// 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. @@ -188,7 +189,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)) + || (a.data.len() > 1 && 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)) } @@ -200,7 +201,7 @@ pub(super) fn spills_as_before(sheet: &gridcraft_model::Sheet, anchor: CellRef, CellRef::new(anchor.row.saturating_add((arr.rows as u32).saturating_sub(1)), anchor.col.saturating_add((arr.cols as u32).saturating_sub(1))); RangeRef::new(anchor, end) == range && !sheet.merges.iter().any(|m| m.intersects(&range)) - && !sheet.tables.iter().any(|t| t.range.contains(anchor)) + && (arr.data.len() <= 1 || !sheet.tables.iter().any(|t| t.range.contains(anchor))) && range .iter() .enumerate() diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index e46e0268e..8dbc4a09a 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -820,6 +820,42 @@ fn arrays_do_not_spill_in_tables() { assert_eq!(t.num("E4"), 4.0); } +#[test] +fn one_value_array_in_a_table_is_not_a_spill() { + // Excel shows a one-value dynamic-array result in a table column; two or more is #SPILL! (#2335). + let table = |range: &str| gridcraft_model::Table { + id: 1, + name: "Table1".into(), + range: gridcraft_core::RangeRef::parse(range).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, + filter: vec![], + table_type: None, + connection_id: None, + }; + let mut t = T::new(); + t.wb.sheet_mut(0).unwrap().tables.push(table("B3:C6")); + t.set("A1", "7"); + t.set("A2", "8"); + t.set("C4", "=FILTER(A1:A2,A1:A2>7)"); + t.set("C5", "=FILTER(A1:A2,A1:A2>0)"); + t.set("H4", "=FILTER(A1:A2,A1:A2>7)"); + t.set("D4", "=ROWS(C4#)"); + assert_eq!((t.num("C4"), t.num("H4"), t.num("D4")), (8.0, 8.0, 1.0)); + assert_eq!(t.get("C5"), Value::Error(CellError::Spill)); + // Recalculating again keeps the one-value result. + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.num("C4"), 8.0); + assert_eq!(t.num("D4"), 1.0); +} + #[test] fn ifs_and_lazy() { let mut t = T::new();