-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (66 loc) · 2.52 KB
/
index.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
#!/usr/bin/env node
"use strict";
const {program} = require('commander');
program.version('0.0.1');
program
.option('-r, --replace', 'replace old sheet')
.option('-p, --pizza-type <type>', 'flavour of pizza')
.action(function() {
// 读取文件
if (typeof require !== ' undefined ') XLSX = require('xlsx');
const workbook1 = XLSX.readFile('./Excel/新集.xlsx');
const workbook2 = XLSX.readFile('./Excel/陈集.xlsx');
// const workbook2 = XLSX.readFile(args[2]);
// 读取sheet1
const first_sheet_name = workbook2.SheetNames[0];
const first_worksheet = workbook2.Sheets[first_sheet_name];
const list1 = XLSX.utils.sheet_to_json(first_worksheet);
// 读取sheet2
const second_sheet_name = workbook2.SheetNames[1];
const second_worksheet = workbook2.Sheets[second_sheet_name];
let list2 = XLSX.utils.sheet_to_json(second_worksheet);
// new_list2 = list2.map()
// 设置正则表达式
const reg = /[0-9]{12,}[A-Z]{2}[0-9]{5}\s[\u4e00-\u9fa5]{2,}/g
const list2New = list2
// 过滤器检查是否处理成功
// .filter((it) => it.DJH == "321081107212JC00071")
.map((item, i) => { //item:每行数据
// 浅拷贝
const itemCopy = Object.assign({}, item);
// 将四个变量放在数组中进行遍历
["ZDSZB", "ZDSZD", "ZDSZN", "ZDSZX"].forEach((key) => {
let str = item[key];
// 正则匹配
let group = str.match(reg); //group:符合正则表达式匹配
if(group !=null){
group.forEach((match) => {
const findItem = list1.find((it) => it["原"] == match);
// ES6的解构
// const findItem = list1.find(原 => 原 == match);
if (findItem != undefined) {
// console.log(
// match,
// findItem["现"],
// itemCopy[key].replace(match, findItem["现"])
// );
itemCopy[key] = itemCopy[key].replace(match, findItem["现"]);
}
})
}
});
return itemCopy;
});
// 将数据导入到新表中再导出到工作簿中
// const sheetNew = XLSX.utils.json_to_sheet(list2New);
// // console.log(sheetNew);
// var ws_name = "SheetJS";
// XLSX.utils.book_append_sheet(workbook2, sheetNew, ws_name);
// XLSX.writeFile(workbook2, 'out.xlsx');
// 直接更新sheet2
// workbook2.Sheets["Sheet2"] = XLSX.utils.json_to_sheet(list2New)
// XLSX.writeFile(workbook2, 'out.xlsx');
});
program.parse(process.argv);
if(program.replace)console.log("OK");
if(program.pizzaType)console.log(`- ${program.pizzaType}`); //命令行输入:node index.js -p 参数名