Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit 1d7908b

Browse files
committed
Merge branch 'release/1.0.1'
2 parents 954e567 + c02dfcc commit 1d7908b

File tree

3 files changed

+212
-5
lines changed

3 files changed

+212
-5
lines changed

CONTRIBUTING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Contributing to FormulaParser
2+
3+
Your contributions to the project are very welcome. If you would like to fix a bug or propose a new feature, you can submit a Pull Request.
4+
5+
To help us merge your Pull Request, please make sure you follow these points:
6+
7+
1. Please make your fix on a separate branch based on `develop` branch. This makes merging much easier.
8+
2. Do not edit files in `dist/` directory (e.g: `formula-parser.js`, `formula-parser.min.js`). Instead, edit files inside the `src/` directory and then use `gulp` or `npm scripts` to make a build.
9+
3. **Very important:** For any change that you make, **please try to also add a test case(s)** in `tests/unit/` or `test/integration/`. This helps us understand the issue and make sure that it will stay fixed forever.
10+
5. Describe the problem in the Pull Request description (of course you would do it, why do I mention that?).
11+
6. **Very important:** Make Pull Request ready to merge into `develop` branch.
12+
13+
Thank you for your commitment!
14+
15+
## Team rules
16+
17+
The Handsontable team utilizes Git-Flow. See [How we use Git-Flow](https://github.com/handsontable/handsontable/wiki/How-we-use-Git-Flow)

README.md

Lines changed: 192 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,192 @@
1-
Formula Parser
2-
=======
3-
Like excel library to parse formulas.
1+
Formula Parser [![Build Status](https://travis-ci.org/handsontable/formula-parser.png?branch=master)](https://travis-ci.org/handsontable/formula-parser)
2+
==========
3+
Library provides a `Parser` class that evaluates excel and mathematical formulas.
4+
5+
- - -
6+
7+
## Install
8+
9+
A recommended way to install Formula Parser is through [NPM](https://www.npmjs.com/) using the following command:
10+
11+
```sh
12+
$ npm install hot-formula-parser --save
13+
```
14+
15+
Node.js:
16+
```js
17+
var FormulaParser = require('hot-formula-parser').Parser;
18+
var parser = new FormulaParser();
19+
20+
parser.parse('SUM(1, 6, 7)'); // It returns `Object {error: null, result: 14}`
21+
```
22+
23+
Browser:
24+
```html
25+
<script src="/node_modules/hot-formula-parser/dist/formula-parser.min.js"></script>
26+
<script>
27+
var parser = new formulaParser.Parser();
28+
29+
parser.parse('SUM(1, 6, 7)'); // It returns `Object {error: null, result: 14}`
30+
</script>
31+
```
32+
## Features
33+
34+
It supports:
35+
36+
* Any numbers, negative and positive as float or integer;
37+
* Arithmetic operations like `+`, `-`, `/`, `*`, `%`, `^`;
38+
* Logical operations like `AND()`, `OR()`, `NOT()`, `XOR()`;
39+
* Comparison operations like `=`, `>`, `>=`, `<`, `<=`, `<>`;
40+
* All JavaScript Math constants like `PI()`, `E()`, `LN10()`, `LN2()`, `LOG10E()`, `LOG2E()`, `SQRT1_2()`, `SQRT2()`;
41+
* String operations like `&` (concatenation eq. `parser.parse('-(2&5)');` will returns `-25`);
42+
* All excel formulas defined in [formula.js](https://github.com/handsontable/formula.js);
43+
* Relative and absolute cell coordinates like `A1`, `$A1`, `A$1`, `$A$1`;
44+
* Build-in variables like `TRUE`, `FALSE`, `NULL`
45+
* Custom variables;
46+
* [TODO] Custom functions/formulas;
47+
* Node and Browser environment.
48+
49+
## API (methods)
50+
51+
```js
52+
var parser = new formulaParser.Parser();
53+
```
54+
55+
### .parse(expression)
56+
57+
Parses and evaluates provided expression. It always returns an object with `result` and `error` properties. `result` property
58+
always keep evaluated value. If error occurs `error` property will be set as:
59+
* `#ERROR!` General error;
60+
* `#DIV/0!` Divide by zero error;
61+
* `#NAME?` Not recognised function name or variable name;
62+
* `#N/A` Indicates that a value is not available to a formula;
63+
* `#NUM!` Occurs when formula encounters an invalid number;
64+
* `#VALUE?` Occurs when one of formula arguments is of the wrong type.
65+
66+
```js
67+
parser.parse('(1 + 5 + (5 * 10)) / 10'); // returns `Object {error: null, result: 5.6}`
68+
parser.parse('SUM(MY_VAR)'); // returns `Object {error: "#NAME?", result: null}`
69+
parser.parse('1;;1'); // returns `Object {error: "#ERROR!", result: null}`
70+
```
71+
72+
### .setVariable(name, value)
73+
74+
Set predefined variable name which can be visible while parsing formula expression.
75+
76+
```js
77+
parser.setVariable('MY_VARIABLE', 5);
78+
parser.setVariable('fooBar', 10);
79+
80+
parser.parse('(1 + MY_VARIABLE + (5 * fooBar)) / fooBar'); // returns `5.6`
81+
```
82+
83+
### .getVariable(name)
84+
85+
Get variable name.
86+
87+
```js
88+
parser.setVariable('fooBar', 10);
89+
90+
parser.getVariable('fooBar'); // returns `10`
91+
```
92+
93+
### .SUPPORTED_FORMULAS
94+
95+
List of all supported formulas function.
96+
97+
```js
98+
require('hot-formula-parser').SUPPORTED_FORMULAS; // An array of formula names
99+
```
100+
101+
## API (hooks)
102+
103+
### 'callVariable' (name, done)
104+
105+
Fired while retrieving variable. If variable was defined earlier using `setVariable` you can overwrite it by this hook.
106+
107+
```js
108+
parser.on('callVariable', function(name, done) {
109+
if (name === 'foo') {
110+
done(Math.PI / 2);
111+
}
112+
});
113+
114+
parser.parse('SUM(SIN(foo), COS(foo))'); // returns `1`
115+
```
116+
117+
### 'callCellValue' (cellCoord, done)
118+
119+
Fired while retrieving cell value by its label (eq: `B3`, `B$3`, `B$3`, `$B$3`).
120+
121+
```js
122+
parser.on('callCellValue', function(cellCoord, done) {
123+
// using label
124+
if (cellCoord.label === 'B$6') {
125+
done('hello');
126+
}
127+
// or using indexes
128+
if (cellCoord.row.index === 5 && cellCoord.row.isAbsolute && cellCoord.column.index === 1 && !cellCoord.column.isAbsolute) {
129+
done('hello');
130+
}
131+
132+
if (cellCoord.label === 'C6') {
133+
done(0.75);
134+
}
135+
});
136+
137+
parser.parse('B$6'); // returns `"hello"`
138+
parser.parse('B$6&" world"'); // returns `"hello world"`
139+
parser.parse('FISHER(C6)'); // returns `0.9729550745276566`
140+
```
141+
142+
### 'callRangeValue' (startCellCoord, endCellCoord, done)
143+
144+
Fired while retrieving cells range value (eq: `A1:B3`, `$A1:B$3`, `A$1:B$3`, `$A$1:$B$3`).
145+
146+
```js
147+
parser.on('callRangeValue', function(startCellCoord, endCellCoord, done) {
148+
var data = [
149+
[1, 2, 3, 4, 5],
150+
[6, 7, 8, 9, 10],
151+
[11, 12, 13, 14, 15],
152+
[16, 17, 18, 19, 20],
153+
];
154+
var fragment = [];
155+
156+
for (var row = startCellCoord.row.index; row <= endCellCoord.row.index; row++) {
157+
var rowData = data[row];
158+
var colFragment = [];
159+
160+
for (var col = startCellCoord.column.index; col <= endCellCoord.column.index; col++) {
161+
colFragment.push(rowData[col]);
162+
}
163+
fragment.push(colFragment);
164+
}
165+
166+
if (fragment) {
167+
done(fragment);
168+
}
169+
});
170+
171+
parser.parse('JOIN(A1:E2)'); // returns `"1,2,3,4,5,6,7,8,9,10"`
172+
parser.parse('COLUMNS(A1:E2)'); // returns `5`
173+
parser.parse('ROWS(A1:E2)'); // returns `2`
174+
parser.parse('COUNT(A1:E2)'); // returns `10`
175+
parser.parse('COUNTIF(A1:E2, ">5")'); // returns `5`
176+
```
177+
178+
### Want to help?
179+
180+
Please see [CONTRIBUTING.md](CONTRIBUTING.md).
181+
182+
### Changelog
183+
184+
To see the list of recent changes, see [Releases section](https://github.com/handsontable/formula-parser/releases).
185+
186+
### License
187+
188+
The MIT License (see the [LICENSE](https://github.com/handsontable/formula-parser/blob/master/LICENSE) file for the full text).
189+
190+
### Contact
191+
192+
You can contact us at hello@handsontable.com.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "formula-parser",
3-
"version": "1.0.0",
2+
"name": "hot-formula-parser",
3+
"version": "1.0.1",
44
"description": "Formula parser",
55
"main": "dist/formula-parser.js",
66
"scripts": {
@@ -61,6 +61,7 @@
6161
"jison": "^0.4.17",
6262
"json-loader": "^0.5.3",
6363
"mocha": "^2.3.4",
64+
"proxyquire": "^1.7.9",
6465
"sinon": "^1.17.2",
6566
"sinon-chai": "^2.8.0",
6667
"vinyl-source-stream": "^1.1.0",

0 commit comments

Comments
 (0)