How to design a spreadsheet (frontend)
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- A grid of a million cells
- Formulas that depend on each other
- Editing, pasting and undoing
- Columns that hold different kinds of data
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Discord
- Clay
The problem
A web spreadsheet shows a grid far larger than the screen, where cells feed formulas in other cells and where one paste or one undo may touch thousands of cells at once. The answer turns on touching just what matters: draw the cells in view and a little around them, recalculate the formulas downstream of an edit rather than the whole grid, and store each edit as a small reversible record rather than a copy of the whole grid. The rest of the round is showing that each of those three decisions holds up at a million cells, 100,000 formulas and 100 undo steps.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How large is a large grid, and how much of it is formulas? | A grid of a few thousand cells can be drawn in full and recalculated in full. A million cells with 100,000 formulas rules both out, and that is where the design decisions start. | Plan for a million cells, 100,000 of them formulas. Most of a grid that size is empty, which matters for how you store it. |
What do formulas need to support: arithmetic between cells, or range functions like a sum over a column as well? | Ranges change the dependency structure. A total over a tall column reads thousands of cells, and a naive map adds an edge for each of them. | Both. Cell references, arithmetic, and functions over ranges such as |
One person editing, or several at the same time? | Several editors at once brings conflict resolution, other people's cursors and undo of changes another person has since touched. That is a different design. | One person for this round. Be ready for the follow-up about two people at once, but don't design for it up front. |
Which kinds of data do columns hold, and does the column decide the type or the cell? | A type per column lets you parse input once at the column's type and give the column its own editor. A type per cell pushes that work to each cell. | Text, numbers, dates and true or false, set per column. The server exposes the column types and lets you change one with a |
Does paste come from other programs, such as Excel, or from within the grid? | Paste from outside arrives as text that has to be parsed by column type and may not fit. Paste from inside carries formulas whose references need shifting. | Both. Assume a paste of a few thousand cells copied from another spreadsheet, and that a person may then press undo. |
Does the whole grid download when it opens, or can the client ask for a region? | A million cells in one response is a long wait before anything shows. A region endpoint lets the grid load what the scroll position needs. | The server answers for a region of rows and columns, so the client can load what is near the viewport and ask for more as it scrolls. |
Who uses it, and with what? | Keyboard and screen reader support decide how cells are drawn, and that decision is hard to reverse later. | Mouse and keyboard, and it has to work with a screen reader. Desktop browsers first. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Scrolling stays smooth across a sheet of a million cells | The number of elements on the page has to stay tied to the screen size, not the grid size: a viewport window computed from the scroll position, positions from cached cumulative sizes, and a scroll handler that does no layout queries. |
A keystroke shows within 200 milliseconds | In this design, typing goes straight into the cell editor and paints before recalculation starts. Long formula work has to leave the main thread, or the typed letter waits behind it. This is the Interaction to Next Paint threshold web.dev calls good, so it is measurable on real visits. |
A formula result updates within 100 milliseconds of an edit | After one edit the engine can afford to run the formulas downstream of that cell, not all 100,000. That needs a reverse map, cell to the formulas reading it, a dirty set that holds each formula once, and a dependency order so a formula runs after its inputs. |
Usable with a keyboard and a screen reader | Arrow keys move the focused cell, one tab stop for the whole grid, and the grid tells assistive technology its true row and column count even when most rows are not on the page. This weighs against painting cells on a canvas with no accessible counterpart. |
Core entities
| Entity | What it holds |
|---|---|
Cell | One addressed cell: what the person typed, what it parsed to, and the formula if there is one. |
Client grid state | Everything the browser holds about the open spreadsheet. |
Dependency map | Which cells each formula reads, and the reverse: who reads each cell or range. |
Viewport window | The rows and columns currently on screen, plus a buffer, derived from the scroll position. |
Selection | The anchor cell and the focus cell; the rectangle between them is the selected range. |
Edit | One recorded change with its inverse, the unit of undo and redo. |
The API
| Endpoint | Notes |
|---|---|
| The opening call. The counts size the scrollbars and the accessible row and column counts before any cell arrives; the column types drive parsing and the per-column editors; the version is what later edits are based on. |
| Cell data by region. The client asks for the blocks around the viewport and marks them loaded, so a fast scroll to the bottom right fetches the blocks there instead of everything in between. State the assumption that the response lists occupied cells and skips empty ones, which keeps the sparse store sparse. |
| A batch of recorded edits against the version the client last saw. In this design the client applies edits locally first and sends in the background, and we'd give each edit on the wire the same change-with-inverse shape as the undo record so one type drives both. The response's meaning on a version that has moved on is not given; we'd treat it as a cue to re-fetch and reconcile. |
| Changes one column's type. The client then re-parses that column once from each cell's raw text and marks the cells whose text no longer fits. |
The high-level design
Step 1 of 4
Store, editor and a grid that draws everything
Start with the shortest path that works: the server returns the cells, a store keyed by address holds them, an editor writes typed text into the store, and a renderer turns the store into cells on the page. It is honest about the data flow, and it already shows the problem the rest of the design answers: the renderer creates an element per cell, so a million cells means a million elements before the first scroll.
What breaks first
The deep dives
A grid of a million cells. Someone opens a spreadsheet of a million cells and drags the scrollbar straight to the bottom right corner. What does the page draw along the way, and what does it draw when the thumb stops?
Formulas that depend on each other. Someone changes one input that thousands of formulas read, some of them through other formulas, and a new formula they just typed points back at itself. What runs again, in what order, and what does the self-referencing cell show?
A concept that helps here: Dependency graphs and incremental recalculation
A concept that helps here: Scheduling work on the main thread
A concept that helps here: Batching UI updatesEditing, pasting and undoing. Someone pastes a block of a few thousand cells copied from another spreadsheet, then presses undo three times. What moves where, how many times do formulas recalculate, and what does each undo restore?
A concept that helps here: Sanitizing pasted HTML
Columns that hold different kinds of data. One column holds numbers, another dates, another true or false, and someone pastes text from another program into all three, some of it in the wrong shape. What does each cell keep, what does it show, and what do formulas that read it get?
A concept that helps here: Sorting and formatting for the reader's language
A concept that helps here: Where state livesConcepts this guide uses
- List virtualization
- Canvas or the DOM
- Accessible widgets
- Dependency graphs and incremental recalculation
- Scheduling work on the main thread
- Batching UI updates
- Undo and redo
- Sanitizing pasted HTML
- Form validation
- Sorting and formatting for the reader's language
- Where state lives
Sources
- Handsontable: Row virtualization
- AG Grid: DOM virtualization of rows and columns
- techinterview.org: Build a spreadsheet, cell editor, formulas and large grids
- HyperFormula: Dependency graph
- Microsoft Learn: Excel recalculation
- Microsoft Support: Excel specifications and limits
- MDN: Using Web Workers
- web.dev: Web Vitals
- W3C ARIA Authoring Practices Guide: Grid pattern
- W3C ARIA Authoring Practices Guide: Grid and table properties
- Google Workspace Updates: Google Docs will now use canvas based rendering
- WebAIM: Google announces seismic change to Docs