Skip to content

Commit

Permalink
optional reforamt numbers during export
Browse files Browse the repository at this point in the history
new option "numbers" that defines decimal mark and thouseads seoparator
of html input and export output. If one of them differs numbers will be
reformated during export process.
  • Loading branch information
hhurz committed Aug 23, 2015
1 parent 0c9b183 commit 40b2f4c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 45 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ jspdf: orientation: 'p'
tableExport: onAfterAutotable: null
onBeforeAutotable: null
onTable: null
numbers: html: decimalMark: '.'
thousandsSeparator: ','
output: decimalMark: '.',
thousandsSeparator: ','
onCellData: null
outputMode: 'file'
tbodySelector: 'tr'
Expand Down
61 changes: 47 additions & 14 deletions tableExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@
ignoreColumn: [],
ignoreRow:[],
jspdf: {orientation: 'p',
unit: 'pt',
format: 'a4',
margins: {left: 20, right: 10, top: 10, bottom: 10},
autotable: {padding: 2,
lineHeight: 12,
fontSize: 8,
tableExport: {onAfterAutotable: null,
onBeforeAutotable: null,
onTable: null
}
}
},
unit: 'pt',
format: 'a4',
margins: {left: 20, right: 10, top: 10, bottom: 10},
autotable: {padding: 2,
lineHeight: 12,
fontSize: 8,
tableExport: {onAfterAutotable: null,
onBeforeAutotable: null,
onTable: null
}
}
},
numbers: {html: {decimalMark: '.',
thousandsSeparator: ','
},
output: {decimalMark: '.',
thousandsSeparator: ','
}
},
onCellData: null,
outputMode: 'file', // file|string|base64
tbodySelector: 'tr',
Expand Down Expand Up @@ -614,7 +621,7 @@
teOptions.doc = null;
}
}

function ForEachVisibleCell(tableRow, selector, rowIndex, cellcallback) {
if (defaults.ignoreRow.indexOf(rowIndex) == -1) {
$(tableRow).filter(':visible').find(selector).each(function (colIndex) {
Expand Down Expand Up @@ -715,6 +722,15 @@
return result;
}

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

return typeof value === "number" || jQuery.isNumeric(value) !== false ? value : false;
}


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

Expand All @@ -723,8 +739,25 @@

if (defaults.htmlContent === true) {
result = $cell.html().trim();
} else {
}
else {
result = $cell.text().trim().replace(/\u00AD/g, ""); // remove soft hyphens

if (defaults.numbers.html.decimalMark != defaults.numbers.output.decimalMark ||
defaults.numbers.html.thousandsSeparator != defaults.numbers.output.thousandsSeparator) {
var number = parseNumber (result);

if ( number !== false ) {
var frac = ("" + number).split('.');
if ( frac.length == 1 )
frac[1] = "";
var mod = frac[0].length > 3 ? frac[0].length % 3 : 0;

result = (number < 0 ? "-" : "") +
(defaults.numbers.output.thousandsSeparator ? ((mod ? frac[0].substr(0, mod) + defaults.numbers.output.thousandsSeparator : "") + frac[0].substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + defaults.numbers.output.thousandsSeparator)) : frac[0]) +
(frac[1].length ? defaults.numbers.output.decimalMark + frac[1] : "");
}
}
}

if (defaults.escape === true) {
Expand Down
Loading

0 comments on commit 40b2f4c

Please sign in to comment.