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. Named ranges bound to old
follow it. The dep graph is marked stale; the caller recalcs.
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 CSV, TSV or JSON file into grid cells using pandas.
CSV and TSV fields are parsed as typed input, as csvload does. JSON
values keep their type: a string stays a label, a boolean becomes
=TRUE/=FALSE. header writes JSON column names into row 0.
Returns -1 if pandas is not installed or the file type is unsupported.
Source code in src/gridcalc/engine.py
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 | |
pdsave
¶
Export grid values to a CSV, TSV or JSON file, with row 0 as headers.
Returns -1 if pandas is not installed, the file type is unsupported, or JSON headers repeat.
Source code in src/gridcalc/engine.py
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 | |
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.