spreadsheet-ops — the spreadsheet as a programmable surface
A spreadsheet is a runtime, not a document. Your job is to emit a checkable artifact — a .xlsx
file that opens, a script that compiles, a cell range you can re-read — never a hand-waved formula in
prose. Pick the tool that matches the runtime (local file vs cloud sheet, one-shot vs event-driven),
write the formula with comma arguments, and verify before you claim it works.
The trap that bites everyone first: openpyxl writes formulas but does not compute them. The string
is stored; Excel evaluates it on open. If you read the cell back with Python it is None until a real
Excel/LibreOffice session has saved it. Internalize that before you touch a workbook.
Pick the tool first (this branches, so decide before coding)
| Your situation | Reach for | Why |
| --- | --- | --- |
| Read and modify an existing local .xlsx | openpyxl | Only mainstream lib that round-trips an existing file. |
| Write a new .xlsx rich with charts/conditional formats, possibly large | xlsxwriter (via pandas ExcelWriter) | Write-only but the richest formatting + low-memory constant_memory mode. |
| Dump a DataFrame to a sheet, minimal fuss | pandas df.to_excel(...) | Wraps an engine; pass engine= explicitly so the result does not depend on what is installed. |
| Read/write a cloud Google Sheet from a script | gspread (+ gspread-formatting for colors/rules) | Friendly wrapper over the Sheets API for CRUD. |
| High-volume cloud writes, must beat quotas | Sheets API v4 batchUpdate | One batched call counts as one request — survives the per-minute limit. |
| In-sheet menus, time-driven triggers, custom functions | Apps Script (V8) | Runs inside the sheet; the only place for onEdit/menus. |
Two you will confuse: xlsxwriter cannot open an existing file — if you need to edit one in place,
that is openpyxl. And gspread vs the raw Sheets API is convenience vs throughput: reach for raw
batchUpdate only when gspread's per-call writes would blow the quota.
Versions seen on PyPI when this was written (2026-06-02): openpyxl 3.1.5, gspread 6.2.1
(Python 3.8+), gspread-formatting 1.2.1, pandas on the 3.x line, Sheets API v4, Apps
Script on the V8 runtime. Treat these as a floor, re-check PyPI before pinning, and pin whatever
you actually resolve — do not copy a point release from a doc as gospel.
Formula rules (each rule, one reason)
- Arguments are comma-separated, always — never semicolons. openpyxl and the file format use
commas regardless of your machine's locale; a semicolon is the single most common "my written
formula is broken" cause. Bad: =SUM(A1;A2) → Good: =SUM(A1,A2).
- A formula string must start with
=.ws["A1"] = "SUM(1,1)"stores literal text; `ws["A1"] =
"=SUM(1,1)" stores a formula. No leading =` means no calculation, ever.
- Anchor references you intend to fill. A lookup table dragged down breaks unless absolute: Bad:
=VLOOKUP(A2,Sheet2!A:B,2,0) → Good: =XLOOKUP(A2,Sheet2!$A:$A,Sheet2!$B:$B).
- Prefer XLOOKUP over VLOOKUP — exact match by default, no magic column index to miscount, works
leftward. VLOOKUP's 4th arg silently defaulting to approximate match is a classic wrong-result bug.
- Prefer dynamic arrays over Ctrl+Shift+Enter array formulas —
=FILTER(...),=SORT(...),
=UNIQUE(...) spill automatically and are readable. A spill blocked by data below it raises
#SPILL!; clear the range, do not array-enter.
- Never deep-nest IFs. Past two levels use
LET(name sub-expressions once) or a helper column.
Bad: =IF(A1>90,"A",IF(A1>80,"B",IF(A1>70,"C","D"))) → Good: a lookup table + XLOOKUP, or
=LET(g,A1, IF(g>90,"A", IF(g>80,"B","C"))) kept shallow.
When generating cross-sheet formulas, SHEET() returns a sheet's index and SHEETS() returns the
sheet count — both have shipped in Excel for years (SHEET/SHEETS since Excel 2013) and exist in Google
Sheets, so they are safe to emit. Full XLOOKUP/SUMIFS/LET patterns and the #VALUE!/#REF!/#N/A/#NAME?