Handsontable Rulejs Missing Recursive Resolution
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:
Store whole
instance.plugin.parse()
object informulasResults[]
, so that theformula
/formula-error
CSS style gets applied. [Note that this is not perfect: it picks up "calculated" formula style correctly, but sometimes what should beformula-error
style only getformula
style --- but sufficient for my needs.]Added
formula
&formula-error
CSS styles (from suppliedhandsontable.formula.css
) into JSFiddle example, so that cell highlighting can be seen.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.
Removed the newly added
beforeRender
hook viainstance.removeHook('beforeRender', beforeRender);
inthis.init = function ()
.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"