xlsx.njs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #!/usr/bin/env node
  2. /* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
  3. /* eslint-env node */
  4. /* vim: set ts=2 ft=javascript: */
  5. var n = "xlsx";
  6. var X = require('../');
  7. try { X = require('../xlsx.flow'); } catch(e) {}
  8. try { require('exit-on-epipe'); } catch(e) {}
  9. var fs = require('fs'), program;
  10. try { program = require('commander'); } catch(e) {
  11. [
  12. "The `xlsx` command line tool is deprecated in favor of `xlsx-cli`.",
  13. "",
  14. "For new versions of node, we recommend using `npx`:",
  15. " $ npx xlsx-cli --help",
  16. "",
  17. "For older versions of node, explicitly install `xlsx-cli` globally:",
  18. " $ npm i -g xlsx-cli",
  19. " $ xlsx-cli --help"
  20. ].forEach(function(m) { console.error(m); });
  21. process.exit(1);
  22. }
  23. program
  24. .version(X.version)
  25. .usage('[options] <file> [sheetname]')
  26. .option('-f, --file <file>', 'use specified workbook')
  27. .option('-s, --sheet <sheet>', 'print specified sheet (default first sheet)')
  28. .option('-N, --sheet-index <idx>', 'use specified sheet index (0-based)')
  29. .option('-p, --password <pw>', 'if file is encrypted, try with specified pw')
  30. .option('-l, --list-sheets', 'list sheet names and exit')
  31. .option('-o, --output <file>', 'output to specified file')
  32. .option('-B, --xlsb', 'emit XLSB to <sheetname> or <file>.xlsb')
  33. .option('-M, --xlsm', 'emit XLSM to <sheetname> or <file>.xlsm')
  34. .option('-X, --xlsx', 'emit XLSX to <sheetname> or <file>.xlsx')
  35. .option('-I, --xlam', 'emit XLAM to <sheetname> or <file>.xlam')
  36. .option('-Y, --ods', 'emit ODS to <sheetname> or <file>.ods')
  37. .option('-8, --xls', 'emit XLS to <sheetname> or <file>.xls (BIFF8)')
  38. .option('-5, --biff5','emit XLS to <sheetname> or <file>.xls (BIFF5)')
  39. .option('-4, --biff4','emit XLS to <sheetname> or <file>.xls (BIFF4)')
  40. .option('-3, --biff3','emit XLS to <sheetname> or <file>.xls (BIFF3)')
  41. .option('-2, --biff2','emit XLS to <sheetname> or <file>.xls (BIFF2)')
  42. .option('-i, --xla', 'emit XLA to <sheetname> or <file>.xla')
  43. .option('-6, --xlml', 'emit SSML to <sheetname> or <file>.xls (2003 XML)')
  44. .option('-T, --fods', 'emit FODS to <sheetname> or <file>.fods (Flat ODS)')
  45. .option('--wk3', 'emit WK3 to <sheetname> or <file>.txt (Lotus WK3)')
  46. .option('--numbers', 'emit NUMBERS to <sheetname> or <file>.numbers')
  47. .option('-S, --formulae', 'emit list of values and formulae')
  48. .option('-j, --json', 'emit formatted JSON (all fields text)')
  49. .option('-J, --raw-js', 'emit raw JS object (raw numbers)')
  50. .option('-A, --arrays', 'emit rows as JS objects (raw numbers)')
  51. .option('-H, --html', 'emit HTML to <sheetname> or <file>.html')
  52. .option('-D, --dif', 'emit DIF to <sheetname> or <file>.dif (Lotus DIF)')
  53. .option('-U, --dbf', 'emit DBF to <sheetname> or <file>.dbf (MSVFP DBF)')
  54. .option('-K, --sylk', 'emit SYLK to <sheetname> or <file>.slk (Excel SYLK)')
  55. .option('-P, --prn', 'emit PRN to <sheetname> or <file>.prn (Lotus PRN)')
  56. .option('-E, --eth', 'emit ETH to <sheetname> or <file>.eth (Ethercalc)')
  57. .option('-t, --txt', 'emit TXT to <sheetname> or <file>.txt (UTF-8 TSV)')
  58. .option('-r, --rtf', 'emit RTF to <sheetname> or <file>.txt (Table RTF)')
  59. .option('--wk1', 'emit WK1 to <sheetname> or <file>.txt (Lotus WK1)')
  60. .option('-z, --dump', 'dump internal representation as JSON')
  61. .option('--props', 'dump workbook properties as CSV')
  62. .option('-F, --field-sep <sep>', 'CSV field separator', ",")
  63. .option('-R, --row-sep <sep>', 'CSV row separator', "\n")
  64. .option('-n, --sheet-rows <num>', 'Number of rows to process (0=all rows)')
  65. .option('--codepage <cp>', 'default to specified codepage when ambiguous')
  66. .option('--req <module>', 'require module before processing')
  67. .option('--sst', 'generate shared string table for XLS* formats')
  68. .option('--compress', 'use compression when writing XLSX/M/B and ODS')
  69. .option('--read', 'read but do not generate output')
  70. .option('--book', 'for single-sheet formats, emit a file per worksheet')
  71. .option('--all', 'parse everything; write as much as possible')
  72. .option('--dev', 'development mode')
  73. .option('--sparse', 'sparse mode')
  74. .option('-q, --quiet', 'quiet mode');
  75. program.on('--help', function() {
  76. console.log(' Default output format is CSV');
  77. console.log(' Support email: dev@sheetjs.com');
  78. console.log(' Web Demo: http://oss.sheetjs.com/js-'+n+'/');
  79. });
  80. /* flag, bookType, default ext */
  81. var workbook_formats = [
  82. ['xlsx', 'xlsx', 'xlsx'],
  83. ['xlsm', 'xlsm', 'xlsm'],
  84. ['xlam', 'xlam', 'xlam'],
  85. ['xlsb', 'xlsb', 'xlsb'],
  86. ['xls', 'xls', 'xls'],
  87. ['xla', 'xla', 'xla'],
  88. ['biff5', 'biff5', 'xls'],
  89. ['numbers', 'numbers', 'numbers'],
  90. ['ods', 'ods', 'ods'],
  91. ['fods', 'fods', 'fods'],
  92. ['wk3', 'wk3', 'wk3']
  93. ];
  94. var wb_formats_2 = [
  95. ['xlml', 'xlml', 'xls']
  96. ];
  97. program.parse(process.argv);
  98. var filename = '', sheetname = '';
  99. if(program.args[0]) {
  100. filename = program.args[0];
  101. if(program.args[1]) sheetname = program.args[1];
  102. }
  103. if(program.sheet) sheetname = program.sheet;
  104. if(program.file) filename = program.file;
  105. if(!filename) {
  106. console.error(n + ": must specify a filename");
  107. process.exit(1);
  108. }
  109. if(!fs.existsSync(filename)) {
  110. console.error(n + ": " + filename + ": No such file or directory");
  111. process.exit(2);
  112. }
  113. if(program.req) program.req.split(",").forEach(function(r) {
  114. require((fs.existsSync(r) || fs.existsSync(r + '.js')) ? require('path').resolve(r) : r);
  115. });
  116. var opts = {}, wb/*:?Workbook*/;
  117. if(program.listSheets) opts.bookSheets = true;
  118. if(program.sheetRows) opts.sheetRows = program.sheetRows;
  119. if(program.password) opts.password = program.password;
  120. var seen = false;
  121. function wb_fmt() {
  122. seen = true;
  123. opts.cellFormula = true;
  124. opts.cellNF = true;
  125. opts.xlfn = true;
  126. if(program.output) sheetname = program.output;
  127. }
  128. function isfmt(m/*:string*/)/*:boolean*/ {
  129. if(!program.output) return false;
  130. var t = m.charAt(0) === "." ? m : "." + m;
  131. return program.output.slice(-t.length) === t;
  132. }
  133. workbook_formats.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { wb_fmt(); } });
  134. wb_formats_2.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { wb_fmt(); } });
  135. if(seen) {
  136. } else if(program.formulae) opts.cellFormula = true;
  137. else opts.cellFormula = false;
  138. var wopts = ({WTF:opts.WTF, bookSST:program.sst}/*:any*/);
  139. if(program.compress) wopts.compression = true;
  140. if(program.all) {
  141. opts.cellFormula = true;
  142. opts.bookVBA = true;
  143. opts.cellNF = true;
  144. opts.cellHTML = true;
  145. opts.cellStyles = true;
  146. opts.sheetStubs = true;
  147. opts.cellDates = true;
  148. wopts.cellFormula = true;
  149. wopts.cellStyles = true;
  150. wopts.sheetStubs = true;
  151. wopts.bookVBA = true;
  152. }
  153. if(program.sparse) opts.dense = false; else opts.dense = true;
  154. if(program.codepage) opts.codepage = +program.codepage;
  155. if(program.dev) {
  156. opts.WTF = true;
  157. wb = X.readFile(filename, opts);
  158. } else try {
  159. wb = X.readFile(filename, opts);
  160. } catch(e) {
  161. var msg = (program.quiet) ? "" : n + ": error parsing ";
  162. msg += filename + ": " + e;
  163. console.error(msg);
  164. process.exit(3);
  165. }
  166. if(program.read) process.exit(0);
  167. if(!wb) { console.error(n + ": error parsing " + filename + ": empty workbook"); process.exit(0); }
  168. /*:: if(!wb) throw new Error("unreachable"); */
  169. if(program.listSheets) {
  170. console.log((wb.SheetNames||[]).join("\n"));
  171. process.exit(0);
  172. }
  173. if(program.dump) {
  174. console.log(JSON.stringify(wb));
  175. process.exit(0);
  176. }
  177. if(program.props) {
  178. if(wb) dump_props(wb);
  179. process.exit(0);
  180. }
  181. /* full workbook formats */
  182. workbook_formats.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) {
  183. wopts.bookType = m[1];
  184. if(wopts.bookType == "numbers") try {
  185. var XLSX_ZAHL = require("../dist/xlsx.zahl");
  186. wopts.numbers = XLSX_ZAHL;
  187. } catch(e) {}
  188. if(wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts);
  189. process.exit(0);
  190. } });
  191. wb_formats_2.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) {
  192. wopts.bookType = m[1];
  193. if(wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts);
  194. process.exit(0);
  195. } });
  196. var target_sheet = sheetname || '';
  197. if(target_sheet === '') {
  198. if(+program.sheetIndex < (wb.SheetNames||[]).length) target_sheet = wb.SheetNames[+program.sheetIndex];
  199. else target_sheet = (wb.SheetNames||[""])[0];
  200. }
  201. var ws;
  202. try {
  203. ws = wb.Sheets[target_sheet];
  204. if(!ws) {
  205. console.error("Sheet " + target_sheet + " cannot be found");
  206. process.exit(3);
  207. }
  208. } catch(e) {
  209. console.error(n + ": error parsing "+filename+" "+target_sheet+": " + e);
  210. process.exit(4);
  211. }
  212. if(!program.quiet && !program.book) console.error(target_sheet);
  213. /* single worksheet file formats */
  214. [
  215. ['biff2', '.xls'],
  216. ['biff3', '.xls'],
  217. ['biff4', '.xls'],
  218. ['sylk', '.slk'],
  219. ['html', '.html'],
  220. ['prn', '.prn'],
  221. ['eth', '.eth'],
  222. ['rtf', '.rtf'],
  223. ['txt', '.txt'],
  224. ['dbf', '.dbf'],
  225. ['wk1', '.wk1'],
  226. ['dif', '.dif']
  227. ].forEach(function(m) { if(program[m[0]] || isfmt(m[1])) {
  228. wopts.bookType = m[0];
  229. if(program.book) {
  230. /*:: if(wb == null) throw new Error("Unreachable"); */
  231. wb.SheetNames.forEach(function(n, i) {
  232. wopts.sheet = n;
  233. X.writeFile(wb, (program.output || sheetname || filename || "") + m[1] + "." + i, wopts);
  234. });
  235. } else X.writeFile(wb, program.output || sheetname || ((filename || "") + m[1]), wopts);
  236. process.exit(0);
  237. } });
  238. function outit(o, fn) { if(fn) fs.writeFileSync(fn, o); else console.log(o); }
  239. function doit(cb) {
  240. /*:: if(!wb) throw new Error("unreachable"); */
  241. if(program.book) wb.SheetNames.forEach(function(n, i) {
  242. /*:: if(!wb) throw new Error("unreachable"); */
  243. outit(cb(wb.Sheets[n]), (program.output || sheetname || filename) + "." + i);
  244. });
  245. else outit(cb(ws), program.output);
  246. }
  247. var jso = {};
  248. switch(true) {
  249. case program.formulae:
  250. doit(function(ws) { return X.utils.sheet_to_formulae(ws).join("\n"); });
  251. break;
  252. case program.arrays: jso.header = 1;
  253. /* falls through */
  254. case program.rawJs: jso.raw = true;
  255. /* falls through */
  256. case program.json:
  257. doit(function(ws) { return JSON.stringify(X.utils.sheet_to_json(ws,jso)); });
  258. break;
  259. default:
  260. if(!program.book) {
  261. var stream = X.stream.to_csv(ws, {FS:program.fieldSep||",", RS:program.rowSep||"\n"});
  262. if(program.output) stream.pipe(fs.createWriteStream(program.output));
  263. else stream.pipe(process.stdout);
  264. } else doit(function(ws) { return X.utils.sheet_to_csv(ws,{FS:program.fieldSep, RS:program.rowSep}); });
  265. break;
  266. }
  267. function dump_props(wb/*:Workbook*/) {
  268. var propaoa = [];
  269. if(Object.assign && Object.entries) propaoa = Object.entries(Object.assign({}, wb.Props, wb.Custprops));
  270. else {
  271. var Keys/*:: :Array<string> = []*/, pi;
  272. if(wb.Props) {
  273. Keys = Object.keys(wb.Props);
  274. for(pi = 0; pi < Keys.length; ++pi) {
  275. if(Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]);
  276. }
  277. }
  278. if(wb.Custprops) {
  279. Keys = Object.keys(wb.Custprops);
  280. for(pi = 0; pi < Keys.length; ++pi) {
  281. if(Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]);
  282. }
  283. }
  284. }
  285. console.log(X.utils.sheet_to_csv(X.utils.aoa_to_sheet(propaoa)));
  286. }