From 9c9aad5df399357be825f3770522a908a2e014b4 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Thu, 23 Jan 2014 10:55:07 -0500 Subject: [PATCH] version bump 0.3.9: general format used by default (when no format or style is found, use general) Other whitespace cleanup --- bits/10_ssf.js | 6 ++--- bits/51_version.js | 2 +- bits/70_xlsx.js | 38 ++++++++++++++--------------- bits/90_utils.js | 15 ++++++------ package.json | 2 +- test.js | 14 +++++++++++ xlsx.js | 61 +++++++++++++++++++++++----------------------- 7 files changed, 75 insertions(+), 63 deletions(-) diff --git a/bits/10_ssf.js b/bits/10_ssf.js index d828615..e6a3b31 100644 --- a/bits/10_ssf.js +++ b/bits/10_ssf.js @@ -101,13 +101,13 @@ var general_fmt = function(v) { 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(/\.$/,""); + 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); + 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"); diff --git a/bits/51_version.js b/bits/51_version.js index e3fc123..12cca7e 100644 --- a/bits/51_version.js +++ b/bits/51_version.js @@ -1 +1 @@ -XLSX.version = '0.3.8'; +XLSX.version = '0.3.9'; diff --git a/bits/70_xlsx.js b/bits/70_xlsx.js index cb68e89..a3fde18 100644 --- a/bits/70_xlsx.js +++ b/bits/70_xlsx.js @@ -162,17 +162,17 @@ function parseSheet(data) { } /* formatting */ - if(cell.s && styles.CellXf) { /* TODO: second check is a hacked guard */ + var fmtid = 0; + if(cell.s && styles.CellXf) { var cf = styles.CellXf[cell.s]; - if(cf && cf.numFmtId && cf.numFmtId !== 0) { + if(cf && cf.numFmtId) fmtid = cf.numFmtId; + } p.raw = p.v; p.rawt = p.t; try { - p.v = SSF.format(cf.numFmtId,p.v,_ssfopts); + p.v = SSF.format(fmtid,p.v,_ssfopts); p.t = 'str'; - } catch(e) { p.v = p.raw; } - } - } + } catch(e) { p.v = p.raw; p.t = p.rawt; } s[cell.r] = p; }); @@ -456,19 +456,19 @@ function parseRels(data, currentFilePath) { var rels = {}; var resolveRelativePathIntoAbsolute = function (to) { -    var toksFrom = currentFilePath.split('/'); - toksFrom.pop(); // folder path -    var toksTo = to.split('/'); -    var reversed = []; -    while (toksTo.length !== 0) { -        var tokTo = toksTo.shift(); -        if (tokTo === '..') { -            toksFrom.pop(); -        } else if (tokTo !== '.') { -            toksFrom.push(tokTo); -        } -    } -    return toksFrom.join('/'); + var toksFrom = currentFilePath.split('/'); + toksFrom.pop(); // folder path + var toksTo = to.split('/'); + var reversed = []; + while (toksTo.length !== 0) { + var tokTo = toksTo.shift(); + if (tokTo === '..') { + toksFrom.pop(); + } else if (tokTo !== '.') { + toksFrom.push(tokTo); + } + } + return toksFrom.join('/'); } data.match(/<[^>]*>/g).forEach(function(x) { diff --git a/bits/90_utils.js b/bits/90_utils.js index 88bf616..dc5c2f7 100644 --- a/bits/90_utils.js +++ b/bits/90_utils.js @@ -15,15 +15,14 @@ function sheet_to_row_object_array(sheet, opts){ var val, row, r, hdr = {}, isempty, R, C, v; var out = []; opts = opts || {}; - if(!sheet["!ref"]) return out; + if(!sheet || !sheet["!ref"]) return out; r = XLSX.utils.decode_range(sheet["!ref"]); for(R=r.s.r, C = r.s.c; C <= r.e.c; ++C) { val = sheet[encode_cell({c:C,r:R})]; - if(val){ - switch(val.t) { - case 's': case 'str': hdr[C] = val.v; break; - case 'n': hdr[C] = val.v; break; - } + if(!val) continue; + switch(val.t) { + case 's': case 'str': hdr[C] = val.v; break; + case 'n': hdr[C] = val.v; break; } } @@ -33,7 +32,7 @@ function sheet_to_row_object_array(sheet, opts){ row = Object.create({ __rowNum__ : R }); for (C = r.s.c; C <= r.e.c; ++C) { val = sheet[encode_cell({c: C,r: R})]; - if(!val) continue; + if(!val || !val.t) continue; v = (val || {}).v; switch(val.t){ case 's': case 'str': case 'b': case 'n': @@ -66,7 +65,7 @@ function sheet_to_csv(sheet, opts) { }; var out = "", txt = ""; opts = opts || {}; - if(!sheet["!ref"]) return out; + if(!sheet || !sheet["!ref"]) return out; var r = XLSX.utils.decode_range(sheet["!ref"]); for(var R = r.s.r; R <= r.e.r; ++R) { var row = []; diff --git a/package.json b/package.json index 4fc82c0..c9da8ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xlsx", - "version": "0.3.8", + "version": "0.3.9", "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 8424d67..6b88a34 100644 --- a/test.js +++ b/test.js @@ -29,6 +29,20 @@ function parsetest(x, wb) { }); }); }); + describe(x + ' should generate JSON', function() { + wb.SheetNames.forEach(function(ws, i) { + it('#' + i + ' (' + ws + ')', function() { + var json = XLSX.utils.sheet_to_row_object_array(wb.Sheets[ws]); + }); + }); + }); + describe(x + ' should generate formulae', function() { + wb.SheetNames.forEach(function(ws, i) { + it('#' + i + ' (' + ws + ')', function() { + var json = XLSX.utils.get_formulae(wb.Sheets[ws]); + }); + }); + }); describe(x + ' should generate correct output', function() { wb.SheetNames.forEach(function(ws, i) { var name = ('./test_files/' + x + '.' + i + '.csv'); diff --git a/xlsx.js b/xlsx.js index 9170b2d..a37a33d 100644 --- a/xlsx.js +++ b/xlsx.js @@ -104,13 +104,13 @@ var general_fmt = function(v) { 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(/\.$/,""); + 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); + 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"); @@ -419,7 +419,7 @@ SSF.load_table = function(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i]) SSF.loa make_ssf(SSF); var XLSX = {}; (function(XLSX){ -XLSX.version = '0.3.8'; +XLSX.version = '0.3.9'; function parsexmltag(tag) { var words = tag.split(/\s+/); var z = {'0': words[0]}; @@ -788,17 +788,17 @@ function parseSheet(data) { } /* formatting */ - if(cell.s && styles.CellXf) { /* TODO: second check is a hacked guard */ + var fmtid = 0; + if(cell.s && styles.CellXf) { var cf = styles.CellXf[cell.s]; - if(cf && cf.numFmtId && cf.numFmtId !== 0) { + if(cf && cf.numFmtId) fmtid = cf.numFmtId; + } p.raw = p.v; p.rawt = p.t; try { - p.v = SSF.format(cf.numFmtId,p.v,_ssfopts); + p.v = SSF.format(fmtid,p.v,_ssfopts); p.t = 'str'; - } catch(e) { p.v = p.raw; } - } - } + } catch(e) { p.v = p.raw; p.t = p.rawt; } s[cell.r] = p; }); @@ -1082,19 +1082,19 @@ function parseRels(data, currentFilePath) { var rels = {}; var resolveRelativePathIntoAbsolute = function (to) { -    var toksFrom = currentFilePath.split('/'); - toksFrom.pop(); // folder path -    var toksTo = to.split('/'); -    var reversed = []; -    while (toksTo.length !== 0) { -        var tokTo = toksTo.shift(); -        if (tokTo === '..') { -            toksFrom.pop(); -        } else if (tokTo !== '.') { -            toksFrom.push(tokTo); -        } -    } -    return toksFrom.join('/'); + var toksFrom = currentFilePath.split('/'); + toksFrom.pop(); // folder path + var toksTo = to.split('/'); + var reversed = []; + while (toksTo.length !== 0) { + var tokTo = toksTo.shift(); + if (tokTo === '..') { + toksFrom.pop(); + } else if (tokTo !== '.') { + toksFrom.push(tokTo); + } + } + return toksFrom.join('/'); } data.match(/<[^>]*>/g).forEach(function(x) { @@ -1304,15 +1304,14 @@ function sheet_to_row_object_array(sheet, opts){ var val, row, r, hdr = {}, isempty, R, C, v; var out = []; opts = opts || {}; - if(!sheet["!ref"]) return out; + if(!sheet || !sheet["!ref"]) return out; r = XLSX.utils.decode_range(sheet["!ref"]); for(R=r.s.r, C = r.s.c; C <= r.e.c; ++C) { val = sheet[encode_cell({c:C,r:R})]; - if(val){ - switch(val.t) { - case 's': case 'str': hdr[C] = val.v; break; - case 'n': hdr[C] = val.v; break; - } + if(!val) continue; + switch(val.t) { + case 's': case 'str': hdr[C] = val.v; break; + case 'n': hdr[C] = val.v; break; } } @@ -1322,7 +1321,7 @@ function sheet_to_row_object_array(sheet, opts){ row = Object.create({ __rowNum__ : R }); for (C = r.s.c; C <= r.e.c; ++C) { val = sheet[encode_cell({c: C,r: R})]; - if(!val) continue; + if(!val || !val.t) continue; v = (val || {}).v; switch(val.t){ case 's': case 'str': case 'b': case 'n': @@ -1355,7 +1354,7 @@ function sheet_to_csv(sheet, opts) { }; var out = "", txt = ""; opts = opts || {}; - if(!sheet["!ref"]) return out; + if(!sheet || !sheet["!ref"]) return out; var r = XLSX.utils.decode_range(sheet["!ref"]); for(var R = r.s.r; R <= r.e.r; ++R) { var row = [];