Skip to content
This repository has been archived by the owner on Dec 5, 2017. It is now read-only.

Commit

Permalink
version bump 0.3.10: markdown tables
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed Jul 15, 2014
1 parent 79f7448 commit c87eaf0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -55,7 +56,7 @@ The node module ships with a binary `j` which has a help message:
```
$ j --help
Usage: j.njs [options] <file> [sheetname]
Usage: j [options] <file> [sheetname]
Options:
Expand All @@ -65,13 +66,15 @@ $ j --help
-s, --sheet <sheet> print specified sheet (default first sheet)
-l, --list-sheets list sheet names and exit
-o, --output <file> output to specified file
-B, --xlsb emit XLSB to <sheetname> or <file>.xlsb
-M, --xlsm emit XLSM to <sheetname> or <file>.xlsm
-X, --xlsx emit XLSX to <sheetname> or <file>.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 <sep> CSV field separator
-R, --row-sep <sep> CSV row separator
-n, --sheet-rows <num> Number of rows to process (0=all rows)
Expand Down
2 changes: 2 additions & 0 deletions bin/j.njs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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)')
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 36 additions & 1 deletion j.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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" ],
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions tests.lst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c87eaf0

Please sign in to comment.