From 4215f07bb34cac01e1c377ee0ef941238ea3b9f7 Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Tue, 20 Oct 2026 07:24:25 +0200 Subject: [PATCH 1/6] Formula entry: a leading + or - starts a formula, like Excel (fixes #2023) Typing `+5+4` entered the text `+5+4`: a leading sign started a formula only when a letter or `(` came next. Now any entry that starts with `+` or `-` and isn't a number on its own is a formula, as in Excel, so `+5+4` enters `=+5+4`. `+5`, `-5` and a lone sign are unchanged. The rule lives in `gridcraft_core::parse::starts_formula`, shared by the engine's entry and the Spanish formula-locale path in the UI, where a signed decimal-comma number (`-1,5`) stays a number. Text to Columns keeps a part that isn't a valid formula as text instead of emptying its cell, as paste and Replace already do. Co-Authored-By: Claude Opus 5.5 --- crates/core/src/parse.rs | 20 +++++++++++++ crates/engine/src/cmd/data.rs | 3 +- crates/engine/src/cmd/edit.rs | 7 +---- crates/engine/src/tests.rs | 42 ++++++++++++++++++++++++++ crates/ui-egui/src/formula_locale.rs | 44 +++++++++++++++++++++------- docs/ui-parity.md | 5 ++-- 6 files changed, 102 insertions(+), 19 deletions(-) diff --git a/crates/core/src/parse.rs b/crates/core/src/parse.rs index b465beaf9e..33127c5e0a 100644 --- a/crates/core/src/parse.rs +++ b/crates/core/src/parse.rs @@ -49,6 +49,13 @@ pub fn parse_number_text_in(s: &str, sys: DateSystem) -> Option { parse_numeric(s.trim(), sys).map(|(n, _)| n) } +/// Whether typed cell input is a formula: it starts with `=`, or, as Lotus 1-2-3 users type it, +/// with `+` or `-` and isn't a number on its own. `+5+4` is the formula `=+5+4`, `-5` the number +/// -5, and a lone `+` or `-` is text. A leading space or apostrophe makes the input text. +pub fn starts_formula(input: &str) -> bool { + input.starts_with('=') || (input.len() > 1 && input.starts_with(['+', '-']) && parse_number_text(input).is_none()) +} + fn parse_numeric(t: &str, sys: DateSystem) -> Option<(f64, Option<&'static str>)> { if t.is_empty() { return None; @@ -363,6 +370,19 @@ mod tests { assert_eq!(p("1.2.3.4").value, Value::text("1.2.3.4")); } + #[test] + fn a_leading_sign_starts_a_formula_unless_the_entry_is_a_number() { + // #2023: Excel reads `+5+4` as `=+5+4`, as Lotus 1-2-3 did. + for s in ["=1", "=", "+5+4", "-5+4", "--5", "+-5", "-.5*2", "-3/4", "+A1", "-SUM(1,2)", "+(5+4)", "-abc", "--"] { + assert!(starts_formula(s), "{s}"); + } + // Signed numbers, percentages and currency stay numbers; a lone sign, a leading space or + // apostrophe and other text stay text. + for s in ["+5", "-5", "+5%", "-$5", "-1e3", "+.5", "-1,234.5", "+", "-", " +5+4", "'+5+4", "5+4", "", "a-b"] { + assert!(!starts_formula(s), "{s}"); + } + } + #[test] fn dates_and_times() { assert_eq!(p("10/7/2026").value, Value::Number(46302.0)); diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 24bd043f7d..7b77953e1b 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -839,7 +839,8 @@ fn text_to_columns(s: &mut Session, p: &Json) -> Result { } } for (c, t) in out { - let cell = super::edit::input_to_cell(&t, sheet, c, &mut cx.wb).unwrap_or(None); + // A part that isn't a valid formula (`--`) stays text, as it does when pasted. + let cell = super::edit::input_to_cell(&t, sheet, c, &mut cx.wb).unwrap_or_else(|_| Some(Cell::value(Value::text(t.as_str())))); let shm = cx.sheet_mut(sheet)?; match cell { Some(x) => shm.set_cell(c, x), diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index fe7f1fe8bc..86b0eeeec7 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -131,12 +131,7 @@ pub(crate) fn input_to_cell(input: &str, sheet: usize, at: CellRef, wb: &mut gri if fmt_is_text { return Ok(Some(Cell { value: Value::text(input), formula: None, style })); } - let is_formula = input.starts_with('=') - || (input.len() > 1 - && (input.starts_with('+') || input.starts_with('-')) - && gridcraft_core::parse::parse_number_text(input).is_none() - && input.chars().nth(1).is_some_and(|c| c.is_ascii_alphabetic() || c == '(')); - if is_formula { + if gridcraft_core::parse::starts_formula(input) { let body = input.strip_prefix('=').unwrap_or(input); // Excel closes missing parentheses for you. let mut text = body.to_string(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 2ac2906548..506bd01915 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -202,6 +202,48 @@ fn enter_values_and_formulas() { assert_eq!(crate::display::cell_text(&d.wb, sh, CellRef::parse("B1").unwrap()), "12%"); } +#[test] +fn a_leading_plus_or_minus_starts_a_formula() { + // #2023: like Excel (and Lotus 1-2-3 before it), typing `+5+4` enters the formula `=+5+4`. + let mut s = s(); + let input = |s: &Session, a: &str| s.doc().unwrap().wb.active().unwrap().cell(CellRef::parse(a).unwrap()).map(|c| c.input_text()); + for (cell, typed, value) in + [("A1", "+5+4", 9.0), ("A2", "-5+4", -1.0), ("A3", "--5", 5.0), ("A4", "-3/4", -0.75), ("A5", "+A1*2", 18.0), ("A6", "-(A1)", -9.0)] + { + s.execute("cell.set", json!({"cell": cell, "input": typed})).unwrap_or_else(|e| panic!("{typed}: {e}")); + assert_eq!(input(&s, cell), Some(format!("={typed}")), "{typed}"); + assert_eq!(v(&s, cell), Value::Number(value), "{typed}"); + } + // A signed number is a number, a lone sign is text, and an apostrophe keeps the text. + for (typed, value) in [ + ("+5", Value::Number(5.0)), + ("-5", Value::Number(-5.0)), + ("-12%", Value::Number(-0.12)), + ("-$5", Value::Number(-5.0)), + ("-", Value::text("-")), + ("+", Value::text("+")), + ("'+5+4", Value::text("+5+4")), + ] { + s.execute("cell.set", json!({"cell": "B1", "input": typed})).unwrap(); + assert_eq!(v(&s, "B1"), value, "{typed}"); + assert!(!input(&s, "B1").is_some_and(|t| t.starts_with('=')), "{typed}"); + } + // Typed, a sign that doesn't start a valid formula is refused like `=--` and the cell keeps + // its value; pasted or split by Text to Columns it stays text. + s.execute("cell.set", json!({"cell": "C1", "input": "kept"})).unwrap(); + for typed in ["--", "+5+", "+49 170 1234567"] { + let e = s.execute("cell.set", json!({"cell": "C1", "input": typed})).unwrap_err().to_string(); + assert!(e.contains("problem with this formula"), "{typed}: {e}"); + assert_eq!(v(&s, "C1"), Value::text("kept"), "{typed}"); + } + s.execute("edit.paste", json!({"at": "D1", "text": "--\t-5+4\n"})).unwrap(); + assert_eq!(v(&s, "D1"), Value::text("--")); + assert_eq!(v(&s, "E1"), Value::Number(-1.0)); + s.execute("cell.set", json!({"cell": "F1", "input": "a,--,b"})).unwrap(); + s.execute("data.textToColumns", json!({"range": "F1", "delimiters": [","]})).unwrap(); + assert_eq!([v(&s, "F1"), v(&s, "G1"), v(&s, "H1")], [Value::text("a"), Value::text("--"), Value::text("b")]); +} + #[test] fn formulas_with_the_wrong_argument_count_are_refused_on_entry() { // #363: =DATEDIF(start, end) was stored and showed #VALUE!. Like Excel, entry refuses it and diff --git a/crates/ui-egui/src/formula_locale.rs b/crates/ui-egui/src/formula_locale.rs index 6e833b95db..26b7ac007d 100644 --- a/crates/ui-egui/src/formula_locale.rs +++ b/crates/ui-egui/src/formula_locale.rs @@ -20,14 +20,16 @@ pub(crate) fn canonical( if locale == FormulaLocale::En || wb.sheet(sheet).is_some_and(|sh| wb.styles.get(sh.style_id(cell)).num_fmt.as_str() == "@") { return Ok(text.to_string()); } - // Same formula detection as the engine's `input_to_cell` (no trimming: " =1" is text). - let body = text; - if body.starts_with('=') - || ((body.starts_with('+') || body.starts_with('-')) && body.chars().nth(1).is_some_and(|c| c.is_ascii_alphabetic() || c == '(')) - { + // Decimal-comma number entry follows the same preference, without rewriting text or dates. + let normalized = text.trim().replace(locale.decimal_separator(), "."); + let number = normalized.strip_suffix('%').unwrap_or(&normalized); + let is_number = number.parse::().is_ok_and(f64::is_finite); + // Same formula detection as the engine's `input_to_cell` (no trimming: " =1" is text), except + // that a signed number in this locale (`-1,5`) is a number, not a formula. + if !is_number && gridcraft_engine::core::parse::starts_formula(text) { let known = |name: &str| wb.name(name, sheet).is_some() || wb.table(name).is_some(); // Match the engine's existing convenience for missing closing parentheses. - let mut body = body.to_string(); + let mut body = text.to_string(); let mut expr = formula::parse_input(&body, locale, known); for _ in 0..8 { if expr.is_ok() { @@ -38,10 +40,7 @@ pub(crate) fn canonical( } return Ok(format!("={}", formula::print(&expr?))); } - // Decimal-comma number entry follows the same preference, without rewriting text or dates. - let normalized = body.trim().replace(locale.decimal_separator(), "."); - let number = normalized.strip_suffix('%').unwrap_or(&normalized); - if number.parse::().is_ok_and(f64::is_finite) { + if is_number { return Ok(normalized); } Ok(text.to_string()) @@ -138,3 +137,28 @@ pub(crate) fn display_template(text: &str, locale: FormulaLocale, wb: &Workbook, } display(text, locale, wb, sheet) } + +#[cfg(test)] +mod tests { + use gridcraft_engine::core::CellRef; + use gridcraft_engine::formula::FormulaLocale; + use gridcraft_engine::model::Workbook; + + use super::canonical; + + #[test] + fn a_leading_sign_starts_a_formula_in_spanish_too() { + // #2023: `+5+4` is a formula in every formula language; in Spanish `-1,5` stays the + // number -1.5 rather than becoming a formula. + let wb = Workbook::new(); + let at = CellRef::parse("A1").unwrap(); + let entry = |text: &str| canonical(text, FormulaLocale::Es, &wb, 0, at).ok(); + assert_eq!(entry("+5+4").as_deref(), Some("=+5+4")); + assert_eq!(entry("-5+4").as_deref(), Some("=-5+4")); + assert_eq!(entry("-SUMA(1;2)").as_deref(), Some("=-SUM(1,2)")); + assert_eq!(entry("-1,5").as_deref(), Some("-1.5")); + assert_eq!(entry("+2,5%").as_deref(), Some("+2.5%")); + assert_eq!(entry("-").as_deref(), Some("-")); + assert_eq!(entry("--"), None); + } +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 34cb502f54..64cf2810e1 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-19 · **Change:** minor (Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-20 · **Change:** minor (a leading + or - starts a formula, #2023) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -47,7 +47,7 @@ this logic, with one undo step. | Selection outline glides to a new selection (Excel: "Provide feedback with animation") | done (10-11): 0.12 s, decelerating, with the fill and fill handle; View ▸ Animations (`view.animations`) turns it off. The timing is an estimate, not yet measured against Excel; the system's reduced-motion setting isn't followed yet | | Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); right-drag menu missing | | Drag-move and drag-copy cells (border drag, Ctrl/Option, Shift to insert) | done (M3) | -| In-cell editing, Enter/Tab movement, Alt/Option+Enter line breaks | done | +| In-cell editing, Enter/Tab movement, Alt/Option+Enter line breaks | done; a leading `+` or `-` starts a formula, as in Excel (`+5+4` enters `=+5+4`, #2023) | | Point mode (click/drag/arrow references), reference colouring, F4 | done | | AutoComplete in columns, function autocomplete and argument tips | done; cell helpers clip to the worksheet and hide when the editor scrolls out (#200) | | Header resize and double-click AutoFit | done (#42, #88 for header dividers) | @@ -161,6 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-20 | minor | Typing a leading `+` or `-` starts a formula whenever the entry isn't a number on its own, as in Excel: `+5+4` enters `=+5+4` and `-5+4` enters `=-5+4` (they stayed text; only a letter or `(` after the sign started one). `+5`, `-5` and `-$5` stay numbers, a lone `+` or `-` stays text, and an entry like `--` that isn't a valid formula is refused like `=--`; pasted or split by Text to Columns it stays text (#2023) | | 2026-10-19 | minor | Ctrl+Enter (`range.fill`) gives each cell what typing the input there gives: a General cell gets the automatic number format (`5%`, `$5`, `3/4/2026`, `=TODAY()`) even when it already held a value, and a cell with its own format, a formatted row or column, or Text keeps it. Cells that held a value used to keep General (`5%` showed 0.05), and empty cells took the active cell's format | | 2026-10-18 | minor | Keytip letters typed with left Alt held continue the sequence (Alt+H, O, A / Alt+H, O, I ran nothing before: Alt+letter cancelled keytips); other Alt combinations, AltGr and macOS Option are unchanged (#1846) | | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | From 1af4d38e716f926781d36af7b09976a177964c09 Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Tue, 20 Oct 2026 07:45:49 +0200 Subject: [PATCH 2/6] Formula entry: keep canonical's body binding, so the diff stays small Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/formula_locale.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/formula_locale.rs b/crates/ui-egui/src/formula_locale.rs index 26b7ac007d..132aef258a 100644 --- a/crates/ui-egui/src/formula_locale.rs +++ b/crates/ui-egui/src/formula_locale.rs @@ -20,16 +20,17 @@ pub(crate) fn canonical( if locale == FormulaLocale::En || wb.sheet(sheet).is_some_and(|sh| wb.styles.get(sh.style_id(cell)).num_fmt.as_str() == "@") { return Ok(text.to_string()); } + let body = text; // Decimal-comma number entry follows the same preference, without rewriting text or dates. - let normalized = text.trim().replace(locale.decimal_separator(), "."); + let normalized = body.trim().replace(locale.decimal_separator(), "."); let number = normalized.strip_suffix('%').unwrap_or(&normalized); let is_number = number.parse::().is_ok_and(f64::is_finite); // Same formula detection as the engine's `input_to_cell` (no trimming: " =1" is text), except // that a signed number in this locale (`-1,5`) is a number, not a formula. - if !is_number && gridcraft_engine::core::parse::starts_formula(text) { + if !is_number && gridcraft_engine::core::parse::starts_formula(body) { let known = |name: &str| wb.name(name, sheet).is_some() || wb.table(name).is_some(); // Match the engine's existing convenience for missing closing parentheses. - let mut body = text.to_string(); + let mut body = body.to_string(); let mut expr = formula::parse_input(&body, locale, known); for _ in 0..8 { if expr.is_ok() { From 0c78ddf79f5300c7e2df33da40ab567c9eaf05c4 Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Tue, 20 Oct 2026 08:04:50 +0200 Subject: [PATCH 3/6] Formula entry: a + or - entry that isn't a valid formula stays text Observed in Excel for the web: `--`, `-----`, `+5+` and `+49 170 1234567` stay text, and `- item` is #NAME?. Only `=` entries are refused. Entry now tries a `+`/`-` entry as `=` followed by the entry and, when that doesn't parse, reads it as a number or text as before; the Spanish formula path does the same with its parser. Text to Columns no longer needs its own text fallback, so that change is dropped. Co-Authored-By: Claude Opus 5.5 --- crates/core/src/parse.rs | 7 ++++--- crates/engine/src/cmd/data.rs | 3 +-- crates/engine/src/cmd/edit.rs | 10 +++++++++- crates/engine/src/tests.rs | 19 ++++++++++++------- crates/ui-egui/src/formula_locale.rs | 16 ++++++++++++---- docs/ui-parity.md | 2 +- 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/crates/core/src/parse.rs b/crates/core/src/parse.rs index 33127c5e0a..949f9909f9 100644 --- a/crates/core/src/parse.rs +++ b/crates/core/src/parse.rs @@ -49,9 +49,10 @@ pub fn parse_number_text_in(s: &str, sys: DateSystem) -> Option { parse_numeric(s.trim(), sys).map(|(n, _)| n) } -/// Whether typed cell input is a formula: it starts with `=`, or, as Lotus 1-2-3 users type it, -/// with `+` or `-` and isn't a number on its own. `+5+4` is the formula `=+5+4`, `-5` the number -/// -5, and a lone `+` or `-` is text. A leading space or apostrophe makes the input text. +/// Whether typed cell input is meant as a formula: it starts with `=`, or, as Lotus 1-2-3 users +/// type it, with `+` or `-` and isn't a number on its own (`+5+4` is `=+5+4`, `-5` the number -5, +/// a lone `+` or `-` text). A leading space or apostrophe makes the input text. Callers parse +/// the formula; a `+` or `-` entry that doesn't parse (`--`) stays text, as in Excel. pub fn starts_formula(input: &str) -> bool { input.starts_with('=') || (input.len() > 1 && input.starts_with(['+', '-']) && parse_number_text(input).is_none()) } diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 7b77953e1b..24bd043f7d 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -839,8 +839,7 @@ fn text_to_columns(s: &mut Session, p: &Json) -> Result { } } for (c, t) in out { - // A part that isn't a valid formula (`--`) stays text, as it does when pasted. - let cell = super::edit::input_to_cell(&t, sheet, c, &mut cx.wb).unwrap_or_else(|_| Some(Cell::value(Value::text(t.as_str())))); + let cell = super::edit::input_to_cell(&t, sheet, c, &mut cx.wb).unwrap_or(None); let shm = cx.sheet_mut(sheet)?; match cell { Some(x) => shm.set_cell(c, x), diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 86b0eeeec7..cfdd1abeba 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -131,7 +131,15 @@ pub(crate) fn input_to_cell(input: &str, sheet: usize, at: CellRef, wb: &mut gri if fmt_is_text { return Ok(Some(Cell { value: Value::text(input), formula: None, style })); } - if gridcraft_core::parse::starts_formula(input) { + // As in Excel, a `+` or `-` entry is a formula when it reads as one (`+5+4` enters `=+5+4`) + // and text when it doesn't (`--`, `+49 170 1234567`); only `=` entries are refused. + if !input.starts_with('=') + && gridcraft_core::parse::starts_formula(input) + && let Ok(cell) = input_to_cell(&format!("={input}"), sheet, at, wb) + { + return Ok(cell); + } + if input.starts_with('=') { let body = input.strip_prefix('=').unwrap_or(input); // Excel closes missing parentheses for you. let mut text = body.to_string(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 506bd01915..7af82fbfc4 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -228,14 +228,19 @@ fn a_leading_plus_or_minus_starts_a_formula() { assert_eq!(v(&s, "B1"), value, "{typed}"); assert!(!input(&s, "B1").is_some_and(|t| t.starts_with('=')), "{typed}"); } - // Typed, a sign that doesn't start a valid formula is refused like `=--` and the cell keeps - // its value; pasted or split by Text to Columns it stays text. - s.execute("cell.set", json!({"cell": "C1", "input": "kept"})).unwrap(); - for typed in ["--", "+5+", "+49 170 1234567"] { - let e = s.execute("cell.set", json!({"cell": "C1", "input": typed})).unwrap_err().to_string(); - assert!(e.contains("problem with this formula"), "{typed}: {e}"); - assert_eq!(v(&s, "C1"), Value::text("kept"), "{typed}"); + // As observed in Excel for the web, an entry that doesn't read as a formula stays text, and + // one that does but names nothing known is #NAME?. Only `=` entries are refused. + for typed in ["--", "-----", "+5+", "+49 170 1234567"] { + s.execute("cell.set", json!({"cell": "C1", "input": typed})).unwrap_or_else(|e| panic!("{typed}: {e}")); + assert_eq!(v(&s, "C1"), Value::text(typed), "{typed}"); + assert_eq!(input(&s, "C1").as_deref(), Some(typed), "{typed}"); } + s.execute("cell.set", json!({"cell": "C2", "input": "- item"})).unwrap(); + assert_eq!(v(&s, "C2"), Value::Error(gridcraft_core::CellError::Name)); + let e = s.execute("cell.set", json!({"cell": "C1", "input": "=--"})).unwrap_err().to_string(); + assert!(e.contains("problem with this formula"), "{e}"); + assert_eq!(v(&s, "C1"), Value::text("+49 170 1234567"), "a refused entry keeps the cell"); + // Pasted or split by Text to Columns, the same rule applies. s.execute("edit.paste", json!({"at": "D1", "text": "--\t-5+4\n"})).unwrap(); assert_eq!(v(&s, "D1"), Value::text("--")); assert_eq!(v(&s, "E1"), Value::Number(-1.0)); diff --git a/crates/ui-egui/src/formula_locale.rs b/crates/ui-egui/src/formula_locale.rs index 132aef258a..dcc4d6db50 100644 --- a/crates/ui-egui/src/formula_locale.rs +++ b/crates/ui-egui/src/formula_locale.rs @@ -25,9 +25,14 @@ pub(crate) fn canonical( let normalized = body.trim().replace(locale.decimal_separator(), "."); let number = normalized.strip_suffix('%').unwrap_or(&normalized); let is_number = number.parse::().is_ok_and(f64::is_finite); - // Same formula detection as the engine's `input_to_cell` (no trimming: " =1" is text), except - // that a signed number in this locale (`-1,5`) is a number, not a formula. - if !is_number && gridcraft_engine::core::parse::starts_formula(body) { + // Same formula detection as the engine's `input_to_cell` (no trimming: " =1" is text): a `+` + // or `-` entry is a formula when it reads as one and text when it doesn't, and a signed number + // in this locale (`-1,5`) is a number. The apostrophe keeps the engine from reading such text + // again in English syntax. + if !body.starts_with('=') && !is_number && gridcraft_engine::core::parse::starts_formula(body) { + return Ok(canonical(&format!("={body}"), locale, wb, sheet, cell).unwrap_or_else(|_| format!("'{body}"))); + } + if body.starts_with('=') { let known = |name: &str| wb.name(name, sheet).is_some() || wb.table(name).is_some(); // Match the engine's existing convenience for missing closing parentheses. let mut body = body.to_string(); @@ -160,6 +165,9 @@ mod tests { assert_eq!(entry("-1,5").as_deref(), Some("-1.5")); assert_eq!(entry("+2,5%").as_deref(), Some("+2.5%")); assert_eq!(entry("-").as_deref(), Some("-")); - assert_eq!(entry("--"), None); + // An entry that doesn't read as a formula stays text; only `=` entries are refused. + assert_eq!(entry("--").as_deref(), Some("'--")); + assert_eq!(entry("+34 612 345 678").as_deref(), Some("'+34 612 345 678")); + assert_eq!(entry("=--"), None); } } diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 64cf2810e1..06e2039841 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -161,7 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| -| 2026-10-20 | minor | Typing a leading `+` or `-` starts a formula whenever the entry isn't a number on its own, as in Excel: `+5+4` enters `=+5+4` and `-5+4` enters `=-5+4` (they stayed text; only a letter or `(` after the sign started one). `+5`, `-5` and `-$5` stay numbers, a lone `+` or `-` stays text, and an entry like `--` that isn't a valid formula is refused like `=--`; pasted or split by Text to Columns it stays text (#2023) | +| 2026-10-20 | minor | Typing a leading `+` or `-` starts a formula whenever the entry isn't a number on its own, as in Excel: `+5+4` enters `=+5+4` and `-5+4` enters `=-5+4` (they stayed text; only a letter or `(` after the sign started one). `+5`, `-5` and `-$5` stay numbers, and a lone `+` or `-` and an entry that doesn't read as a formula (`--`, `+49 170 1234567`) stay text, as observed in Excel for the web; only `=` entries are refused (#2023) | | 2026-10-19 | minor | Ctrl+Enter (`range.fill`) gives each cell what typing the input there gives: a General cell gets the automatic number format (`5%`, `$5`, `3/4/2026`, `=TODAY()`) even when it already held a value, and a cell with its own format, a formatted row or column, or Text keeps it. Cells that held a value used to keep General (`5%` showed 0.05), and empty cells took the active cell's format | | 2026-10-18 | minor | Keytip letters typed with left Alt held continue the sequence (Alt+H, O, A / Alt+H, O, I ran nothing before: Alt+letter cancelled keytips); other Alt combinations, AltGr and macOS Option are unchanged (#1846) | | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | From b9324d91796fc91b53c7e4042de021dcc2cddafc Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:13:27 +0200 Subject: [PATCH 4/6] Formula entry: text that would read as a formula is edited with its apostrophe `text_needs_quote_prefix` still used the old rule (a sign followed by a letter or `(`), so text such as `+5+4` (typed as `'+5+4`, or read from a CSV or XLSX) was edited without its apostrophe and became a formula when confirmed unchanged. It now asks what `input_to_cell` asks: the entry starts a formula and parses as one. Text that stays text anyway (`--`) is still edited as it is. Found by @iluebbe in #2023. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/display.rs | 10 +++++++--- crates/engine/src/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index aa8bdb22dd..ad94c808c1 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -41,12 +41,16 @@ fn text_needs_quote_prefix(t: &str, sys: gridcraft_core::date::DateSystem) -> bo if t.is_empty() { return false; } - let mut chars = t.chars(); - let (first, second) = (chars.next(), chars.next()); + let first = t.chars().next(); if matches!(first, Some('=' | '\'')) { return true; } - if matches!(first, Some('+' | '-')) && second.is_some_and(|c| c.is_ascii_alphabetic() || c == '(') { + // A `+` or `-` entry that reads as a formula becomes one (`+5+4`, `-abc`); one that doesn't + // stays text (`--`, `+49 170 1234567`), as `input_to_cell` decides it. + if matches!(first, Some('+' | '-')) + && gridcraft_core::parse::starts_formula(t) + && crate::cmd::edit::parse_typed_formula(t).is_ok_and(|e| crate::cmd::edit::check_arg_counts(&e).is_ok()) + { return true; } !matches!(gridcraft_core::parse::parse_input(t, sys).value, Value::Text(ref x) if x.as_ref() == t) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 285d64ce97..c10a39a9cd 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -267,6 +267,26 @@ fn a_leading_plus_or_minus_starts_a_formula() { assert_eq!(v(&s, "B1"), value, "{typed}"); assert!(!input(&s, "B1").is_some_and(|t| t.starts_with('=')), "{typed}"); } + // Text that would read as a formula is edited with its apostrophe, so confirming the editor + // unchanged keeps it text; text that wouldn't is edited as it is. + for (typed, shown) in [ + ("'+5+4", "'+5+4"), + ("'-5+4", "'-5+4"), + ("'+1-555-1234", "'+1-555-1234"), + ("'-abc", "'-abc"), + ("--", "--"), + ("+49 170 1234567", "+49 170 1234567"), + ] { + s.execute("cell.set", json!({"cell": "B2", "input": typed})).unwrap(); + let before = v(&s, "B2"); + let edit = { + let d = s.doc().unwrap(); + crate::display::edit_text(&d.wb, d.wb.active().unwrap(), CellRef::parse("B2").unwrap()) + }; + assert_eq!(edit, shown, "{typed}"); + s.execute("cell.set", json!({"cell": "B2", "input": edit})).unwrap(); + assert_eq!(v(&s, "B2"), before, "{typed} stays text after an unchanged edit"); + } // As observed in Excel for the web, an entry that doesn't read as a formula stays text, and // one that does but names nothing known is #NAME?. Only `=` entries are refused. for typed in ["--", "-----", "+5+", "+49 170 1234567"] { From af4f17184c0f5e0891d5dd51e1716f3d0447a498 Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:20:56 +0100 Subject: [PATCH 5/6] Formula entry: `+` before a time stays text, as in Excel Checked by hand in Excel for the web (English (United States) regional format): `+1:30` stays text, while `-1:30` is the formula `=-1:30` (rows 1 to 30), `+1/30` and `-1/30` are formulas and `1:30` is a time. `starts_formula` now leaves a `+` followed by a time to the usual value parsing, which keeps it as text. Raised by @iluebbe in #2023. Co-Authored-By: Claude Opus 5.5 --- crates/core/src/parse.rs | 15 ++++++++++----- crates/engine/src/tests.rs | 21 ++++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/core/src/parse.rs b/crates/core/src/parse.rs index 504a92d01f..ffad72c849 100644 --- a/crates/core/src/parse.rs +++ b/crates/core/src/parse.rs @@ -52,9 +52,14 @@ pub fn parse_number_text_in(s: &str, sys: DateSystem) -> Option { /// Whether typed cell input is meant as a formula: it starts with `=`, or, as Lotus 1-2-3 users /// type it, with `+` or `-` and isn't a number on its own (`+5+4` is `=+5+4`, `-5` the number -5, /// a lone `+` or `-` text). A leading space or apostrophe makes the input text. Callers parse -/// the formula; a `+` or `-` entry that doesn't parse (`--`) stays text, as in Excel. +/// the formula; a `+` or `-` entry that doesn't parse (`--`) stays text, as in Excel. So does `+` +/// before a time (`+1:30`), while `-1:30` is a formula (rows 1 to 30), as Excel for the web does. pub fn starts_formula(input: &str) -> bool { - input.starts_with('=') || (input.len() > 1 && input.starts_with(['+', '-']) && parse_number_text(input).is_none()) + input.starts_with('=') + || (input.len() > 1 + && input.starts_with(['+', '-']) + && parse_number_text(input).is_none() + && !input.strip_prefix('+').is_some_and(|time| time.contains(':') && parse_number_text(time).is_some())) } /// Text that Excel's "Number Stored as Text" check flags: it would be a number if typed (signs, @@ -588,12 +593,12 @@ mod tests { #[test] fn a_leading_sign_starts_a_formula_unless_the_entry_is_a_number() { // #2023: Excel reads `+5+4` as `=+5+4`, as Lotus 1-2-3 did. - for s in ["=1", "=", "+5+4", "-5+4", "--5", "+-5", "-.5*2", "-3/4", "+A1", "-SUM(1,2)", "+(5+4)", "-abc", "--"] { + for s in ["=1", "=", "+5+4", "-5+4", "--5", "+-5", "-.5*2", "-3/4", "+A1", "-SUM(1,2)", "+(5+4)", "-abc", "--", "-1:30", "+1/30", "-1/30"] { assert!(starts_formula(s), "{s}"); } // Signed numbers, percentages and currency stay numbers; a lone sign, a leading space or - // apostrophe and other text stay text. - for s in ["+5", "-5", "+5%", "-$5", "-1e3", "+.5", "-1,234.5", "+", "-", " +5+4", "'+5+4", "5+4", "", "a-b"] { + // apostrophe, `+` before a time and other text stay text. + for s in ["+5", "-5", "+5%", "-$5", "-1e3", "+.5", "-1,234.5", "+", "-", " +5+4", "'+5+4", "5+4", "", "a-b", "+1:30", "+0:30:15"] { assert!(!starts_formula(s), "{s}"); } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 20c434d920..5239762d88 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -251,9 +251,16 @@ fn a_leading_plus_or_minus_starts_a_formula() { // #2023: like Excel (and Lotus 1-2-3 before it), typing `+5+4` enters the formula `=+5+4`. let mut s = s(); let input = |s: &Session, a: &str| s.doc().unwrap().wb.active().unwrap().cell(CellRef::parse(a).unwrap()).map(|c| c.input_text()); - for (cell, typed, value) in - [("A1", "+5+4", 9.0), ("A2", "-5+4", -1.0), ("A3", "--5", 5.0), ("A4", "-3/4", -0.75), ("A5", "+A1*2", 18.0), ("A6", "-(A1)", -9.0)] - { + for (cell, typed, value) in [ + ("A1", "+5+4", 9.0), + ("A2", "-5+4", -1.0), + ("A3", "--5", 5.0), + ("A4", "-3/4", -0.75), + ("A5", "+A1*2", 18.0), + ("A6", "-(A1)", -9.0), + ("A7", "+1/30", 1.0 / 30.0), + ("A8", "-1/30", -1.0 / 30.0), + ] { s.execute("cell.set", json!({"cell": cell, "input": typed})).unwrap_or_else(|e| panic!("{typed}: {e}")); assert_eq!(input(&s, cell), Some(format!("={typed}")), "{typed}"); assert_eq!(v(&s, cell), Value::Number(value), "{typed}"); @@ -281,6 +288,7 @@ fn a_leading_plus_or_minus_starts_a_formula() { ("'-abc", "'-abc"), ("--", "--"), ("+49 170 1234567", "+49 170 1234567"), + ("+1:30", "+1:30"), ] { s.execute("cell.set", json!({"cell": "B2", "input": typed})).unwrap(); let before = v(&s, "B2"); @@ -294,16 +302,19 @@ fn a_leading_plus_or_minus_starts_a_formula() { } // As observed in Excel for the web, an entry that doesn't read as a formula stays text, and // one that does but names nothing known is #NAME?. Only `=` entries are refused. - for typed in ["--", "-----", "+5+", "+49 170 1234567"] { + for typed in ["--", "-----", "+5+", "+49 170 1234567", "+1:30"] { s.execute("cell.set", json!({"cell": "C1", "input": typed})).unwrap_or_else(|e| panic!("{typed}: {e}")); assert_eq!(v(&s, "C1"), Value::text(typed), "{typed}"); assert_eq!(input(&s, "C1").as_deref(), Some(typed), "{typed}"); } s.execute("cell.set", json!({"cell": "C2", "input": "- item"})).unwrap(); assert_eq!(v(&s, "C2"), Value::Error(gridcraft_core::CellError::Name)); + // Excel for the web keeps `+1:30` as text (above) but reads `-1:30` as a formula (rows 1 to 30). + s.execute("cell.set", json!({"cell": "C40", "input": "-1:30"})).unwrap(); + assert_eq!(input(&s, "C40").as_deref(), Some("=-1:30")); let e = s.execute("cell.set", json!({"cell": "C1", "input": "=--"})).unwrap_err().to_string(); assert!(e.contains("problem with this formula"), "{e}"); - assert_eq!(v(&s, "C1"), Value::text("+49 170 1234567"), "a refused entry keeps the cell"); + assert_eq!(v(&s, "C1"), Value::text("+1:30"), "a refused entry keeps the cell"); // Pasted or split by Text to Columns, the same rule applies. s.execute("edit.paste", json!({"at": "D1", "text": "--\t-5+4\n"})).unwrap(); assert_eq!(v(&s, "D1"), Value::text("--")); From 2829120d4dd7ef54db7aa5afa6c44d5e1b464b32 Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:24:10 +0100 Subject: [PATCH 6/6] Recently Used: a sign before a space starts a formula too main's `functions_in_formula` test expected `- SUM(1)` not to be a formula. With #2023's rule, which `is_formula_input` now uses, it is one, as Excel for the web enters `- remember this` as `=- remember this`. --- crates/engine/src/recent.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/recent.rs b/crates/engine/src/recent.rs index 0a1bba2dc2..2bd8a375cb 100644 --- a/crates/engine/src/recent.rs +++ b/crates/engine/src/recent.rs @@ -154,7 +154,9 @@ mod tests { assert_eq!(functions_in_formula("+SUM(1)"), ["SUM"]); assert_eq!(functions_in_formula("-ABS(A1)"), ["ABS"]); assert!(functions_in_formula("-5").is_empty() && functions_in_formula("+1e3").is_empty(), "numbers"); - assert!(functions_in_formula("+").is_empty() && functions_in_formula("- SUM(1)").is_empty(), "not formulas"); + assert!(functions_in_formula("+").is_empty() && functions_in_formula("--").is_empty(), "not formulas"); + // A sign before a space still starts a formula, as Excel takes `- SUM(1)` (#2023). + assert_eq!(functions_in_formula("- SUM(1)"), ["SUM"]); } #[test]