diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index c4a9e8d4b..cadabcfd9 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -558,11 +558,11 @@ fn paste_validations( Ok(()) } -/// A cut takes its cells' conditional formats and validation with it, from sheet `from` to the -/// block at `at` on sheet `to`, replacing the rules there. The moved parts' formulas are handled -/// as the cut cells' are: kept written for where their cells were (on another sheet, with -/// their references to it named), so the reference rewrite for the move shifts those into the -/// block once. `snapshot` is the source sheet as it was cut. +/// A cut, or Move Columns, takes its cells' conditional formats and validation with it, from +/// sheet `from` to the block at `at` on sheet `to`, replacing the rules there. The moved parts' +/// formulas are handled as the moved cells' are: kept written for where their cells were (on +/// another sheet, with their references to it named), so the reference rewrite for the move +/// shifts those into the block once. `snapshot` is the source sheet before the block moved. pub(crate) fn move_cut_rules( cx: &mut crate::cmd::Ctx, snapshot: &gridcraft_model::Sheet, @@ -602,9 +602,8 @@ pub(crate) fn move_cut_rules( } /// Applies a reference adjustment to every reference the workbook holds -/// ([`Workbook::rewrite_references`]: formulas, names, conditional formats, validation, -/// charts, sparklines, PivotTable sources) and its Python cells, and moves the legacy array -/// ranges on `target`. +/// ([`Workbook::rewrite_references`]) and its Python cells, and moves the legacy array ranges +/// on `target`. /// /// [`Workbook::rewrite_references`]: gridcraft_model::Workbook::rewrite_references pub(crate) fn rewrite_all_formulas(wb: &mut gridcraft_model::Workbook, target: &str, e: &gridcraft_formula::adjust::Edit) -> Result<()> { diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index d648aaec8..8ed541472 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -416,7 +416,8 @@ fn define_name(s: &mut Session, p: &Json) -> Result { }) } -/// Formulas that use a renamed name follow it: cell formulas and other names' definitions, on +/// References to a renamed name follow it: every one the workbook holds +/// ([`gridcraft_model::Workbook::rewrite_references`]) and Python cells' `xl()` references, on /// the sheets where `old` resolves to the renamed name (its own sheet for a sheet-scoped name; /// every sheet without a same-named sheet-level name for a workbook-level one). fn rename_name_in_formulas(wb: &mut gridcraft_model::Workbook, old: &str, old_scope: Option, new: &str) -> Result<()> { diff --git a/crates/engine/src/cmd/sheet.rs b/crates/engine/src/cmd/sheet.rs index 053a1f050..7a1c698d3 100644 --- a/crates/engine/src/cmd/sheet.rs +++ b/crates/engine/src/cmd/sheet.rs @@ -405,6 +405,7 @@ fn move_columns(s: &mut Session, p: &Json) -> Result { // The moved columns' rules go with them as a cut's do, so a rule wider than them // stays on its other cells and checks what it did. let snapshot = cx.wb.sheets.get(sheet).cloned().ok_or_else(|| bad("sheet.moveColumns", "no such sheet"))?; + crate::cmd::edit::move_cut_rules(cx, &snapshot, (sheet, sheet), source, CellRef::new(0, destination))?; { let sh = cx.sheet_mut(sheet)?; let pictures: Vec<_> = @@ -412,6 +413,7 @@ fn move_columns(s: &mut Session, p: &Json) -> Result { let cells = sh.take_cells(source); let moved_lines: Vec<_> = (0..count).filter_map(|offset| sh.cols.remove(&(moved_from + offset)).map(|info| (offset, info))).collect(); let movement = Edit::Move { from: source, to_row: 0, to_col: destination }; + // (Its conditional formats and validation no longer touch the moved columns.) shift_cell_features(sh, &movement); for object in sh .charts @@ -437,7 +439,6 @@ fn move_columns(s: &mut Session, p: &Json) -> Result { sh.cols.insert(destination + offset, info); } } - crate::cmd::edit::move_cut_rules(cx, &snapshot, (sheet, sheet), source, CellRef::new(0, destination))?; for cell in touched { cx.touch(sheet, cell); } diff --git a/crates/engine/src/cmd/sheet/tabs.rs b/crates/engine/src/cmd/sheet/tabs.rs index 7209388f3..77852566c 100644 --- a/crates/engine/src/cmd/sheet/tabs.rs +++ b/crates/engine/src/cmd/sheet/tabs.rs @@ -135,18 +135,26 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result { sh.name = name; // A copied sheet's charts plot the copy's own data, as Excel's do (its formulas keep // what they name). - let fix = |t: &str| match gridcraft_formula::parse(t.trim_start_matches('=')) { - Ok(e) => gridcraft_formula::print(&gridcraft_formula::adjust::rename_sheet(e, &base, &sh.name)), - Err(_) => t.to_string(), + // Text that names no other sheet is kept as written (a leading `=` included). + let copy = sh.name.clone(); + let fix = |t: &mut String| { + let Ok(e) = gridcraft_formula::parse(t.trim_start_matches('=')) else { return }; + let before = gridcraft_formula::print(&e); + let after = gridcraft_formula::print(&gridcraft_formula::adjust::rename_sheet(e, &base, ©)); + if after != before { + *t = if t.starts_with('=') { format!("={after}") } else { after }; + } }; for ch in &mut sh.charts { for se in &mut ch.series { - se.values = fix(&se.values); + fix(&mut se.values); for t in [&mut se.categories, &mut se.name, &mut se.bubble_sizes].into_iter().flatten() { - *t = fix(t); + fix(t); } } - ch.source = ch.source.as_deref().map(fix); + if let Some(t) = ch.source.as_mut() { + fix(t); + } } sh.tables.clear(); // table names must be unique sh.preserved = None; // kept file content (form controls, OLE objects…) stays with the original diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index aec86f357..90d9e6a83 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6685,7 +6685,7 @@ fn pivot_new_sheet_destination_respects_workbook_structure_protection() { /// Renaming a sheet renames a PivotTable's source to it, and the PivotTable still refreshes. #[test] -fn sheet_rename_renames_every_reference() { +fn sheet_rename_renames_a_pivot_source() { let mut s = s(); s.execute("range.setValues", json!({"range": "A1:B3", "values": [["Region", "Sales"], ["East", 1], ["West", 2]]})).unwrap(); s.execute("insert.pivotTable", json!({"source": "Sheet1!$A$1:$B$3", "destination": "Sheet1!E3", "rows": ["Region"], "values": ["Sales"]})) @@ -6774,10 +6774,13 @@ fn a_copied_sheets_chart_plots_the_copy() { let mut s = s(); s.execute("range.setValues", json!({"range": "A1:B3", "values": [["x", "y"], ["a", 1], ["b", 2]]})).unwrap(); s.execute("insert.chart", json!({"range": "A1:B3", "type": "column"})).unwrap(); + // A series name written as a formula on no sheet keeps its `=`, and stays a reference. + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).sheet_mut(0).unwrap().charts[0].series[0].name = Some("=A1".into()); s.execute("sheet.move", json!({"sheet": 0, "to": 1, "copy": true})).unwrap(); let wb = &s.doc().unwrap().wb; let copy = wb.sheets.iter().find(|sh| sh.name != "Sheet1").unwrap(); assert!(copy.charts[0].series[0].values.contains(©.name), "{}", copy.charts[0].series[0].values); + assert_eq!(copy.charts[0].series[0].name.as_deref(), Some("=A1")); } /// A rule's formulas follow its ranges: deleting its top-left row re-hosts them at the cell @@ -6928,6 +6931,15 @@ fn moving_a_column_out_of_a_rule_keeps_every_cell_checking_itself() { assert!(ok(&s, at, "5"), "{at} takes 5"); assert!(!ok(&s, at, "-5"), "{at} refuses -5"); } + // Moved left, a whole range of the rule becomes its top-left: the parts left behind still + // check themselves. + let mut s = super::tests::s(); + s.execute("data.validation", json!({"range": "B1:B3,D1:D3", "type": "custom", "formula1": "=B1>0"})).unwrap(); + s.execute("sheet.moveColumns", json!({"cols": "D:D", "before": 1})).unwrap(); + for at in ["B1", "C2"] { + assert!(ok(&s, at, "5"), "{at} takes 5"); + assert!(!ok(&s, at, "-5"), "{at} refuses -5"); + } } /// A rule's formulas are written for the top-left of all its ranges: when an edit removes the diff --git a/docs/architecture.md b/docs/architecture.md index 8cfe17a69..cd9e6a67d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -221,11 +221,11 @@ Conditional-format and validation formulas are written for the top-left cell of ranges. When an edit moves that cell (cuts a range short, or removes the range holding it), the rule's top-left-relative formulas (`CfRule::relative_formulas_mut`, `Validation::formulas_mut`) are first re-hosted at the cell that takes its place (`rehost_rule`, given the ranges as they were -before the edit), then moved once by the walker, so deleting a rule's first row no longer turns it -into `#REF!`. A range moved whole or left in place keeps its formulas, and colour-scale, data-bar -and icon-set thresholds are read as written. A cut takes the rules on its cells with it (the -engine's `Rule` trait, `remove_rule_cells`, `moved_rule_parts`), so their ranges and formulas move -together, once; a cut to another sheet qualifies their references with the source sheet. A cut is +before the edit), then moved once by the walker, so deleting a rule's first row leaves its formulas reading +the same cells. A range moved whole or left in place keeps its formulas, and colour-scale, data-bar +and icon-set thresholds are read as written. A cut, and Move Columns, take the rules on their +cells with them (`move_cut_rules`, on the engine's `Rule` trait, `remove_rule_cells` and +`moved_rule_parts`), so their ranges and formulas move together, once; a cut to another sheet qualifies their references with the source sheet. A cut is pasted as it is: Paste Special refuses to transpose it or tile it over a larger selection, as Excel does.