Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var s=document.createElement('script');
- s.setAttribute('src','http://code.jquery.com/jquery.min.js');
- document.getElementsByTagName('body')[0].appendChild(s);
- function toCSV( rows ){
- var newline = '\n';
- var fsep = ','; // field separator
- var quot = '"'; // field quoter
- var quotquot = [ quot + quot, '\\' + quot ][1]; // choose
- var alwaysQuote = false;
- var quoteIfContainsQuote = true;
- // var trimFields, quoteStringsButNotNumbers(excel), putEqualSignBeforeNumbers(excel), etc.
- var lines = [];
- $.each( rows, function(){
- var line = [];
- $.each( this, function(){
- var s = this;
- var needsQuote = alwaysQuote;
- if( s.indexOf( fsep ) >= 0 )
- needsQuote = true;
- if( s.indexOf( quot ) >= 0 ){
- needsQuote = needsQuote || quoteIfContainsQuote;
- s = s.replace( quot, quotquot );
- }
- if( needsQuote ) s = quot + s + quot;
- line.push( s );
- });
- lines.push( line.join( fsep ));
- });
- return lines.join( newline );
- }
- function xtract( rootTableSelector ){
- var rows = [];
- var csv = [];
- $( rootTableSelector ).each(function(){
- var $t = $(this);
- var row = {};
- row.name = $.trim( $t.find('tr:nth-child(1) > td > b').text());
- row.email = $.trim( $t.find('tr:nth-child(2) > td > div:nth-child(2)').text());
- rows.push( row );
- csv.push([ row.name, row.email ]);
- });
- console.log( rows );
- $('body').prepend('<pre id="result"></pre>');
- $('img,.qprintable').remove();
- $('#result').text( toCSV( csv ) );
- $('#json').text( JSON.stringify( rows) );
- // prompt('this is not csv', csv );
- }
- $(function(){
- xtract('.qprintable2');
- });
Add Comment
Please, Sign In to add comment