-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (138 loc) · 5.11 KB
/
Copy pathindex.js
File metadata and controls
146 lines (138 loc) · 5.11 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
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
const xlsx = require('node-xlsx')
const fs = require('fs');
const path = require('path')
const config = require('./config.js')
// const regFilter = /欢|倩/g
console.log(process.argv)
const sourceDir = path.resolve(__dirname, './sources')
const targetDir = path.resolve(__dirname, './target')
const mkdir = () => {
console.log(`正在创建目录${targetDir}...`)
fs.mkdir(targetDir, (err, data) => {
if (err) {
console.error(err)
throw err
} else {
console.log(`创建目录${targetDir}成功`)
check()
}
})
}
const check = () => {
fs.exists(targetDir, (isExist) => {
if (isExist) {
if (config && config.filterKeys && Array.isArray(config.filterKeys) && config.filterKeys.length) {
const filterKeys = config.filterKeys.filter(item => item)
const regFilter = new RegExp(`${filterKeys.join('|')}`, 'g')
console.log(regFilter)
doExcel(regFilter)
} else {
console.error('参数错误: config.js--filterKeys')
}
} else {
console.error(`${targetDir}目录不存在...`)
mkdir()
}
})
}
const fileType = (files) => {
console.log(files)
const allowExts = config && config.fileExt && config.fileExt.length ? config.fileExt : []
if (files && files.length) {
return files.filter(item => {
const filePath = `${sourceDir}/${item}`
return allowExts && allowExts.length ? !fs.statSync(filePath).isDirectory() && allowExts.includes(path.extname(filePath)) : !fs.statSync(filePath).isDirectory()
}) || []
}
return []
}
const doExcel = (reg) => {
fs.readdir(sourceDir, (err, files) => {
const fileNames = fileType(files)
console.log(fileNames)
if (fileNames && fileNames.length) {
console.log('正在解析Excel文件, 请耐心等待...')
fileNames.forEach(file => {
console.log(file)
const tagetData = []
const sheetList = xlsx.parse(path.resolve(`${sourceDir}/${file}`));
// console.log(sheetList)
sheetList.forEach((sheet, sindex) => {
const sheetDatas = sheet.data || []
// console.log(sheet)
const rowData = {
name: sheet.name,
data: []
}
sheetDatas.forEach((row, rindex) => {
console.log(rindex)
if (rindex > 0) {
for (let i = 0; i< row.length; i++) {
// console.log(row[i])
if (reg.test(row[i])) {
rowData.data.push(row)
break
}
// 构造大数据量
// let j = 0
// while (j < 2000) {
// rowData.data.push(row)
// j++
// }
}
} else {
rowData.data.push(row)
}
})
tagetData.push(rowData)
})
console.log(`数据量级:${tagetData[0].data ? tagetData[0].data.length : 0}`)
console.log('正在转换Buffer数据, 请耐心等待...')
//将经过处理的数据写入新的xlsx文件中
const buffer = xlsx.build(tagetData);
// const buffer = Buffer.from(tagetData);
console.log('正在写入文件,写入操作比较耗时,请耐心等待,在没有出现进一步提示或者报错情况下,请勿操作...')
// 同步写入
fs.writeFileSync(path.resolve(`${targetDir}/export_ ${file}`), buffer, {'flag':'w'});
console.log("写入完成。");
console.log(`请打开以下目录查看文件名为export_${file}的Excel文件,如没有文件请重试!!!`)
console.dir(targetDir)
// 异步写入
// fs.writeFile(path.resolve(targetDir + '/export_' + fileName), buffer, {'flag':'w'}, (werr, data) => {
// if (werr) {
// console.log(werr)
// throw werr
// } else {
// console.log("写入完成。");
// console.log(`请打开以下目录查看文件名为export_${file}的Excel文件,如没有文件请重试!!!`)
// console.dir(targetDir)
// }
// })
// 流写入
// const writerStream = fs.createWriteStream(path.resolve(targetDir + '/export_' + fileName), {'flag':'w'});
// // 使用 utf8 编码写入数据
// writerStream.write(buffer, 'UTF8');
// // 标记文件末尾
// writerStream.end();
// // 处理流事件 --> finish 事件
// writerStream.on('finish', function() {
// /*finish - 所有数据已被写入到底层系统时触发。*/
// console.log("写入完成。");
// console.log(`请打开以下目录查看文件名为export_${file}的Excel文件,如没有文件请重试!!!`)
// console.dir(targetDir)
// });
// writerStream.on('drain', function () {
// console.log("内存干了");
// });
// writerStream.on('error', function(err2){
// console.log(err2.stack);
// throw err2
// });
})
} else {
console.error(`${targetDir}未发现可处理的文件`)
}
})
}
// 程序入口
check()