This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

version bump 0.6.1: more formatting

- updated SSF to 0.5.3

tests without baselines now try to generate CSV output
  • Loading branch information...
SheetJSDev committed Jan 23, 2014
1 parent c52fe38 commit 7fc99d64cb0b655368b159168590ca4c6dbd49a5
Showing with 123 additions and 69 deletions.
  1. +15 −14 bin/xls2csv.njs
  2. +1 −1 bits/01_version.js
  3. +33 −24 bits/10_ssf.js
  4. +15 −1 bits/30_bifffunc.js
  5. +1 −1 bits/80_xls.js
  6. +1 −1 package.json
  7. +7 −0 test.js
  8. +50 −27 xls.js
View
@@ -1,10 +1,11 @@
#!/usr/bin/env node
/* xls.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
+var n = "xls";
/* vim: set ts=2: */
-var XLS = require('../');
+var X = require('../');
var fs = require('fs'), program = require('commander');
program
- .version(XLS.version)
+ .version(X.version)
.usage('[options] <file> [sheetname]')
.option('-f, --file <file>', 'use specified workbook')
.option('-s, --sheet <sheet>', 'print specified sheet (default first sheet)')
@@ -20,7 +21,7 @@ program
program.on('--help', function() {
console.log(' Support email: dev@sheetjs.com');
- console.log(' Web Demo: http://oss.sheetjs.com/js-xls/');
+ console.log(' Web Demo: http://oss.sheetjs.com/js-'+n+'/');
});
program.parse(process.argv);
@@ -34,23 +35,23 @@ if(program.sheet) sheetname = program.sheet;
if(program.file) filename = program.file;
if(!filename) {
- console.error("xls2csv: must specify a filename");
+ console.error(n + "2csv: must specify a filename");
process.exit(1);
}
if(!fs.existsSync(filename)) {
- console.error("xls2csv: " + filename + ": No such file or directory");
+ console.error(n + "2csv: " + filename + ": No such file or directory");
process.exit(2);
}
-if(program.dev) XLS.verbose = 2;
+if(program.dev) X.verbose = 2;
var wb;
-if(program.dev) wb = XLS.readFile(filename);
+if(program.dev) wb = X.readFile(filename);
else try {
- wb = XLS.readFile(filename);
+ wb = X.readFile(filename);
} catch(e) {
- var msg = (program.quiet) ? "" : "xls2csv: error parsing ";
+ var msg = (program.quiet) ? "" : n + "2csv: error parsing ";
msg += filename + ": " + e;
console.error(msg);
process.exit(3);
@@ -70,12 +71,12 @@ try {
ws = wb.Sheets[target_sheet];
if(!ws) throw "Sheet " + target_sheet + " cannot be found";
} catch(e) {
- console.error("xls2csv: error parsing "+filename+" "+target_sheet+": " + e);
+ console.error(n + "2csv: error parsing "+filename+" "+target_sheet+": " + e);
process.exit(4);
}
if(!program.quiet) console.error(target_sheet);
-if(program.formulae) console.log(XLS.utils.get_formulae(ws).join("\n"));
-else if(program.json) console.log(JSON.stringify(XLS.utils.sheet_to_row_object_array(ws)));
-else if(program.rawJs) console.log(JSON.stringify(XLS.utils.sheet_to_row_object_array(ws,{raw:true})));
-else console.log(XLS.utils.make_csv(ws, {FS:program.fieldSep, RS:program.rowSep}));
+if(program.formulae) console.log(X.utils.get_formulae(ws).join("\n"));
+else if(program.json) console.log(JSON.stringify(X.utils.sheet_to_row_object_array(ws)));
+else if(program.rawJs) console.log(JSON.stringify(X.utils.sheet_to_row_object_array(ws,{raw:true})));
+else console.log(X.utils.make_csv(ws, {FS:program.fieldSep, RS:program.rowSep}));
View
@@ -1 +1 @@
-XLS.version = '0.6.0';
+XLS.version = '0.6.1';
View
@@ -5,6 +5,7 @@ var _strrev = function(x) { return String(x).split("").reverse().join("");};
function fill(c,l) { return new Array(l+1).join(c); }
function pad(v,d,c){var t=String(v);return t.length>=d?t:(fill(c||0,d-t.length)+t);}
function rpad(v,d,c){var t=String(v);return t.length>=d?t:(t+fill(c||0,d-t.length));}
+SSF.version = '0.5.3';
/* Options */
var opts_fmt = {};
function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];}
@@ -40,7 +41,9 @@ var table_fmt = {
46: '[h]:mm:ss',
47: 'mmss.0',
48: '##0.0E+0',
- 49: '@'
+ 49: '@',
+ 56: '"上午/下午 "hh"時"mm"分"ss"秒 "',
+ 65535: 'General'
};
var days = [
['Sun', 'Sunday'],
@@ -192,7 +195,6 @@ var write_date = function(type, fmt, val) {
} return fmt.length === 3 ? o : pad(o, 2);
/* TODO: handle the ECMA spec format ee -> yy */
case 'e': { return val.y; } break;
- case 'A': return (val.h>=12 ? 'P' : 'A') + fmt.substr(1);
default: throw 'bad format type ' + type + ' in ' + fmt;
}
};
@@ -226,30 +228,37 @@ var write_num = function(type, fmt, val) {
}
if(fmt[0] === "$") return "$"+write_num(type,fmt.substr(fmt[1]==' '?2:1),val);
var r, ff, aval = val < 0 ? -val : val, sign = val < 0 ? "-" : "";
- if((r = fmt.match(/# (\?+) \/ (\d+)/))) {
- var den = Number(r[2]), rnd = Math.round(aval * den), base = Math.floor(rnd/den);
+ if((r = fmt.match(/# (\?+)([ ]?)\/([ ]?)(\d+)/))) {
+ var den = Number(r[4]), rnd = Math.round(aval * den), base = Math.floor(rnd/den);
var myn = (rnd - base*den), myd = den;
- return sign + (base?base:"") + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[2].length) : pad(myn,r[1].length," ") + "/" + pad(myd,r[2].length));
+ return sign + (base?base:"") + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad(myn,r[1].length," ") + r[2] + "/" + r[3] + pad(myd,r[4].length));
+ }
+ if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
+ if(fmt.match(/^[#?]+$/)) return String(Math.round(val)).replace(/^0$/,"");
+ if(r = fmt.match(/^#*0+\.(0+)/)) {
+ o = Math.round(val * Math.pow(10,r[1].length));
+ return String(o/Math.pow(10,r[1].length)).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.([0-9]*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
+ }
+ if(r = fmt.match(/^# ([?]+)([ ]?)\/([ ]?)([?]+)/)) {
+ var rr = Math.min(Math.max(r[1].length, r[4].length),7);
+ ff = frac(aval, Math.pow(10,rr)-1, true);
+ return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],rr," ") + r[2] + "/" + r[3] + rpad(ff[2],rr," "): fill(" ", 2*rr+1 + r[2].length + r[3].length));
}
- if(fmt.match(/^00*$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
- if(fmt.match(/^####*$/)) return Math.round(val);
switch(fmt) {
- case "0": return Math.round(val);
- case "0.0": o = Math.round(val*10);
- return String(o/10).replace(/^([^\.]+)$/,"$1.0").replace(/\.$/,".0");
- case "0.00": o = Math.round(val*100);
- return String(o/100).replace(/^([^\.]+)$/,"$1.00").replace(/\.$/,".00").replace(/\.([0-9])$/,".$1"+"0");
- case "0.000": o = Math.round(val*1000);
- return String(o/1000).replace(/^([^\.]+)$/,"$1.000").replace(/\.$/,".000").replace(/\.([0-9])$/,".$1"+"00").replace(/\.([0-9][0-9])$/,".$1"+"0");
+ case "0": case "#0": return Math.round(val);
case "#.##": o = Math.round(val*100);
return String(o/100).replace(/^([^\.]+)$/,"$1.").replace(/^0\.$/,".");
case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : "";
case "#,##0": return sign + commaify(String(Math.round(aval)));
- case "#,##0.0": r = Math.round((val-Math.floor(val))*10); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + r;
- case "#,##0.00": r = Math.round((val-Math.floor(val))*100); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + (r < 10 ? "0"+r:r);
- case "# ? / ?": ff = frac(aval, 9, true); return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] === 0 ? " " : ff[1] + "/" + ff[2]);
- case "# ?? / ??": ff = frac(aval, 99, true); return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],2," ") + "/" + rpad(ff[2],2," ") : " ");
- case "# ??? / ???": ff = frac(aval, 999, true); return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],3," ") + "/" + rpad(ff[2],3," ") : " ");
+ case "#,##0.0": r = Math.round((val-Math.floor(val))*10); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,1,"0");
+ case "#,##0.00": r = Math.round((val-Math.floor(val))*100); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,2,"0");
+ case "#,##0.000": r = Math.round((val-Math.floor(val))*1000); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,3,0);
+ case "#,##0.0000": r = Math.round((val-Math.floor(val))*10000); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,4,0);
+ case "#,##0.00000": r = Math.round((val-Math.floor(val))*Math.pow(10,5)); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,5,0);
+ case "#,##0.000000": r = Math.round((val-Math.floor(val))*Math.pow(10,6)); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,6,0);
+ case "#,##0.0000000": r = Math.round((val-Math.floor(val))*Math.pow(10,7)); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,7,0);
+ case "#,##0.00000000": r = Math.round((val-Math.floor(val))*Math.pow(10,8)); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,8,0);
+ case "#,##0.000000000": r = Math.round((val-Math.floor(val))*Math.pow(10,9)); return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(r,9,0);
default:
}
throw new Error("unsupported format |" + fmt + "|");
@@ -334,7 +343,7 @@ function eval_fmt(fmt, v, opts, flen) {
out.push({t:'D', v:o}); break;
case ' ': out.push({t:c,v:c}); ++i; break;
default:
- if("$-+/():!^&'~{}<>=".indexOf(c) === -1)
+ if(",$-+/():!^&'~{}<>=".indexOf(c) === -1)
throw 'unrecognized character ' + fmt[i] + ' in ' + fmt;
out.push({t:'t', v:c}); ++i; break;
}
@@ -356,17 +365,17 @@ function eval_fmt(fmt, v, opts, flen) {
case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'A': case 'e': case 'Z':
out[i].v = write_date(out[i].t, out[i].v, dt);
out[i].t = 't'; break;
- case 'n': case '(':
+ case 'n': case '(': case '?':
var jj = i+1;
- while(out[jj] && ("?D".indexOf(out[jj].t) > -1 || (out[jj].t == " " && (out[jj+1]||{}).t === "?" ) || out[i].t == '(' && (out[jj].t == ')' || out[jj].t == 'n') || out[jj].t == 't' && (out[jj].v == '/' || out[jj].v == '$' || (out[jj].v == ' ' && (out[jj+1]||{}).t == '?')))) {
- if(out[jj].v!==' ') out[i].v += ' ' + out[jj].v;
+ while(out[jj] && ("?D".indexOf(out[jj].t) > -1 || (" t".indexOf(out[jj].t) > -1 && "?t".indexOf((out[jj+1]||{}).t)>-1 && (out[jj+1].t == '?' || out[jj+1].v == '/')) || out[i].t == '(' && (out[jj].t == ')' || out[jj].t == 'n') || out[jj].t == 't' && (out[jj].v == '/' || out[jj].v == '$' || (out[jj].v == ' ' && (out[jj+1]||{}).t == '?')))) {
+ out[i].v += out[jj].v;
delete out[jj]; ++jj;
}
out[i].v = write_num(out[i].t, out[i].v, v);
out[i].t = 't';
i = jj-1; break;
case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
- default: throw "unrecognized type " + out[i].t;
+ default: console.error(out); throw "unrecognized type " + out[i].t;
}
}
return out.map(function(x){return x.v;}).join("");
View
@@ -379,6 +379,21 @@ function parse_MTRSettings(blob, length) {
return [fMTREnabled, fUserSetThreadCount, cUserThreadCount];
}
+/* 2.5.186 */
+function parse_NoteSh(blob, length) {
+ var row = blob.read_shift(2), col = blob.read_shift(2);
+ var flags = blob.read_shift(2), idObj = blob.read_shift(2);
+ var stAuthor = parse_XLUnicodeString(blob);
+ blob.read_shift(1);
+ return stAuthor;
+}
+
+/* 2.4.179 */
+function parse_Note(blob, length) {
+ /* TODO: Support revisions */
+ return parse_NoteSh(blob, length);
+}
+
var parse_Backup = parsebool; /* 2.4.14 */
var parse_Blank = parse_Cell; /* 2.4.20 Just the cell */
var parse_BottomMargin = parse_Xnum; /* 2.4.27 */
@@ -434,7 +449,6 @@ var parse_WriteProtect = parsenoop; /* 2.4.350 empty record */
/* ---- */
var parse_VerticalPageBreaks = parsenoop;
var parse_HorizontalPageBreaks = parsenoop;
-var parse_Note = parsenoop;
var parse_Selection = parsenoop;
var parse_Continue = parsenoop;
var parse_Pane = parsenoop;
View
@@ -336,7 +336,6 @@ function parse_workbook(blob) {
case 'GUIDTypeLib': {
} break;
- case 'Note': break;
case 'MergeCells': break;
@@ -368,6 +367,7 @@ function parse_workbook(blob) {
case 'CondFmt': case 'CF': case 'CF12': case 'CFEx': break;
/* Comments */
+ case 'Note': break;
case 'NameCmt': break;
/* Chart */
View
@@ -1,6 +1,6 @@
{
"name": "xlsjs",
- "version": "0.6.0",
+ "version": "0.6.1",
"author": "sheetjs",
"description": "(one day) a full-featured XLS parser and writer. For now, primitive parser",
"keywords": [ "xls", "office", "excel", "spreadsheet" ],
View
@@ -22,6 +22,13 @@ function parsetest(x, wb) {
assert.equal(names, file);
} : null);
});
+ describe(x + ' should generate CSV', function() {
+ wb.SheetNames.forEach(function(ws, i) {
+ it('#' + i + ' (' + ws + ')', function() {
+ var csv = XLS.utils.make_csv(wb.Sheets[ws]);
+ });
+ });
+ });
describe(x + ' should generate correct output', function() {
wb.SheetNames.forEach(function(ws, i) {
var name = ('./test_files/' + x + '.' + i + '.csv');
Oops, something went wrong.

0 comments on commit 7fc99d6

Please sign in to comment.