@builttocreate/hot-formula-parser

中文文档
5.0.2发布于 2022/11/11MITCommonJS近 30 天下载 8.1万

Formula parser

原始使用文档
文档目录18 节
  1. Install
  2. Features
  3. API (methods)
  4. .parse(expression)
  5. .setVariable(name, value)
  6. .getVariable(name)
  7. .setFunction(name, fn)
  8. .getFunction(name)
  9. .SUPPORTED_FORMULAS
  10. API (hooks)
  11. 'callVariable' (name, done)
  12. 'callFunction' (name, params, done)
  13. 'callCellValue' (cellCoord, done)
  14. 'callRangeValue' (startCellCoord, endCellCoord, done)
  15. Want to help?
  16. Changelog
  17. License
  18. Contact

Formula Parser Build Status Test Coverage hot-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 = require('hot-formula-parser').Parser;
var parser = new FormulaParser();

parser.parse('SUM(1, 6, 7)'); // It returns `Object {error: null, result: 14}`

Browser:

<script src="/node_modules/hot-formula-parser/dist/formula-parser.min.js"></script>
<script>
var parser = new formulaParser.Parser();

parser.parse('SUM(1, 6, 7)'); // It returns `Object {error: null, result: 14}`
</script>

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 = new formulaParser.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.parse('(1 + 5 + (5 * 10)) / 10'); // returns `Object {error: null, result: 5.6}`
parser.parse('SUM(MY_VAR)'); // returns `Object {error: "#NAME?", result: null}`
parser.parse('1;;1'); // returns `Object {error: "#ERROR!", result: null}`

.setVariable(name, value)

Set predefined variable name which can be visible while parsing formula expression.

parser.setVariable('MY_VARIABLE', 5);
parser.setVariable('fooBar', 10);

parser.parse('(1 + MY_VARIABLE + (5 * fooBar)) / fooBar'); // returns `5.6`

.getVariable(name)

Get variable name.

parser.setVariable('fooBar', 10);

parser.getVariable('fooBar'); // returns `10`

.setFunction(name, fn)

Set custom function which can be visible while parsing formula expression.

parser.setFunction('ADD_5', function(params) {
  return params[0] + 5;
});
parser.setFunction('GET_LETTER', function(params) {
  var string = params[0];
  var index = params[1] - 1;

  return string.charAt(index);
});

parser.parse('SUM(4, ADD_5(1))'); // returns `10`
parser.parse('GET_LETTER("Some string", 3)'); // returns `m`

.getFunction(name)

Get custom function.

parser.setFunction('ADD_5', function(params) {
  return params[0] + 5;
});

parser.getFunction('ADD_5')([1]); // returns `6`

.SUPPORTED_FORMULAS

List of all supported formulas function.

require('hot-formula-parser').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.on('callVariable', function(name, done) {
  if (name === 'foo') {
    done(Math.PI / 2);
  }
});

parser.parse('SUM(SIN(foo), COS(foo))'); // 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.on('callFunction', function(name, params, done) {
  if (name === 'ADD_5') {
    done(params[0] + 5);
  }
});

parser.parse('ADD_5(3)'); // returns `8`

'callCellValue' (cellCoord, done)

Fired while retrieving cell value by its label (eq: B3, B$3, B$3, $B$3).

parser.on('callCellValue', function(cellCoord, done) {
  // using label
  if (cellCoord.label === 'B$6') {
    done('hello');
  }
  // or using indexes
  if (cellCoord.row.index === 5 && cellCoord.row.isAbsolute && cellCoord.column.index === 1 && !cellCoord.column.isAbsolute) {
    done('hello');
  }

  if (cellCoord.label === 'C6') {
    done(0.75);
  }
});

parser.parse('B$6'); // returns `"hello"`
parser.parse('B$6&" world"'); // returns `"hello world"`
parser.parse('FISHER(C6)'); // 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.on('callRangeValue', function(startCellCoord, endCellCoord, done) {
  var data = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
  ];
  var fragment = [];

  for (var row = startCellCoord.row.index; row <= endCellCoord.row.index; row++) {
    var rowData = data[row];
    var colFragment = [];

    for (var col = startCellCoord.column.index; col <= endCellCoord.column.index; col++) {
      colFragment.push(rowData[col]);
    }
    fragment.push(colFragment);
  }

  if (fragment) {
    done(fragment);
  }
});

parser.parse('JOIN(A1:E2)'); // returns `"1,2,3,4,5,6,7,8,9,10"`
parser.parse('COLUMNS(A1:E2)'); // returns `5`
parser.parse('ROWS(A1:E2)'); // returns `2`
parser.parse('COUNT(A1:E2)'); // returns `10`
parser.parse('COUNTIF(A1:E2, ">5")'); // 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 [email protected].

版本与发布记录

@builttocreate/hot-formula-parser 共发布 9 个版本,首次发布于 2021/02/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.2latest2022/11/113 年前
5.0.12022/07/044 年前
5.0.02022/06/304 年前
5.0.0-rc32022/06/274 年前
5.0.0-rc22022/06/274 年前
5.0.0-rc12022/06/274 年前

关于 @builttocreate/hot-formula-parser 的常见问题

@builttocreate/hot-formula-parser 是什么?

Formula parser

如何安装 @builttocreate/hot-formula-parser?

在项目目录运行 npm install @builttocreate/hot-formula-parser,或使用 pnpm add / yarn add / bun add。

@builttocreate/hot-formula-parser 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/builttocreate__hot-formula-parser 是否存在。

@builttocreate/hot-formula-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@builttocreate/hot-formula-parser 可以商用吗?

@builttocreate/hot-formula-parser 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@builttocreate/hot-formula-parser 的下载量有多少?

根据 npm 官方下载统计,@builttocreate/hot-formula-parser 在 2026/10/09 – 2026/11/07 这 30 天内被下载 80,960 次,日均约 2,699 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@builttocreate/hot-formula-parser 还在维护吗?

最新版本 5.0.2 发布于 2022/11/11(3 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/02/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。