Formula Parser
Library provides a Parser
class that evaluates excel and mathematical formulas.
Install
A recommended way to install Formula Parser is through NPM using the following command:
$ npm install hot-formula-parser --save
Node.js:
var FormulaParser = Parser;var parser = ; parser; // It returns `Object {error: null, result: 14}`
Browser:
Features
It supports:
- Any numbers, negative and positive as float or integer;
- Arithmetic operations like
+
,-
,/
,*
,%
,^
; - Logical operations like
AND()
,OR()
,NOT()
,XOR()
; - Comparison operations like
=
,>
,>=
,<
,<=
,<>
; - All JavaScript Math constants like
PI()
,E()
,LN10()
,LN2()
,LOG10E()
,LOG2E()
,SQRT1_2()
,SQRT2()
; - String operations like
&
(concatenation eq.parser.parse('-(2&5)');
will return-25
); - All excel formulas defined in formula.js;
- Relative and absolute cell coordinates like
A1
,$A1
,A$1
,$A$1
; - Build-in variables like
TRUE
,FALSE
,NULL
- Custom variables;
- Custom functions/formulas;
- Node and Browser environment.
API (methods)
var parser = ;
.parse(expression)
Parses and evaluates provided expression. It always returns an object with result
and error
properties. result
property
always keep evaluated value. If error occurs error
property will be set as:
#ERROR!
General error;#DIV/0!
Divide by zero error;#NAME?
Not recognised function name or variable name;#N/A
Indicates that a value is not available to a formula;#NUM!
Occurs when formula encounters an invalid number;#VALUE!
Occurs when one of formula arguments is of the wrong type.
parser; // returns `Object {error: null, result: 5.6}`parser; // returns `Object {error: "#NAME?", result: null}`parser; // returns `Object {error: "#ERROR!", result: null}`
.setVariable(name, value)
Set predefined variable name which can be visible while parsing formula expression.
parser;parser; parser; // returns `5.6`
.getVariable(name)
Get variable name.
parser; parser; // returns `10`
.setFunction(name, fn)
Set custom function which can be visible while parsing formula expression.
parser;parser; parser; // returns `10`parser; // returns `m`
.getFunction(name)
Get custom function.
parser; parser1; // returns `6`
.SUPPORTED_FORMULAS
List of all supported formulas function.
SUPPORTED_FORMULAS; // An array of formula names
API (hooks)
'callVariable' (name, done)
Fired while retrieving variable. If variable was defined earlier using setVariable
you can overwrite it by this hook.
parser; parser; // returns `1`
'callFunction' (name, params, done)
Fired while calling function. If function was defined earlier using setFunction
you can overwrite it's result by this hook.
You can also use this to override result of build-in formulas.
parser; parser; // returns `8`
'callCellValue' (cellCoord, done)
Fired while retrieving cell value by its label (eq: B3
, B$3
, B$3
, $B$3
).
parser; parser; // returns `"hello"`parser; // returns `"hello world"`parser; // returns `0.9729550745276566`
'callRangeValue' (startCellCoord, endCellCoord, done)
Fired while retrieving cells range value (eq: A1:B3
, $A1:B$3
, A$1:B$3
, $A$1:$B$3
).
parser; parser; // returns `"1,2,3,4,5,6,7,8,9,10"`parser; // returns `5`parser; // returns `2`parser; // returns `10`parser; // returns `5`
Want to help?
Please see CONTRIBUTING.md.
Changelog
To see the list of recent changes, see Releases section.
License
The MIT License (see the LICENSE file for the full text).
Contact
You can contact us at hello@handsontable.com.