copy Copy chevron-down
Getting started 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.
Copy 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)
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)
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]
All standard Excel error types are supported:
=VLOOKUP("missing", A:B, 2)
=SEQUENCE(10) when cells occupied
Invalid range intersection
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:
Formulas are evaluated repeatedly until convergence
Stops when change between iterations < maxChange
Returns #NUM! if doesn't converge after maxIterations
Define reusable range names (Excel-compatible):
Features:
Case-insensitive (SalesTotal = salestotal)
Can reference ranges or single values
Auto-updates when range changes
Structured References (Excel Tables)
Excel-style table references with column names:
Supported Specifiers:
#All - Entire table including headers
@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
Logical:
Text:
CONCAT, CONCATENATE, TEXTJOIN
UPPER, LOWER, PROPER, TRIM
FIND, SEARCH, SUBSTITUTE, REPLACE
Lookup & Reference:
VLOOKUP, HLOOKUP, XLOOKUP
INDIRECT, OFFSET, ROW, COLUMN
Date & Time:
YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
DATEDIF, NETWORKDAYS, WORKDAY
Array Functions:
SEQUENCE, RANDARRAY, UNIQUE
Financial:
Statistical:
Full function list →arrow-up-right
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:
Tips for Large Workbooks
Use Web Worker mode for workbooks with >1000 formulas
Avoid volatile functions (NOW(), RAND()) in large ranges
Batch updates when modifying multiple cells:
Disable auto-recalc during bulk operations:
Use range formulas instead of copying:
Typical performance on modern hardware:
Range formula (=SUM(A1:A100))
Complex dependency chain (10 levels)
Array formula (=SEQUENCE(1000))
Troubleshooting
Problem: Changing a cell doesn't recalculate dependents
Solutions:
Check that formulas start with =
Verify enqueueCalculation is wired correctly (if custom)
Problem: Valid formula shows #REF! error
Solutions:
Check sheet names match in sheets prop
Verify named ranges are registered
Enable iterative calculation for circular references:
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 functions prop
For Web Worker mode, ensure function is in worker file
Check function name matches formula (case-insensitive)
The formula evaluation system is provided by:
Package: @rowsncolumns/calculation-worker
Further Reading
For formula evaluation issues:
Last Updated: 2025-01-22
Last updated 2 months ago