我正在使用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()
答案 0 :(得分:0)
嗯,如果我理解得好,A1和B1应该是de&#34; id&#34;存储值的元素中的元素,因此您应该进入这些元素并获取值。
在这种情况下,您可以使用document.getelementbyid(attOne).value
获取A1字段内的值;
虽然我的问题有点遗失,但您可能应该发布电子表格的结构。