-
Notifications
You must be signed in to change notification settings - Fork 14
/
generate.js
93 lines (90 loc) · 2.81 KB
/
generate.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
const fs = require("fs");
const path = require("path");
const XLSX = require("xlsx");
const EXCEL_PATH = path.resolve(__dirname, "./excel");
const JSON_PATH = path.resolve(__dirname, "./json_data");
const DEFAULT_SHEET = "Sheet1";
const files = fs.readdirSync(EXCEL_PATH);
let provinces = [],
districts = [],
wards = [];
let tree = {};
files.forEach(file_name => {
if (file_name.includes("tỉnh")) {
const provinces_workbook = XLSX.readFile(
path.resolve(EXCEL_PATH, file_name)
).Sheets[DEFAULT_SHEET];
provinces = XLSX.utils
.sheet_to_json(provinces_workbook, { raw: true })
.map(x => ({
code: x["Mã"],
name: x["Tên"],
unit: x["Cấp"]
}))
.filter(x => x.code);
fs.writeFileSync(
path.resolve(JSON_PATH, "provinces.json"),
JSON.stringify(provinces, null, 2)
);
} else if (file_name.includes("huyện")) {
const districts_workbook = XLSX.readFile(
path.resolve(EXCEL_PATH, file_name)
).Sheets[DEFAULT_SHEET];
districts = XLSX.utils
.sheet_to_json(districts_workbook, { raw: true })
.map(x => ({
code: x["Mã"],
name: x["Tên"],
unit: x["Cấp"],
province_code: x["Mã TP"],
province_name: x["Tỉnh / Thành Phố"],
full_name: `${x["Tên"]}, ${x["Tỉnh / Thành Phố"]}`
}))
.filter(x => x.code);
fs.writeFileSync(
path.resolve(JSON_PATH, "districts.json"),
JSON.stringify(districts, null, 2)
);
} else if (file_name.includes("xã")) {
const wards_workbook = XLSX.readFile(path.resolve(EXCEL_PATH, file_name))
.Sheets[DEFAULT_SHEET];
wards = XLSX.utils
.sheet_to_json(wards_workbook, { raw: true })
.map(x => ({
code: x["Mã"],
name: x["Tên"],
unit: x["Cấp"],
district_code: x["Mã QH"],
district_name: x["Quận Huyện"],
province_code: x["Mã TP"],
province_name: x["Tỉnh / Thành Phố"],
full_name: `${x["Tên"]}, ${x["Quận Huyện"]}, ${x["Tỉnh / Thành Phố"]}`
}))
.filter(x => x.code);
fs.writeFileSync(
path.resolve(JSON_PATH, "wards.json"),
JSON.stringify(wards, null, 2)
);
}
});
provinces.forEach(province => {
tree[province.code] = province;
});
districts.forEach(district => {
if (!tree[district.province_code].districts) {
tree[district.province_code].districts = {};
}
tree[district.province_code].districts[district.code] = district;
});
wards.forEach(ward => {
if (!tree[ward.province_code].districts[ward.district_code].wards) {
tree[ward.province_code].districts[ward.district_code].wards = {};
}
tree[ward.province_code].districts[ward.district_code].wards[
ward.code
] = ward;
});
fs.writeFileSync(
path.resolve(JSON_PATH, "tree.json"),
JSON.stringify(tree, null, 2)
);