sheetjs/dist/xlsx.min.js

5 lines
90 KiB
JavaScript
Raw Normal View History

/* xlsx.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
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.6.1";var opts_fmt={};function fixopts(o){for(var y in opts_fmt)if(o[y]===undefined)o[y]=opts_fmt[y]}SSF.opts=opts_fmt;opts_fmt.date1904=0;opts_fmt.output="";opts_fmt.mode="";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){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>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 new Error("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.floor(86400*(v-date)+1e-6),dow=0;var dout=[],out={D:date,T:time,u:86400*(v-date)-time};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=[1900,2,29];dow=3}else if(date===0){dout=[1900,1,0];dow=6}else{if(date>60)--date;var d=new Date(1900,0,1);d.setDate(d.getDate()+date-1);dout=[d.getFullYear(),d.getMonth()+1,d.getDate()];dow=d.getDay();if(date<60)dow=(dow+6)%7}out.y=dout[0];out.m=dout[1];out.d=dout[2];out.S=time%60;time=Math.floor(time/60);out.M=time%60;time=Math.floor(time/60);out.H=time;out.q=dow;return out};SSF.parse_date_code=parse_date_code;var write_date=function(type,fmt,val){if(val<0)return"";var o,ss;switch(type){case"y":switch(fmt){case"y":case"yy":return pad(val.y%100,2);default:return pad(val.y%1e4,4)}case"m":switch(fmt){case"m":return val.m;case"mm":return pad(val.m,2);case"mmm":return months[val.m-1][1];case"mmmmm":return months[val.m-1][0];default:return months[val.m-1][2]}case"d":switch(fmt){case"d":return val.d;case"dd":return pad(val.d,2);case"ddd":return days[val.q][0];default:return days[val.q][1]}case"h":switch(fmt){case"h":return 1+(val.H+11)%12;case"hh":return pad(1+(val.H+11)%12,2);default:throw"bad hou
var j=0,widx=0;for(var i=0;i!==v.length;++i){switch(v[i].v){case"Worksheets":widx=j;p.Worksheets=+v[++i].v;break;case"Named Ranges":++i;break}}var parts=parseVector(q.TitlesOfParts).map(function(x){return utf8read(x.v)});p.SheetNames=parts.slice(widx,widx+p.Worksheets)}p.Creator=q["dc:creator"];p.LastModifiedBy=q["cp:lastModifiedBy"];p.CreatedDate=new Date(q["dcterms:created"]);p.ModifiedDate=new Date(q["dcterms:modified"]);return p}function parseCustomProps(data){var p={},name;data.match(/<[^>]+>([^<]*)/g).forEach(function(x){var y=parsexmltag(x);switch(y[0]){case"<property":name=y.name;break;case"</property>":name=null;break;default:if(x.indexOf("<vt:")===0){var toks=x.split(">");var type=toks[0].substring(4),text=toks[1];switch(type){case"lpstr":case"lpwstr":case"bstr":case"lpwstr":p[name]=unescapexml(text);break;case"bool":p[name]=parsexmlbool(text,"<vt:bool>");break;case"i1":case"i2":case"i4":case"i8":case"int":case"uint":p[name]=parseInt(text,10);break;case"r4":case"r8":case"decimal":p[name]=parseFloat(text);break;case"filetime":case"date":p[name]=text;break;case"cy":case"error":p[name]=unescapexml(text);break;default:console.warn("Unexpected",x,type,toks)}}}});return p}function parseDeps(data){var d=[];var l=0,i=1;(data.match(/<[^>]*>/g)||[]).forEach(function(x){var y=parsexmltag(x);switch(y[0]){case"<?xml":break;case"<calcChain":case"<calcChain>":case"</calcChain>":break;case"<c":delete y[0];if(y.i)i=y.i;else y.i=i;d.push(y);break}});return d}var ctext={};function parseCT(data){if(!data||!data.match)return data;var ct={workbooks:[],sheets:[],calcchains:[],themes:[],styles:[],coreprops:[],extprops:[],custprops:[],strs:[],comments:[],xmlns:""};(data.match(/<[^>]*>/g)||[]).forEach(function(x){var y=parsexmltag(x);switch(y[0]){case"<?xml":break;case"<Types":ct.xmlns=y.xmlns;break;case"<Default":ctext[y.Extension]=y.ContentType;break;case"<Override":if(y.ContentType in ct2type)ct[ct2type[y.ContentType]].push(y.PartName);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]:"";delete ct.calcchains;return ct}function parseRels(data,currentFilePath){if(!data)return data;if(currentFilePath.charAt(0)!=="/"){currentFilePath="/"+currentFilePath}var rels={};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=resolveRelativePathIntoAbsolute(y.Target);rels[canonictarget]=rel}});return rels}function parse_comments_xml(data,opts){if(data.match(/<comments *\/>/))return[];var authors=[];var commentList=[];data.match(/<authors>([^\u2603]*)<\/authors>/m)[1].split("</author>").forEach(function(x){if(x===""||x.trim()==="")return;authors.push(x.match(/<author[^>]*>(.*)/)[1])});data.match(/<commentList>([^\u2603]*)<\/commentList>/m)[1].split("</comment>").forEach(function(x,index){if(x===""||x.trim()==="")return;var y=parsexmltag(x.match(/<comment[^>]*>/)[0]);var comment={author:y.authorId&&authors[y.authorId]?authors[y.authorId]:undefined,ref:y.ref,guid:y.guid};var cell=decode_cell(y.ref);if(opts.sheetRows&&opts.sheetRows<=cell.r)return;var textMatch=x.match(/<text>([^\u2603]*)<\/text>/m);if(!textMatch||!textMatch[1])return;var rt=parse_si(textMatch[1]);comment.r=rt.r;comment.t=rt.t;if(opts.cellHTML)comment.h=rt.h;commentList.push(comment)});return commentList}var parse_BrtBeginComment=function(data,length){var out={};out.iauthor=data.read_shift(4);var rfx=parse_UncheckedRfX(data,16);out.rfx=rfx.s;out.ref=encode_cell(rfx.s);data.l+=16;return out};var parse_BrtCommentAuthor=parse_XLWideString;var parse_BrtCommentText=parse_RichStr;function parse_
function fixopts(opts){var defaults=[["cellNF",false],["cellHTML",true],["cellFormula",true],["sheetStubs",false],["sheetRows",0,"n"],["bookDeps",false],["bookSheets",false],["bookProps",false],["bookFiles",false],["WTF",false]];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]])})}function parseZip(zip,opts){opts=opts||{};fixopts(opts);reset_cp();var entries=Object.keys(zip.files);var keys=entries.filter(function(x){return x.substr(-1)!="/"}).sort();var dir=parseCT(getzipdata(zip,"[Content_Types].xml"));var xlsb=false;var sheets;if(dir.workbooks.length===0){var binname="xl/workbook.bin";if(!getzipfile(zip,binname))throw new Error("Could not find workbook entry");dir.workbooks.push(binname);xlsb=true}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="";try{propdata=dir.coreprops.length!==0?getzipdata(zip,dir.coreprops[0].replace(/^\//,"")):"";propdata+=dir.extprops.length!==0?getzipdata(zip,dir.extprops[0].replace(/^\//,"")):"";props=propdata!==""?parseProps(propdata):{}}catch(e){}var custprops={};if(!opts.bookSheets||opts.bookProps){if(dir.custprops.length!==0){propdata=getzipdata(zip,dir.custprops[0].replace(/^\//,""),true);if(propdata)custprops=parseCustomProps(propdata)}}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=parseDeps(getzipdata(zip,dir.calcchain.replace(/^\//,"")));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 nmode=getzipdata(zip,"xl/worksheets/sheet.xml",true)?1:0;for(i=0;i!=props.Worksheets;++i){try{path="xl/worksheets/sheet"+(i+1-nmode)+(xlsb?".bin":".xml");path=path.replace(/sheet0\./,"sheet.");relsPath=path.replace(/^(.*)(\/)([^\/]*)$/,"$1/_rels/$3.rels");sheets[props.SheetNames[i]]=parse_ws(getzipdata(zip,path),path,opts);sheetRels[props.SheetNames[i]]=parseRels(getzipdata(zip,relsPath,true),path)}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};if(opts.bookFiles){out.keys=keys;out.files=zip.files}return out}function readSync(data,options){var zip,d=data;var o=options||{};switch(o.type||"base64"){case"file":if(typeof Buffer!=="undefined"){zip=new jszip(d=_fs.readFileSync(data));break}d=_fs.readFileSync(data).toString("base64");case"base64":zip=new jszip(d,{base64:true});break;case"binary":zip=new jszip(d,{base64:false});break}return parseZip(zip,o)}function readFileSync(data,options){var o=options||{};o.type="file";return readSync(data,o)}XLSX.read=readSync;XLSX.readFile=readFileSync;XLSX.parseZip=parseZip;return this})(XLSX);var _chr=function(c){return String.fromCharCode(c)};function encode_col(col){var s="";for(++col;col;col=Math.floor((col-1)/26))s=_chr((col-1)%26+65)+s;return s}function encode_row(row){return""+(row+1)}function encode_cell(cell){return encode_col(cell.c)+encode_row(cell.r)}function decode_col(c){var d=0,i=0;for(;i!==c.length;++i)d=26*d+c.charCodeAt(i)-64;return d-1}function decode_row(rowstr){return Number(rowstr)-1}function split_cell(cstr){return cstr.replace(/(\$?[A-Z]*)(\$?[0-9]*)/,"$1,$2").split(",")}function decode_cell(cstr){var splt=split_cell(cstr);return{c:decode_col(splt[0]),r:decode_row(s
//# sourceMappingURL=dist/xlsx.min.map