engine: paste repeats a copied row down a taller, narrower selection (#3934)

Paste tiled the copy only over a selection whose height and width were
both multiples of the copy's. A row copied into a narrower selection
several rows tall (A1:B1 into A3:A6) was pasted once. Excel repeats it
down every row of the selection at the copy's width. A selection
narrower than the copy now tiles down when its height is a multiple of
the copy's, and one shorter than the copy tiles across when its width
is a multiple.
This commit is contained in:
Zinadin Zidan committed 2026-11-14 22:07:43 +06:00
1 parent c2aba7a3f4
commit 713a66e0d3
2 files changed
+38 -4

No files matched your search

+8 -4
View File
@@ -36,15 +36,19 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result<Json> {
let whole = clip.areas.is_empty() && (clip.selected.is_full_cols() || clip.selected.is_full_rows()) && clip.selected.contains_range(&clip.range);
let src = if whole { clip.selected } else { clip.range };
let (h, w) = if transpose { (src.width(), src.height()) } else { (src.height(), src.width()) };
// Tile the copy over a larger destination that is a multiple of its size.
let tile = sel.height() % h == 0
&& sel.width() % w == 0
// Tile the copy over a larger destination that is a multiple of its size. A selection
// narrower than the copy but a multiple of its height takes it down every row, at the copy's
// width (and a shorter one a multiple of its width across), as in Excel.
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;
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;
let (tiles_r, tiles_c) = if tile { (sel.height() / h, sel.width() / w) } else { (1, 1) };
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.
let skip_rows = match s.doc()?.wb.sheet(dest_sheet) {
+30
View File
@@ -5630,6 +5630,36 @@ fn tiled_paste_clears_destination_cells_under_blank_source_cells() {
assert_eq!(v(&s, "C4"), Value::Empty);
}
/// #3934: a row copied into a taller, narrower selection repeats down every selected row at the
/// copy's width, as in Excel; a column copied into a wider, shorter one repeats across.
#[test]
fn paste_into_a_narrower_selection_repeats_the_copy_down_its_rows() {
for dest in ["A3:A6", "A3:B6"] {
let mut s = s();
s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2]]})).unwrap();
s.execute("edit.copy", json!({"range": "A1:B1"})).unwrap();
s.execute("selection.set", json!({"range": dest})).unwrap();
let r = s.execute("edit.paste", json!({})).unwrap();
assert_eq!(r["pasted"], json!("A3:B6"), "{dest}");
for row in 3..=6 {
assert_eq!(v(&s, &format!("A{row}")), Value::Number(1.0), "{dest} row {row}");
assert_eq!(v(&s, &format!("B{row}")), Value::Number(2.0), "{dest} row {row}");
}
assert_eq!(v(&s, "A2"), Value::Empty);
assert_eq!(v(&s, "A7"), Value::Empty);
}
let mut s = s();
s.execute("range.setValues", json!({"range": "A1", "values": [[1], [2]]})).unwrap();
s.execute("edit.copy", json!({"range": "A1:A2"})).unwrap();
s.execute("selection.set", json!({"range": "C1:F1"})).unwrap();
let r = s.execute("edit.paste", json!({})).unwrap();
assert_eq!(r["pasted"], json!("C1:F2"));
for col in ["C", "D", "E", "F"] {
assert_eq!(v(&s, &format!("{col}1")), Value::Number(1.0), "{col}1");
assert_eq!(v(&s, &format!("{col}2")), Value::Number(2.0), "{col}2");
}
}
/// 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.