Search code examples
handsontable

Handsontable and hot-formula-parser


is it possible to include/register a custom function ('ADD_5') into handsontable?

The following example is working and all cell-values from B2 to B4 are equal to 42. Only B5 = ADD_5(37) returns #NAME? instead of 42.

Where Do I have to define the function and add it to the existing parser of the table?

<div id="example1" class="handsontable"></div>

<script data-jsfiddle="example1">
  $(document).ready(function () {
    var data1 = [
      ['Number', 'Answer'],
      [10, 42],
      [10, '=SUM(A2:A5)'],
      [10, '=A2 + A3 + A4 + A5'],
      [12, '=ADD_5(37)']
    ];
    var container1 = $('#example1');
    container1.handsontable({
      data: data1,
      minSpareRows: 1,
      colHeaders: true,
      rowHeaders: true,
      contextMenu: true,
      manualColumnResize: true,
      formulas: true
    });
    parser.setFunction('ADD_5', function(params) {
      return params[0] + 5;
    });
  });
</script>

Any help would be appreciated.


Solution

  • According to official doc:

    The Formulas plugin allows Handsontable to process formula expressions defined in the provided data. This plugin uses a formula-parser library which takes most of functions from formula.js.

    You would need to add your formula to formula.js then