From f959b02ae98698d4bb2c3d687d0a49024ff873a6 Mon Sep 17 00:00:00 2001 From: Pieter Sheth-Voss Date: Mon, 3 Apr 2017 00:43:05 -0400 Subject: [PATCH] Add freeze panes --- bits/67_wsxml.js | 631 +++++++++++++++++++++++------------------ dist/xlsx.core.min.js | 10 +- dist/xlsx.core.min.map | 2 +- dist/xlsx.full.min.js | 10 +- dist/xlsx.full.min.map | 2 +- dist/xlsx.js | 631 +++++++++++++++++++++++------------------ dist/xlsx.min.js | 10 +- dist/xlsx.min.map | 2 +- tests/test-freeze.js | 2 +- xlsx.js | 631 +++++++++++++++++++++++------------------ 10 files changed, 1075 insertions(+), 856 deletions(-) diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js index c302dde..e0e489b 100644 --- a/bits/67_wsxml.js +++ b/bits/67_wsxml.js @@ -1,6 +1,6 @@ function parse_ws_xml_dim(ws, s) { - var d = safe_decode_range(s); - if(d.s.r<=d.e.r && d.s.c<=d.e.c && d.s.r>=0 && d.s.c>=0) ws["!ref"] = encode_range(d); + var d = safe_decode_range(s); + if (d.s.r <= d.e.r && d.s.c <= d.e.c && d.s.r >= 0 && d.s.c >= 0) ws["!ref"] = encode_range(d); } var mergecregex = //g; var sheetdataregex = /<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/; @@ -9,351 +9,424 @@ var dimregex = /"(\w*:\w*)"/; var colregex = /]*\/>/g; /* 18.3 Worksheets */ function parse_ws_xml(data, opts, rels) { - if(!data) return data; - /* 18.3.1.99 worksheet CT_Worksheet */ - var s = {}; + if (!data) return data; + /* 18.3.1.99 worksheet CT_Worksheet */ + var s = {}; - /* 18.3.1.35 dimension CT_SheetDimension ? */ - var ridx = data.indexOf(" 0) { - var ref = data.substr(ridx,50).match(dimregex); - if(ref != null) parse_ws_xml_dim(s, ref[1]); - } + /* 18.3.1.35 dimension CT_SheetDimension ? */ + var ridx = data.indexOf(" 0) { + var ref = data.substr(ridx, 50).match(dimregex); + if (ref != null) parse_ws_xml_dim(s, ref[1]); + } - /* 18.3.1.55 mergeCells CT_MergeCells */ - var mergecells = []; - if(data.indexOf("")!==-1) { - var merges = data.match(mergecregex); - for(ridx = 0; ridx != merges.length; ++ridx) - mergecells[ridx] = safe_decode_range(merges[ridx].substr(merges[ridx].indexOf("\"")+1)); - } + /* 18.3.1.55 mergeCells CT_MergeCells */ + var mergecells = []; + if (data.indexOf("") !== -1) { + var merges = data.match(mergecregex); + for (ridx = 0; ridx != merges.length; ++ridx) + mergecells[ridx] = safe_decode_range(merges[ridx].substr(merges[ridx].indexOf("\"") + 1)); + } - /* 18.3.1.17 cols CT_Cols */ - var columns = []; - if(opts.cellStyles && data.indexOf("")!==-1) { - /* 18.3.1.13 col CT_Col */ - var cols = data.match(colregex); - parse_ws_xml_cols(columns, cols); - } + /* 18.3.1.17 cols CT_Cols */ + var columns = []; + if (opts.cellStyles && data.indexOf("") !== -1) { + /* 18.3.1.13 col CT_Col */ + var cols = data.match(colregex); + parse_ws_xml_cols(columns, cols); + } - var refguess = {s: {r:1000000, c:1000000}, e: {r:0, c:0} }; + var refguess = {s: {r: 1000000, c: 1000000}, e: {r: 0, c: 0}}; - /* 18.3.1.80 sheetData CT_SheetData ? */ - var mtch=data.match(sheetdataregex); - if(mtch) parse_ws_xml_data(mtch[1], s, opts, refguess); + /* 18.3.1.80 sheetData CT_SheetData ? */ + var mtch = data.match(sheetdataregex); + if (mtch) parse_ws_xml_data(mtch[1], s, opts, refguess); - /* 18.3.1.48 hyperlinks CT_Hyperlinks */ - if(data.indexOf("")!==-1) parse_ws_xml_hlinks(s, data.match(hlinkregex), rels); + /* 18.3.1.48 hyperlinks CT_Hyperlinks */ + if (data.indexOf("") !== -1) parse_ws_xml_hlinks(s, data.match(hlinkregex), rels); - if(!s["!ref"] && refguess.e.c >= refguess.s.c && refguess.e.r >= refguess.s.r) s["!ref"] = encode_range(refguess); - if(opts.sheetRows > 0 && s["!ref"]) { - var tmpref = safe_decode_range(s["!ref"]); - if(opts.sheetRows < +tmpref.e.r) { - tmpref.e.r = opts.sheetRows - 1; - if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r; - if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r; - if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c; - if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c; - s["!fullref"] = s["!ref"]; - s["!ref"] = encode_range(tmpref); - } - } - if(mergecells.length > 0) s["!merges"] = mergecells; - if(columns.length > 0) s["!cols"] = columns; - return s; + if (!s["!ref"] && refguess.e.c >= refguess.s.c && refguess.e.r >= refguess.s.r) s["!ref"] = encode_range(refguess); + if (opts.sheetRows > 0 && s["!ref"]) { + var tmpref = safe_decode_range(s["!ref"]); + if (opts.sheetRows < +tmpref.e.r) { + tmpref.e.r = opts.sheetRows - 1; + if (tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r; + if (tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r; + if (tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c; + if (tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c; + s["!fullref"] = s["!ref"]; + s["!ref"] = encode_range(tmpref); + } + } + if (mergecells.length > 0) s["!merges"] = mergecells; + if (columns.length > 0) s["!cols"] = columns; + return s; } function write_ws_xml_merges(merges) { - if(merges.length == 0) return ""; - var o = ''; - for(var i = 0; i != merges.length; ++i) o += ''; - return o + ''; + if (merges.length == 0) return ""; + var o = ''; + for (var i = 0; i != merges.length; ++i) o += ''; + return o + ''; } function write_ws_xml_pagesetup(setup) { - var pageSetup = writextag('pageSetup', null, { + var pageSetup = writextag('pageSetup', null, { scale: setup.scale || '100', orientation: setup.orientation || 'portrait', - horizontalDpi : setup.horizontalDpi || '4294967292', - verticalDpi : setup.verticalDpi || '4294967292' + horizontalDpi: setup.horizontalDpi || '4294967292', + verticalDpi: setup.verticalDpi || '4294967292' }) return pageSetup; } function parse_ws_xml_hlinks(s, data, rels) { - for(var i = 0; i != data.length; ++i) { - var val = parsexmltag(data[i], true); - if(!val.ref) return; - var rel = rels ? rels['!id'][val.id] : null; - if(rel) { - val.Target = rel.Target; - if(val.location) val.Target += "#"+val.location; - val.Rel = rel; - } else { - val.Target = val.location; - rel = {Target: val.location, TargetMode: 'Internal'}; - val.Rel = rel; - } - var rng = safe_decode_range(val.ref); - for(var R=rng.s.r;R<=rng.e.r;++R) for(var C=rng.s.c;C<=rng.e.c;++C) { - var addr = encode_cell({c:C,r:R}); - if(!s[addr]) s[addr] = {t:"stub",v:undefined}; - s[addr].l = val; - } - } + for (var i = 0; i != data.length; ++i) { + var val = parsexmltag(data[i], true); + if (!val.ref) return; + var rel = rels ? rels['!id'][val.id] : null; + if (rel) { + val.Target = rel.Target; + if (val.location) val.Target += "#" + val.location; + val.Rel = rel; + } else { + val.Target = val.location; + rel = {Target: val.location, TargetMode: 'Internal'}; + val.Rel = rel; + } + var rng = safe_decode_range(val.ref); + for (var R = rng.s.r; R <= rng.e.r; ++R) for (var C = rng.s.c; C <= rng.e.c; ++C) { + var addr = encode_cell({c: C, r: R}); + if (!s[addr]) s[addr] = {t: "stub", v: undefined}; + s[addr].l = val; + } + } } function parse_ws_xml_cols(columns, cols) { - var seencol = false; - for(var coli = 0; coli != cols.length; ++coli) { - var coll = parsexmltag(cols[coli], true); - var colm=parseInt(coll.min, 10)-1, colM=parseInt(coll.max,10)-1; - delete coll.min; delete coll.max; - if(!seencol && coll.width) { seencol = true; find_mdw(+coll.width, coll); } - if(coll.width) { - coll.wpx = width2px(+coll.width); - coll.wch = px2char(coll.wpx); - coll.MDW = MDW; - } - while(colm <= colM) columns[colm++] = coll; - } + var seencol = false; + for (var coli = 0; coli != cols.length; ++coli) { + var coll = parsexmltag(cols[coli], true); + var colm = parseInt(coll.min, 10) - 1, colM = parseInt(coll.max, 10) - 1; + delete coll.min; + delete coll.max; + if (!seencol && coll.width) { + seencol = true; + find_mdw(+coll.width, coll); + } + if (coll.width) { + coll.wpx = width2px(+coll.width); + coll.wch = px2char(coll.wpx); + coll.MDW = MDW; + } + while (colm <= colM) columns[colm++] = coll; + } } function write_ws_xml_cols(ws, cols) { - var o = [""], col, width; - for(var i = 0; i != cols.length; ++i) { - if(!(col = cols[i])) continue; - var p = {min:i+1,max:i+1}; - /* wch (chars), wpx (pixels) */ - width = -1; - if(col.wpx) width = px2char(col.wpx); - else if(col.wch) width = col.wch; - if(width > -1) { p.width = char2width(width); p.customWidth= 1; } - o[o.length] = (writextag('col', null, p)); - } - o[o.length] = ""; - return o.join(""); + var o = [""], col, width; + for (var i = 0; i != cols.length; ++i) { + if (!(col = cols[i])) continue; + var p = {min: i + 1, max: i + 1}; + /* wch (chars), wpx (pixels) */ + width = -1; + if (col.wpx) width = px2char(col.wpx); + else if (col.wch) width = col.wch; + if (width > -1) { + p.width = char2width(width); + p.customWidth = 1; + } + o[o.length] = (writextag('col', null, p)); + } + o[o.length] = ""; + return o.join(""); } function write_ws_xml_cell(cell, ref, ws, opts, idx, wb) { - if(cell.v === undefined && cell.s === undefined) return ""; - var vv = ""; - var oldt = cell.t, oldv = cell.v; - switch(cell.t) { - case 'b': vv = cell.v ? "1" : "0"; break; - case 'n': vv = ''+cell.v; break; - case 'e': vv = BErr[cell.v]; break; - case 'd': - if(opts.cellDates) vv = new Date(cell.v).toISOString(); - else { - cell.t = 'n'; - vv = ''+(cell.v = datenum(cell.v)); - if(typeof cell.z === 'undefined') cell.z = SSF._table[14]; - } - break; - default: vv = cell.v; break; - } - var v = writetag('v', escapexml(vv)), o = {r:ref}; - /* TODO: cell style */ - var os = get_cell_style(opts.cellXfs, cell, opts); - if(os !== 0) o.s = os; - switch(cell.t) { - case 'n': break; - case 'd': o.t = "d"; break; - case 'b': o.t = "b"; break; - case 'e': o.t = "e"; break; - default: - if(opts.bookSST) { - v = writetag('v', ''+get_sst_id(opts.Strings, cell.v)); - o.t = "s"; break; - } - o.t = "str"; break; - } - if(cell.t != oldt) { cell.t = oldt; cell.v = oldv; } - return writextag('c', v, o); + if (cell.v === undefined && cell.s === undefined) return ""; + var vv = ""; + var oldt = cell.t, oldv = cell.v; + switch (cell.t) { + case 'b': + vv = cell.v ? "1" : "0"; + break; + case 'n': + vv = '' + cell.v; + break; + case 'e': + vv = BErr[cell.v]; + break; + case 'd': + if (opts.cellDates) vv = new Date(cell.v).toISOString(); + else { + cell.t = 'n'; + vv = '' + (cell.v = datenum(cell.v)); + if (typeof cell.z === 'undefined') cell.z = SSF._table[14]; + } + break; + default: + vv = cell.v; + break; + } + var v = writetag('v', escapexml(vv)), o = {r: ref}; + /* TODO: cell style */ + var os = get_cell_style(opts.cellXfs, cell, opts); + if (os !== 0) o.s = os; + switch (cell.t) { + case 'n': + break; + case 'd': + o.t = "d"; + break; + case 'b': + o.t = "b"; + break; + case 'e': + o.t = "e"; + break; + default: + if (opts.bookSST) { + v = writetag('v', '' + get_sst_id(opts.Strings, cell.v)); + o.t = "s"; + break; + } + o.t = "str"; + break; + } + if (cell.t != oldt) { + cell.t = oldt; + cell.v = oldv; + } + return writextag('c', v, o); } var parse_ws_xml_data = (function parse_ws_xml_data_factory() { - var cellregex = /<(?:\w+:)?c[ >]/, rowregex = /<\/(?:\w+:)?row>/; - var rregex = /r=["']([^"']*)["']/, isregex = /([\S\s]*?)<\/is>/; - var match_v = matchtag("v"), match_f = matchtag("f"); + var cellregex = /<(?:\w+:)?c[ >]/, rowregex = /<\/(?:\w+:)?row>/; + var rregex = /r=["']([^"']*)["']/, isregex = /([\S\s]*?)<\/is>/; + var match_v = matchtag("v"), match_f = matchtag("f"); -return function parse_ws_xml_data(sdata, s, opts, guess) { - var ri = 0, x = "", cells = [], cref = [], idx = 0, i=0, cc=0, d="", p; - var tag, tagr = 0, tagc = 0; - var sstr; - var fmtid = 0, fillid = 0, do_format = Array.isArray(styles.CellXf), cf; - for(var marr = sdata.split(rowregex), mt = 0, marrlen = marr.length; mt != marrlen; ++mt) { - x = marr[mt].trim(); - var xlen = x.length; - if(xlen === 0) continue; + return function parse_ws_xml_data(sdata, s, opts, guess) { + var ri = 0, x = "", cells = [], cref = [], idx = 0, i = 0, cc = 0, d = "", p; + var tag, tagr = 0, tagc = 0; + var sstr; + var fmtid = 0, fillid = 0, do_format = Array.isArray(styles.CellXf), cf; + for (var marr = sdata.split(rowregex), mt = 0, marrlen = marr.length; mt != marrlen; ++mt) { + x = marr[mt].trim(); + var xlen = x.length; + if (xlen === 0) continue; - /* 18.3.1.73 row CT_Row */ - for(ri = 0; ri < xlen; ++ri) if(x.charCodeAt(ri) === 62) break; ++ri; - tag = parsexmltag(x.substr(0,ri), true); - /* SpreadSheetGear uses implicit r/c */ - tagr = typeof tag.r !== 'undefined' ? parseInt(tag.r, 10) : tagr+1; tagc = -1; - if(opts.sheetRows && opts.sheetRows < tagr) continue; - if(guess.s.r > tagr - 1) guess.s.r = tagr - 1; - if(guess.e.r < tagr - 1) guess.e.r = tagr - 1; + /* 18.3.1.73 row CT_Row */ + for (ri = 0; ri < xlen; ++ri) if (x.charCodeAt(ri) === 62) break; + ++ri; + tag = parsexmltag(x.substr(0, ri), true); + /* SpreadSheetGear uses implicit r/c */ + tagr = typeof tag.r !== 'undefined' ? parseInt(tag.r, 10) : tagr + 1; + tagc = -1; + if (opts.sheetRows && opts.sheetRows < tagr) continue; + if (guess.s.r > tagr - 1) guess.s.r = tagr - 1; + if (guess.e.r < tagr - 1) guess.e.r = tagr - 1; - /* 18.3.1.4 c CT_Cell */ - cells = x.substr(ri).split(cellregex); - for(ri = typeof tag.r === 'undefined' ? 0 : 1; ri != cells.length; ++ri) { - x = cells[ri].trim(); - if(x.length === 0) continue; - cref = x.match(rregex); idx = ri; i=0; cc=0; - x = "":"") + x; - if(cref !== null && cref.length === 2) { - idx = 0; d=cref[1]; - for(i=0; i != d.length; ++i) { - if((cc=d.charCodeAt(i)-64) < 1 || cc > 26) break; - idx = 26*idx + cc; - } - --idx; - tagc = idx; - } else ++tagc; - for(i = 0; i != x.length; ++i) if(x.charCodeAt(i) === 62) break; ++i; - tag = parsexmltag(x.substr(0,i), true); - if(!tag.r) tag.r = utils.encode_cell({r:tagr-1, c:tagc}); - d = x.substr(i); - p = {t:""}; + /* 18.3.1.4 c CT_Cell */ + cells = x.substr(ri).split(cellregex); + for (ri = typeof tag.r === 'undefined' ? 0 : 1; ri != cells.length; ++ri) { + x = cells[ri].trim(); + if (x.length === 0) continue; + cref = x.match(rregex); + idx = ri; + i = 0; + cc = 0; + x = "" : "") + x; + if (cref !== null && cref.length === 2) { + idx = 0; + d = cref[1]; + for (i = 0; i != d.length; ++i) { + if ((cc = d.charCodeAt(i) - 64) < 1 || cc > 26) break; + idx = 26 * idx + cc; + } + --idx; + tagc = idx; + } else ++tagc; + for (i = 0; i != x.length; ++i) if (x.charCodeAt(i) === 62) break; + ++i; + tag = parsexmltag(x.substr(0, i), true); + if (!tag.r) tag.r = utils.encode_cell({r: tagr - 1, c: tagc}); + d = x.substr(i); + p = {t: ""}; - if((cref=d.match(match_v))!== null && cref[1] !== '') p.v=unescapexml(cref[1]); - if(opts.cellFormula && (cref=d.match(match_f))!== null) p.f=unescapexml(cref[1]); + if ((cref = d.match(match_v)) !== null && cref[1] !== '') p.v = unescapexml(cref[1]); + if (opts.cellFormula && (cref = d.match(match_f)) !== null) p.f = unescapexml(cref[1]); - /* SCHEMA IS ACTUALLY INCORRECT HERE. IF A CELL HAS NO T, EMIT "" */ - if(tag.t === undefined && tag.s === undefined && p.v === undefined) { - if(!opts.sheetStubs) continue; - p.t = "stub"; - } - else p.t = tag.t || "n"; - if(guess.s.c > idx) guess.s.c = idx; - if(guess.e.c < idx) guess.e.c = idx; - /* 18.18.11 t ST_CellType */ - switch(p.t) { - case 'n': - p.v = parseFloat(p.v); - if(isNaN(p.v)) p.v = "" // we don't want NaN if p.v is null - break; - case 's': - // if (!p.hasOwnProperty('v')) continue; - sstr = strs[parseInt(p.v, 10)]; - p.v = sstr.t; - p.r = sstr.r; - if(opts.cellHTML) p.h = sstr.h; - break; - case 'str': - p.t = "s"; - p.v = (p.v!=null) ? utf8read(p.v) : ''; - if(opts.cellHTML) p.h = p.v; - break; - case 'inlineStr': - cref = d.match(isregex); - p.t = 's'; - if(cref !== null) { sstr = parse_si(cref[1]); p.v = sstr.t; } else p.v = ""; - break; // inline string - case 'b': p.v = parsexmlbool(p.v); break; - case 'd': - if(!opts.cellDates) { p.v = datenum(p.v); p.t = 'n'; } - break; - /* error string in .v, number in .v */ - case 'e': p.w = p.v; p.v = RBErr[p.v]; break; - } - /* formatting */ - fmtid = fillid = 0; - if(do_format && tag.s !== undefined) { - cf = styles.CellXf[tag.s]; - if (opts.cellStyles) { - p.s = get_cell_style_csf(cf) - } - if(cf != null) { - if(cf.numFmtId != null) fmtid = cf.numFmtId; - if(opts.cellStyles && cf.fillId != null) fillid = cf.fillId; - } + /* SCHEMA IS ACTUALLY INCORRECT HERE. IF A CELL HAS NO T, EMIT "" */ + if (tag.t === undefined && tag.s === undefined && p.v === undefined) { + if (!opts.sheetStubs) continue; + p.t = "stub"; + } + else p.t = tag.t || "n"; + if (guess.s.c > idx) guess.s.c = idx; + if (guess.e.c < idx) guess.e.c = idx; + /* 18.18.11 t ST_CellType */ + switch (p.t) { + case 'n': + p.v = parseFloat(p.v); + if (isNaN(p.v)) p.v = "" // we don't want NaN if p.v is null + break; + case 's': + // if (!p.hasOwnProperty('v')) continue; + sstr = strs[parseInt(p.v, 10)]; + p.v = sstr.t; + p.r = sstr.r; + if (opts.cellHTML) p.h = sstr.h; + break; + case 'str': + p.t = "s"; + p.v = (p.v != null) ? utf8read(p.v) : ''; + if (opts.cellHTML) p.h = p.v; + break; + case 'inlineStr': + cref = d.match(isregex); + p.t = 's'; + if (cref !== null) { + sstr = parse_si(cref[1]); + p.v = sstr.t; + } else p.v = ""; + break; // inline string + case 'b': + p.v = parsexmlbool(p.v); + break; + case 'd': + if (!opts.cellDates) { + p.v = datenum(p.v); + p.t = 'n'; } - safe_format(p, fmtid, fillid, opts); - s[tag.r] = p; + break; + /* error string in .v, number in .v */ + case 'e': + p.w = p.v; + p.v = RBErr[p.v]; + break; + } + /* formatting */ + fmtid = fillid = 0; + if (do_format && tag.s !== undefined) { + cf = styles.CellXf[tag.s]; + if (opts.cellStyles) { + p.s = get_cell_style_csf(cf) + } + if (cf != null) { + if (cf.numFmtId != null) fmtid = cf.numFmtId; + if (opts.cellStyles && cf.fillId != null) fillid = cf.fillId; + } + } + safe_format(p, fmtid, fillid, opts); + s[tag.r] = p; } - } -}; })(); + } + }; +})(); function write_ws_xml_data(ws, opts, idx, wb) { - var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R, C; - for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C); - for(R = range.s.r; R <= range.e.r; ++R) { - r = []; - rr = encode_row(R); - for(C = range.s.c; C <= range.e.c; ++C) { - ref = cols[C] + rr; - if(ws[ref] === undefined) continue; - if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell); - } - if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr})); - } - return o.join(""); + var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R, C; + for (C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C); + for (R = range.s.r; R <= range.e.r; ++R) { + r = []; + rr = encode_row(R); + for (C = range.s.c; C <= range.e.c; ++C) { + ref = cols[C] + rr; + if (ws[ref] === undefined) continue; + if ((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell); + } + if (r.length > 0) o[o.length] = (writextag('row', r.join(""), {r: rr})); + } + return o.join(""); } var WS_XML_ROOT = writextag('worksheet', null, { - 'xmlns': XMLNS.main[0], - 'xmlns:r': XMLNS.r + 'xmlns': XMLNS.main[0], + 'xmlns:r': XMLNS.r }); function write_ws_xml(idx, opts, wb) { - var o = [XML_HEADER, WS_XML_ROOT]; - var s = wb.SheetNames[idx], sidx = 0, rdata = ""; - var ws = wb.Sheets[s]; - if(ws === undefined) ws = {}; - var ref = ws['!ref']; if(ref === undefined) ref = 'A1'; - o[o.length] = (writextag('dimension', null, {'ref': ref})); + var o = [XML_HEADER, WS_XML_ROOT]; + var s = wb.SheetNames[idx], sidx = 0, rdata = ""; + var ws = wb.Sheets[s]; + if (ws === undefined) ws = {}; + var ref = ws['!ref']; + if (ref === undefined) ref = 'A1'; + o[o.length] = (writextag('dimension', null, {'ref': ref})); - var pane = ''; - if (ws['!freeze']) { - pane = writextag('pane',null, ws['!freeze']) - } + var kids = []; + if (ws['!freeze']) { + var pane = ''; + pane = writextag('pane', null, ws['!freeze']) + kids.push(pane) - var sheetView = writextag('sheetView', pane, { + var selection = writextag('selection', null, { + pane: "topLeft" + }) + kids.push(selection) + + var selection = writextag('selection', null, { + pane: "bottomLeft" + }) + kids.push(selection) + + var selection = writextag('selection', null, { + pane: "bottomRight", + activeCell: ws['!freeze'], + sqref: ws['!freeze'] + }) + kids.push(selection) + } + + +// + + var sheetView = writextag('sheetView', kids.join('') || undefined, { showGridLines: opts.showGridLines == false ? '0' : '1', - tabSelected: opts.tabSelected === undefined ? '0' : opts.tabSelected, // see issue #26, need to set WorkbookViews if this is set + tabSelected: opts.tabSelected === undefined ? '0' : opts.tabSelected, // see issue #26, need to set WorkbookViews if this is set workbookViewId: opts.workbookViewId === undefined ? '0' : opts.workbookViewId }); o[o.length] = writextag('sheetViews', sheetView); - if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols'])); - o[sidx = o.length] = ''; - if(ws['!ref'] !== undefined) { - rdata = write_ws_xml_data(ws, opts, idx, wb); - if(rdata.length > 0) o[o.length] = (rdata); - } - if(o.length>sidx+1) { o[o.length] = (''); o[sidx]=o[sidx].replace("/>",">"); } + if (ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols'])); + o[sidx = o.length] = ''; + if (ws['!ref'] !== undefined) { + rdata = write_ws_xml_data(ws, opts, idx, wb); + if (rdata.length > 0) o[o.length] = (rdata); + } + if (o.length > sidx + 1) { + o[o.length] = (''); + o[sidx] = o[sidx].replace("/>", ">"); + } - if(ws['!merges'] !== undefined && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges'])); + if (ws['!merges'] !== undefined && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges'])); - if (ws['!pageSetup'] !== undefined) o[o.length] = write_ws_xml_pagesetup(ws['!pageSetup']); - if (ws['!rowBreaks'] !== undefined) o[o.length] = write_ws_xml_row_breaks(ws['!rowBreaks']); - if (ws['!colBreaks'] !== undefined) o[o.length] = write_ws_xml_col_breaks(ws['!colBreaks']); + if (ws['!pageSetup'] !== undefined) o[o.length] = write_ws_xml_pagesetup(ws['!pageSetup']); + if (ws['!rowBreaks'] !== undefined) o[o.length] = write_ws_xml_row_breaks(ws['!rowBreaks']); + if (ws['!colBreaks'] !== undefined) o[o.length] = write_ws_xml_col_breaks(ws['!colBreaks']); - if(o.length>2) { o[o.length] = (''); o[1]=o[1].replace("/>",">"); } - return o.join(""); + if (o.length > 2) { + o[o.length] = (''); + o[1] = o[1].replace("/>", ">"); + } + return o.join(""); } function write_ws_xml_row_breaks(breaks) { - console.log("Writing breaks") var brk = []; - for (var i=0; i=8)current_codepage=1200;if(opts===undefined||opts.biff!==5){var fHighByte=blob.read_shift(1);if(fHighByte){width=2;encoding="dbcs-cont"}}var o=cch?blob.read_shift(cch,encoding):"";current_codepage=cp;return o}function parse_XLUnicodeRichExtendedString(blob){var cp=current_codepage;current_codepage=1200;var cch=blob.read_shift(2),flags=blob.read_shift(1);var fHighByte=flags&1,fExtSt=flags&4,fRichSt=flags&8;var width=1+(flags&1);var cRun,cbExtRst;var z={};if(fRichSt)cRun=blob.read_shift(2);if(fExtSt)cbExtRst=blob.read_shift(4);var encoding=flags&1?"dbcs-cont":"sbcs-cont";var msg=cch===0?"":blob.read_shift(cch,encoding);if(fRichSt)blob.l+=4*cRun;if(fExtSt)blob.l+=cbExtRst;z.t=msg;if(!fRichSt){z.raw=""+z.t+"";z.r=z.t}current_codepage=cp;return z}function parse_XLUnicodeStringNoCch(blob,cch,opts){var retval;var fHighByte=blob.read_shift(1);if(fHighByte===0){retval=blob.read_shift(cch,"sbcs-cont")}else{retval=blob.read_shift(cch,"dbcs-cont")}return retval}function parse_XLUnicodeString(blob,length,opts){var cch=blob.read_shift(opts!==undefined&&opts.biff>0&&opts.biff<8?1:2);if(cch===0){blob.l++;return""}return parse_XLUnicodeStringNoCch(blob,cch,opts)}function parse_XLUnicodeString2(blob,length,opts){if(opts.biff!==5&&opts.biff!==2)return parse_XLUnicodeString(blob,length,opts);var cch=blob.read_shift(1);if(cch===0){blob.l++;return""}return blob.read_shift(cch,"sbcs-cont")}var parse_ControlInfo=parsenoop;var parse_URLMoniker=function(blob,length){var len=blob.read_shift(4),start=blob.l;var extra=false;if(len>24){blob.l+=len-24;if(blob.read_shift(16)==="795881f43b1d7f48af2c825dc4852763")extra=true;blob.l=start}var url=blob.read_shift((extra?len-24:len)>>1,"utf16le").replace(chr0,"");if(extra)blob.l+=24;return url};var parse_FileMoniker=function(blob,length){var cAnti=blob.read_shift(2);var ansiLength=blob.read_shift(4);var ansiPath=blob.read_shift(ansiLength,"cstr");var endServer=blob.read_shift(2);var versionNumber=blob.read_shift(2);var cbUnicodePathSize=blob.read_shift(4);if(cbUnicodePathSize===0)return ansiPath.replace(/\\/g,"/");var cbUnicodePathBytes=blob.read_shift(4);var usKeyValue=blob.read_shift(2);var unicodePath=blob.read_shift(cbUnicodePathBytes>>1,"utf16le").replace(chr0,"");return unicodePath};var parse_HyperlinkMoniker=function(blob,length){var clsid=blob.read_shift(16);length-=16;switch(clsid){case"e0c9ea79f9bace118c8200aa004ba90b":return parse_URLMoniker(blob,length);case"0303000000000000c000000000000046":return parse_FileMoniker(blob,length);default:throw"unsupported moniker "+clsid}};var parse_HyperlinkString=function(blob,length){var len=blob.read_shift(4);var o=blob.read_shift(len,"utf16le").replace(chr0,"");return o};var parse_Hyperlink=function(blob,length){var end=blob.l+length;var sVer=blob.read_shift(4);if(sVer!==2)throw new Error("Unrecognized streamVersion: "+sVer);var flags=blob.read_shift(2);blob.l+=2;var displayName,targetFrameName,moniker,oleMoniker,location,guid,fileTime;if(flags&16)displayName=parse_HyperlinkString(blob,end-blob.l);if(flags&128)targetFrameName=parse_HyperlinkString(blob,end-blob.l);if((flags&257)===257)moniker=parse_HyperlinkString(blob,end-blob.l);if((flags&257)===1)oleMoniker=parse_HyperlinkMoniker(blob,end-blob.l);if(flags&8)location=parse_HyperlinkString(blob,end-blob.l);if(flags&32)guid=blob.read_shift(16);if(flags&64)fileTime=parse_FILETIME(blob,8);blob.l=end;var target=targetFrameName||moniker||oleMoniker;if(location)target+="#"+location;return{Target:target}};function parse_LongRGBA(blob,length){var r=blob.read_shift(1),g=blob.read_shift(1),b=blob.read_shift(1),a=blob.read_shift(1);return[r,g,b,a]}function parse_LongRGB(blob,length){var x=parse_LongRGBA(blob,length);x[3]=0;return x}function parse_XLSCell(blob,length){var rw=blob.read_shift(2);var col=blob.read_shift(2);var ixfe=blob.read_shift(2);return{r:rw,c:col,ixfe:ixfe}}function parse_frtHeader(blob){var rt=blob.read_shift(2);var flags=blob.read_shift(2);blob.l+=8;return{type:rt,flags:flags}}function parse_OptXLUnicodeString(blob,length,opts){return length===0?"":parse_XLUnicodeString2(blob,length,opts)}var HIDEOBJENUM=["SHOWALL","SHOWPLACEHOLDER","HIDEALL"];var parse_HideObjEnum=parseuint16;function parse_XTI(blob,length){var iSupBook=blob.read_shift(2),itabFirst=blob.read_shift(2,"i"),itabLast=blob.read_shift(2,"i");return[iSupBook,itabFirst,itabLast]}function parse_RkRec(blob,length){var ixfe=blob.read_shift(2);var RK=parse_RkNumber(blob);return[ixfe,RK]}function parse_AddinUdf(blob,length){blob.l+=4;length-=4;var l=blob.l+length;var udfName=parse_ShortXLUnicodeString(blob,length);var cb=blob.read_shift(2);l-=blob.l;if(cb!==l)throw"Malformed AddinUdf: padding = "+l+" != "+cb;blob.l+=cb;return udfName}function parse_Ref8U(blob,length){var rwFirst=blob.read_shift(2);var rwLast=blob.read_shift(2);var colFirst=blob.read_shift(2);var colLast=blob.read_shift(2);return{s:{c:colFirst,r:rwFirst},e:{c:colLast,r:rwLast}}}function parse_RefU(blob,length){var rwFirst=blob.read_shift(2);var rwLast=blob.read_shift(2);var colFirst=blob.read_shift(1);var colLast=blob.read_shift(1);return{s:{c:colFirst,r:rwFirst},e:{c:colLast,r:rwLast}}}var parse_Ref=parse_RefU;function parse_FtCmo(blob,length){blob.l+=4;var ot=blob.read_shift(2);var id=blob.read_shift(2);var flags=blob.read_shift(2);blob.l+=12;return[id,ot,flags]}function parse_FtNts(blob,length){var out={};blob.l+=4;blob.l+=16;out.fSharedNote=blob.read_shift(2);blob.l+=4;return out}function parse_FtCf(blob,length){var out={};blob.l+=4;blob.cf=blob.read_shift(2);return out}var FtTab={21:parse_FtCmo,19:parsenoop,18:function(blob,length){blob.l+=12},17:function(blob,length){blob.l+=8},16:parsenoop,15:parsenoop,13:parse_FtNts,12:function(blob,length){blob.l+=24},11:function(blob,length){blob.l+=10},10:function(blob,length){blob.l+=16},9:parsenoop,8:function(blob,length){blob.l+=6},7:parse_FtCf,6:function(blob,length){blob.l+=6},4:parsenoop,0:function(blob,length){blob.l+=4}};function parse_FtArray(blob,length,ot){var s=blob.l;var fts=[];while(blob.l