-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathsimple.html
More file actions
136 lines (130 loc) · 4.2 KB
/
simple.html
File metadata and controls
136 lines (130 loc) · 4.2 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple for frontend</title>
</head>
<body>
<button onclick="download()">Download</button>
<script type="text/javascript" src="../node_modules/jszip/dist/jszip.min.js"></script>
<script type="text/javascript" src="../dist/xlsx.min.js"></script>
<script type="text/javascript" src="../node_modules/jszip/vendor/FileSaver.js"></script>
<script>
function download(){
var file = new xlsx.File();
var sheet = file.addSheet('Sheet1');
var data = [
['Auto', 200, 90, 'B2-C2'],
['Entertainment', 200, 32, 'B3-C3'],
['Food', 350, 205.75, 'B4-C4'],
['Home', 300, 250, 'B5-C5'],
['Medical', 100, 35, 'B6-C6'],
['Personal Items', 300, 80, 'B7-C7'],
['Travel', 500, 350, 'B8-C8'],
['Utilities', 200, 100, 'B9-C9'],
['Other', 50, 60, 'B10-C10']
];
function border(cell, top, left, bottom, right) {
var light = 'ffded9d4';
var dark = 'ff7e6a54';
cell.style.border.top = 'thin';
cell.style.border.topColor = top ? dark : light;
cell.style.border.left = 'thin';
cell.style.border.leftColor = left ? dark : light;
cell.style.border.bottom = 'thin';
cell.style.border.bottomColor = bottom ? dark : light;
cell.style.border.right = 'thin';
cell.style.border.rightColor = right ? dark : light;
}
function fill(cell, type) {
type = type || 0;
var colors = ['ffffffff', 'ffa2917d', 'ffe4e2de', 'fffff8df', 'fff1eeec'];
// 1: header, 2: first col, 3: second col, 4: gray, 0: white
cell.style.fill.patternType = 'solid';
cell.style.fill.fgColor = colors[type];
cell.style.fill.bgColor = 'ffffffff';
}
var header = sheet.addRow();
header.setHeightCM(0.8);
var headers = ['Category', 'Budget', 'Actual', 'Difference'];
for (var i = 0; i < headers.length; i++) {
var hc = header.addCell();
hc.value = headers[i];
hc.style.align.v = 'center';
if (i > 0) hc.style.align.h = 'right';
hc.style.font.color = 'ffffffff';
border(hc, 0, 0, 1, 0);
fill(hc, 1);
}
var len = data.length;
for (var i = 0; i < len; i++) {
var line = data[i];
var row = sheet.addRow();
row.setHeightCM(0.8);
// Col 1
var cell1 = row.addCell();
cell1.value = line[0];
cell1.style.align.v = 'center';
if (i === 0) {
border(cell1, 1, 0, 0, 1);
} else if (i === len - 1) {
border(cell1, 0, 0, 1, 1);
} else {
border(cell1, 0, 0, 0, 1);
}
fill(cell1, 2);
// Col 2
var cell2 = row.addCell();
cell2.value = line[1];
cell2.numFmt = '$#,##0.00';
cell2.cellType = 'TypeNumeric';
cell2.style.align.v = 'center';
if (i === 0) {
border(cell2, 1, 1, 0, 0);
} else if (i === len - 1) {
border(cell2, 0, 1, 1, 0);
} else {
border(cell2, 0, 1, 0, 0);
}
fill(cell2, 3);
// Col 3
var cell3 = row.addCell();
cell3.value = line[2];
cell3.numFmt = '$#,##0.00';
cell3.cellType = 'TypeNumeric';
cell3.style.align.v = 'center';
if (i === 0) {
border(cell3, 1, 0, 0, 0);
} else if (i === len - 1) {
border(cell3, 0, 0, 1, 0);
} else {
border(cell3, 0, 0, 0, 0);
}
fill(cell3, i % 2 === 0 ? 0 : 4);
// Col 4
var cell4 = row.addCell();
cell4.formula = line[3];
cell4.numFmt = '$#,##0.00';
cell4.cellType = 'TypeFormula';
cell4.style.align.v = 'center';
if (i === 0) {
border(cell4, 1, 0, 0, 0);
} else if (i === len - 1) {
border(cell4, 0, 0, 1, 0);
} else {
border(cell4, 0, 0, 0, 0);
}
fill(cell4, i % 2 === 0 ? 0 : 4);
}
for (var i = 0; i < 4; i++) {
sheet.col(i).width = 20;
}
file
.saveAs('blob')
.then(function(content) {
saveAs(content, "example.xlsx");
});
}
</script>
</body>
</html>