Skip to content Skip to sidebar Skip to footer

Handsontable Rulejs Missing Recursive Resolution

I use RuleJS plugin for Handsontable (see it on GitHub) which works great for basic formulas but seems to lack recursive resolution. I've made a code snippet containing two detaile

Solution 1:

The working code can be found here : http://jsfiddle.net/71o23gp0/8/.

The important part was to replace :

var custom = {
      cellValue: instance.getDataAtCell
};

By

varcustom= {
        cellValue:function(row, col){
          varvalue=instance.getDataAtCell(row, col);if(value&&value[0] ==='=') {
              varformula=value.substr(1).toUpperCase();varcellId=instance.plugin.utils.translateCellCoords({
                  row:row,
                  col:col
              });varitem=instance.plugin.matrix.getItem(cellId);if(!item) {
                  item=instance.plugin.matrix.addItem({
                      id:cellId,
                      formula:formula
                  });
              } else {
                  item=instance.plugin.matrix.updateItem({
                      id:cellId,
                      formula:formula
                  });
              }
              varformulaResult=instance.plugin.parse(formula, {
                  row:row,
                  col:col,
                  id:cellId
              });value=formulaResult.result||'#ERROR';formulasResults[cellId] =value;instance.plugin.matrix.updateItem(item, {
                  formula:formula,
                  value:formulaResult.result,
                  error:formulaResult.error
              });
          }
          returnvalue;
      }
    };

Instead of simply returning the value of the cell, it checks if it's a formula and resolve it if so. Because this method is called by RuleJS when resolving a formula, this make the resolution recursive. The result is then cached for better performance.

There are other minor modifications in the source code, but I've added comments in the fiddle before and after every modification.

Solution 2:

Following on from the answer https://stackoverflow.com/a/35528447/489865 in this question, I have patched a little further. The working code can be found at http://jsfiddle.net/ufL1vts5/.

The essential change is to store in formulasResults[] not just the "result" but rather the whole object returned by instance.plugin.parse(), so that cell highlighting for formulas is picked up, as per:

// parse formulavar newValue = instance.plugin.parse(formula, {row: row, col: col, id: cellId});
// cache result
formulasResults[cellId] = newValue;

My modifications include:

  1. Store whole instance.plugin.parse() object in formulasResults[], so that the formula/formula-error CSS style gets applied. [Note that this is not perfect: it picks up "calculated" formula style correctly, but sometimes what should be formula-error style only get formula style --- but sufficient for my needs.]

  2. Added formula & formula-error CSS styles (from supplied handsontable.formula.css) into JSFiddle example, so that cell highlighting can be seen.

  3. Added 2 extra examples: one from handsontable formula returning #NEED_UPDATE, to show that works too, and one from the example supplied with handsontable-ruleJS at https://github.com/handsontable/handsontable-ruleJS/blob/master/index.html.

  4. Removed the newly added beforeRender hook via instance.removeHook('beforeRender', beforeRender); in this.init = function ().

  5. Changed some code layout in handsontable.formula.js, so that it minimises the differences from that supplied with handsontable-ruleJS.

Post a Comment for "Handsontable Rulejs Missing Recursive Resolution"