From c87eaf0228d1f9139211a4ebfa28cad702e84294 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 14 Jul 2014 22:29:17 -0400 Subject: [PATCH] version bump 0.3.10: markdown tables --- README.md | 11 +++++++---- bin/j.njs | 2 ++ j.js | 37 ++++++++++++++++++++++++++++++++++++- package.json | 2 +- test.js | 2 ++ tests.lst | 5 +++++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dd0e59b..3fcccf6 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,10 @@ is the parsed file. - `to_csv(w) / to_dsv(w, delim)` will generate CSV/DSV respectively - `to_json(w)` will generate JSON row objects - `to_html(w)` will generate simple HTML tables +- `to_formulae(w)` will generate lists of formulae - `to_xml(w)` will generate simple XML -- `to_xlsx(w)` will generate XLSX workbooks -- `to_xlsm(w)` will generate XLSM workbooks +- `to_xlsx(w) / to_xlsm(w) / to_xlsb(w)` will generate XLSX/XLSM/XLSB workbooks +- `to_md(w)` will generate markdown tables ## CLI Tool @@ -55,7 +56,7 @@ The node module ships with a binary `j` which has a help message: ``` $ j --help - Usage: j.njs [options] [sheetname] + Usage: j [options] [sheetname] Options: @@ -65,13 +66,15 @@ $ j --help -s, --sheet print specified sheet (default first sheet) -l, --list-sheets list sheet names and exit -o, --output output to specified file + -B, --xlsb emit XLSB to or .xlsb -M, --xlsm emit XLSM to or .xlsm -X, --xlsx emit XLSX to or .xlsx -S, --formulae print formulae -j, --json emit formatted JSON (all fields text) -J, --raw-js emit raw JS object (raw numbers) - -X, --xml emit XML + -x, --xml emit XML -H, --html emit HTML + -m, --markdown emit markdown table (with pipes) -F, --field-sep CSV field separator -R, --row-sep CSV row separator -n, --sheet-rows Number of rows to process (0=all rows) diff --git a/bin/j.njs b/bin/j.njs index a36ba4b..01eebf1 100755 --- a/bin/j.njs +++ b/bin/j.njs @@ -19,6 +19,7 @@ program .option('-J, --raw-js', 'emit raw JS object (raw numbers)') .option('-x, --xml', 'emit XML') .option('-H, --html', 'emit HTML') + .option('-m, --markdown', 'emit markdown table (with pipes)') .option('-F, --field-sep ', 'CSV field separator', ",") .option('-R, --row-sep ', 'CSV row separator', "\n") .option('-n, --sheet-rows ', 'Number of rows to process (0=all rows)') @@ -106,6 +107,7 @@ else if(program.json) oo = JSON.stringify(J.utils.to_json(w)[target_sheet]); else if(program.rawJs) oo = JSON.stringify(J.utils.to_json(w,true)[target_sheet]); else if(program.xml) oo = J.utils.to_xml(w)[target_sheet]; else if(program.html) oo = J.utils.to_html(w)[target_sheet]; +else if(program.markdown) oo = J.utils.to_md(w)[target_sheet]; else oo = J.utils.to_dsv(w, program.fieldSep, program.rowSep)[target_sheet]; if(program.output) fs.writeFileSync(program.output, oo); diff --git a/j.js b/j.js index 408d1fe..0c92e9b 100644 --- a/j.js +++ b/j.js @@ -61,9 +61,43 @@ function get_cols(sheet, XL) { return hdr; } +function to_md(w) { + var XL = w[0], wb = w[1]; + var tbl = {}; + wb.SheetNames.forEach(function(sheet) { + var ws = wb.Sheets[sheet]; + if(ws["!ref"] == null) return; + var src = "|", val, w; + var range = XL.utils.decode_range(ws["!ref"]); + var R = range.s.r, C; + for(C = range.s.c; C <= range.e.c; ++C) { + val = ws[XL.utils.encode_cell({c:C,r:R})]; + w = val == null ? "" : val.w !== undefined ? val.w : XL.utils.format_cell ? XL.utils.format_cell(val) : val.v; + src += w + "|"; + } + src += "\n|"; + for(C = range.s.c; C <= range.e.c; ++C) { + val = ws[XL.utils.encode_cell({c:C,r:R})]; + w = val == null ? "" : val.w !== undefined ? val.w : XL.utils.format_cell ? XL.utils.format_cell(val) : val.v; + src += " ---- |"; + } + src += "\n"; + for(R = range.s.r+1; R <= range.e.r; ++R) { + src += "|"; + for(C = range.s.c; C <= range.e.c; ++C) { + val = ws[XL.utils.encode_cell({c:C,r:R})]; + w = val == null ? "" : val.w !== undefined ? val.w : XL.utils.format_cell ? XL.utils.format_cell(val) : val.v; + src += w + "|"; + } + src += "\n"; + } + tbl[sheet] = src; + }); + return tbl; +} + function to_html(w) { var XL = w[0], wb = w[1]; - var json = to_json(w); var tbl = {}; wb.SheetNames.forEach(function(sheet) { var ws = wb.Sheets[sheet]; @@ -176,6 +210,7 @@ module.exports = { to_html: to_html, to_html_cols: to_html_cols, to_formulae: to_formulae, + to_md: to_md, get_cols: get_cols }, version: "XLS " + XLS.version + " ; XLSX " + XLSX.version diff --git a/package.json b/package.json index 12a4a08..bc63ac8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "j", - "version": "0.3.9", + "version": "0.3.10", "author": "sheetjs", "description": "CLI tool for working with XLS/XLSX/XLSM/XLSB files", "keywords": [ "excel", "xls", "xlsx", "xlsm", "xlsb", "office", "spreadsheet" ], diff --git a/test.js b/test.js index b69bcf6..b66c3ed 100644 --- a/test.js +++ b/test.js @@ -27,6 +27,8 @@ files.forEach(function(x) { J.utils.to_dsv(wb,",", "\n"); J.utils.to_dsv(wb,";", "\n"); J.utils.to_html(wb); + J.utils.to_html_cols(wb); + J.utils.to_md(wb); J.utils.to_xml(wb); }); it('should round-trip XLSX', x.substr(-8) == ".pending" || x.substr(-8) == ".nowrite" ? null : function() { diff --git a/tests.lst b/tests.lst index 013dff8..fab6b35 100644 --- a/tests.lst +++ b/tests.lst @@ -270,6 +270,7 @@ apachepoi_56482.xls apachepoi_56514.xlsx apachepoi_56563a.xls apachepoi_56563b.xls +apachepoi_56702.xlsx apachepoi_AbnormalSharedFormulaFlag.xls apachepoi_AreaErrPtg.xls apachepoi_AverageTaxRates.xlsx @@ -501,6 +502,10 @@ custom_properties.xlsb custom_properties.xlsb.xml custom_properties.xlsx custom_properties.xlsx.xml +defined_names_simple.xls +defined_names_simple.xlsb +defined_names_simple.xlsx +defined_names_simple.xml excel-reader-xlsx_data01.xlsx excel-reader-xlsx_data02.xlsx excel-reader-xlsx_error02.xlsx