# spreadsheet algorithm

Some years ago Doeke and I were buzy on a project called wForms. The idea was to make some sort of simplified XFORMS like language that could run in current browsers.

It triggered me to implement the Recalculation sequence algorithm of XFORMS in javascript.

The implementation I made was only accessible via an associated Expression object. You had to make javascript expressions like ` $a=$b+$c`

where all $ variables were retrieved from underlying data-objects and the relations were automatically parsed from the expressions. Typical usage:

ev.AddExpression('a','$b*2+($c-$b)+$d');
ev.AddExpression('c','$d+$e+$f+4');
ev.SetData('b',new Value(10));
ev.SetData('d',new Value(22));
ev.SetData('e',new Value(1));
ev.SetData('f',new Value(3));
ev.Recalc(true);
DEBUG("RESULTAAT :"+ev.GetValue('a'));

Nowadays, i am yet again triggered to use a spreadsheet algorithm. But I am more minimalistic and better in designing cleaner interfaces (at least, I hope I am). I've written a new implementation that can be far easier coupled to other code because it doesnt have expressions and it doesnt know anything at all about the nodes it calcs. In fact, it just fires an event to do the calcing. This is the synopsis:

/*** Evaluator class **/
/*
Synopsis:
var e=new Evaluator();
e.addNode('a',['b','d']);// b and d depend on a
e.addNode('b',['d']);
e.addNode('c',['b']);
e.addNode('d',[]);
e.prepare(); // a master dependancy graph is built.
e.addCalcEventListener(function(nodeName){}); // listeners that need to trigger/perform the actual calculation.
e.calc(); // calcs all , based on the masterGraph.
e.calc([['c']) // calcs only the nodes based on the fact that c is dirty.
Asynchronous use:
TODO.
*/

I will upload the code soon.