From 23c0ca259107d0e97f05573acad8c98390460534 Mon Sep 17 00:00:00 2001 From: Travis Clarke Date: Wed, 9 Mar 2016 23:52:55 -0500 Subject: [PATCH] added single quote escaping fo xlsx format --- dist/js/tableexport.js | 119 ++++++++-------- dist/js/tableexport.min.js | 4 +- src/stable/js/tableexport.js | 119 ++++++++-------- src/stable/js/tableexport.min.js | 4 +- src/v3/tableexport-3.1.1.js | 233 +++++++++++++++++++++++++++++++ src/v3/tableexport-3.1.1.min.js | 6 + 6 files changed, 355 insertions(+), 130 deletions(-) create mode 100755 src/v3/tableexport-3.1.1.js create mode 100755 src/v3/tableexport-3.1.1.min.js diff --git a/dist/js/tableexport.js b/dist/js/tableexport.js index 177da35..1baf4d8 100755 --- a/dist/js/tableexport.js +++ b/dist/js/tableexport.js @@ -1,5 +1,5 @@ /*! - * TableExport.js v3.1.0 (http://www.clarketravis.com) + * TableExport.js v3.1.1 (http://www.clarketravis.com) * Copyright 2015 Travis Clarke * Licensed under the MIT license */ @@ -39,7 +39,7 @@ dataObject = JSON.stringify({ data: dataURL, name: name - }), + }).replace(/'/g, "'"), myFile = name + ".xlsx", myContent = $.fn.tableExport.xlsx.buttonContent, myClass = $.fn.tableExport.xlsx.defaultClass; @@ -168,73 +168,66 @@ $.fn.tableExport.rowDel = "\r\n"; + function dateNum(v, date1904) { + if (date1904) v += 1462; + var epoch = Date.parse(v); + return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); + } -}(window)); - -function dateNum(v, date1904) { - if (date1904) v += 1462; - var epoch = Date.parse(v); - return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); -} - -function createSheet(data, opts) { - var ws = {}; - var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; - for (var R = 0; R != data.length; ++R) { - for (var C = 0; C != data[R].length; ++C) { - if (range.s.r > R) range.s.r = R; - if (range.s.c > C) range.s.c = C; - if (range.e.r < R) range.e.r = R; - if (range.e.c < C) range.e.c = C; - var cell = {v: data[R][C]}; - if (cell.v == null) continue; - var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); - - if (typeof cell.v === 'number') cell.t = 'n'; - else if (typeof cell.v === 'boolean') cell.t = 'b'; - else if (cell.v instanceof Date) { - cell.t = 'n'; - cell.z = XLSX.SSF._table[14]; - cell.v = dateNum(cell.v); - } - else cell.t = 's'; + function createSheet(data, opts) { + var ws = {}; + var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; + for (var R = 0; R != data.length; ++R) { + for (var C = 0; C != data[R].length; ++C) { + if (range.s.r > R) range.s.r = R; + if (range.s.c > C) range.s.c = C; + if (range.e.r < R) range.e.r = R; + if (range.e.c < C) range.e.c = C; + var cell = {v: data[R][C]}; + if (cell.v == null) continue; + var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); + + if (typeof cell.v === 'number') cell.t = 'n'; + else if (typeof cell.v === 'boolean') cell.t = 'b'; + else if (cell.v instanceof Date) { + cell.t = 'n'; + cell.z = XLSX.SSF._table[14]; + cell.v = dateNum(cell.v); + } + else cell.t = 's'; - ws[cell_ref] = cell; + ws[cell_ref] = cell; + } } + if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); + return ws; } - if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); - return ws; -} - -function Workbook() { - if (!(this instanceof Workbook)) return new Workbook(); - this.SheetNames = []; - this.Sheets = {}; -} - -function string2ArrayBuffer(s) { - var buf = new ArrayBuffer(s.length); - var view = new Uint8Array(buf); - for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; - return buf; -} - -function export2xlsx(data, name) { - var wb = new Workbook(), - ws = createSheet(data); - - wb.SheetNames.push(name); - wb.Sheets[name] = ws; - - var wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}; - var wbout = XLSX.write(wb, wopts); - - saveAs(new Blob([string2ArrayBuffer(wbout)], - {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), - name + ".xlsx"); -} + function Workbook() { + if (!(this instanceof Workbook)) return new Workbook(); + this.SheetNames = []; + this.Sheets = {}; + } + + function string2ArrayBuffer(s) { + var buf = new ArrayBuffer(s.length); + var view = new Uint8Array(buf); + for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; + return buf; + } + function export2xlsx(data, name) { + var wb = new Workbook(), + ws = createSheet(data); + wb.SheetNames.push(name); + wb.Sheets[name] = ws; + var wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}; + var wbout = XLSX.write(wb, wopts); + saveAs(new Blob([string2ArrayBuffer(wbout)], + {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), + name + ".xlsx"); + } +}(window)); \ No newline at end of file diff --git a/dist/js/tableexport.min.js b/dist/js/tableexport.min.js index e0c8a09..0285478 100755 --- a/dist/js/tableexport.min.js +++ b/dist/js/tableexport.min.js @@ -1,6 +1,6 @@ /*! - * TableExport.js v3.1.0 (http://www.clarketravis.com) + * TableExport.js v3.1.1 (http://www.clarketravis.com) * Copyright 2015 Travis Clarke * Licensed under the MIT license */ -function dateNum(t,e){e&&(t+=1462);var n=Date.parse(t);return(n-new Date(Date.UTC(1899,11,30)))/864e5}function createSheet(t,e){for(var n={},a={s:{c:1e7,r:1e7},e:{c:0,r:0}},o=0;o!=t.length;++o)for(var r=0;r!=t[o].length;++r){a.s.r>o&&(a.s.r=o),a.s.c>r&&(a.s.c=r),a.e.r'+t+"")}function s(n,o,r,l){var s=""+r+"";t(s)}function f(n,o,r,l){var s=""+r+"";t(s),i(l)}function i(t){var e="."+t;return n(e).on("click",function(t){if(n(this).data("obj")){t.preventDefault();var e=n(this).data("obj"),a=e.data,o=e.name;export2xlsx(a,o)}})}var u=n(this),p=r.headings?u.find("tr"):u.find("tr:has(td)"),x="id"===r.fileName?u.attr("id")?u.attr("id"):n.fn.tableExport.defaultFileName:r.fileName,c={xlsx:function(t,e){var a=p.map(function(t,e){var a=n(e).find("th, td");return a.map(function(t,e){return n(e).text()})}).get(),o=JSON.stringify({data:a,name:e}),r=e+".xlsx",l=n.fn.tableExport.xlsx.buttonContent,s=n.fn.tableExport.xlsx.defaultClass;f(o,r,l,s)},xls:function(t,e){var a=n.fn.tableExport.xls.separator,o="data:application/vnd.ms-excel;charset=utf-16,"+encodeURIComponent(p.map(function(t,e){var o=n(e).find("th, td");return o.map(function(t,e){return n(e).html()}).get().join(a)}).get().join(t)),r=e+".xls",l=n.fn.tableExport.xls.buttonContent,f=n.fn.tableExport.xls.defaultClass;s(o,r,l,f)},csv:function(t,e){t='"'+t+'"';var a='"'+n.fn.tableExport.csv.separator+'"',o="data:text/csv;charset=utf-16,"+encodeURIComponent('"'+p.map(function(t,e){var o=n(e).find("th, td");return o.map(function(t,e){return n(e).text().replace(/"/g,'""')}).get().join(a)}).get().join(t)+'"'),r=e+".csv",l=n.fn.tableExport.csv.buttonContent,f=n.fn.tableExport.csv.defaultClass;s(o,r,l,f)},txt:function(t,e){var a=n.fn.tableExport.txt.separator,o="data:text/plain;charset=utf-16,"+encodeURIComponent(p.map(function(t,e){var o=n(e).find("th, td");return o.map(function(t,e){return n(e).text()}).get().join(a)}).get().join(t)),r=e+".txt",l=n.fn.tableExport.txt.buttonContent,f=n.fn.tableExport.txt.defaultClass;s(o,r,l,f)}};r.formats.forEach(function(t){c[t](l,x)})})},n.fn.tableExport.defaults={headings:!0,formats:["xls","csv","txt"],fileName:"id",bootstrap:!0,position:"bottom"},n.fn.tableExport.xlsx={defaultClass:"xlsx",buttonContent:"Export to xlsx"},n.fn.tableExport.xls={defaultClass:"xls",buttonContent:"Export to xls",separator:" "},n.fn.tableExport.csv={defaultClass:"csv",buttonContent:"Export to csv",separator:","},n.fn.tableExport.txt={defaultClass:"txt",buttonContent:"Export to txt",separator:" "},n.fn.tableExport.defaultFileName="myDownload",n.fn.tableExport.defaultButton="button-default",n.fn.tableExport.bootstrap=["btn","btn-default","btn-toolbar"],n.fn.tableExport.rowDel="\r\n"}(window); \ No newline at end of file +!function(t,n){function e(t,n){n&&(t+=1462);var e=Date.parse(t);return(e-new Date(Date.UTC(1899,11,30)))/864e5}function a(t,n){for(var a={},o={s:{c:1e7,r:1e7},e:{c:0,r:0}},r=0;r!=t.length;++r)for(var l=0;l!=t[r].length;++l){o.s.r>r&&(o.s.r=r),o.s.c>l&&(o.s.c=l),o.e.r'+t+"")}function f(a,o,r,l){var s=""+r+"";t(s)}function i(a,o,r,l){var s=""+r+"";t(s),p(l)}function p(t){var n="."+t;return s(n).on("click",function(t){if(s(this).data("obj")){t.preventDefault();var n=s(this).data("obj"),e=n.data,a=n.name;l(e,a)}})}var u=s(this),x=o.headings?u.find("tr"):u.find("tr:has(td)"),c="id"===o.fileName?u.attr("id")?u.attr("id"):s.fn.tableExport.defaultFileName:o.fileName,d={xlsx:function(t,n){var e=x.map(function(t,n){var e=s(n).find("th, td");return e.map(function(t,n){return s(n).text()})}).get(),a=JSON.stringify({data:e,name:n}).replace(/'/g,"'"),o=n+".xlsx",r=s.fn.tableExport.xlsx.buttonContent,l=s.fn.tableExport.xlsx.defaultClass;i(a,o,r,l)},xls:function(t,n){var e=s.fn.tableExport.xls.separator,a="data:application/vnd.ms-excel;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).html()}).get().join(e)}).get().join(t)),o=n+".xls",r=s.fn.tableExport.xls.buttonContent,l=s.fn.tableExport.xls.defaultClass;f(a,o,r,l)},csv:function(t,n){t='"'+t+'"';var e='"'+s.fn.tableExport.csv.separator+'"',a="data:text/csv;charset=utf-16,"+encodeURIComponent('"'+x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text().replace(/"/g,'""')}).get().join(e)}).get().join(t)+'"'),o=n+".csv",r=s.fn.tableExport.csv.buttonContent,l=s.fn.tableExport.csv.defaultClass;f(a,o,r,l)},txt:function(t,n){var e=s.fn.tableExport.txt.separator,a="data:text/plain;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text()}).get().join(e)}).get().join(t)),o=n+".txt",r=s.fn.tableExport.txt.buttonContent,l=s.fn.tableExport.txt.defaultClass;f(a,o,r,l)}};o.formats.forEach(function(t){d[t](r,c)})})},s.fn.tableExport.defaults={headings:!0,formats:["xls","csv","txt"],fileName:"id",bootstrap:!0,position:"bottom"},s.fn.tableExport.xlsx={defaultClass:"xlsx",buttonContent:"Export to xlsx"},s.fn.tableExport.xls={defaultClass:"xls",buttonContent:"Export to xls",separator:" "},s.fn.tableExport.csv={defaultClass:"csv",buttonContent:"Export to csv",separator:","},s.fn.tableExport.txt={defaultClass:"txt",buttonContent:"Export to txt",separator:" "},s.fn.tableExport.defaultFileName="myDownload",s.fn.tableExport.defaultButton="button-default",s.fn.tableExport.bootstrap=["btn","btn-default","btn-toolbar"],s.fn.tableExport.rowDel="\r\n"}(window); \ No newline at end of file diff --git a/src/stable/js/tableexport.js b/src/stable/js/tableexport.js index 177da35..1baf4d8 100755 --- a/src/stable/js/tableexport.js +++ b/src/stable/js/tableexport.js @@ -1,5 +1,5 @@ /*! - * TableExport.js v3.1.0 (http://www.clarketravis.com) + * TableExport.js v3.1.1 (http://www.clarketravis.com) * Copyright 2015 Travis Clarke * Licensed under the MIT license */ @@ -39,7 +39,7 @@ dataObject = JSON.stringify({ data: dataURL, name: name - }), + }).replace(/'/g, "'"), myFile = name + ".xlsx", myContent = $.fn.tableExport.xlsx.buttonContent, myClass = $.fn.tableExport.xlsx.defaultClass; @@ -168,73 +168,66 @@ $.fn.tableExport.rowDel = "\r\n"; + function dateNum(v, date1904) { + if (date1904) v += 1462; + var epoch = Date.parse(v); + return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); + } -}(window)); - -function dateNum(v, date1904) { - if (date1904) v += 1462; - var epoch = Date.parse(v); - return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); -} - -function createSheet(data, opts) { - var ws = {}; - var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; - for (var R = 0; R != data.length; ++R) { - for (var C = 0; C != data[R].length; ++C) { - if (range.s.r > R) range.s.r = R; - if (range.s.c > C) range.s.c = C; - if (range.e.r < R) range.e.r = R; - if (range.e.c < C) range.e.c = C; - var cell = {v: data[R][C]}; - if (cell.v == null) continue; - var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); - - if (typeof cell.v === 'number') cell.t = 'n'; - else if (typeof cell.v === 'boolean') cell.t = 'b'; - else if (cell.v instanceof Date) { - cell.t = 'n'; - cell.z = XLSX.SSF._table[14]; - cell.v = dateNum(cell.v); - } - else cell.t = 's'; + function createSheet(data, opts) { + var ws = {}; + var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; + for (var R = 0; R != data.length; ++R) { + for (var C = 0; C != data[R].length; ++C) { + if (range.s.r > R) range.s.r = R; + if (range.s.c > C) range.s.c = C; + if (range.e.r < R) range.e.r = R; + if (range.e.c < C) range.e.c = C; + var cell = {v: data[R][C]}; + if (cell.v == null) continue; + var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); + + if (typeof cell.v === 'number') cell.t = 'n'; + else if (typeof cell.v === 'boolean') cell.t = 'b'; + else if (cell.v instanceof Date) { + cell.t = 'n'; + cell.z = XLSX.SSF._table[14]; + cell.v = dateNum(cell.v); + } + else cell.t = 's'; - ws[cell_ref] = cell; + ws[cell_ref] = cell; + } } + if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); + return ws; } - if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); - return ws; -} - -function Workbook() { - if (!(this instanceof Workbook)) return new Workbook(); - this.SheetNames = []; - this.Sheets = {}; -} - -function string2ArrayBuffer(s) { - var buf = new ArrayBuffer(s.length); - var view = new Uint8Array(buf); - for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; - return buf; -} - -function export2xlsx(data, name) { - var wb = new Workbook(), - ws = createSheet(data); - - wb.SheetNames.push(name); - wb.Sheets[name] = ws; - - var wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}; - var wbout = XLSX.write(wb, wopts); - - saveAs(new Blob([string2ArrayBuffer(wbout)], - {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), - name + ".xlsx"); -} + function Workbook() { + if (!(this instanceof Workbook)) return new Workbook(); + this.SheetNames = []; + this.Sheets = {}; + } + + function string2ArrayBuffer(s) { + var buf = new ArrayBuffer(s.length); + var view = new Uint8Array(buf); + for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; + return buf; + } + function export2xlsx(data, name) { + var wb = new Workbook(), + ws = createSheet(data); + wb.SheetNames.push(name); + wb.Sheets[name] = ws; + var wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}; + var wbout = XLSX.write(wb, wopts); + saveAs(new Blob([string2ArrayBuffer(wbout)], + {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), + name + ".xlsx"); + } +}(window)); \ No newline at end of file diff --git a/src/stable/js/tableexport.min.js b/src/stable/js/tableexport.min.js index a3cda0f..0285478 100755 --- a/src/stable/js/tableexport.min.js +++ b/src/stable/js/tableexport.min.js @@ -1,6 +1,6 @@ /*! - * TableExport.js v3.1.0 (http://www.clarketravis.com) + * TableExport.js v3.1.1 (http://www.clarketravis.com) * Copyright 2015 Travis Clarke * Licensed under the MIT license */ -;(function(a,c){var b=a.jQuery;b.fn.tableExport=function(f){var g=b.extend({},b.fn.tableExport.defaults,f),i=b.fn.tableExport.rowDel,h,d,e;if(g.bootstrap){h=b.fn.tableExport.bootstrap[0]+" ";d=b.fn.tableExport.bootstrap[1]+" ";e=b.fn.tableExport.bootstrap[2]+" "}else{h=b.fn.tableExport.defaultButton+" ";d=e=""}return this.each(function(){var n=b(this),l=g.headings?n.find("tr"):n.find("tr:has(td)"),q=g.fileName==="id"?(n.attr("id")?n.attr("id"):b.fn.tableExport.defaultFileName):g.fileName,o={xlsx:function(w,r){var x=l.map(function(z,A){var y=b(A).find("th, td");return y.map(function(B,C){return b(C).text()})}).get(),v=JSON.stringify({data:x,name:r}),u=r+".xlsx",t=b.fn.tableExport.xlsx.buttonContent,s=b.fn.tableExport.xlsx.defaultClass;p(v,u,t,s)},xls:function(s,t){var r=b.fn.tableExport.xls.separator,x="data:application/vnd.ms-excel;charset=utf-16,"+encodeURIComponent(l.map(function(z,A){var y=b(A).find("th, td");return y.map(function(B,C){return b(C).html()}).get().join(r)}).get().join(s)),w=t+".xls",v=b.fn.tableExport.xls.buttonContent,u=b.fn.tableExport.xls.defaultClass;j(x,w,v,u)},csv:function(s,t){s='"'+s+'"';var r='"'+b.fn.tableExport.csv.separator+'"',x="data:text/csv;charset=utf-16,"+encodeURIComponent('"'+l.map(function(z,A){var y=b(A).find("th, td");return y.map(function(B,C){return b(C).text().replace(/"/g,'""')}).get().join(r)}).get().join(s)+'"'),w=t+".csv",v=b.fn.tableExport.csv.buttonContent,u=b.fn.tableExport.csv.defaultClass;j(x,w,v,u)},txt:function(s,t){var r=b.fn.tableExport.txt.separator,x="data:text/plain;charset=utf-16,"+encodeURIComponent(l.map(function(z,A){var y=b(A).find("th, td");return y.map(function(B,C){return b(C).text()}).get().join(r)}).get().join(s)),w=t+".txt",v=b.fn.tableExport.txt.buttonContent,u=b.fn.tableExport.txt.defaultClass;j(x,w,v,u)}};g.formats.forEach(function(r){o[r](i,q)});function k(r){var s=n.find("caption:not(.head)");s.length?s.append(r):n.prepend(''+r+"")}function j(v,t,s,r){var u=""+s+"";k(u)}function p(u,t,s,r){var v=""+s+"";k(v);m(r)}function m(s){var r="."+s;return b(r).on("click",function(v){if(b(this).data("obj")){v.preventDefault();var t=b(this).data("obj"),u=t.data,w=t.name;export2xlsx(u,w)}})}})};b.fn.tableExport.defaults={headings:true,formats:["xls","csv","txt"],fileName:"id",bootstrap:true,position:"bottom"};b.fn.tableExport.xlsx={defaultClass:"xlsx",buttonContent:"Export to xlsx"};b.fn.tableExport.xls={defaultClass:"xls",buttonContent:"Export to xls",separator:"\t"};b.fn.tableExport.csv={defaultClass:"csv",buttonContent:"Export to csv",separator:","};b.fn.tableExport.txt={defaultClass:"txt",buttonContent:"Export to txt",separator:" "};b.fn.tableExport.defaultFileName="myDownload";b.fn.tableExport.defaultButton="button-default";b.fn.tableExport.bootstrap=["btn","btn-default","btn-toolbar"];b.fn.tableExport.rowDel="\r\n"}(window));function dateNum(b,a){if(a){b+=1462}var c=Date.parse(b);return(c-new Date(Date.UTC(1899,11,30)))/(24*60*60*1000)}function createSheet(g,f){var b={};var c={s:{c:10000000,r:10000000},e:{c:0,r:0}};for(var e=0;e!=g.length;++e){for(var h=0;h!=g[e].length;++h){if(c.s.r>e){c.s.r=e}if(c.s.c>h){c.s.c=h}if(c.e.rr&&(o.s.r=r),o.s.c>l&&(o.s.c=l),o.e.r'+t+"")}function f(a,o,r,l){var s=""+r+"";t(s)}function i(a,o,r,l){var s=""+r+"";t(s),p(l)}function p(t){var n="."+t;return s(n).on("click",function(t){if(s(this).data("obj")){t.preventDefault();var n=s(this).data("obj"),e=n.data,a=n.name;l(e,a)}})}var u=s(this),x=o.headings?u.find("tr"):u.find("tr:has(td)"),c="id"===o.fileName?u.attr("id")?u.attr("id"):s.fn.tableExport.defaultFileName:o.fileName,d={xlsx:function(t,n){var e=x.map(function(t,n){var e=s(n).find("th, td");return e.map(function(t,n){return s(n).text()})}).get(),a=JSON.stringify({data:e,name:n}).replace(/'/g,"'"),o=n+".xlsx",r=s.fn.tableExport.xlsx.buttonContent,l=s.fn.tableExport.xlsx.defaultClass;i(a,o,r,l)},xls:function(t,n){var e=s.fn.tableExport.xls.separator,a="data:application/vnd.ms-excel;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).html()}).get().join(e)}).get().join(t)),o=n+".xls",r=s.fn.tableExport.xls.buttonContent,l=s.fn.tableExport.xls.defaultClass;f(a,o,r,l)},csv:function(t,n){t='"'+t+'"';var e='"'+s.fn.tableExport.csv.separator+'"',a="data:text/csv;charset=utf-16,"+encodeURIComponent('"'+x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text().replace(/"/g,'""')}).get().join(e)}).get().join(t)+'"'),o=n+".csv",r=s.fn.tableExport.csv.buttonContent,l=s.fn.tableExport.csv.defaultClass;f(a,o,r,l)},txt:function(t,n){var e=s.fn.tableExport.txt.separator,a="data:text/plain;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text()}).get().join(e)}).get().join(t)),o=n+".txt",r=s.fn.tableExport.txt.buttonContent,l=s.fn.tableExport.txt.defaultClass;f(a,o,r,l)}};o.formats.forEach(function(t){d[t](r,c)})})},s.fn.tableExport.defaults={headings:!0,formats:["xls","csv","txt"],fileName:"id",bootstrap:!0,position:"bottom"},s.fn.tableExport.xlsx={defaultClass:"xlsx",buttonContent:"Export to xlsx"},s.fn.tableExport.xls={defaultClass:"xls",buttonContent:"Export to xls",separator:" "},s.fn.tableExport.csv={defaultClass:"csv",buttonContent:"Export to csv",separator:","},s.fn.tableExport.txt={defaultClass:"txt",buttonContent:"Export to txt",separator:" "},s.fn.tableExport.defaultFileName="myDownload",s.fn.tableExport.defaultButton="button-default",s.fn.tableExport.bootstrap=["btn","btn-default","btn-toolbar"],s.fn.tableExport.rowDel="\r\n"}(window); \ No newline at end of file diff --git a/src/v3/tableexport-3.1.1.js b/src/v3/tableexport-3.1.1.js new file mode 100755 index 0000000..1baf4d8 --- /dev/null +++ b/src/v3/tableexport-3.1.1.js @@ -0,0 +1,233 @@ +/*! + * TableExport.js v3.1.1 (http://www.clarketravis.com) + * Copyright 2015 Travis Clarke + * Licensed under the MIT license + */ + +;(function (window, undefined) { + + /*--- GLOBALS ---*/ + var $ = window.jQuery; + + $.fn.tableExport = function (options) { + + var settings = $.extend({}, $.fn.tableExport.defaults, options), + rowD = $.fn.tableExport.rowDel, bootstrapClass, bootstrapTheme, bootstrapSpacing; + + if (settings.bootstrap) { + bootstrapClass = $.fn.tableExport.bootstrap[0] + " "; + bootstrapTheme = $.fn.tableExport.bootstrap[1] + " "; + bootstrapSpacing = $.fn.tableExport.bootstrap[2] + " "; + } else { + bootstrapClass = $.fn.tableExport.defaultButton + " "; + bootstrapTheme = bootstrapSpacing = ""; + } + + + return this.each(function () { + var $el = $(this), + $rows = settings.headings ? $el.find('tr') : $el.find('tr:has(td)'), + fileName = settings.fileName === "id" ? ($el.attr('id') ? $el.attr('id') : $.fn.tableExport.defaultFileName) : settings.fileName, + exporters = { + xlsx: function (rDel, name) { + var dataURL = $rows.map(function (i, val) { + var $cols = $(val).find('th, td'); + return $cols.map(function (i, val) { + return $(val).text() + }); + }).get(), + dataObject = JSON.stringify({ + data: dataURL, + name: name + }).replace(/'/g, "'"), + myFile = name + ".xlsx", + myContent = $.fn.tableExport.xlsx.buttonContent, + myClass = $.fn.tableExport.xlsx.defaultClass; + createObjButton(dataObject, myFile, myContent, myClass); + }, + xls: function (rdel, name) { + var colD = $.fn.tableExport.xls.separator, + dataURL = 'data:application/vnd.ms-excel;charset=utf-16,' + + encodeURIComponent($rows.map(function (i, val) { + var $cols = $(val).find('th, td'); + return $cols.map(function (i, val) { + return $(val).html() + }).get().join(colD); + }).get().join(rdel)), + myFile = name + ".xls", + myContent = $.fn.tableExport.xls.buttonContent, + myClass = $.fn.tableExport.xls.defaultClass; + createButton(dataURL, myFile, myContent, myClass); + }, + csv: function (rdel, name) { + rdel = '"' + rdel + '"'; + var colD = '"' + $.fn.tableExport.csv.separator + '"', + dataURL = 'data:text/csv;charset=utf-16,' + + encodeURIComponent('"' + $rows.map(function (i, val) { + var $cols = $(val).find('th, td'); + return $cols.map(function (i, val) { + return $(val).text().replace(/"/g, '""') + }).get().join(colD); + }).get().join(rdel) + '"'), + myFile = name + ".csv", + myContent = $.fn.tableExport.csv.buttonContent, + myClass = $.fn.tableExport.csv.defaultClass; + createButton(dataURL, myFile, myContent, myClass); + }, + txt: function (rdel, name) { + var colD = $.fn.tableExport.txt.separator, + dataURL = 'data:text/plain;charset=utf-16,' + + encodeURIComponent($rows.map(function (i, val) { + var $cols = $(val).find('th, td'); + return $cols.map(function (i, val) { + return $(val).text() + }).get().join(colD); + }).get().join(rdel)), + myFile = name + ".txt", + myContent = $.fn.tableExport.txt.buttonContent, + myClass = $.fn.tableExport.txt.defaultClass; + createButton(dataURL, myFile, myContent, myClass); + } + }; + + settings.formats.forEach( + function (key) { + exporters[key](rowD, fileName); + } + ); + + function checkCaption(exportButton) { + var $caption = $el.find('caption:not(.head)'); + $caption.length ? $caption.append(exportButton) : $el.prepend('' + exportButton + ''); + } + + function createButton(dataURL, myFile, myContent, myClass) { + var exportButton = "" + myContent + ""; + checkCaption(exportButton); + } + + function createObjButton(dataObject, myFile, myContent, myClass) { + var exportButton = "" + myContent + ""; + checkCaption(exportButton); + addListener(myClass) + } + + function addListener(el) { + var $el = "." + el; + return $($el).on("click", function (e) { + if ($(this).data("obj")) { + e.preventDefault(); + var object = $(this).data("obj"), + data = object.data, + fileName = object.name; + export2xlsx(data, fileName); + } + }); + } + + }); + }; + + // Define the plugin default properties. + $.fn.tableExport.defaults = { + headings: true, // (Boolean), display table headings (th elements) in the first row, (default: true) + formats: ["xls", "csv", "txt"], // (String[]), filetype for the export, (default: ["xls", "csv", "txt"]) + fileName: "id", // (id, String), filename for the downloaded file, (default: "id") + bootstrap: true, // (Boolean), style buttons using bootstrap, (default: true) + position: "bottom" // (top, bottom), position of the caption element relative to table, (default: "bottom") + }; + + $.fn.tableExport.xlsx = { + defaultClass: "xlsx", + buttonContent: "Export to xlsx" + }; + + $.fn.tableExport.xls = { + defaultClass: "xls", + buttonContent: "Export to xls", + separator: "\t" + }; + + $.fn.tableExport.csv = { + defaultClass: "csv", + buttonContent: "Export to csv", + separator: "," + }; + + $.fn.tableExport.txt = { + defaultClass: "txt", + buttonContent: "Export to txt", + separator: " " + }; + + $.fn.tableExport.defaultFileName = "myDownload"; + + $.fn.tableExport.defaultButton = "button-default"; + + $.fn.tableExport.bootstrap = ["btn", "btn-default", "btn-toolbar"]; + + $.fn.tableExport.rowDel = "\r\n"; + + function dateNum(v, date1904) { + if (date1904) v += 1462; + var epoch = Date.parse(v); + return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); + } + + function createSheet(data, opts) { + var ws = {}; + var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; + for (var R = 0; R != data.length; ++R) { + for (var C = 0; C != data[R].length; ++C) { + if (range.s.r > R) range.s.r = R; + if (range.s.c > C) range.s.c = C; + if (range.e.r < R) range.e.r = R; + if (range.e.c < C) range.e.c = C; + var cell = {v: data[R][C]}; + if (cell.v == null) continue; + var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); + + if (typeof cell.v === 'number') cell.t = 'n'; + else if (typeof cell.v === 'boolean') cell.t = 'b'; + else if (cell.v instanceof Date) { + cell.t = 'n'; + cell.z = XLSX.SSF._table[14]; + cell.v = dateNum(cell.v); + } + else cell.t = 's'; + + ws[cell_ref] = cell; + } + } + if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); + return ws; + } + + function Workbook() { + if (!(this instanceof Workbook)) return new Workbook(); + this.SheetNames = []; + this.Sheets = {}; + } + + function string2ArrayBuffer(s) { + var buf = new ArrayBuffer(s.length); + var view = new Uint8Array(buf); + for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; + return buf; + } + + function export2xlsx(data, name) { + var wb = new Workbook(), + ws = createSheet(data); + + wb.SheetNames.push(name); + wb.Sheets[name] = ws; + + var wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}; + var wbout = XLSX.write(wb, wopts); + + saveAs(new Blob([string2ArrayBuffer(wbout)], + {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), + name + ".xlsx"); + } +}(window)); \ No newline at end of file diff --git a/src/v3/tableexport-3.1.1.min.js b/src/v3/tableexport-3.1.1.min.js new file mode 100755 index 0000000..0285478 --- /dev/null +++ b/src/v3/tableexport-3.1.1.min.js @@ -0,0 +1,6 @@ +/*! + * TableExport.js v3.1.1 (http://www.clarketravis.com) + * Copyright 2015 Travis Clarke + * Licensed under the MIT license + */ +!function(t,n){function e(t,n){n&&(t+=1462);var e=Date.parse(t);return(e-new Date(Date.UTC(1899,11,30)))/864e5}function a(t,n){for(var a={},o={s:{c:1e7,r:1e7},e:{c:0,r:0}},r=0;r!=t.length;++r)for(var l=0;l!=t[r].length;++l){o.s.r>r&&(o.s.r=r),o.s.c>l&&(o.s.c=l),o.e.r'+t+"")}function f(a,o,r,l){var s=""+r+"";t(s)}function i(a,o,r,l){var s=""+r+"";t(s),p(l)}function p(t){var n="."+t;return s(n).on("click",function(t){if(s(this).data("obj")){t.preventDefault();var n=s(this).data("obj"),e=n.data,a=n.name;l(e,a)}})}var u=s(this),x=o.headings?u.find("tr"):u.find("tr:has(td)"),c="id"===o.fileName?u.attr("id")?u.attr("id"):s.fn.tableExport.defaultFileName:o.fileName,d={xlsx:function(t,n){var e=x.map(function(t,n){var e=s(n).find("th, td");return e.map(function(t,n){return s(n).text()})}).get(),a=JSON.stringify({data:e,name:n}).replace(/'/g,"'"),o=n+".xlsx",r=s.fn.tableExport.xlsx.buttonContent,l=s.fn.tableExport.xlsx.defaultClass;i(a,o,r,l)},xls:function(t,n){var e=s.fn.tableExport.xls.separator,a="data:application/vnd.ms-excel;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).html()}).get().join(e)}).get().join(t)),o=n+".xls",r=s.fn.tableExport.xls.buttonContent,l=s.fn.tableExport.xls.defaultClass;f(a,o,r,l)},csv:function(t,n){t='"'+t+'"';var e='"'+s.fn.tableExport.csv.separator+'"',a="data:text/csv;charset=utf-16,"+encodeURIComponent('"'+x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text().replace(/"/g,'""')}).get().join(e)}).get().join(t)+'"'),o=n+".csv",r=s.fn.tableExport.csv.buttonContent,l=s.fn.tableExport.csv.defaultClass;f(a,o,r,l)},txt:function(t,n){var e=s.fn.tableExport.txt.separator,a="data:text/plain;charset=utf-16,"+encodeURIComponent(x.map(function(t,n){var a=s(n).find("th, td");return a.map(function(t,n){return s(n).text()}).get().join(e)}).get().join(t)),o=n+".txt",r=s.fn.tableExport.txt.buttonContent,l=s.fn.tableExport.txt.defaultClass;f(a,o,r,l)}};o.formats.forEach(function(t){d[t](r,c)})})},s.fn.tableExport.defaults={headings:!0,formats:["xls","csv","txt"],fileName:"id",bootstrap:!0,position:"bottom"},s.fn.tableExport.xlsx={defaultClass:"xlsx",buttonContent:"Export to xlsx"},s.fn.tableExport.xls={defaultClass:"xls",buttonContent:"Export to xls",separator:" "},s.fn.tableExport.csv={defaultClass:"csv",buttonContent:"Export to csv",separator:","},s.fn.tableExport.txt={defaultClass:"txt",buttonContent:"Export to txt",separator:" "},s.fn.tableExport.defaultFileName="myDownload",s.fn.tableExport.defaultButton="button-default",s.fn.tableExport.bootstrap=["btn","btn-default","btn-toolbar"],s.fn.tableExport.rowDel="\r\n"}(window); \ No newline at end of file