forked from ipanli/xlsx2json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
249 lines (203 loc) · 5.19 KB
/
index.js
File metadata and controls
249 lines (203 loc) · 5.19 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var xlsx = require('./lib/xlsx-to-json.js');
var g_path = require('path');
var shell = require('child_process');
var fs = require('fs');
var glob = require('glob');
var config = require('./config.json');
/**
* all commands
*/
var commands = {
// 帮助
"--help": {
"alias": ["-h"],
"desc": "show this help manual.",
"action": showHelp
},
// 导出指定excel的指定sheet
"--export": {
"alias": ["-e"],
"desc": "export excel to json. --export [files]",
"action": exportJson
},
// 导出指定路径下的所有excel
"--all": {
"alias": ["-a"],
"desc": "export excel to json. --export [files]",
"action": exportAllJson,
"default": true
}
};
//cache of command's key ("--help"...)
var keys = Object.keys(commands);
var alias_map = {}; // mapping of alias_name -> name
for (var key in commands) {
var alias_array = commands[key].alias;
alias_array.forEach(function (e) {
alias_map[e] = key;
});
}
var parsed_cmds = []; //cmds of parsed out.
parsed_cmds = parseCommandLine(process.argv);
parsed_cmds.forEach(function (e) {
exec(e);
});
function checkExcel(path) {
var suffix = path.substr(path.lastIndexOf("."));
if (suffix == ".xlsx") {
return true;
}
console.error("路径不是excel文件:"+path);
return false;
}
function checkDir(path) {
var stat = fs.statSync(path);
if (stat.isDirectory()) {
return true;
}
console.error("路径不是文件夹:"+path);
return false;
}
/**
* export all json
* @param {} args
*/
function exportAllJson(args) {
if (args instanceof Array) {
if(args.length == 2){
var src = args[0];
var dst = args[1];
//是文件 存在 是excel表
if (!checkDir(src)) {
return;
}
//是文件夹 存在
if (!checkDir(src)) {
return;
}
glob(src+"/**/[^~$]*.xlsx", function (err, files) {
if (err) {
console.error("exportJson error:", err);
throw err;
}
files.forEach(function (element, index, array) {
console.log(index+":"+element);
xlsx.toJson(element,"", dst, config.xlsx.head, config.xlsx.arraySeparator);
});
});
} else {
console.log("参数错误请检查,参数长度:"+args.length);
}
}
}
/**
* export json
* args: --export [cmd_line_args] [.xlsx files list].
*/
function exportJson(args) {
if (args instanceof Array) {
if(args.length == 3){
var sheet = args[0];
var excel_file = args[1];
var dst = args[2];
//是文件 存在 是excel表
if (!checkExcel(excel_file)) {
return;
}
//是文件夹 存在
if (!checkDir(dst)) {
return;
}
if (excel_file.indexOf(".xlsx") != -1) {
xlsx.toJson(excel_file, sheet, dst, config.xlsx.head, config.xlsx.arraySeparator);
} else {
console.log("参数错误请检查,后缀错误");
}
} else {
console.log("参数错误请检查,参数长度:"+args.length);
}
}
}
function isEmptyArr(arr) {
if(arr == undefined || arr.length == 0) {
return true;
}
return false;
}
/**
* show help
*/
function showHelp() {
var usage = "usage: \n";
for (var p in commands) {
if (typeof commands[p] !== "function") {
usage += "\t " + p + "\t " + commands[p].alias + "\t " + commands[p].desc + "\n ";
}
}
usage += "\nexamples: ";
usage += "\n\n $node index.js --export\n\tthis will export all files configed to json.";
usage += "\n\n $node index.js --export ./excel/foo.xlsx ./excel/bar.xlsx\n\tthis will export foo and bar xlsx files.";
console.log(usage);
}
/**************************** parse command line *********************************/
/**
* execute a command
*/
function exec(cmd) {
if (typeof cmd.action === "function") {
cmd.action(cmd.args);
}
}
/**
* parse command line args
*/
function parseCommandLine(args) {
var parsed_cmds = [];
if (args.length < 2) {
alert('参数错误,至少输入两个参数');
} else {
var cli = args.slice(2);
var pos = 0;
var cmd;
cli.forEach(function (element, index, array) {
//replace alias name with real name.
if (element.indexOf('--') === -1 && element.indexOf('-') === 0) {
cli[index] = alias_map[element];
}
//parse command and args
if (cli[index].indexOf('--') === -1) {
cmd.args.push(cli[index]);
} else {
if (keys[cli[index]] == "undefined") {
throw new Error("not support command:" + cli[index]);
}
pos = index;
cmd = commands[cli[index]];
if (typeof cmd.args == 'undefined') {
cmd.args = [];
}
parsed_cmds.push(cmd);
}
});
}
return parsed_cmds;
}
/**
* default command when no command line argas provided.
*/
function defaultCommand() {
if (keys.length <= 0) {
throw new Error("Error: there is no command at all!");
}
for (var p in commands) {
if (commands[p]["default"]) {
return commands[p];
}
}
if (keys["--help"]) {
return commands["--help"];
} else {
return commands[keys[0]];
}
}
/*************************************************************************/