From 5ff2a1c735f19142c676a8babf33ecb0119c0c50 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Thu, 13 Feb 2014 01:26:39 -0500 Subject: [PATCH] updating to 0.5.5 --- xlsx.js | 96 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 27 deletions(-) diff --git a/xlsx.js b/xlsx.js index 30ebfb2..6d2a6f6 100644 --- a/xlsx.js +++ b/xlsx.js @@ -424,7 +424,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.5.4'; +XLSX.version = '0.5.5'; var current_codepage, current_cptable, cptable; if(typeof module !== "undefined" && typeof require !== 'undefined') { if(typeof cptable === 'undefined') cptable = require('codepage'); @@ -621,7 +621,7 @@ function prep_blob(blob, pos) { function parsenoop(blob, length) { blob.l += length; } /* [MS-XLSB] 2.1.4 Record */ -var recordhopper = function(data, cb) { +var recordhopper = function(data, cb, opts) { var tmpbyte, cntbyte, length; prep_blob(data, data.l || 0); while(data.l < data.length) { @@ -631,7 +631,7 @@ var recordhopper = function(data, cb) { tmpbyte = data.read_shift(1); length = tmpbyte & 0x7F; for(cntbyte = 1; cntbyte <4 && (tmpbyte & 0x80); ++cntbyte) length += ((tmpbyte = data.read_shift(1)) & 0x7F)<<(7*cntbyte); - var d = R.f(data, length); + var d = R.f(data, length, opts); if(cb(d, R, RT)) return; } }; @@ -1235,6 +1235,11 @@ function insertCommentsIntoSheet(sheetName, sheet, comments) { }); } +/* [MS-XLSB] 2.5.97.4 CellParsedFormula TODO: use similar logic to js-xls */ +var parse_CellParsedFormula = function(data, length) { + var cce = data.read_shift(4); + return parsenoop(data, length-4); +}; var strs = {}; // shared strings var _ssfopts = {}; // spreadsheet formatting options @@ -1325,6 +1330,7 @@ function parse_ws_xml(data, opts) { return s; } + /* [MS-XLSB] 2.4.718 BrtRowHdr */ var parse_BrtRowHdr = function(data, length) { var z = {}; @@ -1345,6 +1351,9 @@ var parse_BrtWsProp = function(data, length) { return z; }; +/* [MS-XLSB] 2.4.303 BrtCellBlank */ +var parse_BrtCellBlank = parsenoop; + /* [MS-XLSB] 2.4.304 BrtCellBool */ var parse_BrtCellBool = function(data, length) { var cell = parse_Cell(data); @@ -1380,7 +1389,7 @@ var parse_BrtCellRk = function(data, length) { return [cell, value, 'n']; }; -/* [MS-XLSB] 2.4.311 BrtCellRk */ +/* [MS-XLSB] 2.4.314 BrtCellSt */ var parse_BrtCellSt = function(data, length) { var cell = parse_Cell(data); var value = parse_XLWideString(data); @@ -1388,40 +1397,58 @@ var parse_BrtCellSt = function(data, length) { }; /* [MS-XLSB] 2.4.647 BrtFmlaBool */ -var parse_BrtFmlaBool = function(data, length) { +var parse_BrtFmlaBool = function(data, length, opts) { var cell = parse_Cell(data); var value = data.read_shift(1); - data.l += length-9; - return [cell, value, 'b' /*, formula */]; + var o = [cell, value, 'b']; + if(opts.cellFormula) { + var formula = parse_CellParsedFormula(data, length-9); + o[3] = ""; /* TODO */ + } + else data.l += length-9; + return o; }; /* [MS-XLSB] 2.4.648 BrtFmlaError */ -var parse_BrtFmlaError = function(data, length) { +var parse_BrtFmlaError = function(data, length, opts) { var cell = parse_Cell(data); - var fBool = data.read_shift(1); - data.l += length-9; - return [cell, fBool, 'e']; + var value = data.read_shift(1); + var o = [cell, value, 'e']; + if(opts.cellFormula) { + var formula = parse_CellParsedFormula(data, length-9); + o[3] = ""; /* TODO */ + } + else data.l += length-9; + return o; }; /* [MS-XLSB] 2.4.649 BrtFmlaNum */ -var parse_BrtFmlaNum = function(data, length) { +var parse_BrtFmlaNum = function(data, length, opts) { var cell = parse_Cell(data); var value = parse_Xnum(data); - data.l += length-16; - return [cell, value, 'n' /*, formula */]; + var o = [cell, value, 'n']; + if(opts.cellFormula) { + var formula = parse_CellParsedFormula(data, length - 16); + o[3] = ""; /* TODO */ + } + else data.l += length-16; + return o; }; /* [MS-XLSB] 2.4.650 BrtFmlaString */ -var parse_BrtFmlaString = function(data, length) { +var parse_BrtFmlaString = function(data, length, opts) { var start = data.l; var cell = parse_Cell(data); var value = parse_XLWideString(data); - data.l = start + length; - return [cell, value, 'str' /*, formula */]; + var o = [cell, value, 'str']; + if(opts.cellFormula) { + var formula = parse_CellParsedFormula(data, start + length - data.l); + o[3] = ""; /* TODO */ + } + else data.l = start + length; + return o; }; -var parse_BrtCellBlank = parsenoop; - /* [MS-XLSB] 2.1.7.61 Worksheet */ var parse_ws_bin = function(data, opts) { if(!data) return data; @@ -1453,7 +1480,7 @@ var parse_ws_bin = function(data, opts) { case 'e': p.raw = val[1]; p.v = BErr[p.raw]; break; case 'str': p.v = utf8read(val[1]); break; } - if(val[3] && opts.cellFormula) p.f = val[3]; + if(opts.cellFormula && val.length > 3) p.f = val[3]; if((cf = styles.CellXf[val[0].iStyleRef])) try { p.w = SSF.format(cf.ifmt,p.v,_ssfopts); if(opts.cellNF) p.z = SSF._table[cf.ifmt]; @@ -1463,7 +1490,6 @@ var parse_ws_bin = function(data, opts) { case 'BrtCellBlank': break; // (blank cell) - case 'BrtFmt': break; // TODO case 'BrtArrFmla': break; // TODO case 'BrtShrFmla': break; // TODO case 'BrtBeginSheet': break; @@ -1490,9 +1516,13 @@ var parse_ws_bin = function(data, opts) { case 'BrtFRTBegin': pass = true; break; case 'BrtFRTEnd': pass = false; break; case 'BrtEndSheet': break; // TODO + case 'BrtBeginMergeCells': break; // TODO + case 'BrtMergeCell': break; // TODO + case 'BrtEndMergeCells': break; // TODO + case 'BrtLegacyDrawing': break; // TODO //default: if(!pass) throw new Error("Unexpected record " + R.n); } - }); + }, opts); s["!ref"] = encode_range(ref); return s; }; @@ -2593,6 +2623,8 @@ function fixopts(opts) { ['sheetStubs', false], /* emit empty cells */ + ['bookSheets', false], /* only try to get sheet names (no Sheets) */ + ['WTF', false] /* WTF mode (do not use) */ ]; defaults.forEach(function(d) { if(typeof opts[d[0]] === 'undefined') opts[d[0]] = d[1]; }); @@ -2611,19 +2643,29 @@ function parseZip(zip, opts) { dir.workbooks.push(binname); xlsb = true; } - strs = {}; - if(dir.sst) strs=parse_sst(getdata(getzipfile(zip, dir.sst.replace(/^\//,''))), dir.sst, opts); - styles = {}; - if(dir.style) styles = parse_sty(getdata(getzipfile(zip, dir.style.replace(/^\//,''))),dir.style); + if(!opts.bookSheets) { + strs = {}; + if(dir.sst) strs=parse_sst(getdata(getzipfile(zip, dir.sst.replace(/^\//,''))), dir.sst, opts); + + styles = {}; + if(dir.style) styles = parse_sty(getdata(getzipfile(zip, dir.style.replace(/^\//,''))),dir.style); + } + + var wb = parse_wb(getdata(getzipfile(zip, dir.workbooks[0].replace(/^\//,''))), dir.workbooks[0], opts); - var wb = parse_wb(getdata(getzipfile(zip, dir.workbooks[0].replace(/^\//,''))), dir.workbooks[0]); var props = {}, propdata = ""; try { propdata = dir.coreprops.length !== 0 ? getdata(getzipfile(zip, dir.coreprops[0].replace(/^\//,''))) : ""; propdata += dir.extprops.length !== 0 ? getdata(getzipfile(zip, dir.extprops[0].replace(/^\//,''))) : ""; props = propdata !== "" ? parseProps(propdata) : {}; } catch(e) { } + + if(opts.bookSheets) { + if(props.Worksheets && props.SheetNames.length > 0) return { SheetNames:props.SheetNames }; + else if(wb.Sheets) return { SheetNames:wb.Sheets.map(function(x) { return x.name; }) }; + } + var deps = {}; if(dir.calcchain) deps=parseDeps(getdata(getzipfile(zip, dir.calcchain.replace(/^\//,'')))); var sheets = {}, i=0;