Skip to content

Commit

Permalink
New export format: tab-separated-values (tsv)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhurz committed Jan 13, 2017
1 parent 998a316 commit 13d930b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tableExport.jquery.plugin
<h3>Export HTML Table to</h3>
<ul>
<li> CSV
<li> TSV
<li> TXT
<li> JSON
<li> XML
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tableExport.jquery.plugin",
"version": "1.6.6",
"version": "1.6.7",
"main": "tableExport.min.js",
"license": "MIT",
"description": "html table export",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tableexport.jquery.plugin",
"version": "1.6.6",
"version": "1.6.7",
"description": "html table export",
"main": "tableExport.min.js",
"dependencies": {
Expand Down
64 changes: 37 additions & 27 deletions tableExport.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @preserve tableExport.jquery.plugin
*
* Copyright (c) 2015,2016 hhurz, https://github.com/hhurz/tableExport.jquery.plugin
* Copyright (c) 2015-2017 hhurz, https://github.com/hhurz/tableExport.jquery.plugin
* Original work Copyright (c) 2014 Giri Raj, https://github.com/kayalshri/
*
* Licensed under the MIT License, http://opensource.org/licenses/mit-license
Expand Down Expand Up @@ -66,7 +66,7 @@
tfootSelector: 'tr', // set empty ('') to prevent export of tfoot rows
theadSelector: 'tr',
tableName: 'myTableName',
type: 'csv', // 'csv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png' or 'pdf'
type: 'csv', // 'csv', 'tsv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png' or 'pdf'
worksheetName: 'xlsWorksheetName'
};

Expand All @@ -85,19 +85,52 @@

colNames = GetColumnNames (el);

if (defaults.type == 'csv' || defaults.type == 'txt') {
if (defaults.type == 'csv' || defaults.type == 'tsv' || defaults.type == 'txt') {

var csvData = "";
var rowlength = 0;
rowIndex = 0;

function csvString(cell, rowIndex, colIndex) {
var result = '';

if (cell !== null) {
var dataString = parseString(cell, rowIndex, colIndex);

var csvValue = (dataString === null || dataString === '') ? '' : dataString.toString();

if (defaults.type == 'tsv') {
if (dataString instanceof Date)
result = dataString.toLocaleString();

// According to http://www.iana.org/assignments/media-types/text/tab-separated-values
// are fields that contain tabs not allowable in tsv encoding
result = replaceAll(csvValue, '\t', ' ');
}
else {
// Takes a string and encapsulates it (by default in double-quotes) if it
// contains the csv field separator, spaces, or linebreaks.
if (dataString instanceof Date)
result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
else {
result = replaceAll(csvValue, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);

if (result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result))
result = defaults.csvEnclosure + result + defaults.csvEnclosure;
}
}
}

return result;
}

var CollectCsvData = function ($rows, rowselector, length) {

$rows.each(function () {
trData = "";
ForEachVisibleCell(this, rowselector, rowIndex, length + $rows.length,
function (cell, row, col) {
trData += csvString(cell, row, col) + defaults.csvSeparator;
trData += csvString(cell, row, col) + (defaults.type == 'tsv' ? '\t' : defaults.csvSeparator);
});
trData = $.trim(trData).substring(0, trData.length - 1);
if (trData.length > 0) {
Expand Down Expand Up @@ -1350,29 +1383,6 @@
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

// Takes a string and encapsulates it (by default in double-quotes) if it
// contains the csv field separator, spaces, or linebreaks.
function csvString(cell, rowIndex, colIndex) {
var result = '';

if (cell !== null) {
var dataString = parseString(cell, rowIndex, colIndex);

var csvValue = (dataString === null || dataString === '') ? '' : dataString.toString();

if (dataString instanceof Date)
result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
else {
result = replaceAll(csvValue, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);

if (result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result))
result = defaults.csvEnclosure + result + defaults.csvEnclosure;
}
}

return result;
}

function parseNumber(value) {
value = value || "0";
value = replaceAll(value, defaults.numbers.html.decimalMark, '.');
Expand Down
Loading

0 comments on commit 13d930b

Please sign in to comment.