Skip to content

Instantly share code, notes, and snippets.

@abiank
Created September 5, 2014 09:10
Show Gist options
  • Save abiank/6b1a06e032ddaca395bb to your computer and use it in GitHub Desktop.
Save abiank/6b1a06e032ddaca395bb to your computer and use it in GitHub Desktop.
formula1.js modifications to make use of formula.js formulas in ethercalc
// prerequisite: browserify formula.js and load it
SocialCalc.Formula.CalculateFunction = function(fname, operand, sheet) {
var fobj, foperand, ffunc, argnum, ttext,args,elem;
var scf = SocialCalc.Formula;
var ok = 1;
var errortext = "";
fobj = scf.FunctionList[fname];
if (fobj) {
foperand = [];
ffunc = fobj[0];
argnum = fobj[1];
scf.CopyFunctionArgs(operand, foperand);
if (argnum != 100) {
if (argnum < 0) {
if (foperand.length < -argnum) {
errortext = scf.FunctionArgsError(fname, operand);
return errortext;
}
}
else {
if (foperand.length != argnum) {
errortext = scf.FunctionArgsError(fname, operand);
return errortext;
}
}
}
errortext = ffunc(fname, operand, foperand, sheet);
}
else {
ttext = fname;
if (operand.length && operand[operand.length - 1].type == "start") { // no arguments - name or zero arg function
operand.pop();
scf.PushOperand(operand, "name", ttext);
}
else {
if (typeof formulajs[fname] === "function") {
// should add a config option to disable this
try {
// apply or call, push result back, treat every result as strings for now
args=[];
var len=operand.length;
for (var z=0;z<len;z++) {
elem=operand.pop();
if (elem.type!="start") args.push(elem.value);
}
foperand = [];
var res = formulajs[fname].apply(window,args.reverse());
console.log("res:"+res);
//scf.PushOperand(operand, isNumber(res)?"n":"t", res);
scf.PushOperand(operand, "t", res);
}
catch (e) {
errortext = JSON.stringify(e);
}
}
else {
errortext = SocialCalc.Constants.s_sheetfuncunknownfunction + " " + ttext + ". ";
}
}
}
return errortext;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment