I am creating a spreadsheet using JavaScript and cannot figure out how to valuate the =SUM field. My code will produce an array ex (=SUM,A1,B1) but I am lost on how to change the attribute of A1 and B1 to grab the value of their associated fields.
function saveCell()
{
// get the text the user just typed into the text box
var cellText = document.getElementById("txtCell").value;
var tokenArray = getFormula(cellText);
// if null... this isn't a formula
if (tokenArray != null)
{
alert("This is a formula to sum from " + tokenArray[1] + " to " + tokenArray[2]);
}
else
currentRef.innerHTML = cellText;
ssArray[currentRow - 1][currentCol - 1] = cellText;
}//end saveCell()
// determines if user entered a formula such as =SUM(A1:B1)
// returns an array with cell coordinates
function getFormula(tbValue)
{
// this is a regular expression pattern which is intended to
// split the string (formula) on any of ":" "(" or ")"
var pattern = /[:|\(|\)]/;
// do the split ... ar is an array
var ar = tbValue.split(pattern);
// convert =sum to upper case
var sum = ar[0].toUpperCase();
var attOne = ar[1];
var attTwo = ar[2];
if (ar.length < 3)
return null;
else if (sum != "=SUM")
return alert("The function '"+sum+"' is not supported...");
else
{
//return an array of strings
return ar;
}
}//end getFormula()
Hmm If I understand well, A1, and B1 should be de “id” of the elements in where the values are stored, so you should get inside those elements and obtain the values.
In such case, you can use
document.getelementbyid(attOne).valueto obtain the value inside the A1 field;Although I’m a bit lost by your question, may be you should post the structure of the spreadsheet.