From 35ca6435a8832868fe7f89ea69d08ef4c9c97236 Mon Sep 17 00:00:00 2001 From: alehel Date: Mon, 23 Nov 2026 21:44:52 +0100 Subject: [PATCH 1/3] 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 over rows the filter hides would skip cells the cut has taken; both lost the cut cells. --- crates/engine/src/cmd/edit/paste_special.rs | 9 ++++++ crates/engine/src/tests.rs | 35 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index ac1a073ec..6eed0e77b 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -77,6 +77,15 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { } let same_doc = s.doc()?.uid == clip.doc_uid; let cut_move = clip.cut && same_doc && what == What::All; + // A cut empties its cells, so pasting it as links would link to the emptied cells, and a + // destination over rows the filter hides would skip cells the cut has taken: both would lose + // the cut cells, so both are refused before the source is cleared. + if cut_move && link { + return Err(EngineError::Other("The cut area can't be pasted as links: paste it without Paste Link.".into())); + } + if cut_move && skip_rows.range(at.row..at.row.saturating_add(tiles_r.saturating_mul(h))).next().is_some() { + return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + } let src_wb = clip.wb.clone(); let Some(src_sheet) = src_wb.sheet(clip.sheet) else { return Err(EngineError::Other("The copied sheet no longer exists.".into())) }; let src_sheet_name = src_sheet.name.clone(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 569e8a023..8b9e3ff77 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7124,3 +7124,38 @@ fn history_charges_the_storage_of_empty_comment_replies() { // 500 replies of no text are still 500 elements. assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>()); } + +/// 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] +fn a_cut_is_not_pasted_as_links() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2]]})).unwrap(); + s.execute("edit.cut", json!({"range": "A1:B1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "link": true, "at": "E1"})).is_err()); + assert_eq!((v(&s, "A1"), v(&s, "B1"), v(&s, "E1")), (Value::Number(1.0), Value::Number(2.0), Value::Empty)); +} + +/// 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)); +} From 7486d1021b17efcc8c738d1dc8cf3e534fcf98a8 Mon Sep 17 00:00:00 2001 From: alehel Date: Mon, 23 Nov 2026 23:26:25 +0100 Subject: [PATCH 2/3] paste: a cut moves only as a plain Paste onto one cell or its own size One rule in one place replaces the Paste Link and filtered-row refusals. A cut in this workbook empties its source before the destination is written, so every paste that would not write each cut cell once, in one place, loses or misplaces cells: a Paste Special option (link, skip blanks, transpose, an operation, or pasting only part such as values), tiling over a larger or differently shaped area, or rows a filter hides. These are refused before anything changes, and the cut stays pending, as in Excel. A single cell or an area of the cut's size takes it. Tests: one per refused mode, each asserting nothing changed, plus a refused paste followed by plain pastes that still move the cut. --- crates/engine/src/cmd/edit/paste_special.rs | 30 ++++++--- crates/engine/src/tests.rs | 74 +++++++++++++++++++++ 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index 6eed0e77b..116c75be4 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -76,15 +76,27 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { return Err(EngineError::Other("Too many cells to paste as links at once.".into())); } let same_doc = s.doc()?.uid == clip.doc_uid; - let cut_move = clip.cut && same_doc && what == What::All; - // A cut empties its cells, so pasting it as links would link to the emptied cells, and a - // destination over rows the filter hides would skip cells the cut has taken: both would lose - // the cut cells, so both are refused before the source is cleared. - if cut_move && link { - return Err(EngineError::Other("The cut area can't be pasted as links: paste it without Paste Link.".into())); - } - if cut_move && skip_rows.range(at.row..at.row.saturating_add(tiles_r.saturating_mul(h))).next().is_some() { - return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + // A cut in this workbook moves its cells: the source is emptied before the destination is + // written. As in Excel, it moves only as a plain Paste onto one cell or an area of its own size + // and shape. Every other way (a Paste Special option, tiling over a larger or differently shaped + // area, a row the filter hides) would write the cut cells somewhere other than one place, or not + // at all, after their source was gone, so it is refused here, before anything changes, and the + // cut stays pending. + let cut_move = clip.cut && same_doc; + if cut_move { + let plain = what == What::All && !link && !skip_blanks && !transpose && op == "none"; + if !plain { + return Err(EngineError::Other("Cut cells can only be pasted with Paste. To use Paste Special, copy them instead.".into())); + } + let anchored = cell_param_on(s, p, "at", dest_sheet)?.is_some() || sel.is_single(); + if !anchored && (sel.height() != h || sel.width() != w) { + return Err(EngineError::Other( + "The cut cells and the paste area aren't the same size and shape: select one cell, or an area the size of the cut cells.".into(), + )); + } + if skip_rows.range(at.row..at.row.saturating_add(tiles_r.saturating_mul(h))).next().is_some() { + return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + } } let src_wb = clip.wb.clone(); let Some(src_sheet) = src_wb.sheet(clip.sheet) else { return Err(EngineError::Other("The copied sheet no longer exists.".into())) }; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 8b9e3ff77..af107e995 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7159,3 +7159,77 @@ fn a_cut_is_not_pasted_into_filtered_rows() { 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)); } + +/// Skip Blanks would empty a cut's blank cells along with their note and format, then skip them: +/// a cut takes no Paste Special option, and nothing changes. +#[test] +fn a_cut_is_not_pasted_skipping_blanks() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1]]})).unwrap(); + s.execute("review.newNote", json!({"cell": "B1", "text": "note", "author": "Example"})).unwrap(); + s.execute("home.bold", json!({"range": "B1"})).unwrap(); + s.execute("edit.cut", json!({"range": "A1:B1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "skipBlanks": true, "at": "E1"})).is_err()); + let d = s.doc().unwrap(); + let sh = d.wb.active().unwrap(); + let b1 = CellRef::parse("B1").unwrap(); + assert!(sh.comments.contains_key(&b1)); + assert!(d.wb.styles.get(sh.style_id(b1)).font.bold); + assert_eq!((v(&s, "A1"), v(&s, "E1")), (Value::Number(1.0), Value::Empty)); +} + +/// Transposing a cut would move references to it as if it were not transposed: refused, and a +/// formula reading the cut cells still reads them. +#[test] +fn a_cut_is_not_pasted_transposed() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2]]})).unwrap(); + s.execute("cell.set", json!({"cell": "H1", "input": "=B1"})).unwrap(); + s.execute("edit.cut", json!({"range": "A1:B1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "transpose": true, "at": "E5"})).is_err()); + assert_eq!(formula_at(&s, "H1").as_deref(), Some("B1")); + assert_eq!((v(&s, "A1"), v(&s, "B1"), v(&s, "E5"), v(&s, "E6")), (Value::Number(1.0), Value::Number(2.0), Value::Empty, Value::Empty)); +} + +/// A cut lands in one place: onto a larger or differently shaped selection it is refused, as in +/// Excel, rather than tiled. +#[test] +fn a_cut_is_not_pasted_over_a_larger_area() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[5]]})).unwrap(); + s.execute("edit.cut", json!({"range": "A1"})).unwrap(); + s.execute("selection.set", json!({"range": "E1:E3"})).unwrap(); + assert!(s.execute("edit.paste", json!({})).is_err()); + assert_eq!((v(&s, "A1"), v(&s, "E1"), v(&s, "E2"), v(&s, "E3")), (Value::Number(5.0), Value::Empty, Value::Empty, Value::Empty)); +} + +/// A paste operation or a partial paste (values only) of a cut would empty the cut cells and +/// combine or keep only part of them: refused, and nothing changes. +#[test] +fn a_cut_is_not_pasted_with_an_operation_or_in_part() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[5]]})).unwrap(); + s.execute("range.setValues", json!({"range": "E1", "values": [[2]]})).unwrap(); + s.execute("edit.cut", json!({"range": "A1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "operation": "add", "at": "E1"})).is_err()); + assert!(s.execute("edit.pasteSpecial", json!({"what": "values", "at": "E1"})).is_err()); + assert_eq!((v(&s, "A1"), v(&s, "E1")), (Value::Number(5.0), Value::Number(2.0))); +} + +/// A refused paste leaves the cut pending: a plain Paste onto one cell, or onto an area of the cut's +/// size, still moves it. +#[test] +fn a_refused_cut_paste_leaves_the_cut_to_paste_plainly() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2]]})).unwrap(); + s.execute("edit.cut", json!({"range": "A1:B1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "transpose": true, "at": "E5"})).is_err()); + s.execute("selection.set", json!({"range": "E1"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert_eq!((v(&s, "A1"), v(&s, "E1"), v(&s, "F1")), (Value::Empty, Value::Number(1.0), Value::Number(2.0))); + assert!(s.clipboard.is_none()); + s.execute("edit.cut", json!({"range": "E1:F1"})).unwrap(); + s.execute("selection.set", json!({"range": "C3:D3"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert_eq!((v(&s, "E1"), v(&s, "C3"), v(&s, "D3")), (Value::Empty, Value::Number(1.0), Value::Number(2.0))); +} From dd86b4315062654fb7d165a5d9f2cb86d800d5bd Mon Sep 17 00:00:00 2001 From: alehel Date: Tue, 24 Nov 2026 04:38:18 +0100 Subject: [PATCH 3/3] paste: a cut is refused over any filter-hidden row it would land on, anchored or tall The hidden-row check read the rows a tiled single-row paste skips, which is empty for a paste at an explicit cell or a taller block, so those wrote into hidden rows after the source was cleared. It now checks the cut's own destination rows. --- crates/engine/src/cmd/edit/paste_special.rs | 10 ++++++++-- crates/engine/src/tests.rs | 22 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index 116c75be4..32683b4de 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -94,8 +94,14 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { "The cut cells and the paste area aren't the same size and shape: select one cell, or an area the size of the cut cells.".into(), )); } - if skip_rows.range(at.row..at.row.saturating_add(tiles_r.saturating_mul(h))).next().is_some() { - return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + // The rows the cut itself would land on, whatever the paste would skip: an anchored or + // taller block is pasted straight down, so `skip_rows` (which only a tiled single row uses) + // can't tell whether it would write into a hidden row. + if let Some(end) = at.offset(i64::from(h) - 1, i64::from(w) - 1) { + let dest = RangeRef::new(at, end); + if s.doc()?.wb.sheet(dest_sheet).is_some_and(|sh| !super::filtered_rows(sh, dest).is_empty()) { + return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + } } } let src_wb = clip.wb.clone(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index af107e995..0cb4bdc1b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7160,6 +7160,28 @@ fn a_cut_is_not_pasted_into_filtered_rows() { assert_eq!((v(&s, "A4"), v(&s, "B4"), v(&s, "E1")), (Value::Number(1.0), Value::Number(2.0), Value::Empty)); } +/// A taller cut, or one pasted at an explicit cell, goes straight down: refused when a row the +/// filter hides is among the rows it would land on, and nothing changes. A visible place takes it. +#[test] +fn an_anchored_cut_is_not_pasted_over_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": "A10", "values": [[1, 2], [3, 4]]})).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": "A10:B11"})).unwrap(); + assert!(s.execute("edit.paste", json!({"at": "A2"})).is_err()); + assert_eq!((v(&s, "A10"), v(&s, "B11")), (Value::Number(1.0), Value::Number(4.0))); + assert_eq!((v(&s, "A2"), v(&s, "A3")), (Value::from("v2"), Value::from("v3"))); + s.execute("edit.paste", json!({"at": "A4"})).unwrap(); + assert_eq!((v(&s, "A4"), v(&s, "B5"), v(&s, "A10")), (Value::Number(1.0), Value::Number(4.0), Value::Empty)); +} + /// Skip Blanks would empty a cut's blank cells along with their note and format, then skip them: /// a cut takes no Paste Special option, and nothing changes. #[test]