fix(autosum): stop the guessed range at a blank cell (#3291)

The single-cell AutoSum guess stepped over one blank cell when a number sat
above it, so a second block of numbers below a gap was summed together with
the first block. It now stops at the first non-number, as Excel does.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Sonnet 5.5 committed 2026-11-03 20:56:00 +01:00
1 parent df137b8ee8
commit 1ff10df557
2 files changed
+14 -4

No files matched your search

+1 -4
View File
@@ -179,10 +179,7 @@ fn guessed_range(sh: &gridcraft_model::sheet::Sheet, at: CellRef) -> Option<Rang
let mut range = None;
if at.row > 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)) {
+13
View File
@@ -2067,6 +2067,19 @@ fn formula_at(s: &Session, a: &str) -> Option<String> {
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