Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
161 commits
Select commit Hold shift + click to select a range
acfd38f
Engine: undo groups make several commands one undo step
prxnvt Oct 12, 2026
d4f1e61
Engine: a recording buffer of top-level commands, apart from the journal
prxnvt Oct 12, 2026
b7bce3b
Engine: document trust for workbooks opened from files
prxnvt Oct 12, 2026
7d1fee8
Engine: workbook data in chunked hidden names (cmd/stash.rs)
prxnvt Oct 12, 2026
e165eaf
Engine: a linked paste of values from a file carries the file's trust
prxnvt Oct 21, 2026
83ae63f
Engine: a stash item with only later chunks reads as missing and fall…
prxnvt Oct 21, 2026
f4e0553
Engine: a command that runs its own undo groups makes one undo step f…
prxnvt Oct 21, 2026
ab27d35
Engine: a full undo history keeps one step per command that runs its …
prxnvt Oct 21, 2026
0457f10
Engine: pasting only formats from a file leaves the destination's tru…
prxnvt Oct 21, 2026
eb3eaa0
Engine: a command keeps its group's name only for a group it ended it…
prxnvt Oct 22, 2026
2287f63
Engine: a group rolled back inside a command keeps the redo history
prxnvt Oct 22, 2026
471114e
Engine: a visible name of the item shape is not workbook data
prxnvt Oct 22, 2026
a4b4872
Model: macros as typed command lists, saved in the workbook
prxnvt Oct 12, 2026
54c0864
Engine: macro.run and macro.check: input checks, footprint dry run, p…
prxnvt Oct 12, 2026
20d2a3b
Model: a macro name with control characters is refused on every path
prxnvt Oct 21, 2026
23c0107
Engine: macro policy checks a step's parameters after tokens resolve;…
prxnvt Oct 21, 2026
7011395
Engine: a macro renamed on a name clash keeps its name within the len…
prxnvt Oct 21, 2026
d305566
Docs: running and checking macros
prxnvt Oct 21, 2026
4966b9c
Engine: opening a file reads its hidden macro names in one bounded pass
prxnvt Oct 21, 2026
3bc1d91
Engine: macro tokens, resolved to A1 references when a step runs
prxnvt Oct 12, 2026
4c6962a
Engine: a partial macro footprint no longer clears later writes; the …
prxnvt Oct 21, 2026
709a995
Model: one damaged macro no longer stops a JSON workbook opening
prxnvt Oct 21, 2026
7a0f591
Docs: macro tokens
prxnvt Oct 21, 2026
bf456c9
Engine: a macro's footprint dry runs keep to the run's timeLimit
prxnvt Oct 21, 2026
45e580a
Engine: macro.debug steps through a macro as one undo step
prxnvt Oct 12, 2026
ee7040a
Docs: macro format and storage
prxnvt Oct 21, 2026
ac7c1ae
Engine: a reference on a sheet named like a token stays a reference
prxnvt Oct 21, 2026
b51d588
Engine: macros can't open, hide or show windows
prxnvt Oct 21, 2026
ccea7a8
Engine: macro debugger reports a step that panics as the step's failure
prxnvt Oct 21, 2026
7d31c5a
Engine: a macro in a newer format is dropped with a warning, not save…
prxnvt Oct 21, 2026
77150be
Engine: col("Header") is refused on a range without a header row
prxnvt Oct 21, 2026
06bbcb5
Engine: macro approvals are keyed on the macro's canonical JSON, not …
prxnvt Oct 21, 2026
62cecec
Docs: debugging macros
prxnvt Oct 21, 2026
3c74dfc
Engine: renaming macros with clashing names on open takes near-linear…
prxnvt Oct 21, 2026
0ea5d3e
Engine: a Name: token reaches a defined name that holds periods
prxnvt Oct 21, 2026
f1a46a2
Docs: the macro policy summary lists the file commands that do run
prxnvt Oct 21, 2026
f553619
Engine: the macro debugger lock also refuses edits that another comma…
prxnvt Oct 21, 2026
6312c7c
Engine: only hidden names are read as macro storage
prxnvt Oct 22, 2026
045cf42
Engine: a column header made by a formula matches its token while for…
prxnvt Oct 22, 2026
c0079aa
Docs: a failed macro run rolls back workbook changes; a file it alrea…
prxnvt Oct 21, 2026
6a004bf
Engine: stopping Record Actions waits while a macro in its workbook i…
prxnvt Oct 21, 2026
66f7993
Merge main into #462
prxnvt Oct 22, 2026
faff24f
Merge main into #465
prxnvt Oct 22, 2026
3eed813
Merge main into #2151
prxnvt Oct 22, 2026
38ad2db
Merge main into #2144
prxnvt Oct 22, 2026
bca1483
Merge main into #2145
prxnvt Oct 22, 2026
602d445
Merge main into #462
prxnvt Oct 23, 2026
18ec913
Engine: a command keeps its group's name only for a step that is exac…
prxnvt Oct 23, 2026
a8e8bf8
Engine: the recording buffer is bounded in bytes and keeps the start …
prxnvt Oct 23, 2026
5248266
Docs: the architecture revision row carries the date the change lands…
prxnvt Oct 23, 2026
5ca8865
Engine: writing a stash item checks for a clashing visible name in on…
prxnvt Oct 23, 2026
114088d
Engine: a command's changes after ending a group, or before opening o…
prxnvt Oct 23, 2026
6ab8d97
Engine: the undo steps made inside a command are bounded while it runs
prxnvt Oct 23, 2026
080b6d9
Engine: a grouped action that closes its document succeeds
prxnvt Oct 23, 2026
7e383f4
Docs: undo groups at a command's boundaries, and the bound on steps m…
prxnvt Oct 23, 2026
66e69cb
Engine: a linked paste carries the file's trust whatever it pastes
prxnvt Oct 24, 2026
6b7c48f
Engine: a stash chunk number with leading zeros names someone else's …
prxnvt Oct 24, 2026
a3b5f8e
Engine: a command that panics leaves the undo history as it found it
prxnvt Oct 24, 2026
456cf73
Engine: a headless call that fails for want of a file is not recorded
prxnvt Oct 24, 2026
d1e51a4
Engine: a later stash chunk on its own is not a listed item
prxnvt Oct 24, 2026
8440427
Tests: a linked paste of column widths or notes copies its metadata a…
prxnvt Oct 24, 2026
d7c3c18
Engine: the panic guard restores entries a nested Undo removed
prxnvt Oct 24, 2026
cef58da
Merge #462 (d7c3c183, with main at 530a8cc8) into #465
prxnvt Oct 24, 2026
378ce3a
Docs: the macro page describes this version
prxnvt Oct 24, 2026
3ca1252
Engine: a group a panicking command ended keeps its step
prxnvt Oct 24, 2026
7ee9d77
Docs: list every paste that carries a file's trust
prxnvt Oct 24, 2026
259b6e6
Model: Repeat and If nest four deep, as the limit says
prxnvt Oct 24, 2026
8219c18
Engine: macro storage shares the list, saves in one pass, keeps unkno…
prxnvt Oct 24, 2026
712bb76
Engine: a grouped action that panics is rolled back before the panic …
prxnvt Oct 24, 2026
4672b6d
Model: an input name that reads as a cell reference is refused
prxnvt Oct 24, 2026
673af7e
Merge #462 (712bb766) into #465
prxnvt Oct 24, 2026
00b6346
Engine: macro tokens resolve only where they mean a reference, and re…
prxnvt Oct 24, 2026
44a2f4e
Merge #465 (673af7ec, with #462 at 712bb766 and main at 530a8cc8) int…
prxnvt Oct 24, 2026
0df540f
Merge #2151 (44a2f4e2, with #465 at 673af7ec, #462 at 712bb766 and ma…
prxnvt Oct 24, 2026
bbf3600
Engine: the recorder counts a dropped step without serializing it
prxnvt Oct 24, 2026
43b0e39
Engine: set_many refuses one key given twice
prxnvt Oct 24, 2026
551eda7
Engine: the xlsx copy gets an empty macro list, not a copy of the sha…
prxnvt Oct 24, 2026
461395d
Engine: col and cols keep the header state, so a later data takes the…
prxnvt Oct 24, 2026
74ff8ac
Engine: macro runs refuse a shift that moves an input, stay clean whe…
prxnvt Oct 24, 2026
fb4acb0
Engine: closing the debugged workbook ends the debugger
prxnvt Oct 24, 2026
a626705
Engine: a dry run puts the journal back even after the journal trimme…
prxnvt Oct 24, 2026
879b96f
Docs: the footprint dry run is skipped when the conflict rule is over…
prxnvt Oct 24, 2026
2215934
Engine: a shift is refused when the footprint cannot follow every write
prxnvt Oct 24, 2026
bac5b9c
Merge #2144 (2215934c, with #2151 at 44a2f4e2, #465 at 673af7ec, #462…
prxnvt Oct 24, 2026
2b33fcf
Engine: a failed command rolls back the undo groups it opened
prxnvt Oct 24, 2026
2e72407
Engine: the recorder skips a call that only opened a dialog
prxnvt Oct 24, 2026
0fa6e78
Engine: stash items keep characters XML can't carry
prxnvt Oct 24, 2026
b907107
Engine: list stash items in one pass over the names
prxnvt Oct 24, 2026
4668fc3
Engine: the name commands leave GridCraft's data names alone
prxnvt Oct 24, 2026
37bf09e
Engine: queries take no copy of the undo history
prxnvt Oct 24, 2026
9bbba19
Engine: a document's maker says where its content came from
prxnvt Oct 24, 2026
f2a7252
Merge main into #462
prxnvt Oct 24, 2026
b44a513
Engine: the name commands refuse only names that hold GridCraft's data
prxnvt Oct 24, 2026
b09a550
UI: the toolbar's Undo and Redo follow the commands' own availability
prxnvt Oct 24, 2026
4a294ae
Engine: the name commands use the stash's own rule for what holds its…
prxnvt Oct 24, 2026
3b25aa0
Docs: the panic guard's note says what happens to a group the command…
prxnvt Oct 24, 2026
95c7e70
Merge #462 (3b25aa0d, with main at b8771d51) into #465
prxnvt Oct 24, 2026
fddf2d9
Merge main at 6ead1fb5 (#462 merged) into #465
prxnvt Oct 24, 2026
f040b4e
Engine: stash listings say how many items were past the cap
prxnvt Oct 24, 2026
6cd4ceb
Engine: Stop Action runs on its own and is not recorded
prxnvt Oct 24, 2026
54bd4ec
Engine: a run that edited the workbook before opening a dialog is rec…
prxnvt Oct 24, 2026
3c722f4
Engine: a visible name of the macro storage shape no longer blocks an…
prxnvt Oct 25, 2026
ac36eee
Engine: a JSON workbook with no sheets keeps its macros
prxnvt Oct 25, 2026
471a40b
Model: every field a macro file sets has a limit, and an input declar…
prxnvt Oct 25, 2026
3905a43
Engine: a macro in a newer format is kept unread and saved back uncha…
prxnvt Oct 25, 2026
da439b7
Tests: a visible clash, a newer macro and a broken input together, th…
prxnvt Oct 25, 2026
4c11929
Tests: a visible legacy name of the macro storage shape survives a save
prxnvt Oct 25, 2026
e285194
Docs: a kept macro is saved back unchanged, not byte for byte
prxnvt Oct 25, 2026
e88760b
Engine: an item too large to read takes no place in a stash listing
prxnvt Oct 25, 2026
3f8e6c2
Engine: a JSON save refuses a macro that skipped the check, as an xls…
prxnvt Oct 25, 2026
9fdf03c
Merge #465 (3f8e6c28, with main at 6ead1fb5, where #462 is merged) in…
prxnvt Oct 25, 2026
9331b05
Merge #2151 (9fdf03cf, with #465 at 3f8e6c28 and main at 6ead1fb5, wh…
prxnvt Oct 25, 2026
c225caf
Merge #2144 (9331b05f, with #2151, #465 at 3f8e6c28 and main at 6ead1…
prxnvt Oct 25, 2026
63a2980
Merge main at 17b9f6ff into #2151
prxnvt Oct 25, 2026
8ea4e27
Merge #2151 (63a29800, with main at 17b9f6ff) into #2144
prxnvt Oct 25, 2026
2fb31f1
Merge #2144 (8ea4e279, with main at 17b9f6ff) into #2145
prxnvt Oct 25, 2026
5860857
Engine: a macro run checks each step for writes over data; the check …
prxnvt Oct 25, 2026
0719458
Engine: macro.check and the footprint: where a macro writes, worked o…
prxnvt Oct 25, 2026
d053b1f
Engine: a run is checked step by step even when its footprint was com…
prxnvt Oct 25, 2026
ad79b07
Engine: a macro's inputs, output and Repeat ranges follow their cells…
prxnvt Oct 25, 2026
65cbcdd
Engine: a macro run names the files it kept, refuses parameters it wo…
prxnvt Oct 25, 2026
567ed0a
Merge #2144 (65cbcdd6) into the footprint pull request
prxnvt Oct 25, 2026
3708c80
Engine: macro.check says whether the run would go ahead; the question…
prxnvt Oct 25, 2026
2cf5b80
Docs: the macro page's conflict rules wrap at the page's width
prxnvt Oct 25, 2026
c715c36
Merge #2144 (2cf5b808) into the footprint pull request
prxnvt Oct 25, 2026
ce2ab9a
Merge the footprint pull request (c715c36c, with #2144) into #2145
prxnvt Oct 25, 2026
d309c85
Engine: Stop Action waits while a macro is being debugged
prxnvt Oct 25, 2026
7dad14e
Engine: the macro debugger locks its workbook to looking around, ends…
prxnvt Oct 25, 2026
f66ebd6
Docs: debugging macros: actions, start options, the lock and the time…
prxnvt Oct 25, 2026
198fe48
Engine: the run's check of each step finds the inputs on their sheet …
prxnvt Oct 25, 2026
e9effe5
Merge #2144 (198fe482) into the footprint pull request
prxnvt Oct 25, 2026
dadffdb
Engine: macro.check shifts and checks again, as the run does, before …
prxnvt Oct 25, 2026
227078f
Merge the footprint pull request (dadffdb3, with #2144) into #2145
prxnvt Oct 25, 2026
90a46da
Engine: macro.run's description lists only what it returns here
prxnvt Oct 25, 2026
267d5bc
Merge #2144 (90a46da4) into the footprint pull request
prxnvt Oct 25, 2026
91bb5ed
Merge the footprint pull request (f6f4585c, with #2144) into #2145
prxnvt Oct 25, 2026
f6f4585
Engine: a dry run puts the document's revision back
prxnvt Oct 25, 2026
f225cd1
Engine: Check Macro waits while a macro is being debugged
prxnvt Oct 25, 2026
bfcf50a
Engine: rolling back an undo group takes back the steps it recorded
prxnvt Oct 25, 2026
0aae1ff
Engine: an xlsx save refuses a macro that skipped the check, as a JSO…
prxnvt Oct 25, 2026
891986b
Merge #2144 (fe14c0d8) into the footprint pull request
prxnvt Oct 25, 2026
fe14c0d
Merge #2151 (0aae1ffe) into #2144
prxnvt Oct 25, 2026
361fff9
Engine: a dry run journals nothing, so the journal and Record Actions…
prxnvt Oct 25, 2026
f0b667a
Merge the footprint pull request (361fff98, with #2144 and #2151) int…
prxnvt Oct 25, 2026
2e8284c
Engine: a panic during a dry run puts the journal pause and the touch…
prxnvt Oct 25, 2026
0a929ea
Engine: a rolled-back group takes back only the steps recorded in its…
prxnvt Oct 25, 2026
1a00ce1
Merge #2151 (0a929eaf) into #2144
prxnvt Oct 25, 2026
24f0c3d
Merge #2144 (1a00ce19) into the footprint pull request
prxnvt Oct 25, 2026
9a22c4b
Merge the footprint pull request (24f0c3df, with #2144 and #2151) int…
prxnvt Oct 25, 2026
b9aa4a6
Merge #2151 (0533480c) into #2144
prxnvt Oct 25, 2026
0533480
Engine: recorded steps carry a number, so a group's rollback finds it…
prxnvt Oct 25, 2026
65634c2
Merge #2144 (b9aa4a6a) into the footprint pull request
prxnvt Oct 25, 2026
bf6a852
Merge the footprint pull request (65634c2b, with #2144 and #2151) int…
prxnvt Oct 25, 2026
910b50c
Merge branch 'main' into pr/macros-run
prxnvt Oct 26, 2026
e85297e
Merge branch 'main' into pr/macros-engine
prxnvt Oct 26, 2026
3772f2c
Macros: write the macro names into xlsx again after the main merge
prxnvt Oct 26, 2026
483276f
Merge #465 (3772f2c6, with main at 42bbf6ba) into #2151
prxnvt Oct 26, 2026
6781aa4
Merge main (f6b0cec6) into #2151
prxnvt Oct 26, 2026
34eee86
Merge #2151 (6781aa49, with main at f6b0cec6) into #2144
prxnvt Oct 26, 2026
d4096ee
Merge #2144 (34eee860, with main at f6b0cec6) into #2550
prxnvt Oct 26, 2026
4d26e39
Merge #2550 (d4096eee, with main at f6b0cec6) into #2145
prxnvt Oct 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/engine/src/cmd/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@ fn paste_special(s: &mut Session, p: &Json) -> Result<Json> {
gridcraft_formula::adjust::Edit::MoveToSheet { from: src, to_sheet: dest_name, to_row: at.row, to_col: at.col }
};
rewrite_all_formulas(&mut cx.wb, &src_sheet_name, &e);
cx.edits.push((src_sheet_name.clone(), e));
cx.structural = true;
}
let end = at.offset_clamped((tiles_r * h) as i64 - 1, (tiles_c * w) as i64 - 1);
Expand Down
29 changes: 24 additions & 5 deletions crates/engine/src/cmd/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Stop Action",
[],
None,
"{} → {stopped: bool}: stops an action left running on the active workbook (an open undo group, such as a debugger's) and puts back the workbook and its undo history as they were when it began",
"{} → {stopped: bool, macro?, message?}: stops an action left running on the active workbook (an open undo group, such as a debugger's) and puts back the workbook and its undo history as they were when it began; a macro being debugged there is stopped (`macro` names it)",
has_doc,
stop_action
),
Expand Down Expand Up @@ -1018,8 +1018,13 @@ fn queries(s: &mut Session, _: &Json) -> Result<Json> {
fn record(s: &mut Session, p: &Json) -> Result<Json> {
if bool_param(p, "stop").unwrap_or(false) || s.recording.is_some() {
let Some(start) = s.recording.take() else { return Ok(json!({"recording": false})) };
let cmds: Vec<Json> =
s.journal.iter().skip(start).filter(|(id, _)| !id.starts_with("automate.")).map(|(id, p)| json!({"command": id, "params": p})).collect();
let cmds: Vec<Json> = s
.journal
.iter()
.skip(start)
.filter(|(id, _)| !id.starts_with("automate.") && !crate::record::NOT_RECORDED.contains(&id.as_str()))
.map(|(id, p)| json!({"command": id, "params": p}))
.collect();
let name = str_param(p, "name").map(str::to_string).unwrap_or_else(|| format!("Script {}", start));
s.execute("automate.newScript", json!({"name": name, "commands": cmds}))?;
return Ok(json!({"recording": false, "saved": name, "steps": cmds.len()}));
Expand Down Expand Up @@ -1142,10 +1147,24 @@ fn run_script(s: &mut Session, p: &Json) -> Result<Json> {
}

/// Automate › Stop Action: rolls back the undo groups open on the active workbook, so Undo and
/// Redo work there again.
/// Redo work there again. A macro being debugged on that workbook is stopped with them.
fn stop_action(s: &mut Session, _: &Json) -> Result<Json> {
// Inside another command the open groups are that command's own (an action in progress):
// a step of it that stopped them would pull the workbook from under the rest of its run.
if s.depth() > 1 {
return Err(EngineError::Disabled("automate.stopAction".into(), "Stop Action runs on its own, not as a step of a script or macro.".into()));
}
let uid = s.doc()?.uid;
Ok(json!({"stopped": s.abort_groups(uid)}))
let mut out = json!({"stopped": s.abort_groups(uid)});
// A macro being debugged here had its group among them: its run is over.
if let Some((name, wrote_files)) = crate::macros::debug::end_stopped(s, uid) {
out["macro"] = json!(name);
out["message"] = json!(format!(
"Macro '{name}', which was being debugged, was stopped and rolled back. {}",
crate::macros::run::rolled_back(&wrote_files)
));
}
Ok(out)
}

fn delete_script(s: &mut Session, p: &Json) -> Result<Json> {
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ fn insert_table(s: &mut Session, p: &Json) -> Result<Json> {
let e = Edit::InsertCells { axis: Axis::Rows, range: RangeRef::new(r.start, CellRef::new(r.start.row, r.end.col)) };
shift_cell_features(sh, &e);
let name = sh.name.clone();
rewrite(&mut cx.wb, &name, &e);
rewrite(cx, &name, &e);
let sh = cx.sheet_mut(sheet)?;
for (i, c) in (r.start.col..=r.end.col).enumerate() {
sh.set_value(CellRef::new(r.start.row, c), gridcraft_core::Value::text(format!("Column{}", i + 1)));
Expand Down
52 changes: 52 additions & 0 deletions crates/engine/src/cmd/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Macros: typed command lists that run as one undo step. See [`crate::macros`] for the model,
//! tokens, runner, debugger and policy. Automate scripts (`automate.*`) are a separate, older feature.

use serde_json::Value as Json;

use super::*;

pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
"macro.run",
"Run Macro",
[],
None,
"{name, inputs?: {Input: \"B2:D9\"} | \"B2:D9\", onConflict?: ask|fail|overwrite|shiftDown|shiftRight, overwrite?: bool, approve?: bool, timeLimit?: seconds} runs as one undo step; any error rolls it all back → {macro, ran, inputs, stopped?, shifted?}",
has_doc,
run
),
cmd!(
query "macro.check",
"Check Macro",
[],
None,
"{name, inputs?, onConflict?, overwrite?, timeLimit?: seconds} validates the inputs and dry-runs the macro on the workbook (then puts everything back) → {ok (macro.run with these parameters would go ahead without a question), errors?, needsApproval?, onConflict, inputs, output?, conflicts, footprint}",
has_doc,
check
),
cmd!(
"macro.debug",
"Step Into",
[],
None,
"{action: start|step|continue|runTo|stop|state, name, inputs?, onConflict?, overwrite?, approve?, timeLimit?: seconds per action (start, as macro.run), step: path (runTo), keep?: bool (stop), breakpoints?: [\"3\", \"2.1\"] (steps of the macro), breakpointsOnly?: bool (the list replaces the steps' own breakpoints)} debugs a macro one step at a time; stop keeps the changes as one undo step or rolls them back. Runs on its own, never as a step of a script or macro. Until it finishes or stops, its workbook runs only macro.debug, queries, selection and sheet changes, Go To, Find, Copy, zoom, Switch Window, Close and Stop Action (other workbooks stay editable) → {state: paused|finished|stopped|failed, current: {step, kind, command?, params?, iteration?}, lastParams, executed, changed (at most 500), changedCells, atBreakpoint?, shifted?, wroteFiles?, message?, error?}",
has_doc,
|s, p| crate::macros::debug::debug(s, p)
),
]
}

fn name_param<'a>(cmd: &str, p: &'a Json) -> Result<&'a str> {
str_param(p, "name").filter(|n| !n.trim().is_empty()).ok_or_else(|| bad(cmd, "missing `name`"))
}

fn run(s: &mut Session, p: &Json) -> Result<Json> {
let name = name_param("macro.run", p)?;
crate::macros::run::run(s, name, p)
}

fn check(s: &mut Session, p: &Json) -> Result<Json> {
let name = name_param("macro.check", p)?;
crate::macros::run::check(s, name, p)
}
31 changes: 29 additions & 2 deletions crates/engine/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod format;
pub mod formulas;
pub mod insert;
pub mod inspect;
pub mod macros;
pub mod pivot;
pub mod print;
pub mod review;
Expand Down Expand Up @@ -143,6 +144,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(view::specs());
v.extend(inspect::specs());
v.extend(extra::specs());
v.extend(macros::specs());
v.extend(draw::specs());
v.extend(spelling::specs());
v.extend(solver::specs());
Expand Down Expand Up @@ -338,6 +340,9 @@ pub struct Ctx<'a> {
/// The command applied sheet protection itself (row/column inserts and deletes the
/// protection allows), so cells it shifts aren't refused as edits of locked cells.
pub protection_checked: bool,
/// The structural edits the command made, in order (the sheet's name, the edit), as
/// references were rewritten for them.
pub edits: Vec<(String, gridcraft_formula::adjust::Edit)>,
}

pub(crate) fn edit<R>(s: &mut Session, f: impl FnOnce(&mut Ctx) -> Result<R>) -> Result<R> {
Expand All @@ -355,15 +360,33 @@ pub(crate) fn commit<R>(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result<R>)
#[allow(clippy::disallowed_methods)]
let t0 = if cfg!(target_arch = "wasm32") { None } else { std::env::var_os("GRIDCRAFT_PROFILE").map(|_| std::time::Instant::now()) };
let mut sel = d.selection.clone();
let mut ctx = Ctx { wb: (*d.wb).clone(), changed: Vec::new(), structural: false, sel: &mut sel, fit_rows: Vec::new(), protection_checked: false };
let mut ctx = Ctx {
wb: (*d.wb).clone(),
changed: Vec::new(),
structural: false,
sel: &mut sel,
fit_rows: Vec::new(),
protection_checked: false,
edits: Vec::new(),
};
let r = f(&mut ctx)?;
let Ctx { mut wb, changed, structural, mut fit_rows, protection_checked, .. } = ctx;
let Ctx { mut wb, changed, structural, mut fit_rows, protection_checked, edits, .. } = ctx;
if !protection_checked {
check_protection(&d.wb, &wb, &d.unlocked_ranges)?;
}
if !structural {
check_arrays(&d.wb, &wb)?;
}
if let Some(log) = d.touched.as_mut() {
for (si, c) in changed.iter().take(TOUCH_LOG_LIMIT.saturating_sub(log.len())) {
if let Some(sh) = wb.sheet(*si) {
log.push((sh.name.clone(), *c));
}
}
}
if let Some(log) = d.edits.as_mut() {
log.extend(edits);
}
if changed.len() <= 200_000 {
fit_rows.extend(changed.iter().map(|(s, c)| (*s, c.row)));
}
Expand Down Expand Up @@ -391,6 +414,10 @@ pub(crate) fn commit<R>(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result<R>)
Ok(r)
}

/// The most cells a dry run's touch log keeps; a footprint whose log is full may be missing writes
/// that kept a cell's value, so a shift is refused then.
pub(crate) const TOUCH_LOG_LIMIT: usize = 1_000_000;

/// Excel's message for an edit of a locked cell on a protected sheet.
pub(crate) const PROTECTED: &str = "The cell or chart you're trying to change is on a protected sheet.";

Expand Down
18 changes: 10 additions & 8 deletions crates/engine/src/cmd/sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ fn shift_sheet_features(sh: &mut Sheet, axis: Axis, at: u32, count: u32, insert:
}

/// Rewrites every formula reference (and chart series) for a structural edit on `target`.
pub(super) fn rewrite(wb: &mut gridcraft_model::Workbook, target: &str, e: &Edit) {
pub(super) fn rewrite(cx: &mut Ctx, target: &str, e: &Edit) {
cx.edits.push((target.to_string(), e.clone()));
let wb = &mut cx.wb;
super::edit::rewrite_all_formulas(wb, target, e);
for si in 0..wb.sheets.len() {
let host = wb.sheets.get(si).map(|s| s.name.clone()).unwrap_or_default();
Expand Down Expand Up @@ -321,7 +323,7 @@ fn insert_lines(s: &mut Session, p: &Json, axis: Axis) -> Result<Json> {
// New lines take the format of the line before (Excel's "Format Same As Above").
inherit_line_format(sh, axis, at, count);
}
rewrite(&mut cx.wb, &name, &Edit::Insert { axis, at, count });
rewrite(cx, &name, &Edit::Insert { axis, at, count });
cx.structural = true;
Ok(Json::Null)
})?;
Expand Down Expand Up @@ -386,7 +388,7 @@ fn delete_lines(s: &mut Session, p: &Json, axis: Axis) -> Result<Json> {
}
shift_sheet_features(sh, axis, at, count, false);
}
rewrite(&mut cx.wb, &name, &Edit::Delete { axis, at, count });
rewrite(cx, &name, &Edit::Delete { axis, at, count });
}
cx.structural = true;
Ok(Json::Null)
Expand Down Expand Up @@ -438,7 +440,7 @@ fn move_columns(s: &mut Session, p: &Json) -> Result<Json> {
sh.cells.shift_cols(before, count as i64);
shift_sheet_features(sh, Axis::Cols, before, count, true);
}
rewrite(&mut cx.wb, &name, &Edit::Insert { axis: Axis::Cols, at: before, count });
rewrite(cx, &name, &Edit::Insert { axis: Axis::Cols, at: before, count });

let mut touched = Vec::new();
{
Expand Down Expand Up @@ -476,14 +478,14 @@ fn move_columns(s: &mut Session, p: &Json) -> Result<Json> {
for cell in touched {
cx.touch(sheet, cell);
}
rewrite(&mut cx.wb, &name, &Edit::Move { from: source, to_row: 0, to_col: destination });
rewrite(cx, &name, &Edit::Move { from: source, to_row: 0, to_col: destination });

{
let sh = cx.sheet_mut(sheet)?;
sh.cells.shift_cols(delete_at + count, -(count as i64));
shift_sheet_features(sh, Axis::Cols, delete_at, count, false);
}
rewrite(&mut cx.wb, &name, &Edit::Delete { axis: Axis::Cols, at: delete_at, count });
rewrite(cx, &name, &Edit::Delete { axis: Axis::Cols, at: delete_at, count });
*cx.sel = crate::Selection {
active: CellRef::new(active_row, final_start),
anchor: CellRef::new(active_row, final_start),
Expand Down Expand Up @@ -528,7 +530,7 @@ fn insert_cells(s: &mut Session, p: &Json) -> Result<Json> {
}
shift_cell_features(sh, &e);
let name = cx.wb.sheet(sheet).map(|s| s.name.clone()).unwrap_or_default();
rewrite(&mut cx.wb, &name, &e);
rewrite(cx, &name, &e);
cx.structural = true;
Ok(Json::Null)
})?;
Expand Down Expand Up @@ -563,7 +565,7 @@ fn delete_cells(s: &mut Session, p: &Json) -> Result<Json> {
shift_cell_features(sh, &e);
}
// References into the deleted cells become #REF!; the rest move.
rewrite(&mut cx.wb, &name, &e);
rewrite(cx, &name, &e);
cx.structural = true;
Ok(Json::Null)
})?;
Expand Down
Loading
Loading