In an app that I will post soon, I needed 11 javascript arrays derived from a table with 55 rows. To do this by either entering by hand, or cutting and pasting each element would be very time consuming and tedious. I therefore thought it would be worthwhile to spend a little time finding a better way.
Copying a column from a table is not easy. Standard selection selects by rows. However, spreadsheets allow columns to be copied and pasted, so the first step was to convert the table to a spreadsheet.
To do this I saved the space delimited table as a csv file which was opened in a spreadsheet application. Any one would work. I then copied a column and pasted it into the textarea of an app I wrote which performs the reformatting into an array that could be pasted into the app I was writing.
Here is the code:
<html xmlns='http://www.w3.org/1999/xhtml'>
<head profile='http://gmpg.org/xfn/11'>
<title>Enter Title Here</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15pt Arial;}a{ text-decoration: }:link { color: rgb(0, 0, 255) }
:visited {color :rgb(100, 0,100) }:hover { }:active { } #t1{position: absolute; width: 100%; height: 90%; top: 10%}
</style>
</head>
<body>
<textarea id=”t1″ name=”t1″ rows=” ” cols=” “></textarea>
<input type=”button” id=”b1″ name=”b1″ value=”Go” onclick = ‘run();’/>
function run() {
document.getElementById(“t1”).value = document.getElementById(“t1”).value.replace(/\n/g,’,’);
document.getElementById(“t1”).value = “var K = [” + document.getElementById(“t1″).value.replace(/,,/g,”) + “];”;
document.getElementById(“t1”).value = document.getElementById(“t1”).value.replace(/,]/g,’]’);
}
</body></html>
The interface contains only a textarea and a button to start the operation once the column has been pasted into the textarea.
document.getElementById(“t1”).value = document.getElementById(“t1”).value.replace(/\n/g,’,’);
This line replaces the line feeds of the column with commas so I now have comma delimited values trailed by a large number of commas,from all the empty cells of the column. I could have copied only the cells with data, but it is easier to let the software remove extraneous commas.
document.getElementById(“t1”).value = “var K = [” + document.getElementById(“t1″).value.replace(/,,/g,”) + “];”;
This line creates the array and deletes all pairs of commas.
document.getElementById(“t1”).value = document.getElementById(“t1”).value.replace(/,]/g,’]’);
If there were an add number of trailing commas, this line selectively removes the last extraneous comma.
Here is an example of an array created by this method:
var K1w =[1,1,0.9999,0.9922,0.9845,0.9768,0.9691,0.9613,0.9536,0.9458,0.9381,0.9303,0.9225,0.9146,0.9068,0.8990,0.8910,0.8831,0.8751,0.8672,0.8592,0.8510,0.8428,0.8347,0.8265,0.8183,0.8098,0.8013,0.7928,0.7843,0.7758,0.7669,0.7579,0.7490,0.7400,0.7311,0.7216,0.7121,0.7025,0.6930,0.6835,0.6732,0.6630,0.6527,0.6425,0.6322,0.6210,0.6098,0.5985,0.5873,0.5761,0.5635,0.5509,0.5384,0.5258,0.5132];
This method can convert a table column with 55 data containing cells to an array in less than a minute.