Formula evaluation

Formula evaluation in Spreadsheet 2 with support for client-side calculation, Excel/Google Sheets compatibility, and custom functions.

Spreadsheet 2 provides a powerful formula evaluation engine with Excel/Google Sheets compatibility. Formula calculations can run on the main thread or in a Web Worker for better performance.

Calculation Modes

The useSpreadsheetState hook supports two calculation modes:

1. Main Thread Calculation (Default)

Formulas are evaluated synchronously on the main UI thread. Best for smaller workbooks or when you need immediate results.

import { useSpreadsheetState } from '@rowsncolumns/spreadsheet-state';

const MySpreadsheet = () => {
  const state = useSpreadsheetState({
    calculationMode: 'single', // Default
    initialSheets: [
      {
        id: 1,
        name: 'Sheet1',
        cells: [
          { rowIndex: 1, columnIndex: 1, formattedValue: '5' },
          { rowIndex: 1, columnIndex: 2, userEnteredValue: '=A1*2' }
        ]
      }
    ]
  });

  return (
    <CanvasGrid
      {...state}
      rowCount={1000}
      columnCount={26}
    />
  );
};

Pros:

  • Simpler setup (no Web Worker needed)

  • Immediate results (no async overhead)

  • Easier debugging

Cons:

  • Can block UI for complex formulas

  • Not suitable for workbooks with thousands of formulas

2. Web Worker Calculation

Formulas are evaluated asynchronously in a Web Worker thread. Best for larger workbooks or when you need non-blocking calculation.

Creating the Worker File:

Pros:

  • Non-blocking UI during calculations

  • Handles large workbooks smoothly

  • Can leverage multiple CPU cores

Cons:

  • Requires Web Worker setup

  • Async results (slight delay)

  • More complex debugging

Excel/Google Sheets Compatibility

Supported Features

We try to support full formula compatibility. If we have missed anything, let us know by email us at [email protected]

Formula Errors

All standard Excel error types are supported:

Error
Description
Example

#REF!

Invalid reference

=A1 (in cell A1)

#NAME!

Unknown function

=UNKNOWNFUNC()

#VALUE!

Wrong value type

=1 + "text"

#DIV/0!

Division by zero

=1/0

#N/A

Value not available

=VLOOKUP("missing", A:B, 2)

#NUM!

Invalid numeric value

=SQRT(-1)

#SPILL!

Array blocked by data

=SEQUENCE(10) when cells occupied

#NULL!

Invalid range intersection

=A1:A10 B1:B10

Array Formulas

Dynamic array formulas that spill across multiple cells (Excel 365 style):

Spill Collision Handling:

  • If spill range overlaps user data, formula returns #SPILL! error

  • Editing a spill cell clears the array and allows user input

  • Deleting a spill cell restores the array formula

Circular References

Enable iterative calculation to resolve circular references (like Excel):

How it works:

  1. Formulas are evaluated repeatedly until convergence

  2. Stops when change between iterations < maxChange

  3. Returns #NUM! if doesn't converge after maxIterations

Named Ranges

Define reusable range names (Excel-compatible):

Features:

  • Case-insensitive (SalesTotal = salestotal)

  • Can reference ranges or single values

  • Cross-sheet support

  • Auto-updates when range changes

Structured References (Excel Tables)

Excel-style table references with column names:

Supported Specifiers:

  • #Headers - Header row

  • #Data - Data rows only

  • #All - Entire table including headers

  • #Totals - Total row

  • @ThisRow - Current row (implicit in [@ColumnName])

Supported Functions

The calculation engine includes 300+ Excel-compatible functions:

Math & Trig:

  • SUM, AVERAGE, COUNT, MIN, MAX

  • ROUND, FLOOR, CEILING, ABS, SQRT

  • SIN, COS, TAN, PI, POWER

Logical:

  • IF, AND, OR, NOT

  • IFERROR, IFNA, IFS

  • SWITCH, TRUE, FALSE

Text:

  • CONCAT, CONCATENATE, TEXTJOIN

  • LEFT, RIGHT, MID, LEN

  • UPPER, LOWER, PROPER, TRIM

  • FIND, SEARCH, SUBSTITUTE, REPLACE

Lookup & Reference:

  • VLOOKUP, HLOOKUP, XLOOKUP

  • INDEX, MATCH

  • INDIRECT, OFFSET, ROW, COLUMN

  • FILTER, SORT, UNIQUE

Date & Time:

  • NOW, TODAY, DATE, TIME

  • YEAR, MONTH, DAY, HOUR, MINUTE, SECOND

  • DATEDIF, NETWORKDAYS, WORKDAY

Array Functions:

  • SEQUENCE, RANDARRAY, UNIQUE

  • SORT, FILTER, TRANSPOSE

  • MAKEARRAY, BYCOL, BYROW

Financial:

  • PMT, PV, FV, RATE, NPER

  • IPMT, PPMT, NPV, IRR

Statistical:

  • STDEV, VAR, MEDIAN, MODE

  • PERCENTILE, QUARTILE

  • CORREL, COVARIANCE

Full function list →

Custom Functions

Add your own functions to extend the calculation engine:

Defining Custom Functions

Registering Custom Functions

Or for Web Worker mode:

Function Autocomplete

Add descriptions for autocomplete suggestions:

Performance Optimization

Tips for Large Workbooks

  1. Use Web Worker mode for workbooks with >1000 formulas

  2. Avoid volatile functions (NOW(), RAND()) in large ranges

  3. Batch updates when modifying multiple cells:

  1. Disable auto-recalc during bulk operations:

  1. Use range formulas instead of copying:

Benchmarks

Typical performance on modern hardware:

Operation
Dataset
Time

Simple formula (=A1*2)

1 cell

<1ms

Range formula (=SUM(A1:A100))

100 cells

2-5ms

Complex dependency chain (10 levels)

1000 formulas

50-100ms

Array formula (=SEQUENCE(1000))

1000 cells

10-20ms

Large workbook recalc

10K formulas

500ms-2s

Troubleshooting

Formulas Not Updating

Problem: Changing a cell doesn't recalculate dependents

Solutions:

  1. Check that formulas start with =

  2. Verify enqueueCalculation is wired correctly (if custom)

#REF! Errors

Problem: Valid formula shows #REF! error

Solutions:

  1. Check sheet names match in sheets prop

  2. Verify named ranges are registered

  3. Enable iterative calculation for circular references:

Slow Performance

Problem: UI freezes during formula entry

Solutions:

  1. Switch to Web Worker mode:

  2. Avoid volatile functions in large ranges

  3. Use manual calculation mode for bulk imports

Custom Functions Not Working

Problem: Custom function returns #NAME! error

Solutions:

  1. Verify function is registered in functions prop

  2. For Web Worker mode, ensure function is in worker file

  3. Check function name matches formula (case-insensitive)

Package Information

The formula evaluation system is provided by:

  • Package: @rowsncolumns/calculation-worker

  • Version: 1.0.15+

  • License: MIT

Installation

Further Reading

Support

For formula evaluation issues:


Last Updated: 2025-01-22

Last updated

Was this helpful?