-
Notifications
You must be signed in to change notification settings - Fork 81
/
reflector.html
96 lines (89 loc) · 2.97 KB
/
reflector.html
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
<!-- reflector.html (C) 2014-present SheetJS - http://sheetjs.com -->
<!-- vim: set ts=2: -->
<pre id="out"></pre>
<div id="hot"></div>
<style>
table, td, th { border-width: 1px; border-style: dotted; }
td, th { white-space: pre; padding: 3px; }
</style>
<br />
<script src="assets/js/shim.js"></script>
<script src="assets/js/xlsx.full.min.js"></script>
<script>
function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {header:1});
if(roa.length > 0) result[sheetName] = roa;
});
return result;
}
function to_csv(workbook, FS) {
var result = [];
console.log(workbook);
workbook.SheetNames.forEach(function(sheetName) {
var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName], {FS:FS||","});
if(csv.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(csv);
}
});
return result.join("\n");
}
function to_html(wb) {
var json = to_json(wb);
wb.SheetNames.forEach(function(sheet) {
var tbl = document.createElement('div');
tbl.innerHTML = XLSX.utils.sheet_to_html(wb.Sheets[sheet]);
document.getElementById('hot').appendChild(tbl);
});
};
function process_wb(wb, fmt) {
var output = "";
switch(fmt) {
case "json": output = JSON.stringify(to_json(wb),2,2); break;
case "csv": output = to_csv(wb); break;
case "tsv": output = to_csv(wb, "\t"); break;
default: output = to_html(wb); return;
}
console.log(output);
var out = document.getElementById('out');
if(out.innerText === undefined) out.textContent = output;
else out.innerText = output;
}
/* from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript */
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var url=getParameterByName("file");
var fmt=getParameterByName("fmt");
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] = data[i];
var wb = XLSX.read(arr, {type:"array"});
console.log(wb);
process_wb(wb, fmt);
}
oReq.send();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36810333-1']);
_gaq.push(['_setDomainName', 'sheetjs.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>