-
Notifications
You must be signed in to change notification settings - Fork 0
/
vividCsvExport.js
89 lines (74 loc) · 2.43 KB
/
vividCsvExport.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
function csvDownload(){
var tableData = [
];
var tableLength = document.getElementById('fullPublicationTable').rows.length;
var tableColLength = document.getElementById('fullPublicationTable').rows[0].cells.length;
var buttonValue = undefined;
var tableRow = undefined;
var headerRow = [];
var i;
var j;
var z;
document.getElementById('more-than-5-exports').style.display = "none";
document.getElementById('no-exports-selected').style.display = "none";
for (z = 1; z <= tableColLength - 1; z++) {
headerRow.push(document.getElementById('fullPublicationTable').rows[0].cells.item(z).innerHTML);
};
tableData.push(headerRow);
for (i = 1; i < tableLength; i++) {
buttonValue = undefined;
if (document.getElementById(i+'th checkbox').checked == true){
buttonValue = document.getElementById(i+'th checkbox').value;
tableRow = [];
for (j = 1; j < tableColLength - 1; j++) {
tableRow.push(document.getElementById('fullPublicationTable').rows[i].cells.item(j).innerHTML);
};
tableData.push(tableRow);
};
};
var csvContent = '';
tableData.forEach(function(infoArray,index){
dataString = infoArray.join(',');
csvContent += index < tableData.length ? dataString + '\n' : dataString;
});
var todaysDate;
todaysDate = new Date();
var day = todaysDate.getDate();
var month = todaysDate.getMonth() + 1;
var year = todaysDate.getFullYear();
if (day < 10) {
day = '0' + day;
};
if (month < 10) {
month = '0' + month;
};
var timestamp = month + "-" + day + "-" + year;
var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) {
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) {
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'tableData:application/octet-stream,' + encodeURIComponent(content);
}
}
if (tableData.length > 5) {
document.getElementById("more-than-5-exports").style.display = "";
};
if (tableData.length < 2) {
document.getElementById("no-exports-selected").style.display = "";
};
if (tableData.length > 1 && tableData.length <6){
download(csvContent, 'Vivid Export ' + timestamp + '.csv', 'text/csv;encoding:utf-8');
};
}