我正在使用 JavaScript 创建电子表格,但无法弄清楚如何评估该=SUM
字段。我的代码将生成一个数组 ex (=SUM,A1,B1)
,但我不知道如何更改 A1 和 B1 的属性以获取其关联字段的值。
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()