diff --git a/bits/10_ssf.js b/bits/10_ssf.js index c48f4d4..1ec83f1 100644 --- a/bits/10_ssf.js +++ b/bits/10_ssf.js @@ -5,7 +5,8 @@ var make_ssf = function(SSF){ String.prototype.reverse=function(){return this.split("").reverse().join("");}; var _strrev = function(x) { return String(x).reverse(); }; function fill(c,l) { return new Array(l+1).join(c); } -function pad(v,d){var t=String(v);return t.length>=d?t:(fill(0,d-t.length)+t);} +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));} /* Options */ var opts_fmt = {}; function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} @@ -23,7 +24,7 @@ var table_fmt = { 11: '0.00E+00', 12: '# ?/?', 13: '# ??/??', - 14: 'm/d/yy', + 14: 'm/d/yy', 15: 'd-mmm-yy', 16: 'd-mmm', 17: 'mmm-yy', @@ -65,48 +66,58 @@ var months = [ ['N', 'Nov', 'November'], ['D', 'Dec', 'December'] ]; -var frac = function(x, D, mixed) { - var n1 = Math.floor(x), d1 = 1; - var n2 = n1+1, d2 = 1; - if(x !== n1) while(d1 <= D && d2 <= D) { - var m = (n1 + n2) / (d1 + d2); - if(x === m) { - if(d1 + d2 <= D) { d1+=d2; n1+=n2; d2=D+1; } - else if(d1 > d2) d2=D+1; - else d1=D+1; - break; - } - else if(x < m) { n2 = n1+n2; d2 = d1+d2; } - else { n1 = n1+n2; d1 = d1+d2; } - } - if(d1 > D) { d1 = d2; n1 = n2; } - if(!mixed) return [0, n1, d1]; - var q = Math.floor(n1/d1); - return [q, n1 - q*d1, d1]; +var frac = function frac(x, D, mixed) { + var sgn = x < 0 ? -1 : 1; + var B = x * sgn; + var P_2 = 0, P_1 = 1, P = 0; + var Q_2 = 1, Q_1 = 0, Q = 0; + var A = B|0; + while(Q_1 < D) { + A = B|0; + P = A * P_1 + P_2; + Q = A * Q_1 + Q_2; + if((B - A) < 0.0000000001) break; + B = 1 / (B - A); + P_2 = P_1; P_1 = P; + Q_2 = Q_1; Q_1 = Q; + } + if(Q > D) { Q = Q_1; P = P_1; } + if(Q > D) { Q = Q_2; P = P_2; } + if(!mixed) return [0, sgn * P, Q]; + var q = Math.floor(sgn * P/Q); + return [q, sgn*P - q*Q, Q]; }; var general_fmt = function(v) { if(typeof v === 'boolean') return v ? "TRUE" : "FALSE"; - if(typeof v === 'number') { - var o, V = v < 0 ? -v : v; - if(V >= 0.1 && V < 1) o = v.toPrecision(9); - else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8); - else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7); - else if(V > Math.pow(10,-9) && V < Math.pow(10,10)) { - o = v.toPrecision(10); if(o.length > 11+(v<0?1:0)) o = v.toExponential(5); - } - else o = v.toPrecision(6); - return o.replace("e","E").replace(/\.0*$/,"").replace(/\.(.*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2"); - } - if(typeof v === 'string') return v; - throw "unsupported value in General format: " + v; + if(typeof v === 'number') { + var o, V = v < 0 ? -v : v; + if(V >= 0.1 && V < 1) o = v.toPrecision(9); + else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8); + else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7); + else if(V >= 0.0001 && V < 0.001) o = v.toPrecision(6); + else if(V >= Math.pow(10,10) && V < Math.pow(10,11)) o = v.toFixed(10).substr(0,12); + else if(V > Math.pow(10,-9) && V < Math.pow(10,11)) { + o = v.toFixed(12).replace(/(\.[0-9]*[1-9])0*$/,"$1").replace(/\.$/,""); + if(o.length > 11+(v<0?1:0)) o = v.toPrecision(10); + if(o.length > 11+(v<0?1:0)) o = v.toExponential(5); + } + else { + o = v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1"); + if(o.length > 11 + (v<0?1:0)) o = v.toPrecision(6); + } + o = o.replace(/(\.[0-9]*[1-9])0+e/,"$1e").replace(/\.0*e/,"e"); + return o.replace("e","E").replace(/\.0*$/,"").replace(/\.([0-9]*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2"); + } + if(typeof v === 'string') return v; + throw "unsupported value in General format: " + v; }; SSF._general = general_fmt; var parse_date_code = function parse_date_code(v,opts) { var date = Math.floor(v), time = Math.round(86400 * (v - date)), dow=0; - var dout=[], out={D:date, T:time, u:86400*(v-date)-time}; fixopts(opts = (opts||{})); + var dout=[], out={D:date, T:time, u:86400*(v-date)-time}; fixopts(opts = (opts||{})); if(opts.date1904) date += 1462; if(date === 60) {dout = [1900,2,29]; dow=3;} - else if(date === 0) {dout = [1900,1,0]; dow=6;} + else if(date === 0) {dout = [1900,1,0]; dow=6;} else { if(date > 60) --date; /* 1 = Jan 1 1900 */ @@ -125,7 +136,7 @@ var parse_date_code = function parse_date_code(v,opts) { }; SSF.parse_date_code = parse_date_code; var write_date = function(type, fmt, val) { - if(val < 0) return ""; + if(val < 0) return ""; switch(type) { case 'y': switch(fmt) { /* year */ case 'y': case 'yy': return pad(val.y % 100,2); @@ -164,13 +175,13 @@ var write_date = function(type, fmt, val) { case 's': switch(fmt) { /* seconds */ case 's': return val.S; case 'ss': return pad(val.S, 2); - case 'ss.0': return pad(val.S,2) + "." + Math.round(10*val.u); + case 'ss.0': return pad(val.S,2) + "." + Math.round(10*val.u); default: throw 'bad second format: ' + fmt; } break; - case 'Z': switch(fmt) { - case '[h]': return val.D*24+val.H; - default: throw 'bad abstime format: ' + fmt; - } break; + case 'Z': switch(fmt) { + case '[h]': return val.D*24+val.H; + default: throw 'bad abstime format: ' + fmt; + } break; /* 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); @@ -180,57 +191,65 @@ var write_date = function(type, fmt, val) { String.prototype.reverse = function() { return this.split("").reverse().join(""); }; var commaify = function(s) { return s.reverse().replace(/.../g,"$&,").reverse().replace(/^,/,""); }; var write_num = function(type, fmt, val) { - if(type === '(') { - var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); - if(val >= 0) return write_num('n', ffmt, val); - return '(' + write_num('n', ffmt, -val) + ')'; - } - var mul = 0, o; - fmt = fmt.replace(/%/g,function(x) { mul++; return ""; }); - if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul); - if(fmt.indexOf("E") > -1) { - var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; - if(fmt == '##0.0E+0') { - var ee = Number(val.toExponential(0).substr(3))%3; - o = (val/Math.pow(10,ee%3)).toPrecision(idx+1+(ee%3)).replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,ee) + "." + $3.substr(ee) + "E"; }); - } else o = val.toExponential(idx); - if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; - if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); - return o.replace("e","E"); - } + if(type === '(') { + var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); + if(val >= 0) return write_num('n', ffmt, val); + return '(' + write_num('n', ffmt, -val) + ')'; + } + var mul = 0, o; + fmt = fmt.replace(/%/g,function(x) { mul++; return ""; }); + if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul); + if(fmt.indexOf("E") > -1) { + var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; + if(fmt == '##0.0E+0') { + var ee = Number(val.toExponential(0).substr(3))%3; + o = (val/Math.pow(10,ee%3)).toPrecision(idx+1+(ee%3)).replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,ee) + "." + $3.substr(ee) + "E"; }); + } else o = val.toExponential(idx); + if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; + if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); + return o.replace("e","E"); + } 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 ? "-" : ""; - 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": 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(val<0?-val:val, 10, true); return (val<0?"-":"") + ff[0] + " " + ff[1] + "/" + ff[2]; - case "# ?? / ??": ff = frac(val<0?-val:val, 100, true); return (val<0?"-":"") + ff[0] + " " + ff[1] + "/" + ff[2]; - default: - } - throw new Error("unsupported format |" + fmt + "|"); + 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); + 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)); + } + if(fmt.match(/^00*$/)) return (val<0?"-":"")+pad(Math.round(Math.abs(val)), fmt.length); + if(fmt.match(/^####*$/)) return "dafuq"; + 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": 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] + "/" + ff[2]); + case "# ?? / ??": ff = frac(aval, 99, true); return sign + (ff[0]||"") + " " + (ff[1] ? pad(ff[1],2," ") + "/" + rpad(ff[2],2," ") : " "); + case "# ??? / ???": ff = frac(aval, 999, true); return sign + (ff[0]||"") + " " + (ff[1] ? pad(ff[1],3," ") + "/" + rpad(ff[2],3," ") : " "); + default: + } + throw new Error("unsupported format |" + fmt + "|"); }; function split_fmt(fmt) { - var out = []; - var in_str = -1; - for(var i = 0, j = 0; i < fmt.length; ++i) { - if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; } - if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; } - if(fmt[i] == '"') { in_str = i; continue; } - if(fmt[i] != ";") continue; - out.push(fmt.slice(j,i)); - j = i+1; - } - out.push(fmt.slice(j)); - if(in_str !=-1) throw "Format |" + fmt + "| unterminated string at " + in_str; - return out; + var out = []; + var in_str = -1; + for(var i = 0, j = 0; i < fmt.length; ++i) { + if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; } + if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; } + if(fmt[i] == '"') { in_str = i; continue; } + if(fmt[i] != ";") continue; + out.push(fmt.slice(j,i)); + j = i+1; + } + out.push(fmt.slice(j)); + if(in_str !=-1) throw "Format |" + fmt + "| unterminated string at " + in_str; + return out; } SSF._split = split_fmt; function eval_fmt(fmt, v, opts, flen) { @@ -241,46 +260,49 @@ function eval_fmt(fmt, v, opts, flen) { while(i < fmt.length) { switch((c = fmt[i])) { case '"': /* Literal text */ - for(o="";fmt[++i] !== '"';) o += fmt[i]; - out.push({t:'t', v:o}); ++i; break; - case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w; - out.push({t:t, v:w}); ++i; break; - case '_': out.push({t:'t', v:" "}); i+=2; break; + for(o="";fmt[++i] !== '"';) o += fmt[i]; + out.push({t:'t', v:o}); ++i; break; + case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w; + out.push({t:t, v:w}); ++i; break; + case '_': out.push({t:'t', v:" "}); i+=2; break; case '@': /* Text Placeholder */ out.push({t:'T', v:v}); ++i; break; /* Dates */ case 'm': case 'd': case 'y': case 'h': case 's': case 'e': - if(v < 0) return ""; + if(v < 0) return ""; if(!dt) dt = parse_date_code(v, opts); o = fmt[i]; while(fmt[++i] === c) o+=c; - if(c === 's' && fmt[i] === '.' && fmt[i+1] === '0') { o+='.'; while(fmt[++i] === '0') o+= '0'; } + if(c === 's' && fmt[i] === '.' && fmt[i+1] === '0') { o+='.'; while(fmt[++i] === '0') o+= '0'; } if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */ if(c === 'h') c = hr; q={t:c, v:o}; out.push(q); lst = c; break; case 'A': - if(!dt) dt = parse_date_code(v, opts); + if(!dt) dt = parse_date_code(v, opts); q={t:c,v:"A"}; - if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;} - else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; } + if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;} + else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; } else q.t = "t"; out.push(q); lst = c; break; case '[': /* TODO: Fix this -- ignore all conditionals and formatting */ - o = c; - while(fmt[i++] !== ']') o += fmt[i]; - if(o == "[h]") out.push({t:'Z', v:o}); - break; - /* Numbers */ - case '0': case '#': - var nn = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) nn += c; - out.push({t:'n', v:nn}); break; - case '?': - o = fmt[i]; while(fmt[++i] === c) o+=c; - q={t:c, v:o}; out.push(q); lst = c; break; - case '*': ++i; if(fmt[i] == ' ') ++i; break; // ** - case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break; - case ' ': out.push({t:c,v:c}); ++i; break; + o = c; + while(fmt[i++] !== ']') o += fmt[i]; + if(o == "[h]") out.push({t:'Z', v:o}); + break; + /* Numbers */ + case '0': case '#': + o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) o += c; + out.push({t:'n', v:o}); break; + case '?': + o = fmt[i]; while(fmt[++i] === c) o+=c; + q={t:c, v:o}; out.push(q); lst = c; break; + case '*': ++i; if(fmt[i] == ' ') ++i; break; // ** + case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': + o = fmt[i]; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i]; + 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; } @@ -298,19 +320,19 @@ function eval_fmt(fmt, v, opts, flen) { /* replace fields */ for(i=0; i < out.length; ++i) { switch(out[i].t) { - case 't': case 'T': case ' ': break; - case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'A': case 'e': case 'Z': + case 't': case 'T': case ' ': break; + 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 '(': - var jj = i+1; - while(out[jj] && (out[jj].t == '?' || out[jj].t == ' ' || out[i].t == '(' && (out[jj].t == ')' || out[jj].t == 'n') || out[jj].t == 't' && (out[jj].v == '/' || out[jj].v == '$'))) { - if(out[jj].v!==' ') 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; break; + case 'n': case '(': + var jj = i+1; + while(out[jj] && ("? D".indexOf(out[jj].t) > -1 || 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; + delete out[jj]; ++jj; + } + out[i].v = write_num(out[i].t, out[i].v, v); + out[i].t = 't'; + i = jj; break; default: throw "unrecognized type " + out[i].t; } } @@ -319,25 +341,25 @@ function eval_fmt(fmt, v, opts, flen) { } SSF._eval = eval_fmt; function choose_fmt(fmt, v, o) { - if(typeof fmt === 'number') fmt = table_fmt[fmt]; + if(typeof fmt === 'number') fmt = table_fmt[fmt]; if(typeof fmt === "string") fmt = split_fmt(fmt); - var l = fmt.length; - switch(fmt.length) { - case 1: fmt = [fmt[0], fmt[0], fmt[0], "@"]; break; - case 2: fmt = [fmt[0], fmt[1], fmt[0], "@"]; break; - case 4: break; - default: throw "cannot find right format for |" + fmt + "|"; - } - if(typeof v !== "number") return [fmt.length, fmt[3]]; - return [l, v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2]]; + var l = fmt.length; + switch(fmt.length) { + case 1: fmt = [fmt[0], fmt[0], fmt[0], "@"]; break; + case 2: fmt = [fmt[0], fmt[fmt[1] === "@"?0:1], fmt[0], "@"]; break; + case 4: break; + default: throw "cannot find right format for |" + fmt + "|"; + } + if(typeof v !== "number") return [fmt.length, fmt[3]]; + return [l, v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2]]; } var format = function format(fmt,v,o) { fixopts(o = (o||{})); if(fmt === 0) return general_fmt(v, o); - fmt = table_fmt[fmt]; + if(typeof fmt === 'number') fmt = table_fmt[fmt]; var f = choose_fmt(fmt, v, o); - return eval_fmt(f[1], v, o, f[0]); + return eval_fmt(f[1], v, o, f[0]); }; SSF._choose = choose_fmt; diff --git a/bits/70_xlsx.js b/bits/70_xlsx.js index 8ba0e86..bd95026 100644 --- a/bits/70_xlsx.js +++ b/bits/70_xlsx.js @@ -464,7 +464,7 @@ function parseZip(zip) { var entries = Object.keys(zip.files); var keys = entries.filter(function(x){return x.substr(-1) != '/';}).sort(); var dir = parseCT(getdata(getzipfile(zip, '[Content_Types].xml'))); - + if(dir.workbooks.length === 0) throw new Error("Could not find workbook entry"); strs = {}; if(dir.sst) strs=parse_sst(getdata(getzipfile(zip, dir.sst.replace(/^\//,'')))); diff --git a/package.json b/package.json index 9e164e2..64050d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xlsx", - "version": "0.3.4", + "version": "0.3.5", "author": "sheetjs", "description": "(one day) a full-featured XLSX parser and writer. For now, primitive parser", "keywords": [ "xlsx", "office", "excel", "spreadsheet" ], diff --git a/test.js b/test.js index 0bc3458..88e1f72 100644 --- a/test.js +++ b/test.js @@ -5,16 +5,18 @@ describe('source', function() { it('should load', function() { XLSX = require('. var files = (fs.existsSync('tests.lst') ? fs.readFileSync('tests.lst', 'utf-8').split("\n") : fs.readdirSync('test_files')).filter(function(x){return x.substr(-5)==".xlsx" || x.substr(-13)==".xlsx.pending"}); +function normalizecsv(x) { return x.replace(/\t/g,",").replace(/#{255}/g,"").replace(/"/g,"").replace(/[\n\r]+/g,"\n").replace(/\n*$/,""); } + function parsetest(x, wb) { describe(x + ' should have all bits', function() { - var sname = './test_files/' + x + '.sheetnames'; + var sname = './test_files/2011/' + x + '.sheetnames'; it('should have all sheets', function() { wb.SheetNames.forEach(function(y) { assert(wb.Sheets[y], 'bad sheet ' + y); }); }); it('should have the right sheet names', fs.existsSync(sname) ? function() { var file = fs.readFileSync(sname, 'utf-8'); var names = wb.SheetNames.join("\n") + "\n"; - assert.equal(file, names); + assert.equal(names, file); } : null); }); describe(x + ' should generate correct output', function() { @@ -23,7 +25,7 @@ function parsetest(x, wb) { it('#' + i + ' (' + ws + ')', fs.existsSync(name) ? function() { var file = fs.readFileSync(name, 'utf-8'); var csv = XLSX.utils.make_csv(wb.Sheets[ws]); - assert.equal(file.replace(/"/g,""), csv.replace(/"/g,""), "CSV badness"); + assert.equal(normalizecsv(csv), normalizecsv(file), "CSV badness"); } : null); }); }); diff --git a/xlsx.js b/xlsx.js index fde090a..1f727bc 100644 --- a/xlsx.js +++ b/xlsx.js @@ -8,7 +8,8 @@ var make_ssf = function(SSF){ String.prototype.reverse=function(){return this.split("").reverse().join("");}; var _strrev = function(x) { return String(x).reverse(); }; function fill(c,l) { return new Array(l+1).join(c); } -function pad(v,d){var t=String(v);return t.length>=d?t:(fill(0,d-t.length)+t);} +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));} /* Options */ var opts_fmt = {}; function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} @@ -26,7 +27,7 @@ var table_fmt = { 11: '0.00E+00', 12: '# ?/?', 13: '# ??/??', - 14: 'm/d/yy', + 14: 'm/d/yy', 15: 'd-mmm-yy', 16: 'd-mmm', 17: 'mmm-yy', @@ -68,48 +69,58 @@ var months = [ ['N', 'Nov', 'November'], ['D', 'Dec', 'December'] ]; -var frac = function(x, D, mixed) { - var n1 = Math.floor(x), d1 = 1; - var n2 = n1+1, d2 = 1; - if(x !== n1) while(d1 <= D && d2 <= D) { - var m = (n1 + n2) / (d1 + d2); - if(x === m) { - if(d1 + d2 <= D) { d1+=d2; n1+=n2; d2=D+1; } - else if(d1 > d2) d2=D+1; - else d1=D+1; - break; - } - else if(x < m) { n2 = n1+n2; d2 = d1+d2; } - else { n1 = n1+n2; d1 = d1+d2; } - } - if(d1 > D) { d1 = d2; n1 = n2; } - if(!mixed) return [0, n1, d1]; - var q = Math.floor(n1/d1); - return [q, n1 - q*d1, d1]; +var frac = function frac(x, D, mixed) { + var sgn = x < 0 ? -1 : 1; + var B = x * sgn; + var P_2 = 0, P_1 = 1, P = 0; + var Q_2 = 1, Q_1 = 0, Q = 0; + var A = B|0; + while(Q_1 < D) { + A = B|0; + P = A * P_1 + P_2; + Q = A * Q_1 + Q_2; + if((B - A) < 0.0000000001) break; + B = 1 / (B - A); + P_2 = P_1; P_1 = P; + Q_2 = Q_1; Q_1 = Q; + } + if(Q > D) { Q = Q_1; P = P_1; } + if(Q > D) { Q = Q_2; P = P_2; } + if(!mixed) return [0, sgn * P, Q]; + var q = Math.floor(sgn * P/Q); + return [q, sgn*P - q*Q, Q]; }; var general_fmt = function(v) { if(typeof v === 'boolean') return v ? "TRUE" : "FALSE"; - if(typeof v === 'number') { - var o, V = v < 0 ? -v : v; - if(V >= 0.1 && V < 1) o = v.toPrecision(9); - else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8); - else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7); - else if(V > Math.pow(10,-9) && V < Math.pow(10,10)) { - o = v.toPrecision(10); if(o.length > 11+(v<0?1:0)) o = v.toExponential(5); - } - else o = v.toPrecision(6); - return o.replace("e","E").replace(/\.0*$/,"").replace(/\.(.*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2"); - } - if(typeof v === 'string') return v; - throw "unsupported value in General format: " + v; + if(typeof v === 'number') { + var o, V = v < 0 ? -v : v; + if(V >= 0.1 && V < 1) o = v.toPrecision(9); + else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8); + else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7); + else if(V >= 0.0001 && V < 0.001) o = v.toPrecision(6); + else if(V >= Math.pow(10,10) && V < Math.pow(10,11)) o = v.toFixed(10).substr(0,12); + else if(V > Math.pow(10,-9) && V < Math.pow(10,11)) { + o = v.toFixed(12).replace(/(\.[0-9]*[1-9])0*$/,"$1").replace(/\.$/,""); + if(o.length > 11+(v<0?1:0)) o = v.toPrecision(10); + if(o.length > 11+(v<0?1:0)) o = v.toExponential(5); + } + else { + o = v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1"); + if(o.length > 11 + (v<0?1:0)) o = v.toPrecision(6); + } + o = o.replace(/(\.[0-9]*[1-9])0+e/,"$1e").replace(/\.0*e/,"e"); + return o.replace("e","E").replace(/\.0*$/,"").replace(/\.([0-9]*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2"); + } + if(typeof v === 'string') return v; + throw "unsupported value in General format: " + v; }; SSF._general = general_fmt; var parse_date_code = function parse_date_code(v,opts) { var date = Math.floor(v), time = Math.round(86400 * (v - date)), dow=0; - var dout=[], out={D:date, T:time, u:86400*(v-date)-time}; fixopts(opts = (opts||{})); + var dout=[], out={D:date, T:time, u:86400*(v-date)-time}; fixopts(opts = (opts||{})); if(opts.date1904) date += 1462; if(date === 60) {dout = [1900,2,29]; dow=3;} - else if(date === 0) {dout = [1900,1,0]; dow=6;} + else if(date === 0) {dout = [1900,1,0]; dow=6;} else { if(date > 60) --date; /* 1 = Jan 1 1900 */ @@ -128,7 +139,7 @@ var parse_date_code = function parse_date_code(v,opts) { }; SSF.parse_date_code = parse_date_code; var write_date = function(type, fmt, val) { - if(val < 0) return ""; + if(val < 0) return ""; switch(type) { case 'y': switch(fmt) { /* year */ case 'y': case 'yy': return pad(val.y % 100,2); @@ -167,13 +178,13 @@ var write_date = function(type, fmt, val) { case 's': switch(fmt) { /* seconds */ case 's': return val.S; case 'ss': return pad(val.S, 2); - case 'ss.0': return pad(val.S,2) + "." + Math.round(10*val.u); + case 'ss.0': return pad(val.S,2) + "." + Math.round(10*val.u); default: throw 'bad second format: ' + fmt; } break; - case 'Z': switch(fmt) { - case '[h]': return val.D*24+val.H; - default: throw 'bad abstime format: ' + fmt; - } break; + case 'Z': switch(fmt) { + case '[h]': return val.D*24+val.H; + default: throw 'bad abstime format: ' + fmt; + } break; /* 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); @@ -183,57 +194,65 @@ var write_date = function(type, fmt, val) { String.prototype.reverse = function() { return this.split("").reverse().join(""); }; var commaify = function(s) { return s.reverse().replace(/.../g,"$&,").reverse().replace(/^,/,""); }; var write_num = function(type, fmt, val) { - if(type === '(') { - var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); - if(val >= 0) return write_num('n', ffmt, val); - return '(' + write_num('n', ffmt, -val) + ')'; - } - var mul = 0, o; - fmt = fmt.replace(/%/g,function(x) { mul++; return ""; }); - if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul); - if(fmt.indexOf("E") > -1) { - var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; - if(fmt == '##0.0E+0') { - var ee = Number(val.toExponential(0).substr(3))%3; - o = (val/Math.pow(10,ee%3)).toPrecision(idx+1+(ee%3)).replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,ee) + "." + $3.substr(ee) + "E"; }); - } else o = val.toExponential(idx); - if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; - if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); - return o.replace("e","E"); - } + if(type === '(') { + var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); + if(val >= 0) return write_num('n', ffmt, val); + return '(' + write_num('n', ffmt, -val) + ')'; + } + var mul = 0, o; + fmt = fmt.replace(/%/g,function(x) { mul++; return ""; }); + if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul); + if(fmt.indexOf("E") > -1) { + var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; + if(fmt == '##0.0E+0') { + var ee = Number(val.toExponential(0).substr(3))%3; + o = (val/Math.pow(10,ee%3)).toPrecision(idx+1+(ee%3)).replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,ee) + "." + $3.substr(ee) + "E"; }); + } else o = val.toExponential(idx); + if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; + if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); + return o.replace("e","E"); + } 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 ? "-" : ""; - 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": 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(val<0?-val:val, 10, true); return (val<0?"-":"") + ff[0] + " " + ff[1] + "/" + ff[2]; - case "# ?? / ??": ff = frac(val<0?-val:val, 100, true); return (val<0?"-":"") + ff[0] + " " + ff[1] + "/" + ff[2]; - default: - } - throw new Error("unsupported format |" + fmt + "|"); + 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); + 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)); + } + if(fmt.match(/^00*$/)) return (val<0?"-":"")+pad(Math.round(Math.abs(val)), fmt.length); + if(fmt.match(/^####*$/)) return "dafuq"; + 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": 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] + "/" + ff[2]); + case "# ?? / ??": ff = frac(aval, 99, true); return sign + (ff[0]||"") + " " + (ff[1] ? pad(ff[1],2," ") + "/" + rpad(ff[2],2," ") : " "); + case "# ??? / ???": ff = frac(aval, 999, true); return sign + (ff[0]||"") + " " + (ff[1] ? pad(ff[1],3," ") + "/" + rpad(ff[2],3," ") : " "); + default: + } + throw new Error("unsupported format |" + fmt + "|"); }; function split_fmt(fmt) { - var out = []; - var in_str = -1; - for(var i = 0, j = 0; i < fmt.length; ++i) { - if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; } - if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; } - if(fmt[i] == '"') { in_str = i; continue; } - if(fmt[i] != ";") continue; - out.push(fmt.slice(j,i)); - j = i+1; - } - out.push(fmt.slice(j)); - if(in_str !=-1) throw "Format |" + fmt + "| unterminated string at " + in_str; - return out; + var out = []; + var in_str = -1; + for(var i = 0, j = 0; i < fmt.length; ++i) { + if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; } + if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; } + if(fmt[i] == '"') { in_str = i; continue; } + if(fmt[i] != ";") continue; + out.push(fmt.slice(j,i)); + j = i+1; + } + out.push(fmt.slice(j)); + if(in_str !=-1) throw "Format |" + fmt + "| unterminated string at " + in_str; + return out; } SSF._split = split_fmt; function eval_fmt(fmt, v, opts, flen) { @@ -244,46 +263,49 @@ function eval_fmt(fmt, v, opts, flen) { while(i < fmt.length) { switch((c = fmt[i])) { case '"': /* Literal text */ - for(o="";fmt[++i] !== '"';) o += fmt[i]; - out.push({t:'t', v:o}); ++i; break; - case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w; - out.push({t:t, v:w}); ++i; break; - case '_': out.push({t:'t', v:" "}); i+=2; break; + for(o="";fmt[++i] !== '"';) o += fmt[i]; + out.push({t:'t', v:o}); ++i; break; + case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w; + out.push({t:t, v:w}); ++i; break; + case '_': out.push({t:'t', v:" "}); i+=2; break; case '@': /* Text Placeholder */ out.push({t:'T', v:v}); ++i; break; /* Dates */ case 'm': case 'd': case 'y': case 'h': case 's': case 'e': - if(v < 0) return ""; + if(v < 0) return ""; if(!dt) dt = parse_date_code(v, opts); o = fmt[i]; while(fmt[++i] === c) o+=c; - if(c === 's' && fmt[i] === '.' && fmt[i+1] === '0') { o+='.'; while(fmt[++i] === '0') o+= '0'; } + if(c === 's' && fmt[i] === '.' && fmt[i+1] === '0') { o+='.'; while(fmt[++i] === '0') o+= '0'; } if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */ if(c === 'h') c = hr; q={t:c, v:o}; out.push(q); lst = c; break; case 'A': - if(!dt) dt = parse_date_code(v, opts); + if(!dt) dt = parse_date_code(v, opts); q={t:c,v:"A"}; - if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;} - else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; } + if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;} + else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; } else q.t = "t"; out.push(q); lst = c; break; case '[': /* TODO: Fix this -- ignore all conditionals and formatting */ - o = c; - while(fmt[i++] !== ']') o += fmt[i]; - if(o == "[h]") out.push({t:'Z', v:o}); - break; - /* Numbers */ - case '0': case '#': - var nn = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) nn += c; - out.push({t:'n', v:nn}); break; - case '?': - o = fmt[i]; while(fmt[++i] === c) o+=c; - q={t:c, v:o}; out.push(q); lst = c; break; - case '*': ++i; if(fmt[i] == ' ') ++i; break; // ** - case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break; - case ' ': out.push({t:c,v:c}); ++i; break; + o = c; + while(fmt[i++] !== ']') o += fmt[i]; + if(o == "[h]") out.push({t:'Z', v:o}); + break; + /* Numbers */ + case '0': case '#': + o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) o += c; + out.push({t:'n', v:o}); break; + case '?': + o = fmt[i]; while(fmt[++i] === c) o+=c; + q={t:c, v:o}; out.push(q); lst = c; break; + case '*': ++i; if(fmt[i] == ' ') ++i; break; // ** + case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break; + case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': + o = fmt[i]; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i]; + 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; } @@ -301,19 +323,19 @@ function eval_fmt(fmt, v, opts, flen) { /* replace fields */ for(i=0; i < out.length; ++i) { switch(out[i].t) { - case 't': case 'T': case ' ': break; - case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'A': case 'e': case 'Z': + case 't': case 'T': case ' ': break; + 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 '(': - var jj = i+1; - while(out[jj] && (out[jj].t == '?' || out[jj].t == ' ' || out[i].t == '(' && (out[jj].t == ')' || out[jj].t == 'n') || out[jj].t == 't' && (out[jj].v == '/' || out[jj].v == '$'))) { - if(out[jj].v!==' ') 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; break; + case 'n': case '(': + var jj = i+1; + while(out[jj] && ("? D".indexOf(out[jj].t) > -1 || 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; + delete out[jj]; ++jj; + } + out[i].v = write_num(out[i].t, out[i].v, v); + out[i].t = 't'; + i = jj; break; default: throw "unrecognized type " + out[i].t; } } @@ -322,25 +344,25 @@ function eval_fmt(fmt, v, opts, flen) { } SSF._eval = eval_fmt; function choose_fmt(fmt, v, o) { - if(typeof fmt === 'number') fmt = table_fmt[fmt]; + if(typeof fmt === 'number') fmt = table_fmt[fmt]; if(typeof fmt === "string") fmt = split_fmt(fmt); - var l = fmt.length; - switch(fmt.length) { - case 1: fmt = [fmt[0], fmt[0], fmt[0], "@"]; break; - case 2: fmt = [fmt[0], fmt[1], fmt[0], "@"]; break; - case 4: break; - default: throw "cannot find right format for |" + fmt + "|"; - } - if(typeof v !== "number") return [fmt.length, fmt[3]]; - return [l, v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2]]; + var l = fmt.length; + switch(fmt.length) { + case 1: fmt = [fmt[0], fmt[0], fmt[0], "@"]; break; + case 2: fmt = [fmt[0], fmt[fmt[1] === "@"?0:1], fmt[0], "@"]; break; + case 4: break; + default: throw "cannot find right format for |" + fmt + "|"; + } + if(typeof v !== "number") return [fmt.length, fmt[3]]; + return [l, v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2]]; } var format = function format(fmt,v,o) { fixopts(o = (o||{})); if(fmt === 0) return general_fmt(v, o); - fmt = table_fmt[fmt]; + if(typeof fmt === 'number') fmt = table_fmt[fmt]; var f = choose_fmt(fmt, v, o); - return eval_fmt(f[1], v, o, f[0]); + return eval_fmt(f[1], v, o, f[0]); }; SSF._choose = choose_fmt; @@ -1022,7 +1044,7 @@ function parseZip(zip) { var entries = Object.keys(zip.files); var keys = entries.filter(function(x){return x.substr(-1) != '/';}).sort(); var dir = parseCT(getdata(getzipfile(zip, '[Content_Types].xml'))); - + if(dir.workbooks.length === 0) throw new Error("Could not find workbook entry"); strs = {}; if(dir.sst) strs=parse_sst(getdata(getzipfile(zip, dir.sst.replace(/^\//,''))));