sheetjs_sheetjs/dist/xlsx.min.js

6 lines
125 KiB
JavaScript
Raw Normal View History

/* xlsx.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
var XLSX={};(function(XLSX){XLSX.version="0.7.2";var current_codepage=1252,current_cptable;if(typeof module!=="undefined"&&typeof require!=="undefined"){if(typeof cptable==="undefined")cptable=require("codepage");current_cptable=cptable[current_codepage]}function reset_cp(){set_cp(1252)}function set_cp(cp){current_codepage=cp;if(typeof cptable!=="undefined")current_cptable=cptable[cp]}var _getchar=function(x){return String.fromCharCode(x)};if(typeof cptable!=="undefined")_getchar=function(x){if(current_codepage===1200)return String.fromCharCode(x);if(current_cptable)return current_cptable.dec[x];return cptable.utils.decode(current_codepage,[x%256,x>>8])[0]};function char_codes(data){return data.split("").map(function(x){return x.charCodeAt(0)})}function debom_xml(data){if(typeof cptable!=="undefined"){if(data.charCodeAt(0)===255&&data.charCodeAt(1)===254){return cptable.utils.decode(1200,char_codes(data.substr(2)))}}return data}var SSF={};var make_ssf=function(SSF){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.7.0";var opts_fmt={date1904:0,output:"",WTF:false};function fixopts(o){for(var y in opts_fmt)if(o[y]===undefined)o[y]=opts_fmt[y]}SSF.opts=opts_fmt;var table_fmt={0:"General",1:"0",2:"0.00",3:"#,##0",4:"#,##0.00",9:"0%",10:"0.00%",11:"0.00E+00",12:"# ?/?",13:"# ??/??",14:"m/d/yy",15:"d-mmm-yy",16:"d-mmm",17:"mmm-yy",18:"h:mm AM/PM",19:"h:mm:ss AM/PM",20:"h:mm",21:"h:mm:ss",22:"m/d/yy h:mm",37:"#,##0 ;(#,##0)",38:"#,##0 ;[Red](#,##0)",39:"#,##0.00;(#,##0.00)",40:"#,##0.00;[Red](#,##0.00)",45:"mm:ss",46:"[h]:mm:ss",47:"mmss.0",48:"##0.0E+0",49:"@",56:'"上午/下午 "hh"時"mm"分"ss"秒 "',65535:"General"};var days=[["Sun","Sunday"],["Mon","Monday"],["Tue","Tuesday"],["Wed","Wednesday"],["Thu","Thursday"],["Fri","Friday"],["Sat","Saturday"]];var months=[["J","Jan","January"],["F","Feb","February"],["M","Mar","March"],["A","Apr","April"],["M","May","May"],["J","Jun","June"],["J","Jul","July"],["A","Aug","August"],["S","Sep","September"],["O","Oct","October"],["N","Nov","November"],["D","Dec","December"]];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=Math.floor(B);while(Q_1<D){A=Math.floor(B);P=A*P_1+P_2;Q=A*Q_1+Q_2;if(B-A<5e-10)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];if(Q===0)throw"Unexpected state: "+P+" "+P_1+" "+P_2+" "+Q+" "+Q_1+" "+Q_2;var q=Math.floor(sgn*P/Q);return[q,sgn*P-q*Q,Q]};var general_fmt=function(v,opts){if(typeof v==="boolean")return v?"TRUE":"FALSE";if(typeof v==="number"){var o,V=v<0?-v:v;if(V>=.1&&V<1)o=v.toPrecision(9);else if(V>=.01&&V<.1)o=v.toPrecision(8);else if(V>=.001&&V<.01)o=v.toPrecision(7);else if(V>=1e-4&&V<.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>(v<0?12:11))o=v.toPrecision(10);if(o.length>(v<0?12:11))o=v.toExponential(5)}else{o=v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1");if(o.length>(v<0?12:11))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 new Error("unsupported value in General format: "+v)};SSF._general=general_fmt;function fix_hijri(date,o){return 0}var parse_date_code=function parse_date_code(v,opts,b2){var date=Math.floor(v),time=Math.floor(86400*(v-date)+1e-6),dow=0;var dout=[];var out={D:date,T:time,u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};fixopts(opts=opts||{});if(opts.date1904)date+=1462;if(date>2958465)return null;if(out.u>.999){out.u=0;if(++time==86400){time=0;++date}}if(date===60){dout=b2?[1317,10,29]:[1
var CT_LIST=function(){var o={workbooks:{xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",xlsm:"application/vnd.ms-excel.sheet.macroEnabled.main+xml",xlsb:"application/vnd.ms-excel.sheet.binary.macroEnabled.main",xltx:"application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"},strs:{xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",xlsb:"application/vnd.ms-excel.sharedStrings"},sheets:{xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",xlsb:"application/vnd.ms-excel.worksheet"},styles:{xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",xlsb:"application/vnd.ms-excel.styles"}};keys(o).forEach(function(k){if(!o[k].xlsm)o[k].xlsm=o[k].xlsx});keys(o).forEach(function(k){keys(o[k]).forEach(function(v){ct2type[o[k][v]]=k})});return o}();var type2ct=evert(ct2type,true);XMLNS.CT="http://schemas.openxmlformats.org/package/2006/content-types";function parse_ct(data,opts){var ctext={};if(!data||!data.match)return data;var ct={workbooks:[],sheets:[],calcchains:[],themes:[],styles:[],coreprops:[],extprops:[],custprops:[],strs:[],comments:[],vba:[],TODO:[],rels:[],xmlns:""};(data.match(/<[^>]*>/g)||[]).forEach(function(x){var y=parsexmltag(x);switch(y[0].replace(/<\w*:/,"<")){case"<?xml":break;case"<Types":ct.xmlns=y["xmlns"+(y[0].match(/<(\w+):/)||["",""])[1]];break;case"<Default":ctext[y.Extension]=y.ContentType;break;case"<Override":if(y.ContentType in ct2type)ct[ct2type[y.ContentType]].push(y.PartName);else if(opts.WTF)console.error(y);break}});if(ct.xmlns!==XMLNS.CT)throw new Error("Unknown Namespace: "+ct.xmlns);ct.calcchain=ct.calcchains.length>0?ct.calcchains[0]:"";ct.sst=ct.strs.length>0?ct.strs[0]:"";ct.style=ct.styles.length>0?ct.styles[0]:"";ct.defaults=ctext;delete ct.calcchains;return ct}var CTYPE_XML_ROOT=writextag("Types",null,{xmlns:XMLNS.CT,"xmlns:xsd":XMLNS.xsd,"xmlns:xsi":XMLNS.xsi});var CTYPE_DEFAULTS=[["xml","application/xml"],["rels",type2ct.rels[0]]].map(function(x){return writextag("Default",null,{Extension:x[0],ContentType:x[1]})});function write_ct(ct,opts){var o=[],v;o.push(XML_HEADER);o.push(CTYPE_XML_ROOT);o=o.concat(CTYPE_DEFAULTS);var f1=function(w){if(ct[w]&&ct[w].length>0){v=ct[w][0];o.push(writextag("Override",null,{PartName:(v[0]=="/"?"":"/")+v,ContentType:CT_LIST[w][opts.bookType||"xlsx"]}))}};var f2=function(w){ct[w].forEach(function(v){o.push(writextag("Override",null,{PartName:(v[0]=="/"?"":"/")+v,ContentType:CT_LIST[w][opts.bookType||"xlsx"]}))})};var f3=function(t){(ct[t]||[]).forEach(function(v){o.push(writextag("Override",null,{PartName:(v[0]=="/"?"":"/")+v,ContentType:type2ct[t][0]}))})};f1("workbooks");f2("sheets");f3("themes");["strs","styles"].forEach(f1);["coreprops","extprops","custprops"].forEach(f3);if(o.length>2){o.push("</Types>");o[1]=o[1].replace("/>",">")}return o.join("")}var RELS={WB:"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",SHEET:"http://sheetjs.openxmlformats.org/officeDocument/2006/relationships/officeDocument"};function parse_rels(data,currentFilePath){if(!data)return data;if(currentFilePath.charAt(0)!=="/"){currentFilePath="/"+currentFilePath}var rels={};var hash={};var resolveRelativePathIntoAbsolute=function(to){var toksFrom=currentFilePath.split("/");toksFrom.pop();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){var y=parsexmltag(x);if(y[0]==="<Relationship"){var rel={};rel.Type=y.Type;rel.Target=y.Target;rel.Id=y.Id;rel.TargetMode=y.TargetMode;var canonictarget=y.TargetMode==="External"?y.Target:resolveRelativePathIntoAbsolute(y.Target);rels[canonictarget]=rel;hash[y.Id]=rel}});rels["!id"]=hash;return rels}XMLNS.RELS="http://schemas.openxmlformats.org/package/2006/relationships";var RELS_ROOT=writextag("Relationships",null,{xmlns:XMLNS.RELS});function write_rels(rels){var o=[];o.push(XML_HEADER);o
var s={},mtch;var ref=data.match(/<dimension ref="([^"]*)"\s*\/>/);if(ref&&ref.length==2&&ref[1].indexOf(":")!==-1)s["!ref"]=ref[1];var mergecells=[];if(data.match(/<\/mergeCells>/)){var merges=data.match(/<mergeCell ref="([A-Z0-9:]+)"\s*\/>/g);mergecells=merges.map(function(range){return decode_range(/<mergeCell ref="([A-Z0-9:]+)"\s*\/>/.exec(range)[1])})}var refguess={s:{r:1e6,c:1e6},e:{r:0,c:0}};var q=["v","f"];var sidx=0;if(mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m))mtch[1].split(/<\/(?:\w+:)?row>/).forEach(function(x){if(x===""||x.trim()==="")return;var row=parsexmltag(x.match(/<(?:\w+:)?row[^>]*>/)[0]);if(opts.sheetRows&&opts.sheetRows<+row.r)return;if(refguess.s.r>row.r-1)refguess.s.r=row.r-1;if(refguess.e.r<row.r-1)refguess.e.r=row.r-1;var cells=x.substr(x.indexOf(">")+1).split(/<(?:\w+:)?c /);cells.forEach(function(c,idx){if(c===""||c.trim()==="")return;var cref=c.match(/r=["']([^"']*)["']/);c="<c "+c;if(cref&&cref.length==2)idx=decode_cell(cref[1]).c;var cell=parsexmltag((c.match(/<c[^>]*>/)||[c])[0]);delete cell[0];var d=c.substr(c.indexOf(">")+1);var p={};q.forEach(function(f){var x=d.match(matchtag(f));if(x)p[f]=unescapexml(x[1])});if(cell.t===undefined&&p.v===undefined){if(!opts.sheetStubs)return;p.t="str";p.v=undefined}else p.t=cell.t?cell.t:"n";if(refguess.s.c>idx)refguess.s.c=idx;if(refguess.e.c<idx)refguess.e.c=idx;switch(p.t){case"n":p.v=parseFloat(p.v);break;case"s":{sidx=parseInt(p.v,10);p.v=strs[sidx].t;p.r=strs[sidx].r;if(opts.cellHTML)p.h=strs[sidx].h}break;case"str":if(p.v)p.v=utf8read(p.v);break;case"inlineStr":var is=d.match(/<is>(.*)<\/is>/);is=is?parse_si(is[1]):{t:"",r:""};p.t="str";p.v=is.t;break;case"b":if(typeof p.v!=="boolean")p.v=parsexmlbool(p.v);break;case"d":p.v=datenum(p.v);p.t="n";break;case"e":p.raw=RBErr[p.v];break}var fmtid=0;if(cell.s&&styles.CellXf){var cf=styles.CellXf[cell.s];if(cf&&cf.numFmtId)fmtid=cf.numFmtId}try{p.w=SSF.format(fmtid,p.v,_ssfopts);if(opts.cellNF)p.z=SSF._table[fmtid]}catch(e){if(opts.WTF)throw e}s[cell.r]=p})});if(data.match(/<\/hyperlinks>/))data.match(/<hyperlink[^>]*\/>/g).forEach(function(h){var val=parsexmltag(h);delete val[0];if(!val.ref)return;var rel=rels["!id"][val.id];if(rel){val.Target=rel.Target;if(val.location)val.Target+="#"+val.location;val.Rel=rel}var rng=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:"str",v:undefined};s[addr].l=val}});if(!s["!ref"]&&refguess.e.c>=refguess.s.c&&refguess.e.r>=refguess.s.r)s["!ref"]=encode_range(refguess);if(opts.sheetRows&&s["!ref"]){var tmpref=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;return s}var WS_XML_ROOT=writextag("worksheet",null,{xmlns:XMLNS.main[0],"xmlns:r":XMLNS.r});var write_ws_xml_cell=function(cell,ref,ws,opts,idx,wb){var vv=cell.v;if(cell.t=="b")vv=cell.v?"1":"0";var v=writextag("v",escapexml(String(vv))),o={r:ref};o.s=get_cell_style(opts.cellXfs,cell,opts);if(o.s===0||o.s==="0")delete o.s;if(typeof cell.v==="undefined")return"";switch(cell.t){case"s":case"str":{if(opts.bookSST){v=writextag("v",String(get_sst_id(opts.Strings,cell.v)));o.t="s";return writextag("c",v,o)}else{o.t="str";return writextag("c",v,o)}}break;case"n":delete o.t;return writextag("c",v,o);case"b":o.t="b";return writextag("c",v,o);case"e":o.t="e";return writextag("c",v,o)}};var write_ws_xml_data=function(ws,opts,idx,wb){var o=[],r=[],range=utils.decode_range(ws["!ref"]),cell,ref;for(var R=range.s.r;R<=range.e.r;++R){r=[];for(var C=range.s.c;C<=range.e.c;++C){ref=utils.encode_cell({c:C,r:R});if(!ws[ref])continue;if(cell=write_ws_xml_cell(ws[ref],ref,ws,opts,idx,wb))r.push(cell)}if(r.length)o.push(writextag("row",r.join(""),{r:encode_row(R)}))}return o.join("")};
function fix_opts(defaults){return function(opts){defaults.forEach(function(d){if(typeof opts[d[0]]==="undefined")opts[d[0]]=d[1];if(d[2]==="n")opts[d[0]]=Number(opts[d[0]])})}}var fix_read_opts=fix_opts([["cellNF",false],["cellHTML",true],["cellFormula",true],["sheetStubs",false],["sheetRows",0,"n"],["bookDeps",false],["bookSheets",false],["bookProps",false],["bookFiles",false],["bookVBA",false],["WTF",false]]);var fix_write_opts=fix_opts([["bookSST",false],["bookType","xlsx"],["WTF",false]]);function parse_zip(zip,opts){make_ssf(SSF);opts=opts||{};fix_read_opts(opts);reset_cp();var entries=keys(zip.files).filter(function(x){return x.substr(-1)!="/"}).sort();var dir=parse_ct(getzipdata(zip,"[Content_Types].xml"),opts);var xlsb=false;var sheets,binname;if(dir.workbooks.length===0){binname="xl/workbook.xml";if(getzipdata(zip,binname,true))dir.workbooks.push(binname)}if(dir.workbooks.length===0){binname="xl/workbook.bin";if(!getzipfile(zip,binname,true))throw new Error("Could not find workbook");dir.workbooks.push(binname);xlsb=true}if(xlsb)set_cp(1200);if(!opts.bookSheets&&!opts.bookProps){strs=[];if(dir.sst)strs=parse_sst(getzipdata(zip,dir.sst.replace(/^\//,"")),dir.sst,opts);styles={};if(dir.style)styles=parse_sty(getzipdata(zip,dir.style.replace(/^\//,"")),dir.style,opts)}var wb=parse_wb(getzipdata(zip,dir.workbooks[0].replace(/^\//,"")),dir.workbooks[0],opts);var props={},propdata="";if(dir.coreprops.length!==0){propdata=getzipdata(zip,dir.coreprops[0].replace(/^\//,""),true);if(propdata)props=parse_core_props(propdata);if(dir.extprops.length!==0){propdata=getzipdata(zip,dir.extprops[0].replace(/^\//,""),true);if(propdata)parse_ext_props(propdata,props)}}var custprops={};if(!opts.bookSheets||opts.bookProps){if(dir.custprops.length!==0){propdata=getzipdata(zip,dir.custprops[0].replace(/^\//,""),true);if(propdata)custprops=parse_cust_props(propdata,opts)}}var out={};if(opts.bookSheets||opts.bookProps){if(props.Worksheets&&props.SheetNames.length>0)sheets=props.SheetNames;else if(wb.Sheets)sheets=wb.Sheets.map(function(x){return x.name});if(opts.bookProps){out.Props=props;out.Custprops=custprops}if(typeof sheets!=="undefined")out.SheetNames=sheets;if(opts.bookSheets?out.SheetNames:opts.bookProps)return out}sheets={};var deps={};if(opts.bookDeps&&dir.calcchain)deps=parse_cc(getzipdata(zip,dir.calcchain.replace(/^\//,"")),dir.calcchain,opts);var i=0;var sheetRels={};var path,relsPath;if(!props.Worksheets){var wbsheets=wb.Sheets;props.Worksheets=wbsheets.length;props.SheetNames=[];for(var j=0;j!=wbsheets.length;++j){props.SheetNames[j]=wbsheets[j].name}}var wbrelsfile="xl/_rels/workbook.xml.rels";var wbrels=parse_rels(getzipdata(zip,wbrelsfile,true),wbrelsfile);if(wbrels)try{wbrels=wb.Sheets.map(function(w){return[w.name,wbrels["!id"][w.id].Target]})}catch(e){wbrels=null}var nmode=getzipdata(zip,"xl/worksheets/sheet.xml",true)?1:0;for(i=0;i!=props.Worksheets;++i){try{if(wbrels)path="xl/"+wbrels[i][1].replace(/[\/]?xl\//,"");else{path="xl/worksheets/sheet"+(i+1-nmode)+(xlsb?".bin":".xml");path=path.replace(/sheet0\./,"sheet.")}relsPath=path.replace(/^(.*)(\/)([^\/]*)$/,"$1/_rels/$3.rels");sheetRels[props.SheetNames[i]]=parse_rels(getzipdata(zip,relsPath,true),path);sheets[props.SheetNames[i]]=parse_ws(getzipdata(zip,path),path,opts,sheetRels[props.SheetNames[i]])}catch(e){if(opts.WTF)throw e}}if(dir.comments)parse_comments(zip,dir.comments,sheets,sheetRels,opts);out={Directory:dir,Workbook:wb,Props:props,Custprops:custprops,Deps:deps,Sheets:sheets,SheetNames:props.SheetNames,Strings:strs,Styles:styles,SSF:SSF.get_table()};if(opts.bookFiles){out.keys=entries;out.files=zip.files}if(opts.bookVBA){if(dir.vba.length>0)out.vbaraw=getzipdata(zip,dir.vba[0],true);else if(dir.defaults.bin==="application/vnd.ms-office.vbaProject")out.vbaraw=getzipdata(zip,"xl/vbaProject.bin",true)}return out}function add_rels(rels,rId,f,type,relobj){if(!relobj)relobj={};if(!rels["!id"])rels["!id"]={};relobj.Id="rId"+rId;relobj.Type=type;relobj.Target=f;if(rels["!id"][relobj.Id])throw new Error("Cannot rewrite rId "+rId);rels["!id"][relobj.Id]=relobj
//# sourceMappingURL=dist/xlsx.min.map