1function tablemath(divID,method,rangeStr) 2{ 3 var result; 4 var div = document.getElementById(divID); 5 var table = div.parentNode.parentNode.parentNode; 6 var setArray = tmSetToArray(table, rangeStr); 7 switch(method) 8 { 9 case 'sum': 10 break; 11 case 'avg': 12 break; 13 default: 14 method = 'unknown'; 15 break; 16 } 17 eval('result = tm'+method+'(setArray);'); 18 div.innerHTML = result; 19} 20function tmSetToArray(table, rangeStr) 21{ 22 //range should be like this x,y:x,y 23 var range = rangeStr.split(':'); 24 var start = range[0].split(','); 25 var end = range[1].split(','); 26 var startX = start[0]; 27 var startY = start[1]; 28 var endX = end[0]; 29 var endY = end[1]; 30 31 var set = new Array(); 32 for(var x = startX; x<=endX; x++) 33 { 34 for(var y = startY; y<=endY; y++) 35 { 36 set.push(tmParseValue(table.rows[y].cells[x])); 37 } 38 } 39 //alert(set); 40 return set; 41} 42 43function tmParseValue(cell) 44{ 45 var value = parseInt(cell.innerHTML); 46 if (isNaN(value)) 47 { 48 value = 0; 49 } 50 return value; 51} 52 53function tmunknown(setArray) 54{ 55 return 'Error'; 56} 57 58function tmsum(setArray) 59{ 60 var sum = 0; 61 for (var i=0; i<setArray.length; i++) 62 { 63 sum += setArray[i]; 64 } 65 return sum; 66} 67 68function tmavg(setArray) 69{ 70 return tmsum(setArray)/setArray.length; 71}