2012-12-04 19:27:20 +00:00
|
|
|
#!/usr/bin/env node
|
2015-04-02 20:32:22 +00:00
|
|
|
/* xlsx.js (C) 2013-2015 SheetJS -- http://sheetjs.com */
|
2014-01-23 06:20:19 +00:00
|
|
|
var n = "xlsx";
|
2014-02-26 19:30:32 +00:00
|
|
|
/* vim: set ts=2 ft=javascript: */
|
2014-01-23 06:20:19 +00:00
|
|
|
var X = require('../');
|
2013-10-30 22:26:31 +00:00
|
|
|
var fs = require('fs'), program = require('commander');
|
|
|
|
program
|
2014-01-23 06:20:19 +00:00
|
|
|
.version(X.version)
|
2013-10-30 22:26:31 +00:00
|
|
|
.usage('[options] <file> [sheetname]')
|
|
|
|
.option('-f, --file <file>', 'use specified workbook')
|
|
|
|
.option('-s, --sheet <sheet>', 'print specified sheet (default first sheet)')
|
2015-04-02 20:32:22 +00:00
|
|
|
.option('-p, --password <pw>', 'if file is encrypted, try with specified pw')
|
2013-10-30 22:26:31 +00:00
|
|
|
.option('-l, --list-sheets', 'list sheet names and exit')
|
2014-05-16 00:33:34 +00:00
|
|
|
.option('-o, --output <file>', 'output to specified file')
|
2014-05-29 22:30:03 +00:00
|
|
|
.option('-B, --xlsb', 'emit XLSB to <sheetname> or <file>.xlsb')
|
2014-05-16 00:33:34 +00:00
|
|
|
.option('-M, --xlsm', 'emit XLSM to <sheetname> or <file>.xlsm')
|
|
|
|
.option('-X, --xlsx', 'emit XLSX to <sheetname> or <file>.xlsx')
|
2014-01-23 06:20:19 +00:00
|
|
|
.option('-S, --formulae', 'print formulae')
|
2014-05-16 00:33:34 +00:00
|
|
|
.option('-j, --json', 'emit formatted JSON (all fields text)')
|
|
|
|
.option('-J, --raw-js', 'emit raw JS object (raw numbers)')
|
2014-01-23 06:20:19 +00:00
|
|
|
.option('-F, --field-sep <sep>', 'CSV field separator', ",")
|
|
|
|
.option('-R, --row-sep <sep>', 'CSV row separator', "\n")
|
2014-02-19 03:03:28 +00:00
|
|
|
.option('-n, --sheet-rows <num>', 'Number of rows to process (0=all rows)')
|
2014-07-28 13:22:32 +00:00
|
|
|
.option('--sst', 'generate sst')
|
2014-05-25 09:04:08 +00:00
|
|
|
.option('--perf', 'do not generate output')
|
2014-06-02 05:19:07 +00:00
|
|
|
.option('--all', 'parse everything; XLS[XMB] write as much as possible')
|
2013-10-30 22:26:31 +00:00
|
|
|
.option('--dev', 'development mode')
|
|
|
|
.option('--read', 'read but do not print out contents')
|
2014-01-23 06:20:19 +00:00
|
|
|
.option('-q, --quiet', 'quiet mode');
|
|
|
|
|
|
|
|
program.on('--help', function() {
|
2014-05-16 00:33:34 +00:00
|
|
|
console.log(' Default output format is CSV');
|
2014-01-23 06:20:19 +00:00
|
|
|
console.log(' Support email: dev@sheetjs.com');
|
|
|
|
console.log(' Web Demo: http://oss.sheetjs.com/js-'+n+'/');
|
|
|
|
});
|
|
|
|
|
|
|
|
program.parse(process.argv);
|
2013-10-30 22:26:31 +00:00
|
|
|
|
2014-08-26 17:40:04 +00:00
|
|
|
/* see https://github.com/SheetJS/j/issues/4 */
|
|
|
|
if(process.version === 'v0.10.31') {
|
|
|
|
var msgs = [
|
|
|
|
"node v0.10.31 is known to crash on OSX and Linux, refusing to proceed.",
|
|
|
|
"see https://github.com/SheetJS/j/issues/4 for the relevant discussion.",
|
|
|
|
"see https://github.com/joyent/node/issues/8208 for the relevant node issue"
|
|
|
|
];
|
|
|
|
msgs.forEach(function(m) { console.error(m); });
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2013-10-30 22:26:31 +00:00
|
|
|
var filename, sheetname = '';
|
|
|
|
if(program.args[0]) {
|
|
|
|
filename = program.args[0];
|
|
|
|
if(program.args[1]) sheetname = program.args[1];
|
2012-12-04 19:27:20 +00:00
|
|
|
}
|
2013-10-30 22:26:31 +00:00
|
|
|
if(program.sheet) sheetname = program.sheet;
|
|
|
|
if(program.file) filename = program.file;
|
|
|
|
|
|
|
|
if(!filename) {
|
2014-05-16 00:33:34 +00:00
|
|
|
console.error(n + ": must specify a filename");
|
2012-12-04 19:27:20 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2013-10-30 22:26:31 +00:00
|
|
|
|
|
|
|
if(!fs.existsSync(filename)) {
|
2014-05-16 00:33:34 +00:00
|
|
|
console.error(n + ": " + filename + ": No such file or directory");
|
2013-10-30 22:26:31 +00:00
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2014-02-15 05:08:18 +00:00
|
|
|
var opts = {}, wb;
|
2014-02-13 06:22:42 +00:00
|
|
|
if(program.listSheets) opts.bookSheets = true;
|
2014-02-19 03:03:28 +00:00
|
|
|
if(program.sheetRows) opts.sheetRows = program.sheetRows;
|
2015-04-02 20:32:22 +00:00
|
|
|
if(program.password) opts.password = program.password;
|
2014-05-16 00:33:34 +00:00
|
|
|
if(program.xlsx || program.xlsm || program.xlsb) {
|
|
|
|
opts.cellNF = true;
|
|
|
|
if(program.output) sheetname = program.output;
|
|
|
|
}
|
2014-05-25 09:04:08 +00:00
|
|
|
else if(program.formulae);
|
|
|
|
else opts.cellFormula = false;
|
|
|
|
|
2014-06-02 05:19:07 +00:00
|
|
|
if(program.all) {
|
|
|
|
opts.cellFormula = true;
|
|
|
|
opts.cellNF = true;
|
|
|
|
opts.cellStyles = true;
|
|
|
|
}
|
|
|
|
|
2014-02-15 05:08:18 +00:00
|
|
|
if(program.dev) {
|
|
|
|
X.verbose = 2;
|
|
|
|
opts.WTF = true;
|
|
|
|
wb = X.readFile(filename, opts);
|
|
|
|
}
|
2013-10-30 22:26:31 +00:00
|
|
|
else try {
|
2014-02-13 06:22:42 +00:00
|
|
|
wb = X.readFile(filename, opts);
|
2013-10-30 22:26:31 +00:00
|
|
|
} catch(e) {
|
2014-05-16 00:33:34 +00:00
|
|
|
var msg = (program.quiet) ? "" : n + ": error parsing ";
|
2013-10-30 22:26:31 +00:00
|
|
|
msg += filename + ": " + e;
|
|
|
|
console.error(msg);
|
|
|
|
process.exit(3);
|
|
|
|
}
|
|
|
|
if(program.read) process.exit(0);
|
|
|
|
|
|
|
|
if(program.listSheets) {
|
|
|
|
console.log(wb.SheetNames.join("\n"));
|
2012-12-04 19:27:20 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2013-10-30 22:26:31 +00:00
|
|
|
|
2014-05-25 09:04:08 +00:00
|
|
|
var wopts = {WTF:opts.WTF, bookSST:program.sst};
|
2014-05-16 00:33:34 +00:00
|
|
|
|
|
|
|
if(program.xlsx) return X.writeFile(wb, sheetname || (filename + ".xlsx"), wopts);
|
|
|
|
if(program.xlsm) return X.writeFile(wb, sheetname || (filename + ".xlsm"), wopts);
|
|
|
|
if(program.xlsb) return X.writeFile(wb, sheetname || (filename + ".xlsb"), wopts);
|
|
|
|
|
2013-10-30 22:26:31 +00:00
|
|
|
var target_sheet = sheetname || '';
|
|
|
|
if(target_sheet === '') target_sheet = wb.SheetNames[0];
|
|
|
|
|
|
|
|
var ws;
|
|
|
|
try {
|
|
|
|
ws = wb.Sheets[target_sheet];
|
|
|
|
if(!ws) throw "Sheet " + target_sheet + " cannot be found";
|
|
|
|
} catch(e) {
|
2014-05-16 00:33:34 +00:00
|
|
|
console.error(n + ": error parsing "+filename+" "+target_sheet+": " + e);
|
2013-10-30 22:26:31 +00:00
|
|
|
process.exit(4);
|
2012-12-04 19:27:20 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:04:08 +00:00
|
|
|
if(program.perf) return;
|
|
|
|
|
2014-07-28 13:22:32 +00:00
|
|
|
var oo = "";
|
2014-05-16 00:50:55 +00:00
|
|
|
if(!program.quiet) console.error(target_sheet);
|
2014-05-16 00:33:34 +00:00
|
|
|
if(program.formulae) oo = X.utils.get_formulae(ws).join("\n");
|
|
|
|
else if(program.json) oo = JSON.stringify(X.utils.sheet_to_row_object_array(ws));
|
|
|
|
else if(program.rawJs) oo = JSON.stringify(X.utils.sheet_to_row_object_array(ws,{raw:true}));
|
|
|
|
else oo = X.utils.make_csv(ws, {FS:program.fieldSep, RS:program.rowSep});
|
|
|
|
|
|
|
|
if(program.output) fs.writeFileSync(program.output, oo);
|
2014-05-16 00:50:55 +00:00
|
|
|
else console.log(oo);
|