-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
316 lines (279 loc) · 9.9 KB
/
index.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
let Excel = require('exceljs');
/** Determined this value based upon experimentation */
PIXELS_PER_EXCEL_WIDTH_UNIT = 6;
const self = module.exports = {
/**
* Will merge a few cells together.
*
* @param sheet -> the sheet you work on
* @param cellIdStart -> like A1
* @param cellIdEnd -> like A2
*/
mergeCells(sheet, cellIdStart, cellIdEnd) {
sheet.mergeCells(cellIdStart + ':' + cellIdEnd);
},
/**
* Will set a default font to all of the empty cells in the sheet.
*/
setFontInAllEmptyCells(sheet, name = 'Arial', size = 12, bold = false, italic = false) {
sheet.columns.forEach(function (row) {
row.style = {font: {name: name, size: size, bold: bold, italic: italic}};
});
},
/**
* Will fit the column width to be in the size of the biggest line in the column
* NOTICE: remember to do this at the end, after you populated all of the cells with values
*
* @param sheet -> your excel spread sheet
* @param colLettersArr -> an array of all of the columns you wish to change width
* @param ignoredRows -> an array of all of the rows you wouldn't like to consider while changing
* the width
*/
fitColumnWidthToText(sheet, colLettersArr = [], ignoredRows = []) {
for (let i = 0; i < colLettersArr.length; i++) {
let maxColumnLength = 0;
let colLetter = colLettersArr[i];
sheet.getColumn(colLetter).eachCell((cell) => {
if (!ignoredRows.includes(cell.row)) {
if (typeof cell.value === 'string') {
const fontSize = cell.font && cell.font.size ? cell.font.size : 11;
let pixelWidth = require('string-pixel-width');
const cellWidth = pixelWidth(cell.value, {size: fontSize});
maxColumnLength = Math.max(maxColumnLength, cellWidth)
}
}
});
sheet.getColumn(colLetter).width = maxColumnLength / PIXELS_PER_EXCEL_WIDTH_UNIT + 1;
}
},
/**
* Also called wrap text. Will set the height of the line to be in the size of the cell content.
*/
wrapText(sheet, cellId) {
if (sheet.getCell(cellId).alignment === undefined) {
sheet.getCell(cellId).alignment = {};
}
sheet.getCell(cellId).alignment.wrapText = true
},
/**
* Will align the text in the cell to be in the middle and center
* * NOTICE: if you're trying to center align a merged cell, just pick one of the merged cells
* and put it here
*/
alignCellCenter(sheet, cellId) {
let cell = sheet.getCell(cellId);
alignCenter(cell)
},
/**
* Will align the text in the element to be in the middle and center.
* NOTICE: if you're trying to center align a merged cell, just pick one of the merged cells
* and put it here
*/
alignCenter(ele) {
alignCenter(ele)
},
/**
* Will return a bunch of cells, from range and from specific locations
*
* @param sheet -> your current excel sheet
* @param range -> 'A1:A5', for example
* @param locationsArr -> ['A1', 'C66', 'F7'], for example
*/
getCells(sheet, range = null, locationsArr = null) {
let cellsStrArr = [];
if (range !== null) {
let rangeArr = range.split(':');
let letterStart = rangeArr[0].match(/[A-Za-z]+/g)[0];
let letterStartASCII = letterStart.charCodeAt(0);
let rangeStart = parseInt(rangeArr[0].match(/[^A-Za-z]+/g)[0]);
let letterEnd = rangeArr[1].match(/[A-Za-z]+/g)[0];
let letterEndASCII = letterEnd.charCodeAt(0);
let rangeEnd = parseInt(rangeArr[1].match(/[^A-Za-z]+/g)[0]);
if (letterStart !== letterEnd) {
for (let i = letterStartASCII; i < letterEndASCII + 1; i++) {
let currLetter = String.fromCharCode(i);
for (let j = rangeStart; j < rangeEnd + 1; j++) {
let ele = currLetter + j;
cellsStrArr.push(ele)
}
}
} else {
for (let i = rangeStart; i < rangeEnd + 1; i++) {
cellsStrArr.push(letterStart + i)
}
}
}
if (locationsArr !== null) {
for (let i = 0; i < locationsArr.length; i++) {
cellsStrArr.push(locationsArr[i])
}
}
cellsStrArr = cellsStrArr.filter(function (elem, pos) {
return cellsStrArr.indexOf(elem) === pos;
});
let cellsArr = [];
for (let i = 0; i < cellsStrArr.length; i++) {
cellsArr.push(self.getCell(sheet, cellsStrArr[i]))
}
return cellsArr;
},
/**
* Will return a specific cell from the worksheet
*/
getCell(sheet, cellId) {
return sheet.getCell(cellId)
}
,
/**
* Will change the row height.
* NOTICE: the default raw height, if didn't changed, is 15.
*
* @param row -> the row in question
* @param newRowHeight -> the new height of the row
*/
setRowHeight(row, newRowHeight) {
row.height = newRowHeight
}
,
/**
* Will read a value from a given cell identifier
*
* @param sheet -> the current sheet obj
* @param cellId -> row and column identifier. Like A4
*/
readValue(sheet, cellId) {
return sheet.getCell(cellId).header
}
,
/**
* Will set a value in a given cell id
*
* @param sheet -> the current sheet obj
* @param cellId -> row and column identifier. Like A4
* @param value -> the value you would like to write
* @param fontName -> the name of the font in the cell
* @param fontSize -> the font size in the cell
* @param bold -> font bold toggle
* @param italic -> font italic toggle
*/
setValue(sheet, cellId, value, fontName = 'Arial', fontSize = 12, bold = false, italic = false) {
sheet.getCell(cellId).value = value;
sheet.getCell(cellId).style.font = {
name: fontName,
size: fontSize,
bold: bold,
italic: italic
};
}
,
/**
* Will add a sheet to the workbook
*/
createSheet(wb, sheetName, rtl = false) {
let sheet = wb.addWorksheet(sheetName);
if (rtl) {
sheet.views = [{rightToLeft: true}];
}
return sheet
}
,
/**
* Will create a new workbook to start the work on
*/
createWorkbook() {
return new Excel.Workbook();
}
,
/**
* Will save the workbook in a given path
*/
saveWorkbook: async function (wb, workbookPath) {
await wb.xlsx.writeFile(workbookPath)
.then(function () {
// done
});
}
,
/**
* Will set the background color of a given cell
*/
setCellBackgroundColor(sheet, cellId, hexColor) {
let cell = sheet.getCell(cellId);
setBackgroundColor(cell, hexColor)
}
,
/**
* Will set the background color of a given element
*/
setElementBackgroundColor(ele, hexColor) {
setBackgroundColor(ele, hexColor)
}
,
/**
* Will set the text color of the text in a given cell
*/
setCellTextColor(sheet, cellId, hexColor) {
sheet.getCell(cellId).font = {
color: {argb: hexColor},
};
}
,
/**
* Will return a row
*/
getRow(sheet, row) {
return sheet.getRow(row)
}
,
/**
* Will return a column
*/
getColumn(sheet, column) {
return sheet.getColumn(column)
}
,
/**
* Will change the entire style of an element
*/
setEleStyle(ele,
fontName = 'Arial',
fontSize = 12,
bold = false,
italic = false,
backgroundColor = null,
fontColor = null) {
ele.eachCell((cell) => {
cell.style.font = {
name: fontName,
size: fontSize,
bold: bold,
italic: italic
};
if (fontColor !== null) {
cell.style.font.color = {argb: fontColor}
}
});
if (backgroundColor !== null) {
self.setElementBackgroundColor(ele, backgroundColor)
}
}
}
;
function setBackgroundColor(ele, hexColor) {
ele.fill = {
type: 'gradient',
gradient: 'angle',
degree: 0,
stops: [
{position: 0, color: {argb: hexColor}},
{position: 1, color: {argb: hexColor}},
]
};
}
function alignCenter(ele) {
if (ele.alignment === undefined) {
ele.alignment = {};
}
ele.alignment.vertical = 'middle';
ele.alignment.horizontal = 'center';
}