Merge PR #1976: Enter bare numbers in Percentage-formatted cells as percents

This commit is contained in:
aiongg
2026-10-22 02:35:44 +00:00
2 changed files with 31 additions and 1 deletions
+11 -1
View File
@@ -176,7 +176,17 @@ pub(crate) fn input_to_cell(input: &str, sheet: usize, at: CellRef, wb: &mut gri
}
return Ok(Some(cell));
}
let parsed = gridcraft_core::parse::parse_input(input, wb.date_system);
let mut parsed = gridcraft_core::parse::parse_input(input, wb.date_system);
// Like Excel's automatic percent entry: a bare number of 1 or more typed into a cell already
// formatted as a percentage is a percent (3 -> 3%); smaller numbers are taken as fractions.
if parsed.format.is_none()
&& !input.contains('%')
&& wb.styles.get(style).num_fmt.as_str().ends_with('%')
&& let Value::Number(n) = parsed.value
&& n.abs() >= 1.0
{
parsed.value = Value::Number(n / 100.0);
}
let mut style = style;
if let Some(code) = parsed.format {
let cur = wb.styles.get(style).num_fmt.as_str().to_string();
+20
View File
@@ -0,0 +1,20 @@
//! Typing a bare number into a Percentage-formatted cell enters it as a percent (issue #1967).
use gridcraft_engine::display::cell_text;
use serde_json::json;
fn enter(input: &str) -> String {
let mut s = gridcraft_engine::Session::new();
s.new_workbook();
s.execute("home.numberFormat", json!({"range": "A1", "format": "Percentage"})).unwrap();
s.execute("cell.set", json!({"cell": "A1", "input": input})).unwrap();
let d = s.doc().unwrap();
cell_text(&d.wb, d.wb.active().unwrap(), gridcraft_engine::core::CellRef::new(0, 0))
}
#[test]
fn bare_number_in_percent_cell_is_a_percent() {
assert_eq!(enter("3"), "3.00%");
assert_eq!(enter("0.03"), "3.00%");
assert_eq!(enter("3%"), "3.00%");
}