Skip to content
James Ross
articlePublished Updated 8 min read

Replacing a spreadsheet process with a typed exception engine

By James Ross

A monthly tracker built by hand is a process with no types. The NetSuite export, the pivot tabs, the twelve customer worksheets and the emailed summaries all agree only because a person checks them. Replacing that with software means making every assumption explicit: which month, which tab, which manufacturer row, and what to do when a name does not match.

A tangled spreadsheet grid collapsing into a typed rules engine that sorts exceptions, showing NetSuite tracker automation.

That is the same contract-first approach used in Designing a multi-brand commerce control plane, applied to a workbook instead of a commerce API.

What the manual process actually was

The process started with a NetSuite report and ended with twelve customer files in someone’s inbox.

A vice president of business development exported “Sales by Retailer vs Prior Period” from NetSuite, flattened it into pivot data, built a pivot summary, then hand-keyed the current month’s actuals into a cumulative tracker workbook. The tracker had twelve tabs: a company roll-up plus eleven customer tabs. Each tab stacked five blocks, one per year and forecast type, and only the current year’s actual month cells were writable. At the end, each customer tab was copy-pasted into its own file and emailed with a short variance note.

The work was repetitive, but it was not simple. The tracker’s formulas cross-reference blocks by absolute row, some customer names differ between NetSuite and the workbook, and the source report’s Division column turned out to be an external-workbook lookup rather than a reliable value. A replacement had to preserve all of that, not tidy it up.

Why a general spreadsheet library cannot write this workbook

The obvious design is to open the tracker with a library, set some cells, and save. That design was rejected after the first round-trip test.

A real tracker is not a clean table. It carries external workbook links that some prior-year formulas depend on, legacy comments stored as VML drawings, theme-tint fills, merged cells, printer settings, and pre-existing broken #REF! formulas that must survive untouched. A general writer reconstructs the file from its object model, and anything the object model does not represent is dropped. In this case, the library’s writer dropped external links and VML comments, which changed the workbook in ways that are invisible until someone opens it in Excel.

The workbook also forbids row insertion. Its year-to-date formulas use SUM ranges and absolute references to sibling blocks; inserting a manufacturer row shifts every block below it and silently changes which rows the formulas cover. New manufacturers are a manual, one-time edit in Excel, followed by a re-run of the engine.

So the rule became: the library reads, the package patcher writes. ExcelJS is used for layout scanning and value extraction, never for output.

The surgical write

The writer treats the XLSX as what it is: a zip of XML parts. It reads the parts with fflate, finds the one <c> element for each planned cell in the target sheet XML, replaces its <v> value, copies every other part byte-for-byte, and writes a new zip.

That gives a useful property: corruption risk is structurally confined to the cells the plan named. If an edit does not match exactly one cell element, the whole run aborts with no output. It is all-or-nothing by design.

The second write is formula widening. A year-to-date SUM covers January through the current month, so each month the end column has to move one to the right:

<!-- before: the YTD SUM ends at June (column G) -->
<f>SUM(B50:G50)</f>

<!-- after: widen by one month to July (column H) -->
<f>SUM(B50:H50)</f>

The widening is idempotent (a second run is a no-op), never shrinks a range, and skips with a warning when a formula does not match the expected shape. The 2026 tracker’s formulas are not uniform, so a regex that assumes one shape would corrupt the ones that differ.

Two package edits make Excel recalculate on open. The writer deletes xl/calcChain.xml and its content-type and relationship entries, then sets fullCalcOnLoad on the workbook’s calculation properties. The calculation chain is only a hint about last-calculated order; leaving a stale one after changing formulas is how a file opens with old totals.

Typed exceptions instead of silent skips

The engine’s real value is not the cell writing. It is that every ambiguous input becomes a named exception with a decision attached.

Exception Detected when What the engine does
Month mismatch The month in the report header disagrees with the month cell in the tracker Hard error, no output
Reconciliation mismatch Detail rows do not sum to the report’s total row Hard error, shows both totals
Unresolved customer A source customer has no configured tab Blocks confirm, returns the list
Unresolved manufacturer A source manufacturer has no existing row label Warns with the amount, never appends a row
Ambiguous alias Two records normalise to the same target Hard error before any write
Layout drift A block title is missing, or a blank label appears before the total row Hard error
Workbook role mismatch The two uploaded files are swapped Corrects the unambiguous reversal, rejects the rest
Nonstandard formula A YTD formula does not match the expected shape Skips it with a warning, leaves it untouched

Two of these deserve emphasis. The engine never inserts a manufacturer row, because that would break the absolute cross-block references described above. And it never guesses which of two source names is the same business; it presents fuzzy suggestions, but a person must confirm the identity.

The resolution screen exists because “unmatched” is not the same as “ignore”. Mapping a customer to a tab, adding a spelling alias inside a long-format tab, and excluding a customer are three different decisions with three different effects. The engine persists each one, keeps a .bak of the mapping file, and rejects a save that would put the same source in two incompatible categories.

Fuzzy name matching that never guesses

Name matching is the part most likely to be automated too far.

The normaliser trims whitespace, collapses repeated spaces, lowercases, strips punctuation, treats & and and as equivalent, removes (USA) and (US), and drops a conservative set of company suffixes. Then it looks up an explicit alias map. Matching is directional: the source is the exact name from the NetSuite export, and the target is an existing tracker label. A customer route and a customer spelling alias are separate fields because they do different jobs.

Fuzzy suggestions rank candidates for a human. They never auto-apply. The failure mode is obvious once stated: two genuinely different manufacturers with similar names merged into one row would aggregate their amounts, and the resulting total would look plausible.

The engine also refuses ambiguity. If two rows in one block canonicalise to the same label, that is a hard error before any write. A duplicate is a data problem that has to be resolved in the workbook, not a tie the software should break.

Two-phase commit and the master tracker

The engine keeps a master tracker on a persistent volume and replaces it only after a run succeeds.

Each run works inside its own upload directory. Parsing, planning, writing and output generation all happen there. Only when every requested output exists does the engine back up the old master, then atomically rename the new one into place. A failed run never touches the master, and the previous version stays restorable.

That matters because the tracker is the cumulative record. A partial write is worse than no write: it would leave the master with one month’s cells updated and the formulas only partly widened. The two-phase commit is what makes a retry safe.

The same storage module owns every path under the data directory. Uploads, masters, backups and logs all go through it, so a path cannot be constructed ad hoc somewhere else and escape the volume.

Testing a spreadsheet writer

You cannot review a binary diff by eye, so the project built its own comparison tools.

The first gate is structural. A zero-edit rewrite of the real tracker must leave every untouched zip part byte-identical, with the only allowed differences being the removed calculation chain and the added recalculation flag. If that fails, nothing downstream matters.

The second gate is a reference replay. Take the real tracker, blank the current month, narrow the year-to-date formulas back to the prior month, then run the engine with the real export and compare the result to the file a person produced by hand. Differences inside the writable cells are reported for review; differences anywhere else are hard failures.

The third is a synthetic round trip: build a tiny tracker and export with no financial data, run the update, and assert the cell values, formulas, number formats, fills and merges with the project’s own XLSX diff. The suite runs on Node’s built-in test runner (Node.js test runner), and Zod validates the configuration and API payloads (Zod).

The real workbooks contain company financials, so they are excluded from the repository. The real-workbook gates skip automatically when those fixtures are absent, which keeps the synthetic suite runnable everywhere.

What broke and what we changed

The library writer changed the workbook. External links and VML comments were dropped on save. The fix was to stop writing with the library entirely and patch the package instead.

Rows could not be inserted. The year-to-date formulas reference sibling blocks by absolute row. The engine now warns on an unmatched manufacturer and asks for a one-time manual row add, then a re-run.

The source report and the pivot tab disagreed. The report’s detail rows and its pivot grand total did not reconcile. The engine treats the reconciled detail as the source of truth and surfaces the discrepancy as a warning; it does not silently pick the larger or the more recent number. That decision belongs to the business owner, and the application says so.

A customer’s route and its spelling are different facts. A route selects the tab; a spelling alias selects the label inside a long-format tab. They are separate fields, and neither substitutes for the other.

Swapped uploads produce confusing errors. Putting the tracker in the report field and the report in the tracker field produces parse errors that point at the wrong file. The engine verifies workbook roles from their worksheet structure before processing, and corrects an unambiguous swap with a warning.

The calculation chain can go stale. A chain left over from the previous month can make Excel show old totals after formulas change. The writer deletes the chain and requests a full recalculation on load.

Frequently asked questions

Why not just use a library like ExcelJS to write the workbook?

Because a general writer rebuilds the file from its object model, and this workbook depends on parts that model does not fully represent: external links, VML comments, theme fills and printer settings. The project uses ExcelJS to read and a small OpenXML patcher to write, so untouched parts stay byte-identical.

Is it safe to edit an XLSX file as a zip?

It is safe when the edit is narrow and verified. An XLSX is an Open Packaging Conventions zip of XML parts, so replacing one cell's value and copying every other part unchanged is a smaller operation than a full rewrite. The risk is in matching the wrong cell, which is why every edit must match exactly one element or the run aborts.

Why can't the engine add a new manufacturer row automatically?

Because the tracker's year-to-date formulas reference sibling blocks by absolute row. Inserting a row shifts those references and changes which rows the formulas cover, usually without any visible error. A person adds the row once in Excel and re-runs the update.

How does the name matching work?

It normalises case, spacing, punctuation, & versus and, country suffixes and a short list of company suffixes, then looks up an explicit alias map. Fuzzy suggestions are shown for a human to confirm. It never auto-merges two names, because merging different manufacturers would silently combine their amounts.

What happens if a customer is missing from the mapping?

The upload is blocked and the unresolved names are returned with their amounts and suggestions. Nothing is written until every blocker is mapped, aliased or explicitly ignored. That is deliberate: a silent skip is indistinguishable from a zero in the finished report.

How do you test something that writes binary files?

With three layers: a structural package diff that asserts untouched parts are identical, a reference replay against a real workbook a person produced, and synthetic round-trip tests with no financial data. The suite ran 145 tests (from the project's build records).

Can this work for other tracker types?

Yes, if the tracker definition is configuration rather than code. The engine is driven by a config file that names the writable block, the month cell, the total row labels and the tabs, plus a mapping file for routes and aliases. A second tracker type needs a new config and mapping, not a new processing engine.