UNPKG

xlsx-extractor

Version:
98 lines (96 loc) 3.09 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = __importDefault(require("commander")); const index_1 = require("../lib/index"); /** * Parse for the output option. * @param arg Argument of command line options. * @return Range. */ const parseRange = (arg) => { const result = { begin: 0, end: 0 }; const range = arg.split('-'); if (1 < range.length) { result.begin = Number(range[0]); result.end = Number(range[1]); } else { // Single mode result.begin = Number(range[0]); result.end = Number(range[0]); } return result; }; /** * Parse for the command line argumens. * @return Parse results. */ const parseArgv = () => { commander_1.default.usage('xlsx-extractor [options]') .description('Extract the colums/rows from XLSX file.') .option('-i, --input <File>', 'Path of the XLSX file') .option('-r, --range <Range>', 'Range of sheets to be output. Specify the numeric value of "N" or "N-N".', parseRange) .option('-c, --count', 'Outputs the number of sheet. This option overrides --range.') .version(require('../../package.json').version, '-v, --version'); commander_1.default.on('--help', () => { console.log(` Examples: $ xlsx-extractor -i sample.xlsx $ xlsx-extractor -i sample.xlsx -c $ xlsx-extractor -i sample.xlsx -r 3 $ xlsx-extractor -i sample.xlsx -r 1-5 See also: https://github.com/akabekobeko/npm-xlsx-extractor/issues`); }); // Print help and exit if there are no arguments if (process.argv.length < 3) { commander_1.default.help(); } commander_1.default.parse(process.argv); const opts = commander_1.default.opts(); return { input: opts.input || '', range: opts.range || { begin: 0, end: 0 } }; }; /** * Entry point of command line interface. */ const main = () => { const options = parseArgv(); if (options.count) { console.log(index_1.getSheetCount(options.input)); } else if (options.range.begin === 0 && options.range.end === 0) { index_1.extractAll(options.input) .then((sheets) => { console.log(JSON.stringify(sheets, null, ' ')); }) .catch((err) => { console.log(err); }); } else if (options.range.begin !== options.range.end) { index_1.extractRange(options.input, options.range.begin, options.range.end) .then((sheets) => { console.log(JSON.stringify(sheets, null, ' ')); }) .catch((err) => { console.log(err); }); } else { index_1.extract(options.input, options.range.begin) .then((sheet) => { console.log(JSON.stringify(sheet, null, ' ')); }) .catch((err) => { console.log(err); }); } }; main();