| 
									
										
										
										
											2012-12-04 19:27:20 +00:00
										 |  |  | #!/usr/bin/env node | 
					
						
							| 
									
										
										
										
											2016-12-31 08:20:45 +00:00
										 |  |  | /* xlsx.js (C) 2013-present  SheetJS -- http://sheetjs.com */ | 
					
						
							| 
									
										
										
										
											2017-05-09 18:07:57 +00:00
										 |  |  | /* eslint-env node */ | 
					
						
							| 
									
										
										
										
											2014-02-26 19:30:32 +00:00
										 |  |  | /* vim: set ts=2 ft=javascript: */ | 
					
						
							| 
									
										
										
										
											2018-01-23 09:07:51 +00:00
										 |  |  | var n = "xlsx"; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | var X = require('../'); | 
					
						
							| 
									
										
										
										
											2021-05-13 19:02:31 +00:00
										 |  |  | try { X = require('../xlsx.flow'); } catch(e) {} | 
					
						
							| 
									
										
										
										
											2016-12-31 08:20:45 +00:00
										 |  |  | require('exit-on-epipe'); | 
					
						
							| 
									
										
										
										
											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)') | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | 	.option('-N, --sheet-index <idx>', 'use specified sheet index (0-based)') | 
					
						
							| 
									
										
										
										
											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') | 
					
						
							| 
									
										
										
										
											2017-02-22 06:57:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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') | 
					
						
							| 
									
										
										
										
											2017-12-30 05:40:35 +00:00
										 |  |  | 	.option('-I, --xlam', 'emit XLAM to <sheetname> or <file>.xlam') | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | 	.option('-Y, --ods',  'emit ODS  to <sheetname> or <file>.ods') | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	.option('-8, --xls',  'emit XLS  to <sheetname> or <file>.xls (BIFF8)') | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | 	.option('-5, --biff5','emit XLS  to <sheetname> or <file>.xls (BIFF5)') | 
					
						
							| 
									
										
										
										
											2021-09-26 22:51:19 +00:00
										 |  |  | 	.option('-4, --biff4','emit XLS  to <sheetname> or <file>.xls (BIFF4)') | 
					
						
							|  |  |  | 	.option('-3, --biff3','emit XLS  to <sheetname> or <file>.xls (BIFF3)') | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | 	.option('-2, --biff2','emit XLS  to <sheetname> or <file>.xls (BIFF2)') | 
					
						
							| 
									
										
										
										
											2018-01-23 09:07:51 +00:00
										 |  |  | 	.option('-i, --xla',  'emit XLA to <sheetname> or <file>.xla') | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | 	.option('-6, --xlml', 'emit SSML to <sheetname> or <file>.xls (2003 XML)') | 
					
						
							| 
									
										
										
										
											2017-04-03 00:16:03 +00:00
										 |  |  | 	.option('-T, --fods', 'emit FODS to <sheetname> or <file>.fods (Flat ODS)') | 
					
						
							| 
									
										
										
										
											2017-02-22 06:57:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	.option('-S, --formulae', 'emit list of values and formulae') | 
					
						
							|  |  |  | 	.option('-j, --json',     'emit formatted JSON (all fields text)') | 
					
						
							|  |  |  | 	.option('-J, --raw-js',   'emit raw JS object (raw numbers)') | 
					
						
							|  |  |  | 	.option('-A, --arrays',   'emit rows as JS objects (raw numbers)') | 
					
						
							|  |  |  | 	.option('-H, --html', 'emit HTML to <sheetname> or <file>.html') | 
					
						
							|  |  |  | 	.option('-D, --dif',  'emit DIF  to <sheetname> or <file>.dif (Lotus DIF)') | 
					
						
							| 
									
										
										
										
											2017-10-27 16:25:54 +00:00
										 |  |  | 	.option('-U, --dbf',  'emit DBF  to <sheetname> or <file>.dbf (MSVFP DBF)') | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	.option('-K, --sylk', 'emit SYLK to <sheetname> or <file>.slk (Excel SYLK)') | 
					
						
							|  |  |  | 	.option('-P, --prn',  'emit PRN  to <sheetname> or <file>.prn (Lotus PRN)') | 
					
						
							| 
									
										
										
										
											2017-12-04 04:41:41 +00:00
										 |  |  | 	.option('-E, --eth',  'emit ETH  to <sheetname> or <file>.eth (Ethercalc)') | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	.option('-t, --txt',  'emit TXT  to <sheetname> or <file>.txt (UTF-8 TSV)') | 
					
						
							|  |  |  | 	.option('-r, --rtf',  'emit RTF  to <sheetname> or <file>.txt (Table RTF)') | 
					
						
							| 
									
										
										
										
											2018-03-19 21:42:55 +00:00
										 |  |  | 	.option('-z, --dump', 'dump internal representation as JSON') | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 	.option('--props',    'dump workbook properties as CSV') | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)') | 
					
						
							| 
									
										
										
										
											2017-12-09 07:17:25 +00:00
										 |  |  | 	.option('--codepage <cp>', 'default to specified codepage when ambiguous') | 
					
						
							|  |  |  | 	.option('--req <module>', 'require module before processing') | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | 	.option('--sst', 'generate shared string table for XLS* formats') | 
					
						
							|  |  |  | 	.option('--compress', 'use compression when writing XLSX/M/B and ODS') | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | 	.option('--read', 'read but do not generate output') | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 	.option('--book', 'for single-sheet formats, emit a file per worksheet') | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | 	.option('--all', 'parse everything; write as much as possible') | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 	.option('--dev', 'development mode') | 
					
						
							| 
									
										
										
										
											2017-04-08 06:55:35 +00:00
										 |  |  | 	.option('--sparse', 'sparse mode') | 
					
						
							| 
									
										
										
										
											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+'/'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | /* flag, bookType, default ext */ | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | var workbook_formats = [ | 
					
						
							|  |  |  | 	['xlsx',   'xlsx', 'xlsx'], | 
					
						
							|  |  |  | 	['xlsm',   'xlsm', 'xlsm'], | 
					
						
							| 
									
										
										
										
											2017-12-30 05:40:35 +00:00
										 |  |  | 	['xlam',   'xlam', 'xlam'], | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	['xlsb',   'xlsb', 'xlsb'], | 
					
						
							|  |  |  | 	['xls',     'xls',  'xls'], | 
					
						
							| 
									
										
										
										
											2017-12-30 05:40:35 +00:00
										 |  |  | 	['xla',     'xla',  'xla'], | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	['biff5', 'biff5',  'xls'], | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	['ods',     'ods',  'ods'], | 
					
						
							|  |  |  | 	['fods',   'fods', 'fods'] | 
					
						
							|  |  |  | ]; | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | var wb_formats_2 = [ | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	['xlml',   'xlml', 'xls'] | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | ]; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | program.parse(process.argv); | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | var filename = '', sheetname = ''; | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 07:17:25 +00:00
										 |  |  | if(program.req) program.req.split(",").forEach(function(r) { | 
					
						
							|  |  |  | 	require((fs.existsSync(r) || fs.existsSync(r + '.js')) ? require('path').resolve(r) : r); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 20:50:45 +00:00
										 |  |  | var opts = {}, wb/*:?Workbook*/; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | var seen = false; | 
					
						
							|  |  |  | function wb_fmt() { | 
					
						
							|  |  |  | 	seen = true; | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | 	opts.cellFormula = true; | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 	opts.cellNF = true; | 
					
						
							|  |  |  | 	if(program.output) sheetname = program.output; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | function isfmt(m/*:string*/)/*:boolean*/ { | 
					
						
							| 
									
										
										
										
											2017-09-12 20:02:06 +00:00
										 |  |  | 	if(!program.output) return false; | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | 	var t = m.charAt(0) === "." ? m : "." + m; | 
					
						
							|  |  |  | 	return program.output.slice(-t.length) === t; | 
					
						
							| 
									
										
										
										
											2017-09-12 20:02:06 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | workbook_formats.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { wb_fmt(); } }); | 
					
						
							| 
									
										
										
										
											2017-09-12 20:02:06 +00:00
										 |  |  | wb_formats_2.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { wb_fmt(); } }); | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | if(seen) { | 
					
						
							|  |  |  | } else if(program.formulae) opts.cellFormula = true; | 
					
						
							| 
									
										
										
										
											2014-05-25 09:04:08 +00:00
										 |  |  | else opts.cellFormula = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | var wopts = ({WTF:opts.WTF, bookSST:program.sst}/*:any*/); | 
					
						
							|  |  |  | if(program.compress) wopts.compression = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | if(program.all) { | 
					
						
							|  |  |  | 	opts.cellFormula = true; | 
					
						
							| 
									
										
										
										
											2017-03-28 04:41:01 +00:00
										 |  |  | 	opts.bookVBA = true; | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | 	opts.cellNF = true; | 
					
						
							| 
									
										
										
										
											2017-04-21 22:02:02 +00:00
										 |  |  | 	opts.cellHTML = true; | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | 	opts.cellStyles = true; | 
					
						
							| 
									
										
										
										
											2017-03-16 01:17:24 +00:00
										 |  |  | 	opts.sheetStubs = true; | 
					
						
							|  |  |  | 	opts.cellDates = true; | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 	wopts.cellStyles = true; | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 	wopts.sheetStubs = true; | 
					
						
							| 
									
										
										
										
											2017-11-20 01:51:14 +00:00
										 |  |  | 	wopts.bookVBA = true; | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-08 06:55:35 +00:00
										 |  |  | if(program.sparse) opts.dense = false; else opts.dense = true; | 
					
						
							| 
									
										
										
										
											2017-12-09 07:17:25 +00:00
										 |  |  | if(program.codepage) opts.codepage = +program.codepage; | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 05:08:18 +00:00
										 |  |  | if(program.dev) { | 
					
						
							|  |  |  | 	opts.WTF = true; | 
					
						
							|  |  |  | 	wb = X.readFile(filename, opts); | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +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); | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | if(!wb) { console.error(n + ": error parsing " + filename + ": empty workbook"); process.exit(0); } | 
					
						
							|  |  |  | /*:: if(!wb) throw new Error("unreachable"); */ | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | if(program.listSheets) { | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | 	console.log((wb.SheetNames||[]).join("\n")); | 
					
						
							| 
									
										
										
										
											2012-12-04 19:27:20 +00:00
										 |  |  | 	process.exit(0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-19 21:42:55 +00:00
										 |  |  | if(program.dump) { | 
					
						
							|  |  |  | 	console.log(JSON.stringify(wb)); | 
					
						
							|  |  |  | 	process.exit(0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | if(program.props) { | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 	if(wb) dump_props(wb); | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 	process.exit(0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | /* full workbook formats */ | 
					
						
							| 
									
										
										
										
											2017-09-22 22:18:51 +00:00
										 |  |  | workbook_formats.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { | 
					
						
							|  |  |  | 		wopts.bookType = m[1]; | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 		if(wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts); | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | 		process.exit(0); | 
					
						
							|  |  |  | } }); | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 20:02:06 +00:00
										 |  |  | wb_formats_2.forEach(function(m) { if(program[m[0]] || isfmt(m[0])) { | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | 		wopts.bookType = m[1]; | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 		if(wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts); | 
					
						
							| 
									
										
										
										
											2017-03-14 08:19:51 +00:00
										 |  |  | 		process.exit(0); | 
					
						
							|  |  |  | } }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | var target_sheet = sheetname || ''; | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | if(target_sheet === '') { | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 	if(+program.sheetIndex < (wb.SheetNames||[]).length) target_sheet = wb.SheetNames[+program.sheetIndex]; | 
					
						
							| 
									
										
										
										
											2017-03-05 00:56:31 +00:00
										 |  |  | 	else target_sheet = (wb.SheetNames||[""])[0]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var ws; | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  | 	ws = wb.Sheets[target_sheet]; | 
					
						
							| 
									
										
										
										
											2017-05-16 17:45:35 +00:00
										 |  |  | 	if(!ws) { | 
					
						
							|  |  |  | 		console.error("Sheet " + target_sheet + " cannot be found"); | 
					
						
							|  |  |  | 		process.exit(3); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | } 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | if(!program.quiet && !program.book) console.error(target_sheet); | 
					
						
							| 
									
										
										
										
											2014-05-25 09:04:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | /* single worksheet file formats */ | 
					
						
							| 
									
										
										
										
											2017-04-01 07:32:12 +00:00
										 |  |  | [ | 
					
						
							|  |  |  | 	['biff2', '.xls'], | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	['biff3', '.xls'], | 
					
						
							|  |  |  | 	['biff4', '.xls'], | 
					
						
							| 
									
										
										
										
											2017-04-01 07:32:12 +00:00
										 |  |  | 	['sylk', '.slk'], | 
					
						
							| 
									
										
										
										
											2017-04-16 04:32:13 +00:00
										 |  |  | 	['html', '.html'], | 
					
						
							| 
									
										
										
										
											2017-04-03 00:16:03 +00:00
										 |  |  | 	['prn', '.prn'], | 
					
						
							| 
									
										
										
										
											2017-12-04 04:41:41 +00:00
										 |  |  | 	['eth', '.eth'], | 
					
						
							| 
									
										
										
										
											2017-10-02 08:15:36 +00:00
										 |  |  | 	['rtf', '.rtf'], | 
					
						
							| 
									
										
										
										
											2017-04-03 00:16:03 +00:00
										 |  |  | 	['txt', '.txt'], | 
					
						
							| 
									
										
										
										
											2017-10-27 16:25:54 +00:00
										 |  |  | 	['dbf', '.dbf'], | 
					
						
							| 
									
										
										
										
											2017-04-01 07:32:12 +00:00
										 |  |  | 	['dif', '.dif'] | 
					
						
							| 
									
										
										
										
											2017-09-12 20:02:06 +00:00
										 |  |  | ].forEach(function(m) { if(program[m[0]] || isfmt(m[1])) { | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 	wopts.bookType = m[0]; | 
					
						
							|  |  |  | 	if(program.book) { | 
					
						
							| 
									
										
										
										
											2017-11-20 01:51:14 +00:00
										 |  |  | 		/*:: if(wb == null) throw new Error("Unreachable"); */ | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 		wb.SheetNames.forEach(function(n, i) { | 
					
						
							|  |  |  | 			wopts.sheet = n; | 
					
						
							|  |  |  | 			X.writeFile(wb, (program.output || sheetname || filename || "") + m[1] + "." + i, wopts); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else X.writeFile(wb, program.output || sheetname || ((filename || "") + m[1]), wopts); | 
					
						
							|  |  |  | 	process.exit(0); | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | } }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | function outit(o, fn) { if(fn) fs.writeFileSync(fn, o); else console.log(o); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function doit(cb) { | 
					
						
							| 
									
										
										
										
											2017-11-20 01:51:14 +00:00
										 |  |  | 	/*:: if(!wb) throw new Error("unreachable"); */ | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 	if(program.book) wb.SheetNames.forEach(function(n, i) { | 
					
						
							| 
									
										
										
										
											2017-11-20 01:51:14 +00:00
										 |  |  | 		/*:: if(!wb) throw new Error("unreachable"); */ | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 		outit(cb(wb.Sheets[n]), (program.output || sheetname || filename) + "." + i); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	else outit(cb(ws), program.output); | 
					
						
							| 
									
										
										
										
											2017-04-09 04:03:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | var jso = {}; | 
					
						
							|  |  |  | switch(true) { | 
					
						
							|  |  |  | 	case program.formulae: | 
					
						
							|  |  |  | 		doit(function(ws) { return X.utils.sheet_to_formulae(ws).join("\n"); }); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case program.arrays: jso.header = 1; | 
					
						
							|  |  |  | 	/* falls through */ | 
					
						
							|  |  |  | 	case program.rawJs: jso.raw = true; | 
					
						
							|  |  |  | 	/* falls through */ | 
					
						
							|  |  |  | 	case program.json: | 
					
						
							|  |  |  | 		doit(function(ws) { return JSON.stringify(X.utils.sheet_to_json(ws,jso)); }); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		if(!program.book) { | 
					
						
							| 
									
										
										
										
											2021-09-26 22:51:19 +00:00
										 |  |  | 			var stream = X.stream.to_csv(ws, {FS:program.fieldSep||",", RS:program.rowSep||"\n"}); | 
					
						
							| 
									
										
										
										
											2017-11-15 18:14:02 +00:00
										 |  |  | 			if(program.output) stream.pipe(fs.createWriteStream(program.output)); | 
					
						
							|  |  |  | 			else stream.pipe(process.stdout); | 
					
						
							|  |  |  | 		} else doit(function(ws) { return X.utils.sheet_to_csv(ws,{FS:program.fieldSep, RS:program.rowSep}); }); | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2017-04-09 04:03:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 03:09:14 +00:00
										 |  |  | function dump_props(wb/*:Workbook*/) { | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 	var propaoa = []; | 
					
						
							|  |  |  | 	if(Object.assign && Object.entries) propaoa = Object.entries(Object.assign({}, wb.Props, wb.Custprops)); | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2019-11-01 03:09:14 +00:00
										 |  |  | 		var Keys/*:: :Array<string> = []*/, pi; | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 		if(wb.Props) { | 
					
						
							|  |  |  | 			Keys = Object.keys(wb.Props); | 
					
						
							|  |  |  | 			for(pi = 0; pi < Keys.length; ++pi) { | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 				if(Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]); | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(wb.Custprops) { | 
					
						
							|  |  |  | 			Keys = Object.keys(wb.Custprops); | 
					
						
							|  |  |  | 			for(pi = 0; pi < Keys.length; ++pi) { | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 				if(Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]); | 
					
						
							| 
									
										
										
										
											2018-05-05 06:34:37 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	console.log(X.utils.sheet_to_csv(X.utils.aoa_to_sheet(propaoa))); | 
					
						
							|  |  |  | } |