I want to export an HTML table into a spreadsheet that will be created by the JavaScript program. I've considered doing this via the JQuery library but I don't know much about it nor do I think that it will be allowed in my situation. I have seen a solution that uses XML but I don't know much about XML either. What I intend on doing is creating a 2 dimensional array and exporting the contents of that array into Excel. Unfortunately, I don't have control over the tagging scheme and I will have to use getElementsByTagName. The tagging scheme is created via a reporting software.
The below code loops to the second row but it stops at the second row. I can't figure out what I am doing wrong. I hope that somebody will be able to explain a remedy to the error.
<!DOCTYPE html>
<html>
<body>
<table>
<tr><td>a</td><td>1</td></tr>
<tr><td>b</td><td>2</td></tr>
<tr><td>c</td><td>3</td></tr>
<tr><td>d</td><td>4</td></tr>
</table>
<script>
var myNodelist = document.getElementsByTagName("td");
var NumTD= (document.getElementsByTagName("td").length);
var NumTR= (document.getElementsByTagName("tr").length);
var NumCol=NumTD/NumTR;
var array1 = [[]];
var c=0;
var r=0;
alert("NumTD is: "+NumTD);
for (var i=0;i<=NumTD;i++) {
alert("On TD: "+i);
alert(myNodelist[i].innerHTML);
alert("r: "+r);
alert("c: "+c);
array1[r][c]=myNodelist[i].innerHTML;
alert("The array is at: "+array1[r][c]);
if((c+1)==NumCol) {
c=0;
} else {
c++;
}
if(c==0) {
r++;
}
}
</script>
</body>
</html>