Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 062f036

Browse files
committed
Changes made to move toward supporting multiple tables to single Excel file. Stuck on how to reference all elements from selector in single call to plugin though.
1 parent 406350e commit 062f036

File tree

3 files changed

+117
-58
lines changed

3 files changed

+117
-58
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
node_modules
3+
/.workspace/

demo/index.html

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script src="../src/jquery.table2excel.js"></script>
77
</head>
88
<body>
9-
<table id="table2excel">
9+
<table class="table2excel" data-tableName="Test Table 1">
1010
<thead>
1111
<tr class="noExl"><td>This shouldn't get exported</td><td>This shouldn't get exported either</td></tr>
1212
<tr><td>This Should get exported as a header</td><td>This should too</td></tr>
@@ -19,11 +19,26 @@
1919
<tr><td colspan="2">This footer spans 2 cells</td></tr>
2020
</tfoot>
2121
</table>
22+
23+
<table class="table2excel" data-tableName="Test Table 2">
24+
<thead>
25+
<tr class="noExl"><td>This shouldn't get exported</td><td>This shouldn't get exported either</td></tr>
26+
<tr><td>This Should get exported as a header</td><td>This should too</td></tr>
27+
</thead>
28+
<tbody>
29+
<tr><td>data1a</td><td>data1b</td></tr>
30+
<tr><td>data2a</td><td>data2b</td></tr>
31+
</tbody>
32+
<tfoot>
33+
<tr><td colspan="2">This footer spans 2 cells</td></tr>
34+
</tfoot>
35+
</table>
36+
2237
<script>
2338
$(function() {
24-
$("#table2excel").table2excel({
39+
$(".table2excel").table2excel({
2540
exclude: ".noExl",
26-
name: "Excel Document Name"
41+
name: "Excel Document Name"
2742
});
2843
});
2944
</script>

src/jquery.table2excel.js

+98-55
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,109 @@
11
//table2excel.js
22
;(function ( $, window, document, undefined ) {
3-
var pluginName = "table2excel",
4-
defaults = {
5-
exclude: ".noExl",
6-
name: "Table2Excel"
7-
};
3+
var pluginName = "table2excel",
84

9-
// The actual plugin constructor
10-
function Plugin ( element, options ) {
11-
this.element = element;
12-
// jQuery has an extend method which merges the contents of two or
13-
// more objects, storing the result in the first object. The first object
14-
// is generally empty as we don't want to alter the default options for
15-
// future instances of the plugin
16-
this.settings = $.extend( {}, defaults, options );
17-
this._defaults = defaults;
18-
this._name = pluginName;
19-
this.init();
20-
}
5+
defaults = {
6+
exclude: ".noExl",
7+
name: "Table2Excel"
8+
};
219

22-
Plugin.prototype = {
23-
init: function () {
24-
var e = this;
25-
e.template = "<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><!--[if gte mso 9]><xml>";
26-
e.template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>";
27-
e.template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>";
28-
e.tableRows = "";
10+
// The actual plugin constructor
11+
function Plugin ( element, options ) {
12+
this.element = element;
13+
// jQuery has an extend method which merges the contents of two or
14+
// more objects, storing the result in the first object. The first object
15+
// is generally empty as we don't want to alter the default options for
16+
// future instances of the plugin
17+
//
18+
this.settings = $.extend( {}, defaults, options );
19+
this._defaults = defaults;
20+
this._name = pluginName;
21+
this.init();
22+
}
2923

30-
// get contents of table except for exclude
31-
$(e.element).find("tr").not(this.settings.exclude).each(function (i,o) {
32-
e.tableRows += "<tr>" + $(o).html() + "</tr>";
33-
});
34-
this.tableToExcel(this.tableRows, this.settings.name);
35-
},
36-
tableToExcel: function (table, name) {
37-
var e = this;
38-
e.uri = "data:application/vnd.ms-excel;base64,";
39-
e.base64 = function (s) {
40-
return window.btoa(unescape(encodeURIComponent(s)));
41-
};
42-
e.format = function (s, c) {
43-
return s.replace(/{(\w+)}/g, function (m, p) {
44-
return c[p];
45-
});
46-
};
47-
e.ctx = {
48-
worksheet: name || "Worksheet",
49-
table: table
50-
};
51-
window.location.href = e.uri + e.base64(e.format(e.template, e.ctx));
24+
Plugin.prototype = {
25+
init: function () {
26+
var e = this;
27+
e.template = {
28+
head: "<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><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>"
29+
,sheet: {
30+
head: "<x:ExcelWorksheet><x:Name>" //{worksheet}
31+
,tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
32+
}
33+
,mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>"
34+
,table: {
35+
head: "<table>" //{table}
36+
,tail: "</table>"
37+
}
38+
,foot: "</body></html>"
5239
}
53-
};
40+
e.tableRows = [];
41+
42+
// get contents of table except for exclude
43+
$(e.element).each( function(i,o) {
44+
var tempRows = "";
45+
$(o).find("tr").not(e.settings.exclude).each(function (i,o) {
46+
tempRows += "<tr>" + $(o).html() + "</tr>";
47+
});
48+
e.tableRows.push(tempRows);
49+
});
5450

55-
$.fn[ pluginName ] = function ( options ) {
56-
this.each(function() {
57-
if ( !$.data( this, "plugin_" + pluginName ) ) {
58-
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
59-
}
51+
52+
e.tableToExcel(e.tableRows, e.settings.name);
53+
},
54+
tableToExcel: function (table, name) {
55+
var e = this, fullTemplate="";
56+
e.uri = "data:application/vnd.ms-excel;base64,";
57+
e.base64 = function (s) {
58+
return window.btoa(unescape(encodeURIComponent(s)));
59+
};
60+
e.format = function (s, c) {
61+
return s.replace(/{(\w+)}/g, function (m, p) {
62+
return c[p];
6063
});
64+
};
65+
e.ctx = {
66+
worksheet: name || "Worksheet",
67+
table: table
68+
};
69+
70+
fullTemplate= e.template.head;
71+
72+
if ( $.isArray(table) ) {
73+
for (var i in table) {
74+
//fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
75+
fullTemplate += e.template.sheet.head + "Table" + i + "" + e.template.sheet.tail;
76+
}
77+
}
78+
79+
fullTemplate += e.template.mid;
80+
81+
if ( $.isArray(table) ) {
82+
for (var i in table) {
83+
fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
84+
}
85+
}
86+
87+
fullTemplate += e.template.foot;
88+
89+
for (var i in table) {
90+
e.ctx["table" + i] = table[i];
91+
}
92+
delete e.ctx.table;
93+
94+
window.location.href = e.uri + e.base64(e.format(fullTemplate, e.ctx));
95+
}
96+
};
97+
98+
$.fn[ pluginName ] = function ( options ) {
99+
this.each(function() {
100+
if ( !$.data( this, "plugin_" + pluginName ) ) {
101+
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
102+
}
103+
});
61104

62-
// chain jQuery functions
63-
return this;
64-
};
105+
// chain jQuery functions
106+
return this;
107+
};
65108

66109
})( jQuery, window, document );

0 commit comments

Comments
 (0)