diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 66374bed2..a1e8c0e9e 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -466,7 +466,7 @@ impl<'a> PassHost<'a> { } } -/// 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 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 { @@ -482,7 +482,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)) } @@ -494,7 +494,7 @@ fn spills_as_before(sheet: &gridcraft_model::Sheet, anchor: CellRef, range: Rang 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() @@ -1144,7 +1144,7 @@ impl Calc { || 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.tables.iter().any(|t| t.range.contains(k.1)); + || (arr.data.len() > 1 && 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 622ed6cc9..7b3d47bee 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -459,6 +459,40 @@ 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)"); + assert_eq!((t.num("C4"), t.num("H4")), (8.0, 8.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); +} + #[test] fn ifs_and_lazy() { let mut t = T::new();