Last active
March 26, 2018 13:27
-
-
Save kaminskypavel/6a296574a6295e61cd7240c37ee43d08 to your computer and use it in GitHub Desktop.
export HTML table to CSV
This file contains 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
// based on table2CSV (https://github.com/rubo77/table2CSV) and https://gist.github.com/adilapapaya/9787842 | |
// paste in console. | |
(function () { | |
jQuery.fn.table2CSV = function (options) { | |
var options = jQuery.extend({ | |
separator: ',', | |
header: [], | |
headerSelector: 'th', | |
columnSelector: 'td', | |
delivery: 'popup', // popup, value, download | |
filename: 'export.csv', // filename to download | |
transform_gt_lt: true // make > and < to > and < | |
}, | |
options); | |
var csvData = []; | |
var headerArr = []; | |
var el = this; | |
//header | |
var numCols = options.header.length; | |
var tmpRow = []; // construct header avalible array | |
if (numCols > 0) { | |
for (var i = 0; i < numCols; i++) { | |
tmpRow[tmpRow.length] = formatData(options.header[i]); | |
} | |
} else { | |
$(el).filter(':visible').find(options.headerSelector).each(function () { | |
if ($(this).css('display') != 'none') | |
tmpRow[tmpRow.length] = formatData($(this).html()); | |
}); | |
} | |
row2CSV(tmpRow); | |
// actual data | |
$(el).find('tr').each(function () { | |
var tmpRow = []; | |
$(this).filter(':visible').find(options.columnSelector).each(function () { | |
if ($(this).css('display') != 'none') | |
tmpRow[tmpRow.length] = formatData($(this).html()); | |
}); | |
row2CSV(tmpRow); | |
}); | |
if (options.delivery == 'popup') { | |
var mydata = csvData.join('\n'); | |
if (options.transform_gt_lt) { | |
mydata = sinri_recover_gt_and_lt(mydata); | |
} | |
return popup(mydata); | |
} else if (options.delivery == 'download') { | |
var mydata = csvData.join('\n'); | |
if (options.transform_gt_lt) { | |
mydata = sinri_recover_gt_and_lt(mydata); | |
} | |
var url = 'data:text/csv;charset=utf8,' + encodeURIComponent(mydata); | |
window.open(url); | |
return true; | |
} else { | |
var mydata = csvData.join('\n'); | |
if (options.transform_gt_lt) { | |
mydata = sinri_recover_gt_and_lt(mydata); | |
} | |
return mydata; | |
} | |
function sinri_recover_gt_and_lt(input) { | |
var regexp = new RegExp(/>/g); | |
var input = input.replace(regexp, '>'); | |
var regexp = new RegExp(/</g); | |
var input = input.replace(regexp, '<'); | |
return input; | |
} | |
function row2CSV(tmpRow) { | |
var tmp = tmpRow.join('') // to remove any blank rows | |
// alert(tmp); | |
if (tmpRow.length > 0 && tmp != '') { | |
var mystr = tmpRow.join(options.separator); | |
csvData[csvData.length] = mystr; | |
} | |
} | |
function formatData(input) { | |
// replace " with “ | |
var regexp = new RegExp(/["]/g); | |
var output = input.replace(regexp, "“"); | |
//HTML | |
var regexp = new RegExp(/\<[^\<]+\>/g); | |
var output = output.replace(regexp, ""); | |
output = output.replace(/ /gi, ' '); //replace | |
if (output == "") | |
return ''; | |
return '"' + output.trim() + '"'; | |
} | |
function popup(data) { | |
var generator = window.open('', 'csv', 'height=400,width=600'); | |
generator.document.write('<html><head><title>CSV</title>'); | |
generator.document.write('</head><body >'); | |
generator.document.write('<textArea cols=70 rows=15 wrap="off" >'); | |
generator.document.write(data); | |
generator.document.write('</textArea>'); | |
generator.document.write('</body></html>'); | |
generator.document.close(); | |
return true; | |
} | |
}; | |
$('table').each(function () { | |
var $table = $(this); | |
var $button = $("<button type='button'>"); | |
$button.text("Export to spreadsheet"); | |
$button.insertAfter($table); | |
$button.click(function () { | |
var csv = $table.table2CSV({ | |
delivery: 'value' | |
}); | |
window.location.href = 'data:text/csv;charset=UTF-8,' | |
+ encodeURIComponent(csv); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment