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:
#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!errorEditing 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:
Formulas are evaluated repeatedly until convergence
Stops when change between iterations <
maxChangeReturns
#NUM!if doesn't converge aftermaxIterations
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,MAXROUND,FLOOR,CEILING,ABS,SQRTSIN,COS,TAN,PI,POWER
Logical:
IF,AND,OR,NOTIFERROR,IFNA,IFSSWITCH,TRUE,FALSE
Text:
CONCAT,CONCATENATE,TEXTJOINLEFT,RIGHT,MID,LENUPPER,LOWER,PROPER,TRIMFIND,SEARCH,SUBSTITUTE,REPLACE
Lookup & Reference:
VLOOKUP,HLOOKUP,XLOOKUPINDEX,MATCHINDIRECT,OFFSET,ROW,COLUMNFILTER,SORT,UNIQUE
Date & Time:
NOW,TODAY,DATE,TIMEYEAR,MONTH,DAY,HOUR,MINUTE,SECONDDATEDIF,NETWORKDAYS,WORKDAY
Array Functions:
SEQUENCE,RANDARRAY,UNIQUESORT,FILTER,TRANSPOSEMAKEARRAY,BYCOL,BYROW
Financial:
PMT,PV,FV,RATE,NPERIPMT,PPMT,NPV,IRR
Statistical:
STDEV,VAR,MEDIAN,MODEPERCENTILE,QUARTILECORREL,COVARIANCE
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
Use Web Worker mode for workbooks with >1000 formulas
Avoid volatile functions (
NOW(),RAND()) in large rangesBatch updates when modifying multiple cells:
Disable auto-recalc during bulk operations:
Use range formulas instead of copying:
Benchmarks
Typical performance on modern hardware:
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:
Check that formulas start with
=Verify
enqueueCalculationis wired correctly (if custom)
#REF! Errors
Problem: Valid formula shows #REF! error
Solutions:
Check sheet names match in
sheetspropVerify named ranges are registered
Enable iterative calculation for circular references:
Slow Performance
Problem: UI freezes during formula entry
Solutions:
Switch to Web Worker mode:
Avoid volatile functions in large ranges
Use manual calculation mode for bulk imports
Custom Functions Not Working
Problem: Custom function returns #NAME! error
Solutions:
Verify function is registered in
functionspropFor Web Worker mode, ensure function is in worker file
Check function name matches formula (case-insensitive)
Package Information
The formula evaluation system is provided by:
Package:
@rowsncolumns/calculation-workerVersion: 1.0.15+
License: MIT
Installation
Further Reading
Support
For formula evaluation issues:
GitHub: rowsncolumns/spreadsheet/issues
Email: [email protected]
Documentation: docs.rowsncolumns.app
Last Updated: 2025-01-22
Last updated
Was this helpful?