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.
This commit is contained in:
Zinadin Zidan committed 2026-11-14 22:35:11 +06:00
1 parent 713a66e0d3
commit dc9066cae0
2 files changed
+27 -1

No files matched your search

+10 -1
View File
@@ -42,12 +42,21 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result<Json> {
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.
+17
View File
@@ -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.