forked from AliyunWorkbench/lifeRestart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsxTransform.js
105 lines (98 loc) · 3.48 KB
/
xlsxTransform.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
import { readFile, writeFile, stat, readdir } from 'fs/promises';
import * as XLSX from 'xlsx';
import { join, extname, dirname } from 'path';
// const { readFile, writeFile, stat, readdir } = require('fs/promises');
// const XLSX = require('xlsx');
// const { join, extname, dirname } = require('path');
async function transform(filePath) {
const xlsxFileBuffer = await readFile(filePath);
const xlsx = XLSX.read(xlsxFileBuffer, {type: 'buffer'});
const sheets = xlsx.Sheets;
const data = {};
for(const sheetName in sheets) {
const sheetRawData = sheets[sheetName];
if(!sheetRawData['!ref']) break;
const rawData = XLSX.utils.sheet_to_json(sheetRawData);
const newData = {};
data[sheetName] = newData;
rawData.shift();
for(const row of rawData) {
const rowData = {};
let mainKey;
for(let key in row) {
const cell = row[key];
if(key[0] == "$") {
key = key.substr(1);
mainKey = cell;
}
if(key.includes(':')) {
const keys = key.split(':');
const lastKey = keys.pop();
let temp = rowData;
for(const subKey of keys) {
if(!temp[subKey]) temp[subKey] = {};
temp = temp[subKey];
}
if(lastKey.includes('[]')) {
const aKey = lastKey.split('[]')[0];
if(!temp[aKey]) temp[aKey] = [cell];
else temp[aKey].push(cell);
} else {
temp[lastKey] = cell;
}
} else if(key.includes('[]')) {
const aKey = key.split('[]')[0];
if(!rowData[aKey]) rowData[aKey] = [cell];
else rowData[aKey].push(cell);
} else {
rowData[key] = cell;
}
}
if(mainKey===undefined) return console.error('No Main Key', rowData);
newData[mainKey] = rowData;
}
}
return data;
}
async function walk(filePath) {
const xlsxPaths = [];
if(Array.isArray(filePath)) {
for(const subPath of filePath)
xlsxPaths.push(await walk(subPath));
return xlsxPaths.flat();
}
const fileStat = await stat(filePath);
if(!fileStat.isDirectory()) {
const ext = extname(filePath);
if( ext=='.xls' || ext=='.xlsx' ) xlsxPaths.push(filePath);
return xlsxPaths;
}
const dirData = await readdir(filePath);
for(const subPath of dirData)
xlsxPaths.push(await walk(join(filePath, subPath)));
return xlsxPaths.flat();
}
async function main() {
const filePaths = process.argv.slice(2);
if(filePaths.length<0) process.exit(0);
const xlsxs = await walk(filePaths);
for(const p of xlsxs) {
const data = await transform(p);
const d = dirname(p);
for(const sheetName in data) {
const savePath = join(d, `${sheetName}.json`);
console.info(`[Transform] XLSX(${p}:${sheetName}) -> JSON(${savePath})`);
await writeFile(
savePath,
JSON.stringify(data[sheetName], null, 4),
);
}
}
console.info(`
------------------------
| Transform Complete |
------------------------
`);
setTimeout(()=>{}, 1000);
}
main();