js/calc.js
/**
 * Solves the mathemathical expression and supports formulas like SUM(2,3), MAX(a1:d3), etc.
 *
 * @param exp Expression that needs to be solved
 * @param forget Internal, you never need to set this param
 * @return A string that is numeric solution to the expression
 *
 * */
Solves the mathemathical expression and supports formulas like SUM(2,3), MAX(a1:d3), etc.
@param exp Expression that needs to be solved
@param forget Internal, you never need to set this param
@return A string that is numeric solution to the expression
Solves the mathemathical expression and supports formulas like SUM ( 2 3 ) MAX ( a1 : d3 ) etc . @param exp Expression that needs to be solved @param forget Internal you never need to set this param @return A string that is numeric solution to the expression
function solve(expression, forget)
{
		var exp = expression;
    var error = "ok";
    console.log("solve", error);
    if (error == "ok")
		{
			var res;
			exp = exp.replace(/\s+/g, "");
			exp = "(" + exp + ")";
			var rxNeg = /([\(\+\-\*\/])\-([A-Za-z0-9\(])/g;
			exp = exp.replace(rxNeg, "$1~$2");
			exp = repairOperators(exp);
			if (!forget)
				_repairedExpression = repairExpression(exp);
			var rxParen = /([A-Za-z]*)\([^()]*\)/g;
			while (exp.indexOf("(") != -1)
			{
				exp = exp.replace(rxParen, solveBracket);
			}
			exp = exp.replace("~", "-");
			res = Number(exp);
		}
		else
		{
			exp = error;
		}
		if (isString(exp))
			exp = stripStringQuotes(exp);
		console.log("solved",exp);
		return exp;
}
function solve ( expression , forget ) { var exp = expression ; var error = "ok" ; console . log ( "solve" , error ) ; if ( error == "ok" ) { var res ; exp = exp . replace ( / \s+ / g , "" ) ; exp = "(" + exp + ")" ; var rxNeg = / ([\(\+\-\*\/])\-([A-Za-z0-9\(]) / g ; exp = exp . replace ( rxNeg , "$1~$2" ) ; exp = repairOperators ( exp ) ; if ( ! forget ) _repairedExpression = repairExpression ( exp ) ; var rxParen = / ([A-Za-z]*)\([^()]*\) / g ; while ( exp . indexOf ( "(" ) != - 1 ) { exp = exp . replace ( rxParen , solveBracket ) ; } exp = exp . replace ( "~" , "-" ) ; res = Number ( exp ) ; } else { exp = error ; } if ( isString ( exp ) ) exp = stripStringQuotes ( exp ) ; console . log ( "solved" , exp ) ; return exp ; }
Solves the mathemathical expression and supports formulas like SUM(2,3), MAX(a1:d3), etc.
Solves the mathemathical expression and supports formulas like SUM ( 2 3 ) MAX ( a1 : d3 ) etc .
// checkValidExpression(expression); // remove white spaces from expression		 // add outer brackets to the expression to enable iterative paren solving // convert negative values from - to ~. E.g. 5+-3, into 5+~3 // var rxNeg:RegsExp = /([\(\+\-\*\/])\-/g; // remember the expression before it gets modified into result // find the innermost brackets, also see if brackets are part of formula - SUM(args) // repeat while all the brackets are gone, method called to replace brackets with a value is solveParen // replace ! back to -, for casting to number, negative if you will // var errEvt : SpreadsheetEvent = new SpreadsheetEvent(SpreadsheetEvent.ERROR); // errEvt.message = isValid; // this.dispatchEvent(errEvt);