| 
									
										
										
										
											2022-10-20 18:47:20 +00:00
										 |  |  | /* sheetjs (C) 2013-present  SheetJS -- https://sheetjs.com */ | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | /* eslint-env node */ | 
					
						
							|  |  |  | /* vim: set ts=2 ft=javascript: */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | var n = "xlsx"; | 
					
						
							|  |  |  | var X = require('xlsx'); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | try { require('exit-on-epipe'); } catch (e) {} | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | var fs = require('fs'), program; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | try { program = require('commander'); } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   [ | 
					
						
							|  |  |  |     "The `xlsx` command line tool is deprecated in favor of `xlsx-cli`.", | 
					
						
							|  |  |  |     "", | 
					
						
							|  |  |  |     "For new versions of node, we recommend using `npx`:", | 
					
						
							|  |  |  |     "    $ npx xlsx-cli --help", | 
					
						
							|  |  |  |     "", | 
					
						
							|  |  |  |     "For older versions of node, explicitly install `xlsx-cli` globally:", | 
					
						
							|  |  |  |     "    $ npm i -g xlsx-cli", | 
					
						
							|  |  |  |     "    $ xlsx-cli --help" | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   ].forEach(function (m) { console.error(m); }); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   process.exit(1); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | program | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   .version(X.version) | 
					
						
							|  |  |  |   .usage('[options] <file> [sheetname]') | 
					
						
							|  |  |  |   .option('-f, --file <file>', 'use specified workbook') | 
					
						
							|  |  |  |   .option('-s, --sheet <sheet>', 'print specified sheet (default first sheet)') | 
					
						
							|  |  |  |   .option('-N, --sheet-index <idx>', 'use specified sheet index (0-based)') | 
					
						
							|  |  |  |   .option('-p, --password <pw>', 'if file is encrypted, try with specified pw') | 
					
						
							|  |  |  |   .option('-l, --list-sheets', 'list sheet names and exit') | 
					
						
							|  |  |  |   .option('-o, --output <file>', 'output to specified file') | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   .option('-B, --xlsb', 'emit XLSB to <sheetname> or <file>.xlsb') | 
					
						
							|  |  |  |   .option('-M, --xlsm', 'emit XLSM to <sheetname> or <file>.xlsm') | 
					
						
							|  |  |  |   .option('-X, --xlsx', 'emit XLSX to <sheetname> or <file>.xlsx') | 
					
						
							|  |  |  |   .option('-I, --xlam', 'emit XLAM to <sheetname> or <file>.xlam') | 
					
						
							|  |  |  |   .option('-Y, --ods',  'emit ODS  to <sheetname> or <file>.ods') | 
					
						
							|  |  |  |   .option('-8, --xls',  'emit XLS  to <sheetname> or <file>.xls (BIFF8)') | 
					
						
							|  |  |  |   .option('-5, --biff5','emit XLS  to <sheetname> or <file>.xls (BIFF5)') | 
					
						
							|  |  |  |   .option('-4, --biff4','emit XLS  to <sheetname> or <file>.xls (BIFF4)') | 
					
						
							|  |  |  |   .option('-3, --biff3','emit XLS  to <sheetname> or <file>.xls (BIFF3)') | 
					
						
							|  |  |  |   .option('-2, --biff2','emit XLS  to <sheetname> or <file>.xls (BIFF2)') | 
					
						
							|  |  |  |   .option('-i, --xla',  'emit XLA to <sheetname> or <file>.xla') | 
					
						
							|  |  |  |   .option('-6, --xlml', 'emit SSML to <sheetname> or <file>.xls (2003 XML)') | 
					
						
							|  |  |  |   .option('-T, --fods', 'emit FODS to <sheetname> or <file>.fods (Flat ODS)') | 
					
						
							|  |  |  |   .option('--wk3',      'emit WK3  to <sheetname> or <file>.txt (Lotus WK3)') | 
					
						
							|  |  |  |   .option('--numbers',  'emit NUMBERS to <sheetname> or <file>.numbers') | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +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)') | 
					
						
							|  |  |  |   .option('-U, --dbf',  'emit DBF  to <sheetname> or <file>.dbf (MSVFP DBF)') | 
					
						
							|  |  |  |   .option('-K, --sylk', 'emit SYLK to <sheetname> or <file>.slk (Excel SYLK)') | 
					
						
							|  |  |  |   .option('-P, --prn',  'emit PRN  to <sheetname> or <file>.prn (Lotus PRN)') | 
					
						
							|  |  |  |   .option('-E, --eth',  'emit ETH  to <sheetname> or <file>.eth (Ethercalc)') | 
					
						
							|  |  |  |   .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)') | 
					
						
							|  |  |  |   .option('--wk1',      'emit WK1  to <sheetname> or <file>.txt (Lotus WK1)') | 
					
						
							|  |  |  |   .option('-z, --dump', 'dump internal representation as JSON') | 
					
						
							|  |  |  |   .option('--props',    'dump workbook properties as CSV') | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   .option('-F, --field-sep <sep>', 'CSV field separator', ",") | 
					
						
							|  |  |  |   .option('-R, --row-sep <sep>', 'CSV row separator', "\n") | 
					
						
							|  |  |  |   .option('-n, --sheet-rows <num>', 'Number of rows to process (0=all rows)') | 
					
						
							| 
									
										
										
										
											2023-10-11 02:18:57 +00:00
										 |  |  |   .option('--date-format <string>', 'output date format, for example yyyy-mm-dd') | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   .option('--codepage <cp>', 'default to specified codepage when ambiguous') | 
					
						
							|  |  |  |   .option('--sst', 'generate shared string table for XLS* formats') | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   .option('-d, --no-dim', 'recalculate worksheet range') | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   .option('--compress', 'use compression when writing XLSX/M/B and ODS') | 
					
						
							|  |  |  |   .option('--read', 'read but do not generate output') | 
					
						
							|  |  |  |   .option('--book', 'for single-sheet formats, emit a file per worksheet') | 
					
						
							|  |  |  |   .option('--all', 'parse everything; write as much as possible') | 
					
						
							|  |  |  |   .option('--dev', 'development mode') | 
					
						
							|  |  |  |   .option('--sparse', 'sparse mode') | 
					
						
							|  |  |  |   .option('-q, --quiet', 'quiet mode'); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | program.on('--help', function () { | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* flag, bookType, default ext */ | 
					
						
							|  |  |  | var workbook_formats = [ | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   ['xlsx',   'xlsx', 'xlsx'], | 
					
						
							|  |  |  |   ['xlsm',   'xlsm', 'xlsm'], | 
					
						
							|  |  |  |   ['xlam',   'xlam', 'xlam'], | 
					
						
							|  |  |  |   ['xlsb',   'xlsb', 'xlsb'], | 
					
						
							|  |  |  |   ['xls',     'xls',  'xls'], | 
					
						
							|  |  |  |   ['xla',     'xla',  'xla'], | 
					
						
							|  |  |  |   ['biff5', 'biff5',  'xls'], | 
					
						
							|  |  |  |   ['numbers', 'numbers', 'numbers'], | 
					
						
							|  |  |  |   ['ods',     'ods',  'ods'], | 
					
						
							|  |  |  |   ['fods',   'fods', 'fods'], | 
					
						
							|  |  |  |   ['wk3',     'wk3',  'wk3'] | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | ]; | 
					
						
							|  |  |  | var wb_formats_2 = [ | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   ['xlml',   'xlml', 'xls'] | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | ]; | 
					
						
							|  |  |  | program.parse(process.argv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var filename = '', sheetname = ''; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.args[0]) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   filename = program.args[0]; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   if (program.args[1]) sheetname = program.args[1]; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.sheet) sheetname = program.sheet; | 
					
						
							|  |  |  | if (program.file) filename = program.file; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (!filename) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   console.error(n + ": must specify a filename"); | 
					
						
							|  |  |  |   process.exit(1); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (!fs.existsSync(filename)) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   console.error(n + ": " + filename + ": No such file or directory"); | 
					
						
							|  |  |  |   process.exit(2); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var opts = {}, wb/*:?Workbook*/; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.listSheets) opts.bookSheets = true; | 
					
						
							|  |  |  | if (program.sheetRows) opts.sheetRows = program.sheetRows; | 
					
						
							|  |  |  | if (program.password) opts.password = program.password; | 
					
						
							|  |  |  | if (program.dim != null) opts.nodim = !program.dim; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | var seen = false; | 
					
						
							|  |  |  | function wb_fmt() { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   seen = true; | 
					
						
							|  |  |  |   opts.cellFormula = true; | 
					
						
							|  |  |  |   opts.cellNF = true; | 
					
						
							|  |  |  |   opts.xlfn = true; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   if (program.output) sheetname = program.output; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | function isfmt(m/*:string*/)/*:boolean*/ { | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   if (!program.output) return false; | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   var t = m.charAt(0) === "." ? m : "." + m; | 
					
						
							|  |  |  |   return program.output.slice(-t.length) === t; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | workbook_formats.forEach(function (m) { if (program[m[0]] || isfmt(m[0])) { wb_fmt(); } }); | 
					
						
							|  |  |  | wb_formats_2.forEach(function (m) { if (program[m[0]] || isfmt(m[0])) { wb_fmt(); } }); | 
					
						
							|  |  |  | if (seen) { | 
					
						
							|  |  |  | } else if (program.formulae) opts.cellFormula = true; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | else opts.cellFormula = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | var wopts = ({ WTF: opts.WTF, bookSST: program.sst }/*:any*/); | 
					
						
							|  |  |  | if (program.compress) wopts.compression = true; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.all) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   opts.cellFormula = true; | 
					
						
							|  |  |  |   opts.bookVBA = true; | 
					
						
							|  |  |  |   opts.cellNF = true; | 
					
						
							|  |  |  |   opts.cellHTML = true; | 
					
						
							|  |  |  |   opts.cellStyles = true; | 
					
						
							|  |  |  |   opts.sheetStubs = true; | 
					
						
							|  |  |  |   opts.cellDates = true; | 
					
						
							|  |  |  |   wopts.cellFormula = true; | 
					
						
							|  |  |  |   wopts.cellStyles = true; | 
					
						
							|  |  |  |   wopts.sheetStubs = true; | 
					
						
							|  |  |  |   wopts.bookVBA = true; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.sparse) opts.dense = false; else opts.dense = true; | 
					
						
							|  |  |  | if (program.codepage) opts.codepage = +program.codepage; | 
					
						
							|  |  |  | if (program.dateFormat) opts.dateNF = program.dateFormat; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.dev) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   opts.WTF = true; | 
					
						
							|  |  |  |   wb = X.readFile(filename, opts); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } else try { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   wb = X.readFile(filename, opts); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   var msg = (program.quiet) ? "" : n + ": error parsing "; | 
					
						
							|  |  |  |   msg += filename + ": " + e; | 
					
						
							|  |  |  |   console.error(msg); | 
					
						
							|  |  |  |   process.exit(3); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.read) process.exit(0); | 
					
						
							|  |  |  | if (!wb) { console.error(n + ": error parsing " + filename + ": empty workbook"); process.exit(0); } | 
					
						
							|  |  |  | /*:: if (!wb) throw new Error("unreachable"); */ | 
					
						
							|  |  |  | if (program.listSheets) { | 
					
						
							|  |  |  |   console.log((wb.SheetNames || []).join("\n")); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   process.exit(0); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.dump) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   console.log(JSON.stringify(wb)); | 
					
						
							|  |  |  |   process.exit(0); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (program.props) { | 
					
						
							|  |  |  |   if (wb) dump_props(wb); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   process.exit(0); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* full workbook formats */ | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | workbook_formats.forEach(function (m) { | 
					
						
							|  |  |  |   if (program[m[0]] || isfmt(m[0])) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     wopts.bookType = m[1]; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (wopts.bookType == "numbers") try { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       var XLSX_ZAHL = require("xlsx/dist/xlsx.zahl"); | 
					
						
							|  |  |  |       wopts.numbers = XLSX_ZAHL; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     } catch (e) {} | 
					
						
							|  |  |  |     if (wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     process.exit(0); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | wb_formats_2.forEach(function (m) { | 
					
						
							|  |  |  |   if (program[m[0]] || isfmt(m[0])) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     wopts.bookType = m[1]; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (wb) X.writeFile(wb, program.output || sheetname || ((filename || "") + "." + m[2]), wopts); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     process.exit(0); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var target_sheet = sheetname || ''; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (target_sheet === '') { | 
					
						
							|  |  |  |   if (+program.sheetIndex < (wb.SheetNames || []).length) target_sheet = wb.SheetNames[+program.sheetIndex]; | 
					
						
							|  |  |  |   else target_sheet = (wb.SheetNames || [""])[0]; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ws; | 
					
						
							|  |  |  | try { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   ws = wb.Sheets[target_sheet]; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   if (!ws) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     console.error("Sheet " + target_sheet + " cannot be found"); | 
					
						
							|  |  |  |     process.exit(3); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | } catch (e) { | 
					
						
							|  |  |  |   console.error(n + ": error parsing " + filename + " " + target_sheet + ": " + e); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   process.exit(4); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | if (!program.quiet && !program.book) console.error(target_sheet); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* single worksheet file formats */ | 
					
						
							|  |  |  | [ | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   ['biff2', '.xls'], | 
					
						
							|  |  |  |   ['biff3', '.xls'], | 
					
						
							|  |  |  |   ['biff4', '.xls'], | 
					
						
							|  |  |  |   ['sylk', '.slk'], | 
					
						
							|  |  |  |   ['html', '.html'], | 
					
						
							|  |  |  |   ['prn', '.prn'], | 
					
						
							|  |  |  |   ['eth', '.eth'], | 
					
						
							|  |  |  |   ['rtf', '.rtf'], | 
					
						
							|  |  |  |   ['txt', '.txt'], | 
					
						
							|  |  |  |   ['dbf', '.dbf'], | 
					
						
							|  |  |  |   ['wk1', '.wk1'], | 
					
						
							|  |  |  |   ['dif', '.dif'] | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | ].forEach(function (m) { | 
					
						
							|  |  |  |   if (program[m[0]] || isfmt(m[1])) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   wopts.bookType = m[0]; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (program.book) { | 
					
						
							|  |  |  |       /*:: if (wb == null) throw new Error("Unreachable"); */ | 
					
						
							|  |  |  |       wb.SheetNames.forEach(function (n, i) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | function outit(o, fn) { if (fn) fs.writeFileSync(fn, o); else console.log(o); } | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function doit(cb) { | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   /*:: if (!wb) throw new Error("unreachable"); */ | 
					
						
							|  |  |  |   if (program.book) wb.SheetNames.forEach(function (n, i) { | 
					
						
							|  |  |  |     /*:: if (!wb) throw new Error("unreachable"); */ | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     outit(cb(wb.Sheets[n]), (program.output || sheetname || filename) + "." + i); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   else outit(cb(ws), program.output); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var jso = {}; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  | switch (true) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   case program.formulae: | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     doit(function (ws) { return X.utils.sheet_to_formulae(ws).join("\n"); }); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     break; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   case program.arrays: jso.header = 1; | 
					
						
							|  |  |  |   /* falls through */ | 
					
						
							|  |  |  |   case program.rawJs: jso.raw = true; | 
					
						
							|  |  |  |   /* falls through */ | 
					
						
							|  |  |  |   case program.json: | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     doit(function (ws) { return JSON.stringify(X.utils.sheet_to_json(ws, jso)); }); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     break; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   default: | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (!program.book) { | 
					
						
							|  |  |  |       var stream = X.stream.to_csv(ws, { FS: program.fieldSep || ",", RS: program.rowSep || "\n" }); | 
					
						
							|  |  |  |       if (program.output) stream.pipe(fs.createWriteStream(program.output)); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       else stream.pipe(process.stdout); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     } else doit(function (ws) { return X.utils.sheet_to_csv(ws, { FS: program.fieldSep, RS: program.rowSep }); }); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |     break; | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function dump_props(wb/*:Workbook*/) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   var propaoa = []; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |   if (Object.assign && Object.entries) propaoa = Object.entries(Object.assign({}, wb.Props, wb.Custprops)); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |   else { | 
					
						
							|  |  |  |     var Keys/*:: :Array<string> = []*/, pi; | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (wb.Props) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       Keys = Object.keys(wb.Props); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |       for (pi = 0; pi < Keys.length; ++pi) { | 
					
						
							|  |  |  |         if (Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |     if (wb.Custprops) { | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       Keys = Object.keys(wb.Custprops); | 
					
						
							| 
									
										
										
										
											2025-01-06 02:51:20 +00:00
										 |  |  |       for (pi = 0; pi < Keys.length; ++pi) { | 
					
						
							|  |  |  |         if (Object.prototype.hasOwnProperty.call(Keys, Keys[pi])) propaoa.push([Keys[pi], Keys[/*::+*/Keys[pi]]]); | 
					
						
							| 
									
										
										
										
											2022-08-08 06:59:57 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   console.log(X.utils.sheet_to_csv(X.utils.aoa_to_sheet(propaoa))); | 
					
						
							| 
									
										
										
										
											2022-08-01 08:15:50 +00:00
										 |  |  | } |