Skip to content

Calc: open without recalculating (file.open recalculate:false; honour manual calc mode) - #1465

Open
sky-helper wants to merge 1 commit into
storytold:mainfrom
sky-helper:p1-open-no-recalc
Open

sky-helper wants to merge 1 commit into
storytold:mainfrom
sky-helper:p1-open-no-recalc

Conversation

@sky-helper

Copy link
Copy Markdown

Summary

file.open can open a workbook with the results stored in it instead of calculating every formula again, and a workbook saved in manual calculation mode now opens that way by default.

  • recalculate: false keeps the stored results: a TODAY() keeps the date it was saved with, and array results (legacy and dynamic) show as stored. recalculate: true always calculates.
  • Without the flag, formulas are calculated again as before, unless the workbook was saved in manual calculation mode and doesn't ask to be calculated on load. A workbook asks when its .xlsx has <calcPr fullCalcOnLoad="1">, its .xls has an Uncalced record, or GridCraft couldn't keep some of its cached array results (see How).
  • The result carries recalculated: bool; a non-boolean recalculate is refused before the file is read. A path that is already open is switched to as it is: the result has alreadyOpen, plus recalculateIgnored: true when recalculate was given.
  • A formula stored without a cached result is blank until it is calculated.
  • view.newWindow doesn't calculate its copy, so a second window shows the same results.

Why

Some workbooks have to read exactly as they were saved (approval forms, signed reports). GridCraft recalculated every formula on open, so volatile functions showed the day the file was viewed and any difference between calculation engines showed up as changed numbers; the workbook's manual calculation mode wasn't taken into account on open either.

Behaviour change

  • A manual-calculation .xlsx (or .xls) that doesn't ask for a full calculation on load now opens with its stored results by default; with fullCalcOnLoad (.xls: Uncalced) it is calculated as before. Automatic-mode workbooks are unchanged.
  • This applies to every entry point that opens through file.open without recalculate: the desktop and web UI (File › Open, recent files, files opened from the OS), the CLI (run --in, convert, eval --in, cat, info, mcp --in), MCP open_workbook, and the control channel's app.open.

How

  • Model: CalcSettings::full_calc_on_load (#[serde(default)]): the stored results are out of date or incomplete. Calc::recalc_all clears it; the XLSX writer writes fullCalcOnLoad while it is set (and, as before, when a formula has no cached result). XLSX reads calcPr fullCalcOnLoad; XLS maps Uncalced (0x005E) to it.
  • Array results (XLSX and XLS readers): the values cached in an array formula's range used to be dropped so the formula could fill its range on recalculation (Legacy array formulas: fill the range they were entered in #156). They are now kept as the formula's spilled values (Sheet::spill / spill_ranges) rather than as cell contents. Opening with recalculation is unchanged: recalc_all clears the spill state and lays it out again from the formulas. A range keeps them only when it starts at its formula, holds at most 1 million cells (what the writer caches for one range; the constant is now shared) and fits in 4 million cells per workbook. Otherwise they are dropped as before and the workbook is marked full_calc_on_load, so a manual-mode workbook opens calculated and saving asks the next reader to calculate.
  • Engine: DocState::with_recalc(wb, path, title, recalculate); with false the dependency graph is still built.

After opening with stored results

  • In automatic mode an edit recalculates the formulas that depend on it and, as after any edit, the formulas GridCraft always recalculates and their dependents: volatile functions (TODAY(), NOW(), RAND()…), INDIRECT, OFFSET, and formulas that use names (defined names, LET/LAMBDA variables), table references or unknown functions. The first edit can change those from their stored results even when it is unrelated to them.
  • In manual mode an edit calculates the formulas it enters (entering an array formula can recalculate more). Calculate Now (F9) calculates everything.

Limitations / follow-ups

  • Manual calculation mode is honoured on open only: structural edits (inserting or deleting rows, columns, cells or sheets, sorting, filtering, grouping, hiding rows or columns, row heights) recalculate the whole workbook whatever the mode (existing behaviour, unchanged here).
  • GridCraft's JSON format doesn't store spilled values, so a manual-mode JSON workbook opens with empty spill areas until calculated.
  • Entry points that can't pass recalculate yet: the desktop/web Open dialog and recent files, the password prompt that reopens an encrypted workbook, the control channel's app.open, and MCP open_workbook (execute_command with file.open reaches it).
  • Circular references are reported once a formula on the loop is calculated (a new window carries over those already found).

Tests

  • Checks: the cargo xtask ci steps, run one by one: fmt, clippy (--workspace --all-targets -D warnings, allowing only collapsible_match and nonminimal_bool, which the local Rust 1.95 clippy reports in untouched gridcraft-xlsx code), cargo test --workspace (1614 passed), assets, layers, wasm, parity, keytips.
  • engine: open_without_recalculating_shows_stored_results, manual_calculation_workbooks_open_with_their_stored_results, out_of_date_results_stay_marked_when_saved, edits_recalculate_dependents_after_opening_with_stored_results, array_results_show_when_opening_with_stored_results, a_new_window_shows_the_same_results, opening_an_open_file_again_ignores_recalculate_visibly
  • xlsx: full_calc_on_load_is_read_and_kept, array_ranges_keep_cached_results_only_within_bounds (hostile ranges, per-range and per-workbook caps), uncalced_marks_the_results_out_of_date, array_results_past_the_cached_range_size_are_dropped_and_marked; updated fixtures, array_export, roundtrip and the XLS reads_values_formulas_formats_and_layout
  • calc: full_recalculation_clears_full_calc_on_load; model: calc_settings_without_full_calc_on_load

Related

🤖 Generated with Claude Code

… manual calc mode)

GridCraft calculated every formula again whenever a workbook opened. A
workbook that has to read exactly as it was saved (an approval form, a
signed report) then showed the day it was viewed in TODAY(), and any
difference between calculation engines as changed numbers. A workbook
saved in manual calculation mode was calculated on open all the same.

file.open takes {recalculate?: bool}. false opens the workbook with the
results stored in the file, array results included; true always
calculates. Without it, formulas are calculated again as before, unless
the workbook was saved in manual calculation mode and doesn't ask to be
calculated on load: an .xlsx with fullCalcOnLoad, an .xls with an
Uncalced record, or a workbook whose cached array results couldn't be
kept does ask. The result says whether it `recalculated`; a non-boolean
`recalculate` is refused before the file is read. A path that is
already open is switched to as it is, and the result says
`recalculateIgnored` when `recalculate` was given. A formula stored
without a result opens blank until it is calculated.

Behaviour change: a manual-mode .xlsx or .xls that doesn't ask to be
calculated on load now opens with its stored results by default, in
every entry point that opens through file.open without `recalculate`:
the desktop and web UI, the CLI (convert, info, cat, eval --in, run --in,
mcp --in), MCP open_workbook and the control channel's app.open.
Automatic-mode workbooks are unchanged.

After opening with stored results the dependency graph is built anyway.
In automatic mode an edit recalculates the formulas that depend on it
and, as after any edit, the formulas GridCraft always recalculates with
their dependents: volatile functions, INDIRECT, OFFSET, and formulas
using names (defined names, LET/LAMBDA variables), table references or
unknown functions. The first edit can therefore change those from their
stored results even when it is unrelated to them. In manual mode an edit
calculates the formulas it enters; Calculate Now (F9) calculates
everything. Manual mode is honoured on open only: structural edits
(inserting or deleting rows, columns, cells or sheets, sorting,
filtering, grouping, hiding, row heights) still recalculate the whole
workbook whatever the mode.

CalcSettings::full_calc_on_load records that the stored results are out
of date or incomplete. XLSX reads calcPr fullCalcOnLoad and writes it
back while it is set (as well as, as before, when a formula has no
cached result); XLS maps Uncalced (0x005E) to it; a full recalculation
clears it.

To show array results without calculating, the XLSX and XLS readers keep
the values cached in an array formula's range as the values it spilled
(Sheet::spill / spill_ranges) instead of dropping them (storytold#156). A
recalculation still clears them and lays the range out again from the
formula, so opening with recalculation is unchanged. A range keeps them
only when it starts at its formula, holds at most 1 million cells (what
the writer caches for one range; MAX_CACHED_RANGE_CELLS is shared) and
fits in 4 million cells per workbook, since a file names a range for
free while later edits and saves walk it. Otherwise they are dropped as
before and the workbook is marked full_calc_on_load, so a manual-mode
workbook opens calculated and a save asks the next reader to calculate.

view.newWindow no longer calculates its copy: a second window on the
same workbook shows the same results and the circular references found
so far.

GridCraft's JSON format doesn't store spilled values, so a manual-mode
JSON workbook opens with empty spill areas until it is calculated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReviewerA — source-read COMMENT (not an approval)

I read the head of the diff: calculation opens without recalculating (file.open {recalculate: false}) and honours manual calculation mode. The workbook shows the results stored in the file (a TODAY() keeps its saved value), and the doc comment of the recalculation changes accordingly. Excel opens in manual calculation mode without recalculating and shows saved values, so the oracle matches; a read-only viewer benefits. The safety point is to make sure the user knows values may be stale (Excel shows "Calculate" in the status bar), and that a saved value of a volatile function is not presented as current. It is related to gridcraft#2308 (calculate only missing results), and the two should be consistent. I have not run it, and no exact-head CI result exists for this head. Nothing was run.

I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.

Status: waiting_author.

@Pavel199760

Copy link
Copy Markdown

I independently implemented a minimal version of this while digging into a 3.8 M-formula workbook (details in #4462): Trust::FromFile opens go through the existing DocState::from_saved() so the file's cached values show immediately — branch open/trust-cached-values, PR #4568. Your design (per-file fullCalcOnLoad honouring, a recalculate: parameter) is the better product answer — happy to close mine in favor of yours, or fold the two together.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants