diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index d72f814a1..b38571d64 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -179,10 +179,7 @@ fn guessed_range(sh: &gridcraft_model::sheet::Sheet, at: CellRef) -> Option 0 && (numeric(CellRef::new(at.row - 1, at.col)) || at.col == 0 || !numeric(CellRef::new(at.row, at.col - 1))) { let mut top = at.row - 1; - while top > 0 - && (numeric(CellRef::new(top - 1, at.col)) - || sh.value(CellRef::new(top - 1, at.col)).is_empty() && top > 1 && numeric(CellRef::new(top - 2, at.col))) - { + while top > 0 && numeric(CellRef::new(top - 1, at.col)) { top -= 1; } if numeric(CellRef::new(at.row - 1, at.col)) { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index fe42b6977..0fccb81e1 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2067,6 +2067,19 @@ fn formula_at(s: &Session, a: &str) -> Option { s.doc().unwrap().wb.active().unwrap().cell(CellRef::parse(a).unwrap()).and_then(|c| c.formula.as_ref().map(|f| f.text.clone())) } +#[test] +fn autosum_of_a_single_cell_stops_at_a_blank_cell() { + // #3291: a blank row between two blocks of numbers ended the guess one block too late, so the + // second total summed the first block (and its total) as well. + let mut s = s(); + s.execute("range.setValues", json!({"range": "B4", "values": [[1], [2], [3]]})).unwrap(); + s.execute("cell.set", json!({"cell": "B7", "input": "=SUM(B4:B6)"})).unwrap(); + s.execute("range.setValues", json!({"range": "B9", "values": [[10], [20]]})).unwrap(); + s.execute("selection.set", json!({"cell": "B11"})).unwrap(); + s.execute("formulas.autoSum", json!({})).unwrap(); + assert_eq!(v(&s, "B11"), Value::Number(30.0)); +} + #[test] fn autosum_over_a_row_totals_it_to_the_right() { // As in Excel: a row selected and AutoSum puts one total to its right, not a one-cell total