engine¶
The workbook model: a Grid of Sheets, each a sparse dict[(col, row)] -> Cell, plus the array type ranges evaluate to, the reference parsing and adjustment rules, and JSON load/save.
engine
¶
Mode
¶
Bases: IntEnum
Grid
¶
Source code in src/gridcalc/engine.py
add_sheet
¶
add_sheet(name: str) -> Sheet
Append a new sheet. Returns the sheet.
NOTE (phase 1): the dep graph keys are still (c, r) tuples
and do not carry sheet identity. Until phase 2 (sheet-qualified
references) lands, formulas on different sheets that touch the
same (c, r) collide in the dep graph. Treat multi-sheet
workbooks as preview-only until then.
Source code in src/gridcalc/engine.py
move_sheet
¶
Reorder name to new_idx (zero-based).
Active-sheet identity is preserved: if the active sheet is the one being moved, it follows; if some other sheet is active, its index is recomputed so the same sheet stays active.
Dep graph keys carry sheet names rather than indices, so reordering doesn't invalidate the graph -- no rebuild needed.
Source code in src/gridcalc/engine.py
rename_sheet
¶
Rename a sheet and rewrite formula text that references the old name.
Walks every formula cell on every sheet and rewrites any
<old>! sheet prefix to <new>!. Skips matches inside
double-quoted string literals so a user formula like
="Other!A1" is left untouched. Invalidates the cached AST
on each rewritten cell so the next recalc re-parses with the
new sheet name.
Caller is responsible for rebuilding the dep graph and
triggering a recalc; cmd_sheet does both.
Source code in src/gridcalc/engine.py
next_sheet
¶
Advance the active sheet by one, wrapping at the end. No-op on a single-sheet workbook.
prev_sheet
¶
Retreat the active sheet by one, wrapping at the start. No-op on a single-sheet workbook.
load_lib
¶
Load a formula lib's builtins into the eval namespace.
load_requires
¶
Load required modules into the eval namespace.
allow_unknown passes through to load_modules: unclassified
modules are refused unless the caller has explicitly approved them.
Source code in src/gridcalc/engine.py
clear_all
¶
setcells_bulk
¶
Set many cells, deferring recalc until all are written.
Each tuple is (col, row, text). Out-of-bounds entries are ignored. Roughly N x faster than calling setcell() N times because recalc() runs once instead of after every cell.
Source code in src/gridcalc/engine.py
can_insert
¶
True when inserting count lines at at would lose no data.
The sheet is a fixed NROW x NCOL grid, so an insert near the end pushes
the last lines past the edge. Silently dropping them is data loss the
user never asked for and cannot see, so callers check first and refuse.
axis is "R" for rows or "C" for columns.
Source code in src/gridcalc/engine.py
insertrow
¶
Insert one row at at. False, without mutating, if that would push
a populated row off the bottom of the sheet.
Source code in src/gridcalc/engine.py
insertcol
¶
Insert one column at at. False, without mutating, if that would
push a populated column off the right edge of the sheet.
Source code in src/gridcalc/engine.py
csvsave
¶
Export evaluated cell values to CSV.
Source code in src/gridcalc/engine.py
csvload
¶
Import cells from a CSV file. Numbers become NUM cells, rest become LABELs.
Source code in src/gridcalc/engine.py
pdload
¶
Load a file into grid cells using pandas for type inference.
Supports CSV, TSV, Excel (.xlsx/.xls), JSON, and Parquet. Column headers become labels in row 0 when header=True.
Source code in src/gridcalc/engine.py
pdsave
¶
Export grid cells to a file using pandas.
Supports CSV, TSV, Excel (.xlsx), JSON, and Parquet. Row 0 is used as column headers.
Source code in src/gridcalc/engine.py
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 | |
Sheet
¶
A single named sheet's cell store, cycle set, and cursor.
Workbook-level state (mode, code, named ranges, dep graph, etc.)
lives on Grid; each Sheet only owns the data that varies
per-tab. Grid exposes _cells / cells / cc / cr /
_circular as properties that delegate to sheets[active] so
existing single-sheet code keeps working unchanged.
Source code in src/gridcalc/engine.py
Cell
¶
Source code in src/gridcalc/engine.py
Vec
¶
Source code in src/gridcalc/engine.py
at
¶
1-based 2D access. Treats a 1D Vec as a column vector (n×1)
so at(i, 1) walks the flat data.
Source code in src/gridcalc/engine.py
row
¶
row(i: int) -> Vec
1-based row extraction. Returns a 1D Vec.
A 1D Vec is treated as a column vector (n×1), so row(i)
returns a 1-element Vec for valid i.
Source code in src/gridcalc/engine.py
col
¶
col(j: int) -> Vec
1-based column extraction. Returns a 1D Vec.
A 1D Vec is treated as a column vector (n×1), so col(1)
returns the whole vec; other indices raise.
Source code in src/gridcalc/engine.py
iter_rows
¶
Iterate rows as plain lists. A 1D Vec is treated as
column-shaped (n×1), so each element yields its own 1-element row.
Source code in src/gridcalc/engine.py
NamedRange
¶
NamedRange(
name: str = "",
c1: int = 0,
r1: int = 0,
c2: int = 0,
r2: int = 0,
sheet: str | None = None,
)
Source code in src/gridcalc/engine.py
ref
¶
Parse a cell reference. Returns (chars_consumed, col, row) or None.
refabs
¶
Parse a cell reference at the start of s.
Returns a RefMatch (still tuple-unpackable as
n, col, row, abs_col, abs_row), or None if no ref matches.
Source code in src/gridcalc/engine.py
col_name
¶
cellname
¶
adjust_refs
¶
Shift every relative cell reference in text by (dcol, drow).
Absolute ($-prefixed) columns/rows are left unchanged. Used both by
replicate (copy a formula across the grid) and by the frontends' paste, so
the two share one definition of reference adjustment.