-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexcel.js
executable file
·83 lines (65 loc) · 1.68 KB
/
excel.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
#!/usr/bin/env node
const Excel = require("exceljs");
const path = require("path");
const argv2 = process.argv[2];
let filePath;
if (argv2.charAt("0") !== "/" && argv2.charAt("0") !== "~") {
filePath = path.resolve(process.cwd(), argv2);
} else {
filePath = argv2;
}
const data = require(filePath);
function getName(path) {
let filename = path.split("/").pop();
if (filename.includes(".")) {
filename = filename.split(".");
filename.pop();
filename = filename.join(".");
}
return filename;
}
let filename;
if (process.argv[3]) {
filename = getName(process.argv[3]);
} else {
filename = getName(filePath);
}
filename = `${filename}.xlsx`.replace("_time_", new Date().toLocaleString());
const workbook = new Excel.stream.xlsx.WorkbookWriter({
filename
});
const worksheet = workbook.addWorksheet("Sheet");
worksheet.columns = [
{ header: "key", key: "header" },
{ header: "value", key: "value" }
];
const dataArray = [];
getTiledData(data, dataArray);
const length = dataArray.length;
// 当前进度
let current_num = 0;
const time_monit = 400;
let temp_time = Date.now();
// 开始添加数据
for (let i in dataArray) {
worksheet.addRow(dataArray[i]).commit();
current_num = i;
if (Date.now() - temp_time > time_monit) {
temp_time = Date.now();
console.log(((current_num / length) * 100).toFixed(2) + "%");
}
}
workbook.commit();
console.log("done");
function getTiledData(data, arr, fatherKey = "") {
for (let key in data) {
if (typeof data[key] === "object") {
getTiledData(data[key], arr, fatherKey + key + ".");
} else {
arr.push({
header: fatherKey + key.replace(".", ""),
value: data[key]
});
}
}
}