From 80e7525d290254876756f4c83d594fa846107a24 Mon Sep 17 00:00:00 2001 From: alehel Date: Mon, 23 Nov 2026 13:04:58 +0100 Subject: [PATCH] paste: a cut is refused as Paste Link or into filtered-out rows before its source is cleared Paste Link would write links to the cells the cut empties, and a destination in rows the filter hides would skip every cell the cut writes; both lost the cut cells. --- crates/engine/src/cmd/edit/paste_special.rs | 16 +++++++++---- crates/engine/src/tests.rs | 26 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index f81495ea3..42bbc5037 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -86,10 +86,18 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { _ => src_sheet.name.clone(), }; // A cut moves its block as it is, as Excel's does: it can't be transposed, tiled over a - // larger selection or pasted skipping blanks (the move's references, rules and arrays follow - // the whole block unturned, and its rules replace the destination's under every cell). - if cut_move && (transpose || skip_blanks || tiles_r > 1 || tiles_c > 1) { - return Err(EngineError::Other("The cut area can only be pasted as it is: paste it without Transpose or Skip Blanks, into one cell.".into())); + // larger selection, pasted skipping blanks or pasted as links (the move's references, rules + // and arrays follow the whole block unturned, its rules replace the destination's under every + // cell, and links would read the cells the cut empties). These are refused before the source + // is cleared. + if cut_move && (transpose || skip_blanks || link || tiles_r > 1 || tiles_c > 1) { + return Err(EngineError::Other( + "The cut area can only be pasted as it is: paste it without Transpose, Skip Blanks or Paste Link, into one cell.".into(), + )); + } + // Nor can it skip rows the filter hides: the skipped cells would leave the sheet with the cut. + if cut_move && skip_rows.range(at.row..at.row.saturating_add(h)).next().is_some() { + return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); } edit(s, |cx| { // Styles from another workbook must be re-interned. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 9cd7fbd94..cdb36ea32 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7557,6 +7557,8 @@ fn a_cut_is_not_transposed_or_tiled() { s.execute("edit.cut", json!({"range": "A1:C1"})).unwrap(); assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "transpose": true, "at": "E1"})).is_err()); assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "skipBlanks": true, "at": "E1"})).is_err()); + // Paste Link would write links to the cells the cut empties: refused before the source is cleared. + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "link": true, "at": "E1"})).is_err()); s.execute("selection.set", json!({"range": "E1:G2"})).unwrap(); assert!(s.execute("edit.pasteSpecial", json!({"what": "all"})).is_err()); let sh = s.doc().unwrap().wb.sheets[0].clone(); @@ -7567,6 +7569,30 @@ fn a_cut_is_not_transposed_or_tiled() { assert_eq!(s.doc().unwrap().wb.sheets[0].cond_formats[0].ranges.iter().map(|r| r.a1()).collect::>(), vec!["E1:G1".to_string()]); } +/// A cut pasted into a row the filter hides would skip every cell it writes, so the cells would +/// leave the sheet with the cut: it is refused and nothing changes. A visible row takes it. +#[test] +fn a_cut_is_not_pasted_into_filtered_rows() { + let mut s = s(); + let mut rows = vec![json!(["Val", "Tag"])]; + for i in 2..=5 { + rows.push(json!([format!("v{i}"), if i == 3 { "hide" } else { "keep" }])); + } + s.execute("range.setValues", json!({"range": "A1", "values": rows})).unwrap(); + s.execute("range.setValues", json!({"range": "E1", "values": [[1, 2]]})).unwrap(); + s.execute("selection.set", json!({"range": "A1:B1"})).unwrap(); + s.execute("data.filter", json!({})).unwrap(); + s.execute("data.filterBy", json!({"column": "B", "values": ["keep"]})).unwrap(); + s.execute("edit.cut", json!({"range": "E1:F1"})).unwrap(); + s.execute("selection.set", json!({"range": "A3:B3"})).unwrap(); + assert!(s.execute("edit.paste", json!({})).is_err()); + assert_eq!((v(&s, "E1"), v(&s, "F1")), (Value::Number(1.0), Value::Number(2.0))); + assert_eq!(v(&s, "A3"), Value::from("v3")); + s.execute("selection.set", json!({"range": "A4:B4"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert_eq!((v(&s, "A4"), v(&s, "B4"), v(&s, "E1")), (Value::Number(1.0), Value::Number(2.0), Value::Empty)); +} + /// Internal hyperlinks keep the place they name as typed, as Excel's "Place in This Document" /// links do: renaming or deleting a sheet, or inserting rows, leaves them as they were. #[test]