diff --git a/crates/engine/src/cmd/edit/selection.rs b/crates/engine/src/cmd/edit/selection.rs index e6037c09d3..8c9ed676a0 100644 --- a/crates/engine/src/cmd/edit/selection.rs +++ b/crates/engine/src/cmd/edit/selection.rs @@ -52,6 +52,13 @@ impl crate::DocState { // View state: set in place, so the workbook is neither copied nor made dirty. self.wb.set_active_sheet(i); self.selection = self.sheet_selections.get(i).cloned().unwrap_or_default(); + // A sheet's first visit starts at A1, which may be part of a merged cell: take it whole, + // as a click would (#1348). A selection the sheet remembers comes back as it was. + if self.selection == crate::Selection::default() + && let Some(sh) = self.wb.sheet(i) + { + self.selection.expand_merges(sh); + } } } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b4cf67c65b..1238be31dc 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -345,7 +345,11 @@ impl DocState { wb.refresh_equations(); let scripts_trusted = !cmd::extra::has_scripts(&wb); let wb = Arc::new(wb); - let sel = Selection::at(wb.active().map(|s| s.view_active).unwrap_or_default()); + let mut sel = Selection::at(wb.active().map(|s| s.view_active).unwrap_or_default()); + // A merged active cell is selected whole, as a click on it selects it (#1348). + if let Some(sh) = wb.active() { + sel.expand_merges(sh); + } DocState { saved: wb.clone(), wb, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4853b5eb95..1843ae0c51 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7792,6 +7792,49 @@ fn history_charges_the_storage_of_empty_comment_replies() { assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>()); } +/// #1348: the selection a workbook opens with, and the one a sheet gets on its first visit, take +/// a merged active cell whole, as a click on it does: a merge at A1 and one elsewhere. A +/// selection the sheet remembers comes back as it was, even inside a merged cell. +#[test] +fn opening_and_switching_sheets_select_a_merged_active_cell_whole() { + let mut wb = gridcraft_model::Workbook::new(); + let sh = wb.sheet_mut(0).unwrap(); + sh.set_value(CellRef::new(0, 0), Value::text("Timesheet")); + sh.merges.push(gridcraft_core::RangeRef::parse("A1:AG1").unwrap()); + sh.merges.push(gridcraft_core::RangeRef::parse("C5:E6").unwrap()); + sh.view_active = CellRef::parse("C5").unwrap(); + let mut second = gridcraft_model::Sheet::new("Sheet2"); + second.merges.push(gridcraft_core::RangeRef::parse("A1:C2").unwrap()); + wb.sheets.push(std::sync::Arc::new(second)); + wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("Sheet3"))); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let sel = |s: &Session| { + let sel = &s.doc().unwrap().selection; + (sel.a1(), sel.active.a1()) + }; + let pair = |a: &str, b: &str| (a.to_string(), b.to_string()); + + let mut s = Session::new(); + s.execute("file.open", json!({"name": "timesheet.xlsx", "base64": crate::io::base64_encode(&bytes)})).unwrap(); + assert_eq!(sel(&s), pair("C5:E6", "C5"), "on open, a merge away from A1"); + + s.execute("sheet.activate", json!({"sheet": "Sheet2"})).unwrap(); + assert_eq!(sel(&s), pair("A1:C2", "A1"), "first visit of a sheet, a merge at A1"); + s.execute("sheet.activate", json!({"sheet": "Sheet3"})).unwrap(); + assert_eq!(sel(&s), pair("A1", "A1"), "an unmerged A1 stays a single cell"); + + // A remembered selection is not expanded again: set one inside Sheet2's merge directly. + s.execute("sheet.activate", json!({"sheet": "Sheet2"})).unwrap(); + s.doc_mut().unwrap().selection = crate::Selection::at(CellRef::parse("B2").unwrap()); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + assert_eq!(sel(&s), pair("C5:E6", "C5"), "Sheet1's remembered selection"); + s.execute("selection.set", json!({"cell": "B3"})).unwrap(); + s.execute("sheet.activate", json!({"sheet": "Sheet2"})).unwrap(); + assert_eq!(sel(&s), pair("B2", "B2"), "a remembered selection comes back as it was"); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + assert_eq!(sel(&s), pair("B3", "B3"), "a remembered selection comes back as it was"); +} + /// Paste Link would write links to the cells a cut empties, losing them: a cut can't be pasted /// as links, and nothing changes. #[test]