-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
161 lines (140 loc) · 3.85 KB
/
app.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
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
var os = require('os');
var fs = require('fs');
var path = require('path');
var xlsx2csv = require('./lib/index.js');
//Default configuration
var config = {
force: false,
sheet: undefined,
verbose: process.env.NODE_VERBOSE == "true" || process.env.NODE_VERBOSE == "1",
source: undefined,
target: undefined
};
//Prints help message
function help() {
console.log("Usage:");
console.log(" xlsx2csv [options] <input.xlsx> <output.csv>");
console.log("");
console.log("Options:");
console.log(" --force Force overwrite file");
console.log(" --data [type] Data type to export: formula, value or display");
console.log(" --sheet [name] Sheet name to export");
console.log(" --range [A1:C3] Range to export");
console.log(" --separator [char] CSV column separator, e.g. '\\t'");
console.log(" --line-end [char] End of line char(s), e.g. '\\r\\n'");
console.log(" --help Print this message");
console.log(" --verbose Enable detailed logging");
console.log(" --version Print version number");
console.log("");
console.log("Examples:");
console.log(" xlsx2csv --version");
console.log(" xlsx2csv --verbose --range A1:M30 --separator , file.xlsx");
console.log(" xlsx2csv --data formula --sheet Sample file.xlsx file.csv");
}
//Command line interface
var args = process.argv.slice(2);
for (var i = 0; i < args.length; i++) {
switch (args[i]) {
case "--help":
help();
process.exit(0);
break;
case "-f":
case "--force":
config.force = true;
break;
case "--sheet":
config.sheet = args[++i];
break;
case "--range":
config.range = args[++i]; //"A1:Z99"
break;
case "--data":
config.data = args[++i]; //"formula" or "value" or "display"
break;
case "--separator":
config.separator = args[++i].replace("\\t", "\t");
break;
case "--line-end":
config.lineEnd = args[++i].replace("\\r", "\r").replace("\\n", "\n");
break;
case "--verbose":
config.verbose = true;
break;
case "-v":
case "--version":
console.log(require('./package.json').version);
process.exit(0);
break;
default:
if (args[i].indexOf('-') == 0) {
console.error("Unknown command line argument: " + args[i]);
process.exit(2);
}
else if (!config.source) {
config.source = args[i];
}
else if (!config.target) {
config.target = args[i];
}
else {
console.error("Too many arguments: " + args[i]);
process.exit(2);
}
break;
}
}
if (args.length == 0) {
help();
process.exit(0);
}
//Check if the source file argument is defined
if (!config.source) {
console.error("Source file not defined!");
process.exit(2);
}
//Check if the source file exists
if (!fs.existsSync(config.source)) {
console.error("File not found: " + config.source);
process.exit(2);
}
//Check if output file is ready to overwrite
if (config.target && !config.force && fs.existsSync(config.target)) {
console.error("File already exists: " + config.target);
process.exit(3);
}
//Convert XLSX to CSV
var options = {
verbose: config.verbose,
data: config.data,
sheet: config.sheet,
range: config.range,
separator: config.separator,
lineEnd: config.lineEnd
};
xlsx2csv(config.source, options, function(error, result) {
if (error) {
console.error(error);
process.exit(1);
return;
}
if (!config.target) {
//Print to console
console.log(result);
if (config.verbose) {
console.log("Done!");
}
}
else {
//Write to file
fs.writeFileSync(config.target, result);
console.log("Done!");
}
process.exit(0);
});
/*
setTimeout(function() {
console.log("Timeout!");
process.exit(1);
}, 60000);
*/