mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-16 15:23:57 +00:00
Merge pull request #4183 from ZIDAN44/fix/3934-paste-row-down-narrow-selection
engine: paste repeats a copied row down a taller, narrower selection (#3934)
This commit is contained in:
2 files changed
+65
-5
No files matched your search
@@ -36,15 +36,28 @@ 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;
|
||||
// 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;
|
||||
let (tiles_r, tiles_c) = if tile { (sel.height() / h, sel.width() / w) } else { (1, 1) };
|
||||
&& 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.
|
||||
let skip_rows = match s.doc()?.wb.sheet(dest_sheet) {
|
||||
|
||||
@@ -5818,6 +5818,53 @@ 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");
|
||||
}
|
||||
}
|
||||
|
||||
/// #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.
|
||||
|
||||
Reference in new issue
Block a user