returnfts}varparse_FontIndex=parseuint16;functionparse_BOF(blob,length){varo={};o.BIFFVer=blob.read_shift(2);length-=2;switch(o.BIFFVer){case1536:case1280:case2:case7:break;default:if(length>6)thrownewError("Unexpected BIFF Ver "+o.BIFFVer)}blob.read_shift(length);returno}functionparse_InterfaceHdr(blob,length){if(length===0)return1200;varq;if((q=blob.read_shift(2))!==1200)throw"InterfaceHdr codePage "+q;return1200}functionparse_WriteAccess(blob,length,opts){if(opts.enc){blob.l+=length;return""}varl=blob.l;varUserName=parse_XLUnicodeString(blob,0,opts);blob.read_shift(length+l-blob.l);returnUserName}functionparse_BoundSheet8(blob,length,opts){varpos=blob.read_shift(4);varhidden=blob.read_shift(1)&3;vardt=blob.read_shift(1);switch(dt){case0:dt="Worksheet";break;case1:dt="Macrosheet";break;case2:dt="Chartsheet";break;case6:dt="VBAModule";break}varname=parse_ShortXLUnicodeString(blob,0,opts);if(name.length===0)name="Sheet1";return{pos:pos,hs:hidden,dt:dt,name:name}}functionparse_SST(blob,length){varcnt=blob.read_shift(4);varucnt=blob.read_shift(4);varstrs=[];for(vari=0;i!=ucnt;++i){strs.push(parse_XLUnicodeRichExtendedString(blob))}strs.Count=cnt;strs.Unique=ucnt;returnstrs}functionparse_ExtSST(blob,length){varextsst={};extsst.dsst=blob.read_shift(2);blob.l+=length-2;returnextsst}functionparse_Row(blob,length){varrw=blob.read_shift(2),col=blob.read_shift(2),Col=blob.read_shift(2),rht=blob.read_shift(2);blob.read_shift(4);varflags=blob.read_shift(1);blob.read_shift(1);blob.read_shift(2);return{r:rw,c:col,cnt:Col-col}}functionparse_ForceFullCalculation(blob,length){varheader=parse_frtHeader(blob);if(header.type!=2211)throw"Invalid Future Record "+header.type;varfullcalc=blob.read_shift(4);returnfullcalc!==0}varparse_CompressPictures=parsenoop2;functionparse_RecalcId(blob,length){blob.read_shift(2);returnblob.read_shift(4)}functionparse_DefaultRowHeight(blob,length){varf=blob.read_shift(2);varfl={Unsynced:f&1,DyZero:(f&2)>>1,ExAsc:(f&4)>>2,ExDsc:(f&8)>>3};varmiyRw=blob.read_shift(2);return[fl,miyRw]}functionparse_Window1(blob,length){varxWn=blob.read_shift(2),yWn=blob.read_shift(2),dxWn=blob.read_shift(2),dyWn=blob.read_shift(2);varflags=blob.read_shift(2),iTabCur=blob.read_shift(2),iTabFirst=blob.read_shift(2);varctabSel=blob.read_shift(2),wTabRatio=blob.read_shift(2);return{Pos:[xWn,yWn],Dim:[dxWn,dyWn],Flags:flags,CurTab:iTabCur,FirstTab:iTabFirst,Selected:ctabSel,TabRatio:wTabRatio}}functionparse_Font(blob,length,opts){blob.l+=14;varname=parse_ShortXLUnicodeString(blob,0,opts);returnname}functionparse_LabelSst(blob,length){varcell=parse_XLSCell(blob);cell.isst=blob.read_shift(4);returncell}functionparse_Label(blob,length,opts){vartarget=blob.l+length;varcell=parse_XLSCell(blob,6);if(opts.biff==2)blob.l++;varstr=parse_XLUnicodeString(blob,target-blob.l,opts);cell.val=str;returncell}functionparse_Format(blob,length,opts){varifmt=blob.read_shift(2);varfmtstr=parse_XLUnicodeString2(blob,0,opts);return[ifmt,fmtstr]}varparse_BIFF2Format=parse_XLUnicodeString2;functionparse_Dimensions(blob,length,opts){varend=blob.l+length;varw=opts.biff==8||!opts.biff?4:2;varr=blob.read_shift(w),R=blob.read_shift(w),c=blob.read_shift(2),C=blob.read_shift(2);blob.l=end;return{s:{r:r,c:c},e:{r:R,c:C}}}functionparse_RK(blob,length){varrw=blob.read_shift(2),col=blob.read_shift(2);varrkrec=parse_RkRec(blob);return{r:rw,c:col,ixfe:rkrec[0],rknum:rkrec[1]}}functionparse_MulRk(blob,length){vartarget=blob.l+length-2;varrw=blob.read_shift(2),col=blob.read_shift(2);varrkrecs=[];while(blob.l<target)rkrecs.push(parse_RkRec(blob));if(blob.l!==target)throw"MulRK read error";varlastcol=blob.read_shift(2);if(rkrecs.length!=lastcol-col+1)throw"MulRK length mismatch";return{r:rw,c:col,C:lastcol,rkrec:rkrecs}}functionparse_MulBlank(blob,length){vartarget=blob.l+length-2;varrw=blob.read_shift(2),col=blob.read_shift(2);varixfes=[];while(blob.l<target)ixfes.push(blob.read_shift(2));if(blob.l!==target)throw"MulBlank read error";varlastcol=blob.read_shift(2);if(ixfes.length!=lastcol-
}functioncycle_width(collw){returnchar2width(px2char(width2px(collw)))}functionfind_mdw_colw(collw){vardelta=Infinity,_MDW=MIN_MDW;for(MDW=MIN_MDW;MDW<MAX_MDW;++MDW)if(Math.abs(collw-cycle_width(collw))<delta){delta=Math.abs(collw-cycle_width(collw));_MDW=MDW}MDW=_MDW}functionfind_mdw_wpx(wpx){vardelta=Infinity,guess=0,_MDW=MIN_MDW;for(MDW=MIN_MDW;MDW<MAX_MDW;++MDW){guess=char2width_(px2char_(wpx))*256;guess=guess%1;if(guess>.5)guess--;if(Math.abs(guess)<delta){delta=Math.abs(guess);_MDW=MDW}}MDW=_MDW}functionprocess_col(coll){if(coll.width){coll.wpx=width2px(coll.width);coll.wch=px2char(coll.wpx);coll.MDW=MDW}elseif(coll.wpx){coll.wch=px2char(coll.wpx);coll.width=char2width(coll.wch);coll.MDW=MDW}if(coll.customWidth)deletecoll.customWidth}varXLMLPatternTypeMap={None:"none",Solid:"solid",Gray50:"mediumGray",Gray75:"darkGray",Gray25:"lightGray",HorzStripe:"darkHorizontal",VertStripe:"darkVertical",ReverseDiagStripe:"darkDown",DiagStripe:"darkUp",DiagCross:"darkGrid",ThickDiagCross:"darkTrellis",ThinHorzStripe:"lightHorizontal",ThinVertStripe:"lightVertical",ThinReverseDiagStripe:"lightDown",ThinHorzCross:"lightGrid"};functionparse_borders(t,styles,themes,opts){styles.Borders=[];varborder={},sub_border={};t[0].match(tagregex).forEach(function(x){vary=parsexmltag(x);switch(y[0]){case"<borders":case"<borders>":case"</borders>":break;case"<border":case"<border>":border={};if(y.diagonalUp){border.diagonalUp=y.diagonalUp}if(y.diagonalDown){border.diagonalDown=y.diagonalDown}styles.Borders.push(border);break;case"</border>":break;case"<left":case"<left/>":break;case"</left>":break;case"<right":case"<right/>":break;case"</right>":break;case"<top":case"<top/>":break;case"</top>":break;case"<bottom":case"<bottom/>":break;case"</bottom>":break;case"<diagonal":case"<diagonal/>":break;case"</diagonal>":break;case"<horizontal":case"<horizontal/>":break;case"</horizontal>":break;case"<vertical":case"<vertical/>":break;case"</vertical>":break;case"<start":case"<start/>":break;case"</start>":break;case"<end":case"<end/>":break;case"</end>":break;case"<color":case"<color/>":break;case"</color>":break;default:if(opts&&opts.WTF)thrownewError("unrecognized "+y[0]+" in borders")}})}functionparse_fills(t,styles,themes,opts){styles.Fills=[];varfill={};t[0].match(tagregex).forEach(function(x){vary=parsexmltag(x);switch(y[0]){case"<fills":case"<fills>":case"</fills>":break;case"<fill>":break;case"</fill>":styles.Fills.push(fill);fill={};break;case"<fill>":break;case"</fill>":styles.Fills.push(fill);fill={};break;case"<patternFill":case"<patternFill>":if(y.patternType)fill.patternType=y.patternType;break;case"<patternFill/>":case"</patternFill>":break;case"<bgColor":if(!fill.bgColor)fill.bgColor={};if(y.indexed)fill.bgColor.indexed=parseInt(y.indexed,10);if(y.theme)fill.bgColor.theme=parseInt(y.theme,10);if(y.tint)fill.bgColor.tint=parseFloat(y.tint);if(y.rgb)fill.bgColor.rgb=y.rgb.slice(-6);break;case"<bgColor/>":case"</bgColor>":break;case"<fgColor":if(!fill.fgColor)fill.fgColor={};if(y.theme)fill.fgColor.theme=parseInt(y.theme,10);if(y.tint)fill.fgColor.tint=parseFloat(y.tint);if(y.rgb)fill.fgColor.rgb=y.rgb.slice(-6);break;case"<fgColor/>":case"</fgColor>":break;case"<stop":case"<stop/>":break;case"</stop>":break;case"<color":case"<color/>":break;case"</color>":break;default:if(opts&&opts.WTF)thrownewError("unrecognized "+y[0]+" in fills")}})}functionparse_fonts(t,styles,themes,opts){styles.Fonts=[];varfont={};t[0].match(tagregex).forEach(function(x){vary=parsexmltag(x);switch(y[0]){case"<fonts":case"<fonts>":case"</fonts>":break;case"<font":case"<font>":break;case"</font>":case"<font/>":styles.Fonts.push(font);font={};break;case"<name":if(y.val)font.name=y.val;break;case"<name/>":case"</name>":break;case"<b":break;case"<b/>":font.bold=true;break;case"<i":break;case"<i/>":font.italic=true;break;case"<u":font.underline=true;break;case"<u/>":font.underline=true;break;case"<strike":break;case"<strike/>":font.strike=true;break;case"<outline/>":font.outline=true;break;case"<shadow/>":font.shadow=true;break;case"<sz":if(y.val)
push_defaults(wb.CalcPr,CalcPrDef);push_defaults_array(wb.WBView,WBViewDef);push_defaults_array(wb.Sheets,SheetDef);_ssfopts.date1904=parsexmlbool(wb.WBProps.date1904,"date1904")}functioncheck_wb(wb){if(!wb||!wb.SheetNames||!wb.Sheets)thrownewError("Invalid Workbook");for(vari=0;i<wb.SheetNames.length;++i)for(varj=0;j<i;++j)if(wb.SheetNames[i]==wb.SheetNames[j])thrownewError("Duplicate Sheet Name: "+wb.SheetNames[i])}varwbnsregex=/<\w+:workbook/;functionparse_wb_xml(data,opts){if(!data)thrownewError("Could not find file");varwb={AppVersion:{},WBProps:{},WBView:[],Sheets:[],CalcPr:{},Names:{"!names":[]},xmlns:""};varpass=false,xmlns="xmlns";vardname={},dnstart=0;data.replace(tagregex,functionxml_wb(x,idx){vary=parsexmltag(x);switch(strip_ns(y[0])){case"<?xml":break;case"<workbook":if(x.match(wbnsregex))xmlns="xmlns"+x.match(/<(\w+):/)[1];wb.xmlns=y[xmlns];break;case"</workbook>":break;case"<fileVersion":deletey[0];wb.AppVersion=y;break;case"<fileVersion/>":case"</fileVersion>":break;case"<fileSharing":case"<fileSharing/>":break;case"<workbookPr":deletey[0];wb.WBProps=y;break;case"<workbookPr/>":deletey[0];wb.WBProps=y;break;case"</workbookPr>":break;case"<workbookProtection":break;case"<workbookProtection/>":break;case"<bookViews>":case"</bookViews>":break;case"<workbookView":deletey[0];wb.WBView.push(y);break;case"</workbookView>":break;case"<sheets>":case"</sheets>":break;case"<sheet":deletey[0];y.name=unescapexml(utf8read(y.name));wb.Sheets.push(y);break;case"</sheet>":break;case"<functionGroups":case"<functionGroups/>":break;case"<functionGroup":break;case"<externalReferences":case"</externalReferences>":case"<externalReferences>":break;case"<externalReference":break;case"<definedNames/>":break;case"<definedNames>":case"<definedNames":pass=true;break;case"</definedNames>":pass=false;break;case"<definedName":{dname={};dname.Name=y.name;if(y.comment)dname.Comment=y.comment;dnstart=idx+x.length}break;case"</definedName>":{dname.Ref=data.slice(dnstart,idx);wb.Names[dname.Name]=dname;wb.Names["!names"].push(dname.Name)}break;case"<definedName/>":break;case"<calcPr":deletey[0];wb.CalcPr=y;break;case"<calcPr/>":deletey[0];wb.CalcPr=y;break;case"</calcPr>":break;case"<oleSize":break;case"<customWorkbookViews>":case"</customWorkbookViews>":case"<customWorkbookViews":break;case"<customWorkbookView":case"</customWorkbookView>":break;case"<pivotCaches>":case"</pivotCaches>":case"<pivotCaches":break;case"<pivotCache":break;case"<smartTagPr":case"<smartTagPr/>":break;case"<smartTagTypes":case"<smartTagTypes>":case"</smartTagTypes>":break;case"<smartTagType":break;case"<webPublishing":case"<webPublishing/>":break;case"<fileRecoveryPr":case"<fileRecoveryPr/>":break;case"<webPublishObjects>":case"<webPublishObjects":case"</webPublishObjects>":break;case"<webPublishObject":break;case"<extLst>":case"</extLst>":case"<extLst/>":break;case"<ext":pass=true;break;case"</ext>":pass=false;break;case"<ArchID":break;case"<AlternateContent":pass=true;break;case"</AlternateContent>":pass=false;break;default:if(!pass&&opts.WTF)thrownewError("unrecognized "+y[0]+" in workbook")}returnx});if(XMLNS.main.indexOf(wb.xmlns)===-1)thrownewError("Unknown Namespace: "+wb.xmlns);parse_wb_defaults(wb);returnwb}varWB_XML_ROOT=writextag("workbook",null,{xmlns:XMLNS.main[0],"xmlns:r":XMLNS.r});functionsafe1904(wb){if(!wb.Workbook)return"false";if(!wb.Workbook.WBProps)return"false";returnparsexmlbool(wb.Workbook.WBProps.date1904)?"true":"false"}functionwrite_wb_xml(wb,opts){varo=[XML_HEADER];o[o.length]=WB_XML_ROOT;o[o.length]=writextag("workbookPr",null,{date1904:safe1904(wb)});o[o.length]="<sheets>";for(vari=0;i!=wb.SheetNames.length;++i)o[o.length]=writextag("sheet",null,{name:escapexml(wb.SheetNames[i].substr(0,31)),sheetId:""+(i+1),"r:id":"rId"+(i+1)});o[o.length]="</sheets>";if(o.length>2){o[o.length]="</workbook>";o[1]=o[1].replace("/>",">")}returno.join("")}functionparse_BrtBundleSh(data,length){varz={};z.hsState=data.read_shift(4);z.iTabID=data.read_shift(4);z.strRelID=parse_RelID(data,length-8);z.name=parse_XLWideSt
varout={};varDirectory={};varfound_sheet=false;varrange={};varlast_formula=null;varsst=[];varcur_sheet="";varPreamble={};varlastcell,last_cell="",cc,cmnt,rng,rngC,rngR;varshared_formulae={};vararray_formulae=[];vartemp_val;varcountry;varcell_valid=true;varXFs=[];varpalette=[];varget_rgb=functiongetrgb(icv){if(icv<8)returnXLSIcv[icv];if(icv<64)returnpalette[icv-8]||XLSIcv[icv];returnXLSIcv[icv]};varprocess_cell_style=functionpcs(cell,line,options){varxfd=line.XF.data;if(!xfd||!xfd.patternType||!options||!options.cellStyles)return;line.s={};line.s.patternType=xfd.patternType;vart;if(t=rgb2Hex(get_rgb(xfd.icvFore))){line.s.fgColor={rgb:t}}if(t=rgb2Hex(get_rgb(xfd.icvBack))){line.s.bgColor={rgb:t}}};varaddcell=functionaddcell(cell,line,options){if(file_depth>1)return;if(!cell_valid)return;if(options.cellStyles&&line.XF&&line.XF.data)process_cell_style(cell,line,options);lastcell=cell;last_cell=encode_cell(cell);if(range.s){if(cell.r<range.s.r)range.s.r=cell.r;if(cell.c<range.s.c)range.s.c=cell.c}if(range.e){if(cell.r+1>range.e.r)range.e.r=cell.r+1;if(cell.c+1>range.e.c)range.e.c=cell.c+1}if(options.cellFormula&&line.f){for(varafi=0;afi<array_formulae.length;++afi){if(array_formulae[afi][0].s.c>cell.c)continue;if(array_formulae[afi][0].s.r>cell.r)continue;if(array_formulae[afi][0].e.c<cell.c)continue;if(array_formulae[afi][0].e.r<cell.r)continue;line.F=encode_range(array_formulae[afi][0]);if(array_formulae[afi][0].s.c!=cell.c)deleteline.f;if(array_formulae[afi][0].s.r!=cell.r)deleteline.f;if(line.f)line.f=""+stringify_formula(array_formulae[afi][1],range,cell,supbooks,opts);break}}if(options.sheetRows&&lastcell.r>=options.sheetRows)cell_valid=false;elseout[last_cell]=line};varopts={enc:false,sbcch:0,snames:[],sharedf:shared_formulae,arrayf:array_formulae,rrtabid:[],lastuser:"",biff:8,codepage:0,winlocked:0,cellStyles:!!options&&!!options.cellStyles,WTF:!!options&&!!options.wtf};if(options.password)opts.password=options.password;varmergecells=[];varobjects=[];varcolinfo=[],rowinfo=[];vardefwidth=0,defheight=0;varseencol=false;varsupbooks=[[]];varsbc=0,sbci=0,sbcli=0;supbooks.SheetNames=opts.snames;supbooks.sharedf=opts.sharedf;supbooks.arrayf=opts.arrayf;varlast_Rn="";varfile_depth=0;varBIFF2Fmt=0;opts.codepage=1200;set_cp(1200);while(blob.l<blob.length-1){vars=blob.l;varRecordType=blob.read_shift(2);if(RecordType===0&&last_Rn==="EOF")break;varlength=blob.l===blob.length?0:blob.read_shift(2),y;varR=XLSRecordEnum[RecordType];if(R&&R.f){if(options.bookSheets){if(last_Rn==="BoundSheet8"&&R.n!=="BoundSheet8")break}last_Rn=R.n;if(R.r===2||R.r==12){varrt=blob.read_shift(2);length-=2;if(!opts.enc&&rt!==RecordType)throw"rt mismatch";if(R.r==12){blob.l+=10;length-=10}}varval;if(R.n==="EOF")val=R.f(blob,length,opts);elseval=slurp(R,blob,length,opts);varRn=R.n;switch(Rn){case"Date1904":wb.opts.Date1904=val;break;case"WriteProtect":wb.opts.WriteProtect=true;break;case"FilePass":if(!opts.enc)blob.l=0;opts.enc=val;if(opts.WTF)console.error(val);if(!options.password)thrownewError("File is password-protected");if(val.Type!==0)thrownewError("Encryption scheme unsupported");if(!val.valid)thrownewError("Password is incorrect");break;case"WriteAccess":opts.lastuser=val;break;case"FileSharing":break;case"CodePage":if(val===21010)val=1200;elseif(val===32769)val=1252;opts.codepage=val;set_cp(val);break;case"RRTabId":opts.rrtabid=val;break;case"WinProtect":opts.winlocked=val;break;case"Template":break;case"RefreshAll":wb.opts.RefreshAll=val;break;case"BookBool":break;case"UsesELFs":break;case"MTRSettings":break;case"CalcCount":wb.opts.CalcCount=val;break;case"CalcDelta":wb.opts.CalcDelta=val;break;case"CalcIter":wb.opts.CalcIter=val;break;case"CalcMode":wb.opts.CalcMode=val;break;case"CalcPrecision":wb.opts.CalcPrecision=val;break;case"CalcSaveRecalc":wb.opts.CalcSaveRecalc=val;break;case"CalcRefMode":opts.CalcRefMode=val;break;case"Uncalced":break;case"ForceFullCalculation":wb.opts.FullCalc=val;break;case"WsBool":break;case"XF":XFs.push(val);break;case"ExtSST":break;case"BookExt":break;case"R