Last active
February 18, 2023 17:43
-
-
Save blizzardengle/6c7ae6e4b10a7ce6f16731bfb4bcba06 to your computer and use it in GitHub Desktop.
Capture Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Loop through a table and pull out certain cells of data. | |
* | |
* This loops through a the rows of a table and grabs the requested cell(s) from each row. | |
* Cell data is seperated with tabs when multiple cells of data are requested OR with the | |
* combine flag set to true the data will be combined into a single string per row. | |
* | |
* Version: Alpha | |
* | |
* @param {int} id The value of the tables HTML id attribute. | |
* @param {array} cells An array of cell numbers you would like; counting starts at 0! | |
* @param {bool} combine Set to true to combine multiple cells of data into a single string; default false. | |
*/ | |
function captureTable( id, cells, combine ) { | |
var tbl = document.getElementById( id ); | |
var tr = tbl.querySelectorAll('tr'); | |
var td; | |
var rowCount = tr.length; | |
var cellCount = 0; | |
var cell; | |
var output = ''; | |
for ( var row = 0; row < rowCount; row++ ) { | |
td = tr[ row ].querySelectorAll('td'); | |
cellCount = td.length; | |
for ( cell = 0; cell < cellCount; cell++ ){ | |
if ( combine ) { | |
if ( cells.indexOf( cell ) > -1 ) { | |
output += td[ cell ].innerText.trim(); | |
} | |
} else { | |
if ( cells.indexOf( cell ) > -1 ) { | |
output += td[ cell ].innerText.trim() + '\t'; | |
} | |
} | |
} | |
output += '\n'; | |
} | |
console.log( output ); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
worked as advertised, applied an id
test
to the table in question and didand got the column I wanted