-
Notifications
You must be signed in to change notification settings - Fork 2
/
excellentexport.js
159 lines (137 loc) · 5.7 KB
/
excellentexport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* ExcellentExport 2.0.0
* A client side Javascript export to Excel.
*
* @author: Jordi Burgos (jordiburgos@gmail.com)
* @url: https://github.com/jmaister/excellentexport
*
*/
/*jslint browser: true, bitwise: true, vars: true, white: true */
/*global define, exports, module */
(function (global) {
'use strict';
var ExcellentExport = (function() {
function b64toBlob(b64Data, contentType, sliceSize) {
// function taken from http://stackoverflow.com/a/16245768/2591950
// author Jeremy Banks http://stackoverflow.com/users/1114/jeremy-banks
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = window.atob(b64Data);
var byteArrays = [];
var offset;
for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
var i;
for (i = 0; i < slice.length; i = i + 1) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new window.Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new window.Blob(byteArrays, {
type: contentType
});
return blob;
}
var version = "1.5.0";
var uri = {excel: 'data:application/vnd.ms-excel;base64,', csv: 'data:application/csv;base64,'};
var template = {excel: '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'};
var csvDelimiter = ",";
var csvNewLine = "\r\n";
var base64 = function(s) {
return window.btoa(window.unescape(encodeURIComponent(s)));
};
var format = function(s, c) {
return s.replace(new RegExp("{(\\w+)}", "g"), function(m, p) {
return c[p];
});
};
var get = function(element) {
if (!element.nodeType) {
return document.getElementById(element);
}
return element;
};
var fixCSVField = function(value) {
var fixedValue = value;
var addQuotes = (value.indexOf(csvDelimiter) !== -1) || (value.indexOf('\r') !== -1) || (value.indexOf('\n') !== -1);
var replaceDoubleQuotes = (value.indexOf('"') !== -1);
if (replaceDoubleQuotes) {
fixedValue = fixedValue.replace(/"/g, '""');
}
if (addQuotes || replaceDoubleQuotes) {
fixedValue = '"' + fixedValue + '"';
}
return fixedValue;
};
var tableToCSV = function(table) {
var data = "";
var i, j, row, col;
for (i = 0; i < table.rows.length; i=i+1) {
row = table.rows[i];
for (j = 0; j < row.cells.length; j=j+1) {
col = row.cells[j];
data = data + (j ? csvDelimiter : '') + fixCSVField(col.textContent.trim());
}
data = data + csvNewLine;
}
return data;
};
function createDownloadLink(anchor, base64data, exporttype, filename) {
var blob;
if (window.navigator.msSaveBlob) {
blob = b64toBlob(base64data, exporttype);
window.navigator.msSaveBlob(blob, filename);
return false;
} else if(window.URL.createObjectURL) {
blob = b64toBlob(base64data, exporttype);
var blobUrl = window.URL.createObjectURL(blob, exporttype, filename);
anchor.href = blobUrl;
} else {
var hrefvalue = "data:" + exporttype + ";base64," + base64data;
anchor.download = filename;
anchor.href = hrefvalue;
}
// Return true to allow the link to work
return true;
}
var ee = {
/** @export */
excel: function(anchor, table, name) {
table = get(table);
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
var b64 = base64(format(template.excel, ctx));
return createDownloadLink(anchor, b64, 'application/vnd.ms-excel','export.xls');
},
/** @export */
csv: function(anchor, table, delimiter, newLine) {
if (delimiter !== undefined && delimiter) {
csvDelimiter = delimiter;
}
if (newLine !== undefined && newLine) {
csvNewLine = newLine;
}
table = get(table);
var csvData = tableToCSV(table);
var b64 = base64(csvData);
return createDownloadLink(anchor,b64,'application/csv','export.csv');
}
};
return ee;
}());
// AMD support
if (typeof define === 'function' && define.amd) {
define(function () { return ExcellentExport; });
// CommonJS and Node.js module support.
} else if (typeof exports !== 'undefined') {
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = ExcellentExport;
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.ExcellentExport = ExcellentExport;
} else {
global.ExcellentExport = ExcellentExport;
}
})(this);