From 31d62214635295d9def146d70d5af6a92cf11c9b Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 02:37:01 +0000 Subject: [PATCH] Integration: #1725 prefill Remove Duplicates with the current region when only the cursor is placed --- crates/ui-egui/src/dialogs.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 412bd3951..33dccd0ee 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -458,7 +458,7 @@ impl Dialog { Check { key: "header", label: "My data has headers" }, Text { key: "columnsText", label: "Columns (blank = all, e.g. A,C):" }, ], - json!({"range": sel, "header": true}), + json!({"range": remove_duplicates_range(app, &sel), "header": true}), ), "advancedFilter" => { let list = app @@ -875,6 +875,22 @@ fn validation_defaults(app: &SheetApp) -> Json { v } +/// The range Remove Duplicates starts with. A block selection is used as given; with only the +/// cursor placed, the table's data rows or the current region, as the command picks when it gets +/// no range (a single-cell range would remove nothing). +fn remove_duplicates_range(app: &SheetApp, sel: &str) -> String { + let Some(d) = app.session.active() else { return sel.to_string() }; + let Some(sh) = d.wb.active() else { return sel.to_string() }; + if !d.selection.current().is_single() { + return sel.to_string(); + } + let range = match sh.table_at(d.selection.active) { + Some(t) => t.data_range().map(|dr| gridcraft_engine::core::RangeRef::new(t.range.start, dr.end)).unwrap_or(t.range), + None => gridcraft_engine::selection::current_region(sh, d.selection.active), + }; + range.a1() +} + fn abs(r: &gridcraft_engine::core::RangeRef) -> String { let a = |c: gridcraft_engine::core::CellRef| format!("${}${}", gridcraft_engine::core::col_to_letters(c.col), c.row + 1); if r.is_single() { a(r.start) } else { format!("{}:{}", a(r.start), a(r.end)) } @@ -2958,6 +2974,17 @@ mod tests { assert_eq!(build_params(&dialog)["range"], "D2:E6"); } + #[test] + fn remove_duplicates_with_only_the_cursor_prefills_the_current_region() { + let mut session = gridcraft_engine::Session::default(); + session.new_workbook(); + session.execute("range.setValues", json!({"range": "B2:C4", "values": [["a", 1], ["a", 1], ["b", 2]]})).unwrap(); + session.execute("selection.set", json!({"range": "C3"})).unwrap(); + let mut app = SheetApp::new(session, Default::default()); + let dialog = Dialog::open(&mut app, "removeDuplicates", json!({})).unwrap(); + assert_eq!(dialog.values.get("range"), Some(&json!("B2:C4"))); + } + #[test] fn data_validation_opens_on_the_rule_of_the_active_cell() { let mut session = gridcraft_engine::Session::default();