Skip to content
This repository has been archived by the owner on Jul 25, 2021. It is now read-only.

Commit

Permalink
added single quote escaping fo xlsx format
Browse files Browse the repository at this point in the history
  • Loading branch information
clarketm committed Mar 10, 2016
1 parent b2d312f commit 23c0ca2
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 130 deletions.
119 changes: 56 additions & 63 deletions dist/js/tableexport.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
4 changes: 2 additions & 2 deletions dist/js/tableexport.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 56 additions & 63 deletions src/stable/js/tableexport.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -39,7 +39,7 @@
dataObject = JSON.stringify({
data: dataURL,
name: name
}),
}).replace(/'/g, "&#39;"),
myFile = name + ".xlsx",
myContent = $.fn.tableExport.xlsx.buttonContent,
myClass = $.fn.tableExport.xlsx.defaultClass;
Expand Down Expand Up @@ -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));
Loading

0 comments on commit 23c0ca2

Please sign in to comment.