-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxlsx_exporter.html
86 lines (76 loc) · 2.35 KB
/
xlsx_exporter.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
<!DOCTYPE html>
<html>
<head>
<title>Export to Excel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.2.7/alasql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>
<script type="text/javascript">
var dashboardname;
var exportData;
var sheetNames;
var doneSheets;
var sheets;
function getCurrentViz() {
return parent.parent.tableau.VizManager.getVizs()[0];
};
function exportToExcel(){
dashboardname = '';
exportData = [];
sheetNames = [];
doneSheets = 0;
sheets = [];
var viz = getCurrentViz();
workbook = viz.getWorkbook();
dashboardname = workbook.getActiveSheet().getName()
sheets = workbook.getActiveSheet().getWorksheets()
options = {
maxRows: 0,
ignoreSelection: true,
includeAllColumns: false
};
for (var i = 0; i < sheets.length; i++) {
sheetName = sheets[i].getName()
sheetNames.push({sheetid:sheetName, header:true})
sheets[i].getSummaryDataAsync(options).then(function(t) {
var niceData = buildData(t);
exportData.push(niceData);
doneSheets++;
writeToFile();
});
}
}
function buildData(table) {
var columns = table.getColumns();
var data = table.getData();
function reduceToObjects(cols, data) {
var fieldNameMap = $.map(cols, function(col) {
return col.getFieldName()
});
var dataToReturn = $.map(data, function(d) {
return d.reduce(function(memo, value, idx) {
memo[fieldNameMap[idx]] = value.value;
return memo;
}, {});
});
return dataToReturn;
}
var niceData = reduceToObjects(columns, data);
return (niceData)
}
function writeToFile() {
if (doneSheets == sheets.length) {
var sql = 'SELECT INTO XLSX("' + dashboardname + '.xlsx",?) FROM ?';
var res = alasql(sql, [sheetNames, exportData]);
}
}
</script>
</head>
<body>
<a class="btn icon-btn btn-default" href="#" onClick = "exportToExcel()">
<span class="glyphicon btn-glyphicon glyphicon-save img-circle text-muted"></span>
Export to Excel
</a>
</body>
</html>