-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporToXLS.js
More file actions
59 lines (52 loc) · 1.78 KB
/
exporToXLS.js
File metadata and controls
59 lines (52 loc) · 1.78 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
// add XML export to file
import "https://unpkg.com/write-excel-file@1.x/bundle/write-excel-file.min.js";
const regexISO = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/;
export async function exportXLSFile(headers, lines, fileTitle) {
try {
console.log({ headers, lines });
const headersXLS = Object.values(headers).map((h) => ({
value: h,
fontWeight: 'bold',
}));
const columns = headersXLS.map((e) => ({ width: e.value.length + 2 }));
const dataXLS = [];
dataXLS.push(headersXLS);
for (const line of lines) {
const row = [];
dataXLS.push(row);
let i = 0;
for (const [key, label] of Object.entries(headers)) {
const cell = { value: line[key] };
let myLength = 10;
// -- number
const n = Number(cell.value);
if (!Number.isNaN(n)) {
cell.value = n;
cell.type = Number;
} else if (typeof cell.value === 'string') { // date
myLength = cell.value.length;
const fulldate = cell.value.endsWith('Z');
const t = fulldate ? cell.value : cell.value + 'T00:00:00.000Z';
if (regexISO.test(t)) {
cell.value = new Date(t);
cell.type = Date;
cell.format = fulldate ? 'yyyy-mm-dd hh:mm:ss' : 'yyyy-mm-dd'
myLength = fulldate ? 18 : 11;
}
}
row.push(cell);
myLength = Math.min(myLength, 33) + 2;
if (columns[i].width < myLength) columns[i].width = myLength;
i++;
}
}
console.log('## columns', {columns});
const exportedFilename = fileTitle + '.xlsx' || 'export.xlsx';
await writeXlsxFile(dataXLS, {
columns,
fileName: exportedFilename,
});
} catch (e) {
console.log(e, e.stack);
}
}