Skip to content

Commit 4361ec7

Browse files
committed
feat: json转excel
1 parent 3b701af commit 4361ec7

File tree

3 files changed

+75
-53
lines changed

3 files changed

+75
-53
lines changed

example/jsonToExcel/.DS_Store

6 KB
Binary file not shown.

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs')
33

44
const { generateI18nJson } = require('./modules/copy-i18n/index')
55
const { excelToJson } = require('./modules/jsonToExcel/excelToJson')
6+
const { jsonToExcel } = require('./modules/jsonToExcel/index')
67

78
//获得命令运行时的路径
89
const getCwd = () => process.cwd();
@@ -35,4 +36,10 @@ program
3536
excelToJson(filename, sheetName, languages)
3637
})
3738

39+
program
40+
.command('j2e fileName sheetName')
41+
.description('读取当前文件夹下的json文件转成excel')
42+
.action((filename, sheetName) => {
43+
jsonToExcel(filename, sheetName)
44+
})
3845
program.parse(process.argv);

modules/jsonToExcel/index.js

+68-53
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,39 @@
22
const fs = require('fs');
33
const xlsx = require('node-xlsx').default
44

5+
const getCwd = () => process.cwd();
6+
57
/**
68
*
79
* @param {Array} jsonData
810
* @returns
911
*/
1012
function flatObject(jsonData) {
11-
let stack = []
12-
const retObj = {}
13-
function flat(obj, index) {
14-
if (typeof obj === 'string') {
15-
const flatKey = stack.join('.')
16-
const value = retObj[flatKey]
17-
if (!value) {
18-
retObj[flatKey] = []
19-
}
20-
retObj[flatKey].push({index: index, value: obj})
21-
return obj;
22-
}
23-
const keys = Object.keys(obj);
24-
keys.forEach(key => {
25-
stack.push(key);
26-
flat(obj[key], index)
27-
stack.pop();
28-
})
13+
let stack = []
14+
const retObj = {}
15+
function flat(obj, index) {
16+
if (typeof obj === 'string') {
17+
const flatKey = stack.join('.')
18+
const value = retObj[flatKey]
19+
if (!value) {
20+
retObj[flatKey] = []
21+
}
22+
retObj[flatKey].push({ index: index, value: obj })
23+
return obj;
2924
}
25+
const keys = Object.keys(obj);
26+
keys.forEach(key => {
27+
stack.push(key);
28+
flat(obj[key], index)
29+
stack.pop();
30+
})
31+
}
3032

31-
jsonData.forEach((item, index) => {
32-
flat(item, index + 1)
33-
});
33+
jsonData.forEach((item, index) => {
34+
flat(item, index + 1)
35+
});
3436

35-
return retObj;
37+
return retObj;
3638
}
3739

3840
/**
@@ -42,44 +44,57 @@ function flatObject(jsonData) {
4244
* @returns
4345
*/
4446
function handleData(data, length) {
45-
const retAry = []
46-
for ([key, value] of Object.entries(data)) {
47-
const tempAry = [key]
48-
for (let i = 1; i <= length; i++) {
49-
if (value.length && value[0].index === i) {
50-
tempAry[i] = value.shift().value;
51-
} else {
52-
tempAry[i] = null
53-
}
54-
}
55-
retAry.push(tempAry)
47+
const retAry = []
48+
for ([key, value] of Object.entries(data)) {
49+
const tempAry = [key]
50+
for (let i = 1; i <= length; i++) {
51+
if (value.length && value[0].index === i) {
52+
tempAry[i] = value.shift().value;
53+
} else {
54+
tempAry[i] = null
55+
}
5656
}
57-
return retAry;
57+
retAry.push(tempAry)
58+
}
59+
return retAry;
5860
}
5961

6062
function readFile() {
61-
const ary = [];
62-
ary.push(require('./zh.json'))
63-
ary.push(require('./en.json'))
64-
ary.push(require('./th.json'))
65-
return ary;
63+
const ary = [];
64+
console.log(getCwd())
65+
const files = fs.readdirSync('./')
66+
67+
files.forEach((file) => {
68+
if (file.endsWith('.json')) {
69+
ary.push(require(`${getCwd()}/${file}`))
70+
}
71+
})
72+
73+
return ary;
6674
}
6775

6876
function write(fileName, sheetName) {
69-
const files = readFile();
70-
const data = flatObject(files, files.length);
71-
const handledData = handleData(data, 3)
72-
fs.writeFileSync('./test.json', JSON.stringify(handledData))
73-
const buffer = xlsx.build(
74-
[
75-
{
76-
name: sheetName,
77-
data: handledData
78-
}
79-
]
80-
)
77+
const files = readFile();
78+
const data = flatObject(files, files.length);
79+
const handledData = handleData(data, 3)
80+
const buffer = xlsx.build(
81+
[
82+
{
83+
name: sheetName,
84+
data: handledData
85+
}
86+
]
87+
)
8188

82-
fs.writeFileSync(`./${fileName}.xlsx`, buffer)
89+
fs.writeFileSync(`./${fileName}.xlsx`, buffer)
90+
console.log('生成成功')
8391
}
8492

85-
write('translate', 'sheetName')
93+
/**
94+
*
95+
* @param {*} fileName 文件名
96+
* @param {*} sheetName 表名
97+
*/
98+
exports.jsonToExcel = (fileName, sheetName) => {
99+
write(fileName, sheetName)
100+
}

0 commit comments

Comments
 (0)