像Excel一样读取表
程序员文章站
2022-03-18 17:01:35
...
我在这里写的一些代码可能对某人有用,我已经看到很多人想要这种类型的代码,所以这里是我的示例。
<input type="text" id="showval"><input type="text" id="search" value="A:1"><input type="button" value="Show Value" onclick="ReadCell(document.getElementById('search').value, 'ExcelView')"><br><br>
<table border=1 id="ExcelView" name="ExcelView">
<tr><td>Cell 1</td><td>Cell 2</td><td>Cell 3</td><td>Cell 4</td></tr>
<tr><td>Cell 5</td><td>Cell 6</td><td>Cell 7</td><td>Cell 8</td></tr>
<tr><td>Cell 9</td><td>Cell 10</td><td>Cell 11</td><td>Cell 12</td></tr>
</table>
<script language="javascript">
function ReadCell(val, tbl) {
val = val.toUpperCase();
var alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var table = document.getElementById(tbl);
var sVal = val.split(":");
var col = sVal[0];
if (isnumeric(col)) { alphaconv = col } else { var alphaconv = alphabet.indexOf(col); }
var row = sVal[1]-1;
var gval = "";
if (sVal.length==2 && isnumeric(alphaconv) && isnumeric(row)) {
var rows = table.getElementsByTagName("TR");
if (row>=rows.length) { return false; } else { var cols = rows[row].getElementsByTagName("TD"); }
if (alphaconv>cols.length) { return false; } else { var gval = cols[alphaconv].innerHTML; }
if (gval!="") { document.getElementById("showval").value = gval; }
}
}
function isnumeric(sText) {
var ValidChars = "0123456789";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
</script>
From: https://bytes.com/topic/javascript/insights/646585-reading-tables-like-excel