From dc9066cae0fbce36e82cd3a74c7cf9c216fc6c72 Mon Sep 17 00:00:00 2001 From: Zinadin Zidan Date: Sat, 14 Nov 2026 22:35:11 +0600 Subject: [PATCH] engine: bound a row repeated down a narrow selection by the area it fills (#3934) Repeating a copy down a selection narrower than it writes the copy's width on every row, so the 4,000,000-cell limit on a tiled paste now counts that area rather than the selection's. Past it the copy is pasted once, as a tiled paste over too large a selection already is. --- crates/engine/src/cmd/edit/paste_special.rs | 11 ++++++++++- crates/engine/src/tests.rs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index 1b2cc247b..96fed75bc 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -42,12 +42,21 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { let multiple = sel.height() % h == 0 && sel.width() % w == 0; let down = sel.width() < w && sel.height() > h && sel.height() % h == 0; let across = sel.height() < h && sel.width() > w && sel.width() % w == 0; + // The cells the tiles cover: the selection itself, or for `down`/`across` the selection at the + // copy's width/height, which can be much larger than what was selected. + let area = if down { + u64::from(sel.height()) * u64::from(w) + } else if across { + u64::from(h) * u64::from(sel.width()) + } else { + sel.count() + }; let tile = (multiple || down || across) && !sel.is_single() && cell_param_on(s, p, "at", dest_sheet)?.is_none() && !sel.is_full_cols() && !sel.is_full_rows() - && sel.count() <= 4_000_000; + && area <= 4_000_000; let (tiles_r, tiles_c) = if tile { ((sel.height() / h).max(1), (sel.width() / w).max(1)) } else { (1, 1) }; // A single cell or row repeated down a filtered selection skips the rows the filter hides; a // taller block is pasted straight down, hidden rows included, as in Excel. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 6df5a196d..c0317aee4 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -5660,6 +5660,23 @@ fn paste_into_a_narrower_selection_repeats_the_copy_down_its_rows() { } } +/// #3934: repeating a wide copy down a narrow selection writes the copy's width on every row, so +/// the 4,000,000-cell limit on a tiled paste counts that area, not the selection's. Past it the +/// copy is pasted once, as a tiled paste over too large a selection already is. +#[test] +fn a_row_repeated_down_a_narrow_selection_is_bounded_by_the_area_it_fills() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1]]})).unwrap(); + s.execute("range.setValues", json!({"range": "P1", "values": [[2]]})).unwrap(); + s.execute("edit.copy", json!({"range": "A1:P1"})).unwrap(); + // 300,000 rows selected, 4,800,000 cells once the copy's 16 columns are repeated down them. + s.execute("selection.set", json!({"range": "A3:A300002"})).unwrap(); + let r = s.execute("edit.paste", json!({})).unwrap(); + assert_eq!(r["pasted"], json!("A3:P3")); + assert_eq!(v(&s, "P3"), Value::Number(2.0)); + assert_eq!(v(&s, "A4"), Value::Empty); +} + /// Excel refuses a paste that would run off the sheet rather than pasting part of it: whole /// columns below row 1, whole rows right of column A, a whole column transposed, or a block at /// the last row or column.