mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-18 16:14:57 +00:00
Delete separate rows or columns in one pass (#2385)
* Delete separate rows or columns in one pass Fixes #2287
This commit is contained in:
1 parent
bf48def687
commit
272ae54101
5 files changed
+148
-30
No files matched your search
@@ -35,7 +35,7 @@ use go_to::go_to_special;
|
||||
#[cfg(test)]
|
||||
use go_to::{GO_TO_MAX_AREAS, GO_TO_RECENT, reference_parts};
|
||||
use paste_special::paste_special;
|
||||
pub(crate) use paste_special::{rewrite_all_formulas, rewrite_cell_links, rewrite_python_refs};
|
||||
pub(crate) use paste_special::{rewrite_all_formulas_many, rewrite_cell_links, rewrite_python_refs};
|
||||
use selection::{select_all, select_col, select_region, select_row, selection_move, selection_next, selection_next_corner, selection_set};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
|
||||
@@ -566,10 +566,15 @@ fn paste_validations(
|
||||
|
||||
/// Applies a reference adjustment to every cell formula and defined name in the workbook.
|
||||
pub(crate) fn rewrite_all_formulas(wb: &mut gridcraft_model::Workbook, target: &str, e: &gridcraft_formula::adjust::Edit) -> Result<()> {
|
||||
rewrite_all_formulas_many(wb, target, std::slice::from_ref(e))
|
||||
}
|
||||
|
||||
/// [`rewrite_all_formulas`] for several edits applied in order, visiting each formula once.
|
||||
pub(crate) fn rewrite_all_formulas_many(wb: &mut gridcraft_model::Workbook, target: &str, edits: &[gridcraft_formula::adjust::Edit]) -> Result<()> {
|
||||
use gridcraft_formula::adjust::Edit;
|
||||
// Rows and columns inserted or deleted on the target sheet move its legacy array ranges too
|
||||
// (a cut moves its arrays itself).
|
||||
let moves_arrays = matches!(e, Edit::Insert { .. } | Edit::Delete { .. } | Edit::InsertCells { .. } | Edit::DeleteCells { .. });
|
||||
let moves_arrays = |e: &Edit| matches!(e, Edit::Insert { .. } | Edit::Delete { .. } | Edit::InsertCells { .. } | Edit::DeleteCells { .. });
|
||||
for si in 0..wb.sheets.len() {
|
||||
let host = wb.sheets.get(si).map(|s| s.name.clone()).unwrap_or_default();
|
||||
let keys: Vec<(CellRef, std::sync::Arc<Formula>)> =
|
||||
@@ -577,16 +582,29 @@ pub(crate) fn rewrite_all_formulas(wb: &mut gridcraft_model::Workbook, target: &
|
||||
if keys.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let on_target = moves_arrays && host.eq_ignore_ascii_case(target);
|
||||
let on_target = host.eq_ignore_ascii_case(target);
|
||||
let Some(sheet) = wb.sheet_mut(si) else { continue };
|
||||
for (c, f) in keys {
|
||||
// Borrows the tree and copies and prints only the formulas the edit changes. A formula
|
||||
// the edit leaves alone keeps its stored text, even if that text is not canonical.
|
||||
let Some(new) = f.with_expr(|x| gridcraft_formula::adjust::adjust_if_changed(x, &host, target, e)) else { continue };
|
||||
let array = match f.array {
|
||||
Some(a) if on_target => crate::cmd::sheet::map_range(a, e),
|
||||
a => a,
|
||||
// Borrows the tree and copies and prints only the formulas the edits change. A formula
|
||||
// the edits leave alone keeps its stored text, even if that text is not canonical.
|
||||
let Some(new) = f.with_expr(|x| {
|
||||
let mut cur: Option<gridcraft_formula::Expr> = None;
|
||||
for e in edits {
|
||||
if let Some(n) = gridcraft_formula::adjust::adjust_if_changed(cur.as_ref().unwrap_or(x), &host, target, e) {
|
||||
cur = Some(n);
|
||||
}
|
||||
}
|
||||
cur
|
||||
}) else {
|
||||
continue;
|
||||
};
|
||||
let mut array = f.array;
|
||||
for e in edits {
|
||||
array = match array {
|
||||
Some(a) if on_target && moves_arrays(e) => crate::cmd::sheet::map_range(a, e),
|
||||
a => a,
|
||||
};
|
||||
}
|
||||
let nf = match new {
|
||||
Some(new) => Formula { array, dynamic: f.dynamic, ..Formula::from_expr(new) },
|
||||
None if array != f.array => Formula { array, ..(*f).clone() },
|
||||
@@ -600,12 +618,12 @@ pub(crate) fn rewrite_all_formulas(wb: &mut gridcraft_model::Workbook, target: &
|
||||
for n in wb.names.iter_mut() {
|
||||
if let Ok(expr) = gridcraft_formula::parse_name(&n.formula) {
|
||||
// Names are workbook-level: unqualified refs don't occur; qualified ones adjust.
|
||||
let new = gridcraft_formula::adjust::adjust(expr, "\u{0}", target, e);
|
||||
let new = edits.iter().fold(expr, |expr, e| gridcraft_formula::adjust::adjust(expr, "\u{0}", target, e));
|
||||
n.formula = gridcraft_formula::print(&new);
|
||||
}
|
||||
}
|
||||
rewrite_cell_links(wb, |host, expr| gridcraft_formula::adjust::adjust(expr, host, target, e));
|
||||
rewrite_python_refs(wb, &|host, expr| gridcraft_formula::adjust::adjust(expr, host, target, e))
|
||||
rewrite_cell_links(wb, |host, expr| edits.iter().fold(expr, |expr, e| gridcraft_formula::adjust::adjust(expr, host, target, e)));
|
||||
rewrite_python_refs(wb, &|host, expr| edits.iter().fold(expr, |expr, e| gridcraft_formula::adjust::adjust(expr, host, target, e)))
|
||||
}
|
||||
|
||||
/// Runs `f` over every reference a floating object holds (a chart's series values, categories,
|
||||
|
||||
@@ -242,15 +242,20 @@ fn shift_sheet_features(sh: &mut Sheet, axis: Axis, at: u32, count: u32, insert:
|
||||
/// Rewrites every formula reference (chart series included), sparkline source and PivotTable
|
||||
/// source for a structural edit on `target`.
|
||||
pub(super) fn rewrite(cx: &mut Ctx, target: &str, e: &Edit) -> Result<()> {
|
||||
cx.edits.push((target.to_string(), e.clone()));
|
||||
rewrite_many(cx, target, std::slice::from_ref(e))
|
||||
}
|
||||
|
||||
/// [`rewrite`] for several edits applied in order, parsing each reference once.
|
||||
fn rewrite_many(cx: &mut Ctx, target: &str, edits: &[Edit]) -> Result<()> {
|
||||
cx.edits.extend(edits.iter().map(|e| (target.to_string(), e.clone())));
|
||||
let wb = &mut cx.wb;
|
||||
super::edit::rewrite_all_formulas(wb, target, e)?;
|
||||
super::edit::rewrite_all_formulas_many(wb, target, edits)?;
|
||||
for si in 0..wb.sheets.len() {
|
||||
let host = wb.sheets.get(si).map(|s| s.name.clone()).unwrap_or_default();
|
||||
let Some(sh) = wb.sheet_mut(si) else { continue };
|
||||
let fix = |text: &str| -> String {
|
||||
match gridcraft_formula::parse(text) {
|
||||
Ok(expr) => gridcraft_formula::print(&gridcraft_formula::adjust::adjust(expr, &host, target, e)),
|
||||
Ok(expr) => gridcraft_formula::print(&edits.iter().fold(expr, |expr, e| gridcraft_formula::adjust::adjust(expr, &host, target, e))),
|
||||
Err(_) => text.to_string(),
|
||||
}
|
||||
};
|
||||
@@ -263,7 +268,7 @@ pub(super) fn rewrite(cx: &mut Ctx, target: &str, e: &Edit) -> Result<()> {
|
||||
let body = pt.source.trim().trim_start_matches('=');
|
||||
if let Ok(expr) = gridcraft_formula::parse(body) {
|
||||
let before = gridcraft_formula::print(&expr);
|
||||
let after = gridcraft_formula::print(&gridcraft_formula::adjust::adjust(expr, &host, target, e));
|
||||
let after = gridcraft_formula::print(&edits.iter().fold(expr, |expr, e| gridcraft_formula::adjust::adjust(expr, &host, target, e)));
|
||||
if after != before {
|
||||
pt.source = after;
|
||||
}
|
||||
@@ -378,23 +383,21 @@ fn delete_lines(s: &mut Session, p: &Json, axis: Axis) -> Result<Json> {
|
||||
let out = edit(s, |cx| {
|
||||
let name = cx.wb.sheet(sheet).map(|s| s.name.clone()).unwrap_or_default();
|
||||
cx.protection_checked = true;
|
||||
// Last area first, so the earlier ones keep their positions.
|
||||
for (at, count) in areas {
|
||||
{
|
||||
let sh = cx.sheet_mut(sheet)?;
|
||||
let block = match axis {
|
||||
Axis::Rows => RangeRef::rows(at, (at + count - 1).min(MAX_ROWS - 1)),
|
||||
Axis::Cols => RangeRef::cols(at, (at + count - 1).min(MAX_COLS - 1)),
|
||||
};
|
||||
sh.take_cells(block);
|
||||
match axis {
|
||||
Axis::Rows => sh.cells.shift_rows(at + count, -(count as i64)),
|
||||
Axis::Cols => sh.cells.shift_cols(at + count, -(count as i64)),
|
||||
}
|
||||
{
|
||||
let sh = cx.sheet_mut(sheet)?;
|
||||
// Cells move once, past every deleted area above or left of them.
|
||||
let ascending: Vec<(u32, u32)> = areas.iter().rev().copied().collect();
|
||||
match axis {
|
||||
Axis::Rows => sh.cells.delete_rows(&ascending),
|
||||
Axis::Cols => sh.cells.delete_cols(&ascending),
|
||||
}
|
||||
// Last area first, so the earlier ones keep their positions.
|
||||
for &(at, count) in &areas {
|
||||
shift_sheet_features(sh, axis, at, count, false);
|
||||
}
|
||||
rewrite(cx, &name, &Edit::Delete { axis, at, count })?;
|
||||
}
|
||||
let edits: Vec<Edit> = areas.iter().map(|&(at, count)| Edit::Delete { axis, at, count }).collect();
|
||||
rewrite_many(cx, &name, &edits)?;
|
||||
cx.structural = true;
|
||||
Ok(Json::Null)
|
||||
})?;
|
||||
|
||||
@@ -869,6 +869,65 @@ fn deleting_a_multi_area_row_selection_removes_every_area_and_undoes_once() {
|
||||
assert_eq!((v(&s, "A1"), v(&s, "B1"), v(&s, "C1")), (Value::Number(2.0), Value::Number(4.0), Value::Empty));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::disallowed_methods)] // native-only test: timing the two deletes
|
||||
fn deleting_many_separate_rows_takes_about_as_long_as_one_block() {
|
||||
// #2287: every 10th row of a 12,000-row sheet, as 1,200 single-row areas.
|
||||
let (rows, step) = (12_000u32, 10u32);
|
||||
let values: Vec<serde_json::Value> = (0..rows).map(|r| json!([r, r + 100_000, r + 200_000])).collect();
|
||||
let deleted = |r: u32| r >= 1 && (r - 1).is_multiple_of(step);
|
||||
let list = (0..rows).filter(|&r| deleted(r)).map(|r| format!("{0}:{0}", r + 1)).collect::<Vec<_>>().join(",");
|
||||
let build = || {
|
||||
let mut s = s();
|
||||
s.execute("range.setValues", json!({"range": "A1", "values": values})).unwrap();
|
||||
s.execute("cell.set", json!({"cell": "E1", "input": format!("=SUM(A1:A{rows})")})).unwrap();
|
||||
s.execute("cell.set", json!({"cell": "E3", "input": "=B25"})).unwrap();
|
||||
s
|
||||
};
|
||||
let mut block = build();
|
||||
let t = std::time::Instant::now();
|
||||
block.execute("home.deleteRows", json!({"rows": format!("2:{}", rows / step + 1)})).unwrap();
|
||||
let block_time = t.elapsed();
|
||||
|
||||
let mut s = build();
|
||||
let t = std::time::Instant::now();
|
||||
s.execute("home.deleteRows", json!({"rows": list})).unwrap();
|
||||
let separate_time = t.elapsed();
|
||||
|
||||
let kept: Vec<u32> = (0..rows).filter(|&r| !deleted(r)).collect();
|
||||
let d = s.doc().unwrap();
|
||||
let sh = d.wb.active().unwrap();
|
||||
assert_eq!(sh.used_range().unwrap().end.row as usize, kept.len() - 1);
|
||||
for (i, r) in kept.iter().enumerate() {
|
||||
assert_eq!(sh.value(CellRef::new(i as u32, 0)), Value::Number(*r as f64));
|
||||
assert_eq!(sh.value(CellRef::new(i as u32, 2)), Value::Number((*r + 200_000) as f64));
|
||||
}
|
||||
assert_eq!(s.execute("cell.get", json!({"cell": "E1"})).unwrap()["formula"], format!("=SUM(A1:A{})", kept.len()));
|
||||
// B25 loses rows 2, 12 and 22 above it; the formula itself moves from E3 to E2.
|
||||
assert_eq!(s.execute("cell.get", json!({"cell": "E2"})).unwrap()["formula"], "=B22");
|
||||
assert!(
|
||||
separate_time < block_time * 20 + std::time::Duration::from_millis(500),
|
||||
"separate rows took {separate_time:?}, one block took {block_time:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_many_separate_columns_matches_per_area_result() {
|
||||
let mut s = s();
|
||||
let values: Vec<serde_json::Value> = (0..50).map(|r| json!((0..40).map(|c| r * 100 + c).collect::<Vec<_>>())).collect();
|
||||
s.execute("range.setValues", json!({"range": "A1", "values": values})).unwrap();
|
||||
s.execute("cell.set", json!({"cell": "A60", "input": "=SUM(A1:AN1)+H2"})).unwrap();
|
||||
// Columns B, E, H (refs) and F:G.
|
||||
s.execute("home.deleteColumns", json!({"cols": "B:B,E:E,H:H,F:G"})).unwrap();
|
||||
let kept: Vec<u32> = (0..40).filter(|c| ![1, 4, 5, 6, 7].contains(c)).collect();
|
||||
for r in 0..50u32 {
|
||||
for (i, c) in kept.iter().enumerate() {
|
||||
assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::new(r, i as u32)), Value::Number((r * 100 + c) as f64));
|
||||
}
|
||||
}
|
||||
assert_eq!(s.execute("cell.get", json!({"cell": "A60"})).unwrap()["formula"], "=SUM(A1:AI1)+#REF!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_columns_reorders_without_overwriting_and_undoes_once() {
|
||||
let mut s = s();
|
||||
|
||||
@@ -290,6 +290,44 @@ impl CellStore {
|
||||
let moved = self.take_rows(0..=u32::MAX, from..=u32::MAX);
|
||||
self.put_rows(moved.into_iter().map(|(row, cols)| (row, shift_keys(cols, delta))));
|
||||
}
|
||||
/// Deletes the rows in `spans` (`(first, count)`, ascending and disjoint) and closes the gaps
|
||||
/// in one pass: each later cell moves up once by the number of deleted rows above it.
|
||||
pub fn delete_rows(&mut self, spans: &[(u32, u32)]) {
|
||||
self.delete_lines(spans, true);
|
||||
}
|
||||
/// Column form of [`CellStore::delete_rows`].
|
||||
pub fn delete_cols(&mut self, spans: &[(u32, u32)]) {
|
||||
self.delete_lines(spans, false);
|
||||
}
|
||||
fn delete_lines(&mut self, spans: &[(u32, u32)], rows: bool) {
|
||||
let Some(&(first, _)) = spans.first() else { return };
|
||||
// Lines deleted before the end of each span, for mapping a kept line to its new place.
|
||||
let mut before = Vec::with_capacity(spans.len());
|
||||
let mut total = 0u32;
|
||||
for &(_, count) in spans {
|
||||
total = total.saturating_add(count);
|
||||
before.push(total);
|
||||
}
|
||||
let map = |v: u32| -> Option<u32> {
|
||||
let i = spans.partition_point(|&(at, _)| at <= v);
|
||||
let Some(i) = i.checked_sub(1) else { return Some(v) };
|
||||
let (at, count) = spans.get(i).copied().unwrap_or_default();
|
||||
if v - at < count { None } else { Some(v - before.get(i).copied().unwrap_or_default()) }
|
||||
};
|
||||
let keys: Vec<CellRef> = if rows {
|
||||
let r = RangeRef::new(CellRef::new(first, 0), CellRef::new(gridcraft_core::MAX_ROWS - 1, gridcraft_core::MAX_COLS - 1));
|
||||
self.iter_range(r).map(|(c, _)| c).collect()
|
||||
} else {
|
||||
self.iter().filter(|(c, _)| c.col >= first).map(|(c, _)| c).collect()
|
||||
};
|
||||
let moved: Vec<(CellRef, Cell)> = keys.into_iter().filter_map(|c| self.remove(c).map(|cell| (c, cell))).collect();
|
||||
for (c, cell) in moved {
|
||||
let to = if rows { map(c.row).map(|r| CellRef::new(r, c.col)) } else { map(c.col).map(|x| CellRef::new(c.row, x)) };
|
||||
if let Some(to) = to {
|
||||
self.set(to, cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Shifts cells within rows `r0..=r1` and columns `>= from` horizontally (Insert/Delete Cells
|
||||
/// with shift right/left).
|
||||
pub fn shift_cols_in_rows(&mut self, r0: u32, r1: u32, from: u32, delta: i64) {
|
||||
|
||||
Reference in new issue
Block a user