Server-side Spreadsheet

Update spreadsheet state and run formula evaluation on the backend

Use Spreadsheet from libs/spreadsheet-state/interface/spreadsheet-interface.ts when you want to apply spreadsheet operations on the server (API routes, workers, job processors), then evaluate formulas before persisting or broadcasting changes.

Import

Use the server entrypoint:

import { Spreadsheet } from "@rowsncolumns/spreadsheet-state/server";

1) Initialize state

import type { CellData, Sheet, TableView } from "@rowsncolumns/spreadsheet";
import type { SheetData } from "@rowsncolumns/spreadsheet-state";
import { Spreadsheet } from "@rowsncolumns/spreadsheet-state/server";

const spreadsheet = new Spreadsheet();

const sheets: Sheet[] = [
  { sheetId: 1, rowCount: 200, columnCount: 26, title: "Sheet1" },
];
const sheetData: SheetData<CellData> = { 1: [] };
const tables: TableView[] = [];

spreadsheet.sheets = sheets;
spreadsheet.sheetData = sheetData;
spreadsheet.tables = tables;

2) Apply updates

Use changeBatch for cell values/formulas (single or multi-range):

You can also use structural APIs like insertRow, insertColumn, deleteRow, deleteColumn, changeFormatting, updateTable, etc. on the same Spreadsheet instance.

3) Run evaluation

After backend edits, explicitly flush calculations:

Useful variants:

  • await spreadsheet.calculatePending(): Runs only queued operations and returns calculated result entries.

  • await spreadsheet.flushCalculations(): Runs queued operations but does not return result entries.

  • await spreadsheet.calculateAll(): Scans current sheetData formulas and evaluates them.

If you replace sheetData directly (outside changeBatch), call calculateAll() for a full formula pass, or call rebuildGraph() and enqueue explicit operations before calculatePending().

4) Read updated values

calculatePending/flushCalculations/calculateAll apply results back into spreadsheet.sheetData via the internal calculation pipeline.

5) Persist or broadcast patches

Every mutation pushes history patches that you can forward to persistence/collab layers.

For backend runtimes, provide a calculation worker explicitly:

Last updated