Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading Tables Like Excel

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#1   May 14 '07
Heres some code I wrote that may become useful to someone, I have seen many people wanting this type of code so here is my example.
Expand|Select|Wrap|Line Numbers
  1. <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>
  2. <table border=1 id="ExcelView" name="ExcelView">
  3. <tr><td>Cell 1</td><td>Cell 2</td><td>Cell 3</td><td>Cell 4</td></tr>
  4. <tr><td>Cell 5</td><td>Cell 6</td><td>Cell 7</td><td>Cell 8</td></tr>
  5. <tr><td>Cell 9</td><td>Cell 10</td><td>Cell 11</td><td>Cell 12</td></tr>
  6. </table>
  7. <script language="javascript">
  8. function ReadCell(val, tbl) {
  9.  val = val.toUpperCase();
  10.  var alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  11.  var table = document.getElementById(tbl);
  12.  var sVal = val.split(":");
  13.  var col = sVal[0];
  14.  if (isnumeric(col)) { alphaconv = col } else { var alphaconv = alphabet.indexOf(col); }
  15.  var row = sVal[1]-1;
  16.  var gval = "";
  17.  if (sVal.length==2 && isnumeric(alphaconv) && isnumeric(row)) { 
  18.   var rows = table.getElementsByTagName("TR");
  19.   if (row>=rows.length) { return false; } else { var cols = rows[row].getElementsByTagName("TD"); }
  20.   if (alphaconv>cols.length) { return false; } else { var gval = cols[alphaconv].innerHTML; }
  21.   if (gval!="") { document.getElementById("showval").value = gval; }
  22.  }
  23. }
  24.  
  25. function isnumeric(sText) {
  26.    var ValidChars = "0123456789";
  27.    var IsNumber=true;
  28.    var Char;
  29.    for (i = 0; i < sText.length && IsNumber == true; i++) 
  30.       { 
  31.       Char = sText.charAt(i); 
  32.       if (ValidChars.indexOf(Char) == -1) 
  33.          {
  34.          IsNumber = false;
  35.          }
  36.       }
  37.    return IsNumber;
  38. }
  39.  
  40. </script>
  41.  



Newbie
 
Join Date: May 2007
Location: Moscow, Russia
Posts: 1
#2   May 23 '07

re: Reading Tables Like Excel


Thank you. It is very interesting script.
Familiar Sight
 
Join Date: Oct 2006
Posts: 141
#3   Jul 31 '07

re: Reading Tables Like Excel


Thanks . it really helped me .
Reply


Similar JavaScript / Ajax / DHTML bytes