forked from exceljs/exceljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testBookOutS.js
294 lines (271 loc) · 7.53 KB
/
testBookOutS.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const _ = require('../lib/utils/under-dash.js');
const Excel = require('../excel');
// const { Workbook } = Excel;
const {WorkbookWriter} = Excel.stream.xlsx;
const filename = process.argv[2];
const wb = new WorkbookWriter({
filename,
useSharedStrings: false,
useStyles: false,
});
const ws = wb.addWorksheet('blort');
const fonts = {
arialBlackUI14: {
name: 'Arial Black',
family: 2,
size: 14,
underline: true,
italic: true,
},
comicSansUdB16: {
name: 'Comic Sans MS',
family: 4,
size: 16,
underline: 'double',
bold: true,
},
};
const alignments = [
{text: 'Top Left', alignment: {horizontal: 'left', vertical: 'top'}},
{
text: 'Middle Centre',
alignment: {horizontal: 'center', vertical: 'middle'},
},
{
text: 'Bottom Right',
alignment: {horizontal: 'right', vertical: 'bottom'},
},
{text: 'Wrap Text', alignment: {wrapText: true}},
{text: 'Indent 1', alignment: {indent: 1}},
{text: 'Indent 2', alignment: {indent: 2}},
{
text: 'Rotate 15',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 15},
},
{
text: 'Rotate 30',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 30},
},
{
text: 'Rotate 45',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 45},
},
{
text: 'Rotate 60',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 60},
},
{
text: 'Rotate 75',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 75},
},
{
text: 'Rotate 90',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: 90},
},
{
text: 'Rotate -15',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -55},
},
{
text: 'Rotate -30',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -30},
},
{
text: 'Rotate -45',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -45},
},
{
text: 'Rotate -60',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -60},
},
{
text: 'Rotate -75',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -75},
},
{
text: 'Rotate -90',
alignment: {horizontal: 'right', vertical: 'bottom', textRotation: -90},
},
{
text: 'Vertical Text',
alignment: {
horizontal: 'right',
vertical: 'bottom',
textRotation: 'vertical',
},
},
];
// const badAlignments = [
// { text: 'Rotate -91', alignment: { textRotation: -91 } },
// { text: 'Rotate 91', alignment: { textRotation: 91 } },
// { text: 'Indent -1', alignment: { indent: -1 } },
// { text: 'Blank', alignment: {} },
// ];
const borders = {
thin: {
top: {style: 'thin'},
left: {style: 'thin'},
bottom: {style: 'thin'},
right: {style: 'thin'},
},
doubleRed: {
color: {argb: 'FFFF0000'},
top: {style: 'double'},
left: {style: 'double'},
bottom: {style: 'double'},
right: {style: 'double'},
},
thickRainbow: {
top: {style: 'double', color: {argb: 'FFFF00FF'}},
left: {style: 'double', color: {argb: 'FF00FFFF'}},
bottom: {style: 'double', color: {argb: 'FF00FF00'}},
right: {style: 'double', color: {argb: 'FF00FF'}},
diagonal: {
style: 'double',
color: {argb: 'FFFFFF00'},
up: true,
down: true,
},
},
};
const fills = {
redDarkVertical: {
type: 'pattern',
pattern: 'darkVertical',
fgColor: {argb: 'FFFF0000'},
},
redGreenDarkTrellis: {
type: 'pattern',
pattern: 'darkTrellis',
fgColor: {argb: 'FFFF0000'},
bgColor: {argb: 'FF00FF00'},
},
blueWhiteHGrad: {
type: 'gradient',
gradient: 'angle',
degree: 0,
stops: [
{position: 0, color: {argb: 'FF0000FF'}},
{position: 1, color: {argb: 'FFFFFFFF'}},
],
},
rgbPathGrad: {
type: 'gradient',
gradient: 'path',
center: {left: 0.5, top: 0.5},
stops: [
{position: 0, color: {argb: 'FFFF0000'}},
{position: 0.5, color: {argb: 'FF00FF00'}},
{position: 1, color: {argb: 'FF0000FF'}},
],
},
};
ws.columns = [
{header: 'Col 1', key: 'key', width: 25},
{header: 'Col 2', key: 'name', width: 32},
{header: 'Col 3', key: 'age', width: 21},
{header: 'Col 4', key: 'addr1', width: 18},
{header: 'Col 5', key: 'addr2', width: 8},
{header: 'Col 6', width: 8},
{header: 'Col 7', width: 8},
{
header: 'Col 8',
width: 8,
style: {font: fonts.comicSansUdB16, alignment: alignments[1].alignment},
},
];
ws.getCell('A2').value = 7;
ws.getCell('B2').value = 'Hello, World!';
ws.getCell('B2').font = fonts.comicSansUdB16;
ws.getCell('B2').border = borders.thin;
ws.getCell('C2').value = -5.55;
ws.getCell('C2').numFmt = '"£"#,##0.00;[Red]-"£"#,##0.00';
ws.getCell('C2').font = fonts.arialBlackUI14;
ws.getCell('D2').value = 3.14;
ws.getCell('D2').value = new Date();
ws.getCell('D2').numFmt = 'd-mmm-yyyy';
ws.getCell('D2').font = fonts.comicSansUdB16;
ws.getCell('D2').border = borders.doubleRed;
ws.getCell('E2').value = `${['Hello', 'World'].join(', ')}!`;
ws.getRow(2).commit();
ws.getCell('A3').value = {
text: 'www.google.com',
hyperlink: 'http://www.google.com',
};
ws.getCell('A4').value = 'Boo!';
ws.getCell('C4').value = 'Hoo!';
ws.mergeCells('A4', 'C4');
ws.getRow(4).commit();
ws.getCell('A5').value = 1;
ws.getCell('B5').value = 2;
ws.getCell('C5').value = {formula: 'A5+B5', result: 3};
ws.getRow(5).commit();
ws.getCell('A6').value = 'Hello';
ws.getCell('B6').value = 'World';
ws.getCell('C6').value = {
formula: 'CONCATENATE(A6,", ",B6,"!")',
result: 'Hello, World!',
};
ws.getCell('C6').border = borders.thickRainbow;
ws.getRow(6).commit();
ws.getCell('A7').value = 1;
ws.getCell('B7').value = 2;
ws.getCell('C7').value = {formula: 'A7+B7'};
ws.getRow(7).commit();
const now = new Date();
ws.getCell('A8').value = now;
ws.getCell('B8').value = 0;
ws.getCell('C8').value = {formula: 'A8+B8', result: now};
ws.getRow(8).commit();
ws.getCell('A9').value = 1.6;
ws.getCell('A9').numFmt = '# ?/?';
ws.getCell('B9').value = 1.6;
ws.getCell('B9').numFmt = 'h:mm:ss';
ws.getCell('C9').value = 0.016;
ws.getCell('C9').numFmt = '0.00%';
ws.getCell('D9').value = 1.6;
ws.getCell('D9').numFmt = '[Green]#,##0 ;[Red](#,##0)';
ws.getCell('E9').value = 1.6;
ws.getCell('E9').numFmt = '#0.000';
ws.getCell('F9').value = 0.016;
ws.getCell('F9').numFmt = '# ?/?%';
ws.getRow(9).commit();
ws.getCell('A10').value = '<';
ws.getCell('B10').value = '>';
ws.getCell('C10').value = '<a>';
ws.getCell('D10').value = '><';
ws.getRow(10).commit();
ws.getRow(11).height = 40;
_.each(alignments, (alignment, index) => {
const rowNumber = 11;
const colNumber = index + 1;
const cell = ws.getCell(rowNumber, colNumber);
cell.value = alignment.text;
cell.alignment = alignment.alignment;
});
ws.getRow(11).commit();
const row12 = ws.getRow(12);
row12.height = 40;
row12.getCell(1).value = 'Blue White Horizontal Gradient';
row12.getCell(1).fill = fills.blueWhiteHGrad;
row12.getCell(2).value = 'Red Dark Vertical';
row12.getCell(2).fill = fills.redDarkVertical;
row12.getCell(3).value = 'Red Green Dark Trellis';
row12.getCell(3).fill = fills.redGreenDarkTrellis;
row12.getCell(4).value = 'RGB Path Gradient';
row12.getCell(4).fill = fills.rgbPathGrad;
// row and column styles
ws.getRow(13).font = fonts.arialBlackUI14;
ws.getCell('H12').value = 'Foo';
ws.getCell('G13').value = 'Foo';
ws.getCell('H13').value = 'Bar';
ws.getCell('I13').value = 'Baz';
ws.getCell('H14').value = 'Baz';
// ws.getRow(13).commit();
wb.commit()
.then(() => {
console.log('Done.');
})
.catch(error => {
console.log(error.message);
});