xlsx.js 715 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711
  1. /*! xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ts=2: */
  3. /*exported XLSX */
  4. /*global global, exports, module, require:false, process:false, Buffer:false, ArrayBuffer:false */
  5. var XLSX = {};
  6. function make_xlsx_lib(XLSX){
  7. XLSX.version = '0.16.9';
  8. var current_codepage = 1200, current_ansi = 1252;
  9. /*global cptable:true, window */
  10. if(typeof module !== "undefined" && typeof require !== 'undefined') {
  11. if(typeof cptable === 'undefined') {
  12. if(typeof global !== 'undefined') global.cptable = require('./dist/cpexcel.js');
  13. else if(typeof window !== 'undefined') window.cptable = require('./dist/cpexcel.js');
  14. }
  15. }
  16. var VALID_ANSI = [ 874, 932, 936, 949, 950 ];
  17. for(var i = 0; i <= 8; ++i) VALID_ANSI.push(1250 + i);
  18. /* ECMA-376 Part I 18.4.1 charset to codepage mapping */
  19. var CS2CP = ({
  20. 0: 1252, /* ANSI */
  21. 1: 65001, /* DEFAULT */
  22. 2: 65001, /* SYMBOL */
  23. 77: 10000, /* MAC */
  24. 128: 932, /* SHIFTJIS */
  25. 129: 949, /* HANGUL */
  26. 130: 1361, /* JOHAB */
  27. 134: 936, /* GB2312 */
  28. 136: 950, /* CHINESEBIG5 */
  29. 161: 1253, /* GREEK */
  30. 162: 1254, /* TURKISH */
  31. 163: 1258, /* VIETNAMESE */
  32. 177: 1255, /* HEBREW */
  33. 178: 1256, /* ARABIC */
  34. 186: 1257, /* BALTIC */
  35. 204: 1251, /* RUSSIAN */
  36. 222: 874, /* THAI */
  37. 238: 1250, /* EASTEUROPE */
  38. 255: 1252, /* OEM */
  39. 69: 6969 /* MISC */
  40. });
  41. var set_ansi = function(cp) { if(VALID_ANSI.indexOf(cp) == -1) return; current_ansi = CS2CP[0] = cp; };
  42. function reset_ansi() { set_ansi(1252); }
  43. var set_cp = function(cp) { current_codepage = cp; set_ansi(cp); };
  44. function reset_cp() { set_cp(1200); reset_ansi(); }
  45. function char_codes(data) { var o = []; for(var i = 0, len = data.length; i < len; ++i) o[i] = data.charCodeAt(i); return o; }
  46. function utf16leread(data) {
  47. var o = [];
  48. for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i) + (data.charCodeAt(2*i+1)<<8));
  49. return o.join("");
  50. }
  51. function utf16beread(data) {
  52. var o = [];
  53. for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i+1) + (data.charCodeAt(2*i)<<8));
  54. return o.join("");
  55. }
  56. var debom = function(data) {
  57. var c1 = data.charCodeAt(0), c2 = data.charCodeAt(1);
  58. if(c1 == 0xFF && c2 == 0xFE) return utf16leread(data.slice(2));
  59. if(c1 == 0xFE && c2 == 0xFF) return utf16beread(data.slice(2));
  60. if(c1 == 0xFEFF) return data.slice(1);
  61. return data;
  62. };
  63. var _getchar = function _gc1(x) { return String.fromCharCode(x); };
  64. var _getansi = function _ga1(x) { return String.fromCharCode(x); };
  65. if(typeof cptable !== 'undefined') {
  66. set_cp = function(cp) { current_codepage = cp; set_ansi(cp); };
  67. debom = function(data) {
  68. if(data.charCodeAt(0) === 0xFF && data.charCodeAt(1) === 0xFE) { return cptable.utils.decode(1200, char_codes(data.slice(2))); }
  69. return data;
  70. };
  71. _getchar = function _gc2(x) {
  72. if(current_codepage === 1200) return String.fromCharCode(x);
  73. return cptable.utils.decode(current_codepage, [x&255,x>>8])[0];
  74. };
  75. _getansi = function _ga2(x) {
  76. return cptable.utils.decode(current_ansi, [x])[0];
  77. };
  78. }
  79. var DENSE = null;
  80. var DIF_XL = true;
  81. var Base64 = (function make_b64(){
  82. var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  83. return {
  84. encode: function(input) {
  85. var o = "";
  86. var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
  87. for(var i = 0; i < input.length; ) {
  88. c1 = input.charCodeAt(i++);
  89. e1 = (c1 >> 2);
  90. c2 = input.charCodeAt(i++);
  91. e2 = ((c1 & 3) << 4) | (c2 >> 4);
  92. c3 = input.charCodeAt(i++);
  93. e3 = ((c2 & 15) << 2) | (c3 >> 6);
  94. e4 = (c3 & 63);
  95. if (isNaN(c2)) { e3 = e4 = 64; }
  96. else if (isNaN(c3)) { e4 = 64; }
  97. o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4);
  98. }
  99. return o;
  100. },
  101. decode: function b64_decode(input) {
  102. var o = "";
  103. var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
  104. input = input.replace(/[^\w\+\/\=]/g, "");
  105. for(var i = 0; i < input.length;) {
  106. e1 = map.indexOf(input.charAt(i++));
  107. e2 = map.indexOf(input.charAt(i++));
  108. c1 = (e1 << 2) | (e2 >> 4);
  109. o += String.fromCharCode(c1);
  110. e3 = map.indexOf(input.charAt(i++));
  111. c2 = ((e2 & 15) << 4) | (e3 >> 2);
  112. if (e3 !== 64) { o += String.fromCharCode(c2); }
  113. e4 = map.indexOf(input.charAt(i++));
  114. c3 = ((e3 & 3) << 6) | e4;
  115. if (e4 !== 64) { o += String.fromCharCode(c3); }
  116. }
  117. return o;
  118. }
  119. };
  120. })();
  121. var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node);
  122. var Buffer_from = function(){};
  123. if(typeof Buffer !== 'undefined') {
  124. var nbfs = !Buffer.from;
  125. if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
  126. Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
  127. // $FlowIgnore
  128. if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
  129. // $FlowIgnore
  130. if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
  131. }
  132. function new_raw_buf(len) {
  133. /* jshint -W056 */
  134. return has_buf ? Buffer.alloc(len) : new Array(len);
  135. /* jshint +W056 */
  136. }
  137. function new_unsafe_buf(len) {
  138. /* jshint -W056 */
  139. return has_buf ? Buffer.allocUnsafe(len) : new Array(len);
  140. /* jshint +W056 */
  141. }
  142. var s2a = function s2a(s) {
  143. if(has_buf) return Buffer_from(s, "binary");
  144. return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
  145. };
  146. function s2ab(s) {
  147. if(typeof ArrayBuffer === 'undefined') return s2a(s);
  148. var buf = new ArrayBuffer(s.length), view = new Uint8Array(buf);
  149. for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  150. return buf;
  151. }
  152. function a2s(data) {
  153. if(Array.isArray(data)) return data.map(function(c) { return String.fromCharCode(c); }).join("");
  154. var o = []; for(var i = 0; i < data.length; ++i) o[i] = String.fromCharCode(data[i]); return o.join("");
  155. }
  156. function a2u(data) {
  157. if(typeof Uint8Array === 'undefined') throw new Error("Unsupported");
  158. return new Uint8Array(data);
  159. }
  160. function ab2a(data) {
  161. if(typeof ArrayBuffer == 'undefined') throw new Error("Unsupported");
  162. if(data instanceof ArrayBuffer) return ab2a(new Uint8Array(data));
  163. var o = new Array(data.length);
  164. for(var i = 0; i < data.length; ++i) o[i] = data[i];
  165. return o;
  166. }
  167. var bconcat = function(bufs) { return [].concat.apply([], bufs); };
  168. var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/g;
  169. /* ssf.js (C) 2013-present SheetJS -- http://sheetjs.com */
  170. /*jshint -W041 */
  171. var SSF = ({});
  172. var make_ssf = function make_ssf(SSF){
  173. SSF.version = '0.11.2';
  174. function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
  175. function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
  176. function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  177. function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
  178. function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
  179. function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
  180. function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  181. var p2_32 = Math.pow(2,32);
  182. function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
  183. function isgeneral(s, i) { i = i || 0; return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; }
  184. var days = [
  185. ['Sun', 'Sunday'],
  186. ['Mon', 'Monday'],
  187. ['Tue', 'Tuesday'],
  188. ['Wed', 'Wednesday'],
  189. ['Thu', 'Thursday'],
  190. ['Fri', 'Friday'],
  191. ['Sat', 'Saturday']
  192. ];
  193. var months = [
  194. ['J', 'Jan', 'January'],
  195. ['F', 'Feb', 'February'],
  196. ['M', 'Mar', 'March'],
  197. ['A', 'Apr', 'April'],
  198. ['M', 'May', 'May'],
  199. ['J', 'Jun', 'June'],
  200. ['J', 'Jul', 'July'],
  201. ['A', 'Aug', 'August'],
  202. ['S', 'Sep', 'September'],
  203. ['O', 'Oct', 'October'],
  204. ['N', 'Nov', 'November'],
  205. ['D', 'Dec', 'December']
  206. ];
  207. function init_table(t) {
  208. t[0]= 'General';
  209. t[1]= '0';
  210. t[2]= '0.00';
  211. t[3]= '#,##0';
  212. t[4]= '#,##0.00';
  213. t[9]= '0%';
  214. t[10]= '0.00%';
  215. t[11]= '0.00E+00';
  216. t[12]= '# ?/?';
  217. t[13]= '# ??/??';
  218. t[14]= 'm/d/yy';
  219. t[15]= 'd-mmm-yy';
  220. t[16]= 'd-mmm';
  221. t[17]= 'mmm-yy';
  222. t[18]= 'h:mm AM/PM';
  223. t[19]= 'h:mm:ss AM/PM';
  224. t[20]= 'h:mm';
  225. t[21]= 'h:mm:ss';
  226. t[22]= 'm/d/yy h:mm';
  227. t[37]= '#,##0 ;(#,##0)';
  228. t[38]= '#,##0 ;[Red](#,##0)';
  229. t[39]= '#,##0.00;(#,##0.00)';
  230. t[40]= '#,##0.00;[Red](#,##0.00)';
  231. t[45]= 'mm:ss';
  232. t[46]= '[h]:mm:ss';
  233. t[47]= 'mmss.0';
  234. t[48]= '##0.0E+0';
  235. t[49]= '@';
  236. t[56]= '"上午/下午 "hh"時"mm"分"ss"秒 "';
  237. }
  238. var table_fmt = {};
  239. init_table(table_fmt);
  240. /* Defaults determined by systematically testing in Excel 2019 */
  241. /* These formats appear to default to other formats in the table */
  242. var default_map = [];
  243. var defi = 0;
  244. // 5 -> 37 ... 8 -> 40
  245. for(defi = 5; defi <= 8; ++defi) default_map[defi] = 32 + defi;
  246. // 23 -> 0 ... 26 -> 0
  247. for(defi = 23; defi <= 26; ++defi) default_map[defi] = 0;
  248. // 27 -> 14 ... 31 -> 14
  249. for(defi = 27; defi <= 31; ++defi) default_map[defi] = 14;
  250. // 50 -> 14 ... 58 -> 14
  251. for(defi = 50; defi <= 58; ++defi) default_map[defi] = 14;
  252. // 59 -> 1 ... 62 -> 4
  253. for(defi = 59; defi <= 62; ++defi) default_map[defi] = defi - 58;
  254. // 67 -> 9 ... 68 -> 10
  255. for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 58;
  256. // 72 -> 14 ... 75 -> 17
  257. for(defi = 72; defi <= 75; ++defi) default_map[defi] = defi - 58;
  258. // 69 -> 12 ... 71 -> 14
  259. for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 57;
  260. // 76 -> 20 ... 78 -> 22
  261. for(defi = 76; defi <= 78; ++defi) default_map[defi] = defi - 56;
  262. // 79 -> 45 ... 81 -> 47
  263. for(defi = 79; defi <= 81; ++defi) default_map[defi] = defi - 34;
  264. // 82 -> 0 ... 65536 -> 0 (omitted)
  265. /* These formats technically refer to Accounting formats with no equivalent */
  266. var default_str = [];
  267. // 5 -- Currency, 0 decimal, black negative
  268. default_str[5] = default_str[63] = '"$"#,##0_);\\("$"#,##0\\)';
  269. // 6 -- Currency, 0 decimal, red negative
  270. default_str[6] = default_str[64] = '"$"#,##0_);[Red]\\("$"#,##0\\)';
  271. // 7 -- Currency, 2 decimal, black negative
  272. default_str[7] = default_str[65] = '"$"#,##0.00_);\\("$"#,##0.00\\)';
  273. // 8 -- Currency, 2 decimal, red negative
  274. default_str[8] = default_str[66] = '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)';
  275. // 41 -- Accounting, 0 decimal, No Symbol
  276. default_str[41] = '_(* #,##0_);_(* \\(#,##0\\);_(* "-"_);_(@_)';
  277. // 42 -- Accounting, 0 decimal, $ Symbol
  278. default_str[42] = '_("$"* #,##0_);_("$"* \\(#,##0\\);_("$"* "-"_);_(@_)';
  279. // 43 -- Accounting, 2 decimal, No Symbol
  280. default_str[43] = '_(* #,##0.00_);_(* \\(#,##0.00\\);_(* "-"??_);_(@_)';
  281. // 44 -- Accounting, 2 decimal, $ Symbol
  282. default_str[44] = '_("$"* #,##0.00_);_("$"* \\(#,##0.00\\);_("$"* "-"??_);_(@_)';
  283. function frac(x, D, mixed) {
  284. var sgn = x < 0 ? -1 : 1;
  285. var B = x * sgn;
  286. var P_2 = 0, P_1 = 1, P = 0;
  287. var Q_2 = 1, Q_1 = 0, Q = 0;
  288. var A = Math.floor(B);
  289. while(Q_1 < D) {
  290. A = Math.floor(B);
  291. P = A * P_1 + P_2;
  292. Q = A * Q_1 + Q_2;
  293. if((B - A) < 0.00000005) break;
  294. B = 1 / (B - A);
  295. P_2 = P_1; P_1 = P;
  296. Q_2 = Q_1; Q_1 = Q;
  297. }
  298. if(Q > D) { if(Q_1 > D) { Q = Q_2; P = P_2; } else { Q = Q_1; P = P_1; } }
  299. if(!mixed) return [0, sgn * P, Q];
  300. var q = Math.floor(sgn * P/Q);
  301. return [q, sgn*P - q*Q, Q];
  302. }
  303. function parse_date_code(v,opts,b2) {
  304. if(v > 2958465 || v < 0) return null;
  305. var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
  306. var dout=[];
  307. 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};
  308. if(Math.abs(out.u) < 1e-6) out.u = 0;
  309. if(opts && opts.date1904) date += 1462;
  310. if(out.u > 0.9999) {
  311. out.u = 0;
  312. if(++time == 86400) { out.T = time = 0; ++date; ++out.D; }
  313. }
  314. if(date === 60) {dout = b2 ? [1317,10,29] : [1900,2,29]; dow=3;}
  315. else if(date === 0) {dout = b2 ? [1317,8,29] : [1900,1,0]; dow=6;}
  316. else {
  317. if(date > 60) --date;
  318. /* 1 = Jan 1 1900 in Gregorian */
  319. var d = new Date(1900, 0, 1);
  320. d.setDate(d.getDate() + date - 1);
  321. dout = [d.getFullYear(), d.getMonth()+1,d.getDate()];
  322. dow = d.getDay();
  323. if(date < 60) dow = (dow + 6) % 7;
  324. if(b2) dow = fix_hijri(d, dout);
  325. }
  326. out.y = dout[0]; out.m = dout[1]; out.d = dout[2];
  327. out.S = time % 60; time = Math.floor(time / 60);
  328. out.M = time % 60; time = Math.floor(time / 60);
  329. out.H = time;
  330. out.q = dow;
  331. return out;
  332. }
  333. SSF.parse_date_code = parse_date_code;
  334. var basedate = new Date(1899, 11, 31, 0, 0, 0);
  335. var dnthresh = basedate.getTime();
  336. var base1904 = new Date(1900, 2, 1, 0, 0, 0);
  337. function datenum_local(v, date1904) {
  338. var epoch = v.getTime();
  339. if(date1904) epoch -= 1461*24*60*60*1000;
  340. else if(v >= base1904) epoch += 24*60*60*1000;
  341. return (epoch - (dnthresh + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000)) / (24 * 60 * 60 * 1000);
  342. }
  343. /* The longest 32-bit integer text is "-4294967296", exactly 11 chars */
  344. function general_fmt_int(v) { return v.toString(10); }
  345. SSF._general_int = general_fmt_int;
  346. /* ECMA-376 18.8.30 numFmt*/
  347. /* Note: `toPrecision` uses standard form when prec > E and E >= -6 */
  348. var general_fmt_num = (function make_general_fmt_num() {
  349. var trailing_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)$/;
  350. function strip_decimal(o) {
  351. return (o.indexOf(".") == -1) ? o : o.replace(trailing_zeroes_and_decimal, "$1");
  352. }
  353. /* General Exponential always shows 2 digits exp and trims the mantissa */
  354. var mantissa_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)[Ee]/;
  355. var exp_with_single_digit = /(E[+-])(\d)$/;
  356. function normalize_exp(o) {
  357. if(o.indexOf("E") == -1) return o;
  358. return o.replace(mantissa_zeroes_and_decimal,"$1E").replace(exp_with_single_digit,"$10$2");
  359. }
  360. /* exponent >= -9 and <= 9 */
  361. function small_exp(v) {
  362. var w = (v<0?12:11);
  363. var o = strip_decimal(v.toFixed(12)); if(o.length <= w) return o;
  364. o = v.toPrecision(10); if(o.length <= w) return o;
  365. return v.toExponential(5);
  366. }
  367. /* exponent >= 11 or <= -10 likely exponential */
  368. function large_exp(v) {
  369. var o = strip_decimal(v.toFixed(11));
  370. return (o.length > (v<0?12:11) || o === "0" || o === "-0") ? v.toPrecision(6) : o;
  371. }
  372. function general_fmt_num_base(v) {
  373. var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
  374. if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
  375. else if(Math.abs(V) <= 9) o = small_exp(v);
  376. else if(V === 10) o = v.toFixed(10).substr(0,12);
  377. else o = large_exp(v);
  378. return strip_decimal(normalize_exp(o.toUpperCase()));
  379. }
  380. return general_fmt_num_base;
  381. })();
  382. SSF._general_num = general_fmt_num;
  383. /*
  384. "General" rules:
  385. - text is passed through ("@")
  386. - booleans are rendered as TRUE/FALSE
  387. - "up to 11 characters" displayed for numbers
  388. - Default date format (code 14) used for Dates
  389. TODO: technically the display depends on the width of the cell
  390. */
  391. function general_fmt(v, opts) {
  392. switch(typeof v) {
  393. case 'string': return v;
  394. case 'boolean': return v ? "TRUE" : "FALSE";
  395. case 'number': return (v|0) === v ? v.toString(10) : general_fmt_num(v);
  396. case 'undefined': return "";
  397. case 'object':
  398. if(v == null) return "";
  399. if(v instanceof Date) return format(14, datenum_local(v, opts && opts.date1904), opts);
  400. }
  401. throw new Error("unsupported value in General format: " + v);
  402. }
  403. SSF._general = general_fmt;
  404. function fix_hijri(date, o) {
  405. /* TODO: properly adjust y/m/d and */
  406. o[0] -= 581;
  407. var dow = date.getDay();
  408. if(date < 60) dow = (dow + 6) % 7;
  409. return dow;
  410. }
  411. //var THAI_DIGITS = "\u0E50\u0E51\u0E52\u0E53\u0E54\u0E55\u0E56\u0E57\u0E58\u0E59".split("");
  412. /*jshint -W086 */
  413. function write_date(type, fmt, val, ss0) {
  414. var o="", ss=0, tt=0, y = val.y, out, outl = 0;
  415. switch(type) {
  416. case 98: /* 'b' buddhist year */
  417. y = val.y + 543;
  418. /* falls through */
  419. case 121: /* 'y' year */
  420. switch(fmt.length) {
  421. case 1: case 2: out = y % 100; outl = 2; break;
  422. default: out = y % 10000; outl = 4; break;
  423. } break;
  424. case 109: /* 'm' month */
  425. switch(fmt.length) {
  426. case 1: case 2: out = val.m; outl = fmt.length; break;
  427. case 3: return months[val.m-1][1];
  428. case 5: return months[val.m-1][0];
  429. default: return months[val.m-1][2];
  430. } break;
  431. case 100: /* 'd' day */
  432. switch(fmt.length) {
  433. case 1: case 2: out = val.d; outl = fmt.length; break;
  434. case 3: return days[val.q][0];
  435. default: return days[val.q][1];
  436. } break;
  437. case 104: /* 'h' 12-hour */
  438. switch(fmt.length) {
  439. case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
  440. default: throw 'bad hour format: ' + fmt;
  441. } break;
  442. case 72: /* 'H' 24-hour */
  443. switch(fmt.length) {
  444. case 1: case 2: out = val.H; outl = fmt.length; break;
  445. default: throw 'bad hour format: ' + fmt;
  446. } break;
  447. case 77: /* 'M' minutes */
  448. switch(fmt.length) {
  449. case 1: case 2: out = val.M; outl = fmt.length; break;
  450. default: throw 'bad minute format: ' + fmt;
  451. } break;
  452. case 115: /* 's' seconds */
  453. if(fmt != 's' && fmt != 'ss' && fmt != '.0' && fmt != '.00' && fmt != '.000') throw 'bad second format: ' + fmt;
  454. if(val.u === 0 && (fmt == "s" || fmt == "ss")) return pad0(val.S, fmt.length);
  455. if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
  456. else tt = ss0 === 1 ? 10 : 1;
  457. ss = Math.round((tt)*(val.S + val.u));
  458. if(ss >= 60*tt) ss = 0;
  459. if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
  460. o = pad0(ss,2 + ss0);
  461. if(fmt === 'ss') return o.substr(0,2);
  462. return "." + o.substr(2,fmt.length-1);
  463. case 90: /* 'Z' absolute time */
  464. switch(fmt) {
  465. case '[h]': case '[hh]': out = val.D*24+val.H; break;
  466. case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
  467. case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
  468. default: throw 'bad abstime format: ' + fmt;
  469. } outl = fmt.length === 3 ? 1 : 2; break;
  470. case 101: /* 'e' era */
  471. out = y; outl = 1; break;
  472. }
  473. var outstr = outl > 0 ? pad0(out, outl) : "";
  474. return outstr;
  475. }
  476. /*jshint +W086 */
  477. function commaify(s) {
  478. var w = 3;
  479. if(s.length <= w) return s;
  480. var j = (s.length % w), o = s.substr(0,j);
  481. for(; j!=s.length; j+=w) o+=(o.length > 0 ? "," : "") + s.substr(j,w);
  482. return o;
  483. }
  484. var write_num = (function make_write_num(){
  485. var pct1 = /%/g;
  486. function write_num_pct(type, fmt, val){
  487. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  488. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  489. }
  490. function write_num_cm(type, fmt, val){
  491. var idx = fmt.length - 1;
  492. while(fmt.charCodeAt(idx-1) === 44) --idx;
  493. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  494. }
  495. function write_num_exp(fmt, val){
  496. var o;
  497. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  498. if(fmt.match(/^#+0.0E\+0$/)) {
  499. if(val == 0) return "0.0E+0";
  500. else if(val < 0) return "-" + write_num_exp(fmt, -val);
  501. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  502. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  503. if(ee < 0) ee += period;
  504. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  505. if(o.indexOf("e") === -1) {
  506. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  507. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  508. else o += "E+" + (fakee - ee);
  509. while(o.substr(0,2) === "0.") {
  510. o = o.charAt(0) + o.substr(2,period) + "." + o.substr(2+period);
  511. o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
  512. }
  513. o = o.replace(/\+-/,"-");
  514. }
  515. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  516. } else o = val.toExponential(idx);
  517. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  518. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  519. return o.replace("e","E");
  520. }
  521. var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
  522. function write_num_f1(r, aval, sign) {
  523. var den = parseInt(r[4],10), rr = Math.round(aval * den), base = Math.floor(rr/den);
  524. var myn = (rr - base*den), myd = den;
  525. return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length));
  526. }
  527. function write_num_f2(r, aval, sign) {
  528. return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
  529. }
  530. var dec1 = /^#*0*\.([0#]+)/;
  531. var closeparen = /\).*[0#]/;
  532. var phone = /\(###\) ###\\?-####/;
  533. function hashq(str) {
  534. var o = "", cc;
  535. for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
  536. case 35: break;
  537. case 63: o+= " "; break;
  538. case 48: o+= "0"; break;
  539. default: o+= String.fromCharCode(cc);
  540. }
  541. return o;
  542. }
  543. function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
  544. function dec(val, d) {
  545. var _frac = val - Math.floor(val), dd = Math.pow(10,d);
  546. if (d < ('' + Math.round(_frac * dd)).length) return 0;
  547. return Math.round(_frac * dd);
  548. }
  549. function carry(val, d) {
  550. if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
  551. return 1;
  552. }
  553. return 0;
  554. }
  555. function flr(val) {
  556. if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0));
  557. return ""+Math.floor(val);
  558. }
  559. function write_num_flt(type, fmt, val) {
  560. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  561. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  562. if(val >= 0) return write_num_flt('n', ffmt, val);
  563. return '(' + write_num_flt('n', ffmt, -val) + ')';
  564. }
  565. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
  566. if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
  567. if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
  568. if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  569. var o;
  570. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  571. if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
  572. if(fmt.match(/^[#?]+$/)) {
  573. o = pad0r(val,0); if(o === "0") o = "";
  574. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  575. }
  576. if((r = fmt.match(frac1))) return write_num_f1(r, aval, sign);
  577. if(fmt.match(/^#+0+$/)) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
  578. if((r = fmt.match(dec1))) {
  579. o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1])).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
  580. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  581. }
  582. fmt = fmt.replace(/^#+([0.])/, "$1");
  583. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  584. return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  585. }
  586. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
  587. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  588. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val) + carry(val, r[1].length))) + "." + pad0(dec(val, r[1].length),r[1].length);
  589. }
  590. if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
  591. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  592. o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
  593. ri = 0;
  594. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  595. }
  596. if(fmt.match(phone)) {
  597. o = write_num_flt(type, "##########", val);
  598. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  599. }
  600. var oa = "";
  601. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  602. ri = Math.min(r[4].length,7);
  603. ff = frac(aval, Math.pow(10,ri)-1, false);
  604. o = "" + sign;
  605. oa = write_num("n", r[1], ff[1]);
  606. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  607. o += oa + r[2] + "/" + r[3];
  608. oa = rpad_(ff[2],ri);
  609. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  610. o += oa;
  611. return o;
  612. }
  613. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  614. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  615. ff = frac(aval, Math.pow(10,ri)-1, true);
  616. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  617. }
  618. if((r = fmt.match(/^[#0?]+$/))) {
  619. o = pad0r(val, 0);
  620. if(fmt.length <= o.length) return o;
  621. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  622. }
  623. if((r = fmt.match(/^([#0?]+)\.([#0]+)$/))) {
  624. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  625. ri = o.indexOf(".");
  626. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  627. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  628. }
  629. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  630. ri = dec(val, r[1].length);
  631. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length);
  632. }
  633. switch(fmt) {
  634. case "###,##0.00": return write_num_flt(type, "#,##0.00", val);
  635. case "###,###":
  636. case "##,###":
  637. case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
  638. case "###,###.00": return write_num_flt(type, "###,##0.00",val).replace(/^0\./,".");
  639. case "#,###.00": return write_num_flt(type, "#,##0.00",val).replace(/^0\./,".");
  640. default:
  641. }
  642. throw new Error("unsupported format |" + fmt + "|");
  643. }
  644. function write_num_cm2(type, fmt, val){
  645. var idx = fmt.length - 1;
  646. while(fmt.charCodeAt(idx-1) === 44) --idx;
  647. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  648. }
  649. function write_num_pct2(type, fmt, val){
  650. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  651. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  652. }
  653. function write_num_exp2(fmt, val){
  654. var o;
  655. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  656. if(fmt.match(/^#+0.0E\+0$/)) {
  657. if(val == 0) return "0.0E+0";
  658. else if(val < 0) return "-" + write_num_exp2(fmt, -val);
  659. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  660. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  661. if(ee < 0) ee += period;
  662. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  663. if(!o.match(/[Ee]/)) {
  664. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  665. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  666. else o += "E+" + (fakee - ee);
  667. o = o.replace(/\+-/,"-");
  668. }
  669. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  670. } else o = val.toExponential(idx);
  671. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  672. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  673. return o.replace("e","E");
  674. }
  675. function write_num_int(type, fmt, val) {
  676. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  677. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  678. if(val >= 0) return write_num_int('n', ffmt, val);
  679. return '(' + write_num_int('n', ffmt, -val) + ')';
  680. }
  681. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
  682. if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
  683. if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
  684. if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  685. var o;
  686. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  687. if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
  688. if(fmt.match(/^[#?]+$/)) {
  689. o = (""+val); if(val === 0) o = "";
  690. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  691. }
  692. if((r = fmt.match(frac1))) return write_num_f2(r, aval, sign);
  693. if(fmt.match(/^#+0+$/)) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
  694. if((r = fmt.match(dec1))) {
  695. o = (""+val).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1]));
  696. o = o.replace(/\.(\d*)$/,function($$, $1) {
  697. return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
  698. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  699. }
  700. fmt = fmt.replace(/^#+([0.])/, "$1");
  701. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  702. return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  703. }
  704. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify((""+aval));
  705. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  706. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
  707. }
  708. if((r = fmt.match(/^#,#*,#0/))) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
  709. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  710. o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
  711. ri = 0;
  712. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  713. }
  714. if(fmt.match(phone)) {
  715. o = write_num_int(type, "##########", val);
  716. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  717. }
  718. var oa = "";
  719. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  720. ri = Math.min(r[4].length,7);
  721. ff = frac(aval, Math.pow(10,ri)-1, false);
  722. o = "" + sign;
  723. oa = write_num("n", r[1], ff[1]);
  724. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  725. o += oa + r[2] + "/" + r[3];
  726. oa = rpad_(ff[2],ri);
  727. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  728. o += oa;
  729. return o;
  730. }
  731. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  732. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  733. ff = frac(aval, Math.pow(10,ri)-1, true);
  734. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  735. }
  736. if((r = fmt.match(/^[#0?]+$/))) {
  737. o = "" + val;
  738. if(fmt.length <= o.length) return o;
  739. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  740. }
  741. if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
  742. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  743. ri = o.indexOf(".");
  744. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  745. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  746. }
  747. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  748. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length);
  749. }
  750. switch(fmt) {
  751. case "###,###":
  752. case "##,###":
  753. case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
  754. default:
  755. if(fmt.match(/\.[0#?]*$/)) return write_num_int(type, fmt.slice(0,fmt.lastIndexOf(".")), val) + hashq(fmt.slice(fmt.lastIndexOf(".")));
  756. }
  757. throw new Error("unsupported format |" + fmt + "|");
  758. }
  759. return function write_num(type, fmt, val) {
  760. return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
  761. };})();
  762. function split_fmt(fmt) {
  763. var out = [];
  764. var in_str = false/*, cc*/;
  765. for(var i = 0, j = 0; i < fmt.length; ++i) switch((/*cc=*/fmt.charCodeAt(i))) {
  766. case 34: /* '"' */
  767. in_str = !in_str; break;
  768. case 95: case 42: case 92: /* '_' '*' '\\' */
  769. ++i; break;
  770. case 59: /* ';' */
  771. out[out.length] = fmt.substr(j,i-j);
  772. j = i+1;
  773. }
  774. out[out.length] = fmt.substr(j);
  775. if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
  776. return out;
  777. }
  778. SSF._split = split_fmt;
  779. var abstime = /\[[HhMmSs\u0E0A\u0E19\u0E17]*\]/;
  780. function fmt_is_date(fmt) {
  781. var i = 0, /*cc = 0,*/ c = "", o = "";
  782. while(i < fmt.length) {
  783. switch((c = fmt.charAt(i))) {
  784. case 'G': if(isgeneral(fmt, i)) i+= 6; i++; break;
  785. case '"': for(;(/*cc=*/fmt.charCodeAt(++i)) !== 34 && i < fmt.length;){/*empty*/} ++i; break;
  786. case '\\': i+=2; break;
  787. case '_': i+=2; break;
  788. case '@': ++i; break;
  789. case 'B': case 'b':
  790. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") return true;
  791. /* falls through */
  792. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  793. /* falls through */
  794. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g': return true;
  795. case 'A': case 'a': case '上':
  796. if(fmt.substr(i, 3).toUpperCase() === "A/P") return true;
  797. if(fmt.substr(i, 5).toUpperCase() === "AM/PM") return true;
  798. if(fmt.substr(i, 5).toUpperCase() === "上午/下午") return true;
  799. ++i; break;
  800. case '[':
  801. o = c;
  802. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  803. if(o.match(abstime)) return true;
  804. break;
  805. case '.':
  806. /* falls through */
  807. case '0': case '#':
  808. while(i < fmt.length && ("0#?.,E+-%".indexOf(c=fmt.charAt(++i)) > -1 || (c=='\\' && fmt.charAt(i+1) == "-" && "0#".indexOf(fmt.charAt(i+2))>-1))){/* empty */}
  809. break;
  810. case '?': while(fmt.charAt(++i) === c){/* empty */} break;
  811. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break;
  812. case '(': case ')': ++i; break;
  813. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  814. while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1){/* empty */} break;
  815. case ' ': ++i; break;
  816. default: ++i; break;
  817. }
  818. }
  819. return false;
  820. }
  821. SSF.is_date = fmt_is_date;
  822. function eval_fmt(fmt, v, opts, flen) {
  823. var out = [], o = "", i = 0, c = "", lst='t', dt, j, cc;
  824. var hr='H';
  825. /* Tokenize */
  826. while(i < fmt.length) {
  827. switch((c = fmt.charAt(i))) {
  828. case 'G': /* General */
  829. if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
  830. out[out.length] = {t:'G', v:'General'}; i+=7; break;
  831. case '"': /* Literal text */
  832. for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
  833. out[out.length] = {t:'t', v:o}; ++i; break;
  834. case '\\': var w = fmt.charAt(++i), t = (w === "(" || w === ")") ? w : 't';
  835. out[out.length] = {t:t, v:w}; ++i; break;
  836. case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
  837. case '@': /* Text Placeholder */
  838. out[out.length] = {t:'T', v:v}; ++i; break;
  839. case 'B': case 'b':
  840. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") {
  841. if(dt==null) { dt=parse_date_code(v, opts, fmt.charAt(i+1) === "2"); if(dt==null) return ""; }
  842. out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
  843. }
  844. /* falls through */
  845. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  846. c = c.toLowerCase();
  847. /* falls through */
  848. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
  849. if(v < 0) return "";
  850. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  851. o = c; while(++i < fmt.length && fmt.charAt(i).toLowerCase() === c) o+=c;
  852. if(c === 'm' && lst.toLowerCase() === 'h') c = 'M';
  853. if(c === 'h') c = hr;
  854. out[out.length] = {t:c, v:o}; lst = c; break;
  855. case 'A': case 'a': case '上':
  856. var q={t:c, v:c};
  857. if(dt==null) dt=parse_date_code(v, opts);
  858. if(fmt.substr(i, 3).toUpperCase() === "A/P") { if(dt!=null) q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
  859. else if(fmt.substr(i,5).toUpperCase() === "AM/PM") { if(dt!=null) q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
  860. else if(fmt.substr(i,5).toUpperCase() === "上午/下午") { if(dt!=null) q.v = dt.H >= 12 ? "下午" : "上午"; q.t = 'T'; i+=5; hr='h'; }
  861. else { q.t = "t"; ++i; }
  862. if(dt==null && q.t === 'T') return "";
  863. out[out.length] = q; lst = c; break;
  864. case '[':
  865. o = c;
  866. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  867. if(o.slice(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
  868. if(o.match(abstime)) {
  869. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  870. out[out.length] = {t:'Z', v:o.toLowerCase()};
  871. lst = o.charAt(1);
  872. } else if(o.indexOf("$") > -1) {
  873. o = (o.match(/\$([^-\[\]]*)/)||[])[1]||"$";
  874. if(!fmt_is_date(fmt)) out[out.length] = {t:'t',v:o};
  875. }
  876. break;
  877. /* Numbers */
  878. case '.':
  879. if(dt != null) {
  880. o = c; while(++i < fmt.length && (c=fmt.charAt(i)) === "0") o += c;
  881. out[out.length] = {t:'s', v:o}; break;
  882. }
  883. /* falls through */
  884. case '0': case '#':
  885. o = c; while(++i < fmt.length && "0#?.,E+-%".indexOf(c=fmt.charAt(i)) > -1) o += c;
  886. out[out.length] = {t:'n', v:o}; break;
  887. case '?':
  888. o = c; while(fmt.charAt(++i) === c) o+=c;
  889. out[out.length] = {t:c, v:o}; lst = c; break;
  890. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break; // **
  891. case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
  892. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  893. o = c; while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1) o+=fmt.charAt(i);
  894. out[out.length] = {t:'D', v:o}; break;
  895. case ' ': out[out.length] = {t:c, v:c}; ++i; break;
  896. case '$': out[out.length] = {t:'t', v:'$'}; ++i; break;
  897. default:
  898. if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
  899. out[out.length] = {t:'t', v:c}; ++i; break;
  900. }
  901. }
  902. /* Scan for date/time parts */
  903. var bt = 0, ss0 = 0, ssm;
  904. for(i=out.length-1, lst='t'; i >= 0; --i) {
  905. switch(out[i].t) {
  906. case 'h': case 'H': out[i].t = hr; lst='h'; if(bt < 1) bt = 1; break;
  907. case 's':
  908. if((ssm=out[i].v.match(/\.0+$/))) ss0=Math.max(ss0,ssm[0].length-1);
  909. if(bt < 3) bt = 3;
  910. /* falls through */
  911. case 'd': case 'y': case 'M': case 'e': lst=out[i].t; break;
  912. case 'm': if(lst === 's') { out[i].t = 'M'; if(bt < 2) bt = 2; } break;
  913. case 'X': /*if(out[i].v === "B2");*/
  914. break;
  915. case 'Z':
  916. if(bt < 1 && out[i].v.match(/[Hh]/)) bt = 1;
  917. if(bt < 2 && out[i].v.match(/[Mm]/)) bt = 2;
  918. if(bt < 3 && out[i].v.match(/[Ss]/)) bt = 3;
  919. }
  920. }
  921. /* time rounding depends on presence of minute / second / usec fields */
  922. switch(bt) {
  923. case 0: break;
  924. case 1:
  925. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  926. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  927. if(dt.M >= 60) { dt.M = 0; ++dt.H; }
  928. break;
  929. case 2:
  930. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  931. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  932. break;
  933. }
  934. /* replace fields */
  935. var nstr = "", jj;
  936. for(i=0; i < out.length; ++i) {
  937. switch(out[i].t) {
  938. case 't': case 'T': case ' ': case 'D': break;
  939. case 'X': out[i].v = ""; out[i].t = ";"; break;
  940. case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
  941. out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
  942. out[i].t = 't'; break;
  943. case 'n': case '?':
  944. jj = i+1;
  945. while(out[jj] != null && (
  946. (c=out[jj].t) === "?" || c === "D" ||
  947. ((c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/')) ||
  948. (out[i].t === '(' && (c === ' ' || c === 'n' || c === ')')) ||
  949. (c === 't' && (out[jj].v === '/' || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?'))
  950. )) {
  951. out[i].v += out[jj].v;
  952. out[jj] = {v:"", t:";"}; ++jj;
  953. }
  954. nstr += out[i].v;
  955. i = jj-1; break;
  956. case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
  957. }
  958. }
  959. var vv = "", myv, ostr;
  960. if(nstr.length > 0) {
  961. if(nstr.charCodeAt(0) == 40) /* '(' */ {
  962. myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v);
  963. ostr = write_num('n', nstr, myv);
  964. } else {
  965. myv = (v<0 && flen > 1 ? -v : v);
  966. ostr = write_num('n', nstr, myv);
  967. if(myv < 0 && out[0] && out[0].t == 't') {
  968. ostr = ostr.substr(1);
  969. out[0].v = "-" + out[0].v;
  970. }
  971. }
  972. jj=ostr.length-1;
  973. var decpt = out.length;
  974. for(i=0; i < out.length; ++i) if(out[i] != null && out[i].t != 't' && out[i].v.indexOf(".") > -1) { decpt = i; break; }
  975. var lasti=out.length;
  976. if(decpt === out.length && ostr.indexOf("E") === -1) {
  977. for(i=out.length-1; i>= 0;--i) {
  978. if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
  979. if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
  980. else if(jj < 0) out[i].v = "";
  981. else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
  982. out[i].t = 't';
  983. lasti = i;
  984. }
  985. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  986. }
  987. else if(decpt !== out.length && ostr.indexOf("E") === -1) {
  988. jj = ostr.indexOf(".")-1;
  989. for(i=decpt; i>= 0; --i) {
  990. if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
  991. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
  992. vv = out[i].v.substr(j+1);
  993. for(; j>=0; --j) {
  994. if(jj>=0 && (out[i].v.charAt(j) === "0" || out[i].v.charAt(j) === "#")) vv = ostr.charAt(jj--) + vv;
  995. }
  996. out[i].v = vv;
  997. out[i].t = 't';
  998. lasti = i;
  999. }
  1000. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  1001. jj = ostr.indexOf(".")+1;
  1002. for(i=decpt; i<out.length; ++i) {
  1003. if(out[i] == null || ('n?('.indexOf(out[i].t) === -1 && i !== decpt)) continue;
  1004. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
  1005. vv = out[i].v.substr(0,j);
  1006. for(; j<out[i].v.length; ++j) {
  1007. if(jj<ostr.length) vv += ostr.charAt(jj++);
  1008. }
  1009. out[i].v = vv;
  1010. out[i].t = 't';
  1011. lasti = i;
  1012. }
  1013. }
  1014. }
  1015. for(i=0; i<out.length; ++i) if(out[i] != null && 'n?'.indexOf(out[i].t)>-1) {
  1016. myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
  1017. out[i].v = write_num(out[i].t, out[i].v, myv);
  1018. out[i].t = 't';
  1019. }
  1020. var retval = "";
  1021. for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
  1022. return retval;
  1023. }
  1024. SSF._eval = eval_fmt;
  1025. var cfregex = /\[[=<>]/;
  1026. var cfregex2 = /\[(=|>[=]?|<[>=]?)(-?\d+(?:\.\d*)?)\]/;
  1027. function chkcond(v, rr) {
  1028. if(rr == null) return false;
  1029. var thresh = parseFloat(rr[2]);
  1030. switch(rr[1]) {
  1031. case "=": if(v == thresh) return true; break;
  1032. case ">": if(v > thresh) return true; break;
  1033. case "<": if(v < thresh) return true; break;
  1034. case "<>": if(v != thresh) return true; break;
  1035. case ">=": if(v >= thresh) return true; break;
  1036. case "<=": if(v <= thresh) return true; break;
  1037. }
  1038. return false;
  1039. }
  1040. function choose_fmt(f, v) {
  1041. var fmt = split_fmt(f);
  1042. var l = fmt.length, lat = fmt[l-1].indexOf("@");
  1043. if(l<4 && lat>-1) --l;
  1044. if(fmt.length > 4) throw new Error("cannot find right format for |" + fmt.join("|") + "|");
  1045. if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
  1046. switch(fmt.length) {
  1047. case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
  1048. case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
  1049. case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
  1050. case 4: break;
  1051. }
  1052. var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
  1053. if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
  1054. if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
  1055. var m1 = fmt[0].match(cfregex2);
  1056. var m2 = fmt[1].match(cfregex2);
  1057. return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
  1058. }
  1059. return [l, ff];
  1060. }
  1061. function format(fmt,v,o) {
  1062. if(o == null) o = {};
  1063. var sfmt = "";
  1064. switch(typeof fmt) {
  1065. case "string":
  1066. if(fmt == "m/d/yy" && o.dateNF) sfmt = o.dateNF;
  1067. else sfmt = fmt;
  1068. break;
  1069. case "number":
  1070. if(fmt == 14 && o.dateNF) sfmt = o.dateNF;
  1071. else sfmt = (o.table != null ? (o.table) : table_fmt)[fmt];
  1072. if(sfmt == null) sfmt = (o.table && o.table[default_map[fmt]]) || table_fmt[default_map[fmt]];
  1073. if(sfmt == null) sfmt = default_str[fmt] || "General";
  1074. break;
  1075. }
  1076. if(isgeneral(sfmt,0)) return general_fmt(v, o);
  1077. if(v instanceof Date) v = datenum_local(v, o.date1904);
  1078. var f = choose_fmt(sfmt, v);
  1079. if(isgeneral(f[1])) return general_fmt(v, o);
  1080. if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
  1081. else if(v === "" || v == null) return "";
  1082. return eval_fmt(f[1], v, o, f[0]);
  1083. }
  1084. function load_entry(fmt, idx) {
  1085. if(typeof idx != 'number') {
  1086. idx = +idx || -1;
  1087. for(var i = 0; i < 0x0188; ++i) {
  1088. if(table_fmt[i] == undefined) { if(idx < 0) idx = i; continue; }
  1089. if(table_fmt[i] == fmt) { idx = i; break; }
  1090. }
  1091. if(idx < 0) idx = 0x187;
  1092. }
  1093. table_fmt[idx] = fmt;
  1094. return idx;
  1095. }
  1096. SSF.load = load_entry;
  1097. SSF._table = table_fmt;
  1098. SSF.get_table = function get_table() { return table_fmt; };
  1099. SSF.load_table = function load_table(tbl) {
  1100. for(var i=0; i!=0x0188; ++i)
  1101. if(tbl[i] !== undefined) load_entry(tbl[i], i);
  1102. };
  1103. SSF.init_table = init_table;
  1104. SSF.format = format;
  1105. };
  1106. make_ssf(SSF);
  1107. /* map from xlml named formats to SSF TODO: localize */
  1108. var XLMLFormatMap/*{[string]:string}*/ = ({
  1109. "General Number": "General",
  1110. "General Date": SSF._table[22],
  1111. "Long Date": "dddd, mmmm dd, yyyy",
  1112. "Medium Date": SSF._table[15],
  1113. "Short Date": SSF._table[14],
  1114. "Long Time": SSF._table[19],
  1115. "Medium Time": SSF._table[18],
  1116. "Short Time": SSF._table[20],
  1117. "Currency": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  1118. "Fixed": SSF._table[2],
  1119. "Standard": SSF._table[4],
  1120. "Percent": SSF._table[10],
  1121. "Scientific": SSF._table[11],
  1122. "Yes/No": '"Yes";"Yes";"No";@',
  1123. "True/False": '"True";"True";"False";@',
  1124. "On/Off": '"Yes";"Yes";"No";@'
  1125. });
  1126. var SSFImplicit/*{[number]:string}*/ = ({
  1127. "5": '"$"#,##0_);\\("$"#,##0\\)',
  1128. "6": '"$"#,##0_);[Red]\\("$"#,##0\\)',
  1129. "7": '"$"#,##0.00_);\\("$"#,##0.00\\)',
  1130. "8": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  1131. "23": 'General', "24": 'General', "25": 'General', "26": 'General',
  1132. "27": 'm/d/yy', "28": 'm/d/yy', "29": 'm/d/yy', "30": 'm/d/yy', "31": 'm/d/yy',
  1133. "32": 'h:mm:ss', "33": 'h:mm:ss', "34": 'h:mm:ss', "35": 'h:mm:ss',
  1134. "36": 'm/d/yy',
  1135. "41": '_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)',
  1136. "42": '_("$"* #,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)',
  1137. "43": '_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)',
  1138. "44": '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
  1139. "50": 'm/d/yy', "51": 'm/d/yy', "52": 'm/d/yy', "53": 'm/d/yy', "54": 'm/d/yy',
  1140. "55": 'm/d/yy', "56": 'm/d/yy', "57": 'm/d/yy', "58": 'm/d/yy',
  1141. "59": '0',
  1142. "60": '0.00',
  1143. "61": '#,##0',
  1144. "62": '#,##0.00',
  1145. "63": '"$"#,##0_);\\("$"#,##0\\)',
  1146. "64": '"$"#,##0_);[Red]\\("$"#,##0\\)',
  1147. "65": '"$"#,##0.00_);\\("$"#,##0.00\\)',
  1148. "66": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
  1149. "67": '0%',
  1150. "68": '0.00%',
  1151. "69": '# ?/?',
  1152. "70": '# ??/??',
  1153. "71": 'm/d/yy',
  1154. "72": 'm/d/yy',
  1155. "73": 'd-mmm-yy',
  1156. "74": 'd-mmm',
  1157. "75": 'mmm-yy',
  1158. "76": 'h:mm',
  1159. "77": 'h:mm:ss',
  1160. "78": 'm/d/yy h:mm',
  1161. "79": 'mm:ss',
  1162. "80": '[h]:mm:ss',
  1163. "81": 'mmss.0'
  1164. });
  1165. /* dateNF parse TODO: move to SSF */
  1166. var dateNFregex = /[dD]+|[mM]+|[yYeE]+|[Hh]+|[Ss]+/g;
  1167. function dateNF_regex(dateNF) {
  1168. var fmt = typeof dateNF == "number" ? SSF._table[dateNF] : dateNF;
  1169. fmt = fmt.replace(dateNFregex, "(\\d+)");
  1170. return new RegExp("^" + fmt + "$");
  1171. }
  1172. function dateNF_fix(str, dateNF, match) {
  1173. var Y = -1, m = -1, d = -1, H = -1, M = -1, S = -1;
  1174. (dateNF.match(dateNFregex)||[]).forEach(function(n, i) {
  1175. var v = parseInt(match[i+1], 10);
  1176. switch(n.toLowerCase().charAt(0)) {
  1177. case 'y': Y = v; break; case 'd': d = v; break;
  1178. case 'h': H = v; break; case 's': S = v; break;
  1179. case 'm': if(H >= 0) M = v; else m = v; break;
  1180. }
  1181. });
  1182. if(S >= 0 && M == -1 && m >= 0) { M = m; m = -1; }
  1183. var datestr = (("" + (Y>=0?Y: new Date().getFullYear())).slice(-4) + "-" + ("00" + (m>=1?m:1)).slice(-2) + "-" + ("00" + (d>=1?d:1)).slice(-2));
  1184. if(datestr.length == 7) datestr = "0" + datestr;
  1185. if(datestr.length == 8) datestr = "20" + datestr;
  1186. var timestr = (("00" + (H>=0?H:0)).slice(-2) + ":" + ("00" + (M>=0?M:0)).slice(-2) + ":" + ("00" + (S>=0?S:0)).slice(-2));
  1187. if(H == -1 && M == -1 && S == -1) return datestr;
  1188. if(Y == -1 && m == -1 && d == -1) return timestr;
  1189. return datestr + "T" + timestr;
  1190. }
  1191. var DO_NOT_EXPORT_CFB = true;
  1192. /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
  1193. /* vim: set ts=2: */
  1194. /*jshint eqnull:true */
  1195. /*exported CFB */
  1196. /*global Uint8Array:false, Uint16Array:false */
  1197. /* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  1198. /* vim: set ts=2: */
  1199. /*exported CRC32 */
  1200. var CRC32;
  1201. (function (factory) {
  1202. /*jshint ignore:start */
  1203. /*eslint-disable */
  1204. factory(CRC32 = {});
  1205. /*eslint-enable */
  1206. /*jshint ignore:end */
  1207. }(function(CRC32) {
  1208. CRC32.version = '1.2.0';
  1209. /* see perf/crc32table.js */
  1210. /*global Int32Array */
  1211. function signed_crc_table() {
  1212. var c = 0, table = new Array(256);
  1213. for(var n =0; n != 256; ++n){
  1214. c = n;
  1215. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1216. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1217. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1218. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1219. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1220. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1221. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1222. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  1223. table[n] = c;
  1224. }
  1225. return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
  1226. }
  1227. var T = signed_crc_table();
  1228. function crc32_bstr(bstr, seed) {
  1229. var C = seed ^ -1, L = bstr.length - 1;
  1230. for(var i = 0; i < L;) {
  1231. C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
  1232. C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
  1233. }
  1234. if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
  1235. return C ^ -1;
  1236. }
  1237. function crc32_buf(buf, seed) {
  1238. if(buf.length > 10000) return crc32_buf_8(buf, seed);
  1239. var C = seed ^ -1, L = buf.length - 3;
  1240. for(var i = 0; i < L;) {
  1241. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1242. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1243. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1244. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1245. }
  1246. while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1247. return C ^ -1;
  1248. }
  1249. function crc32_buf_8(buf, seed) {
  1250. var C = seed ^ -1, L = buf.length - 7;
  1251. for(var i = 0; i < L;) {
  1252. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1253. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1254. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1255. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1256. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1257. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1258. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1259. C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1260. }
  1261. while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
  1262. return C ^ -1;
  1263. }
  1264. function crc32_str(str, seed) {
  1265. var C = seed ^ -1;
  1266. for(var i = 0, L=str.length, c, d; i < L;) {
  1267. c = str.charCodeAt(i++);
  1268. if(c < 0x80) {
  1269. C = (C>>>8) ^ T[(C ^ c)&0xFF];
  1270. } else if(c < 0x800) {
  1271. C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
  1272. C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
  1273. } else if(c >= 0xD800 && c < 0xE000) {
  1274. c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
  1275. C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
  1276. C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
  1277. C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
  1278. C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
  1279. } else {
  1280. C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
  1281. C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
  1282. C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
  1283. }
  1284. }
  1285. return C ^ -1;
  1286. }
  1287. CRC32.table = T;
  1288. CRC32.bstr = crc32_bstr;
  1289. CRC32.buf = crc32_buf;
  1290. CRC32.str = crc32_str;
  1291. }));
  1292. /* [MS-CFB] v20171201 */
  1293. var CFB = (function _CFB(){
  1294. var exports = {};
  1295. exports.version = '1.1.4';
  1296. /* [MS-CFB] 2.6.4 */
  1297. function namecmp(l, r) {
  1298. var L = l.split("/"), R = r.split("/");
  1299. for(var i = 0, c = 0, Z = Math.min(L.length, R.length); i < Z; ++i) {
  1300. if((c = L[i].length - R[i].length)) return c;
  1301. if(L[i] != R[i]) return L[i] < R[i] ? -1 : 1;
  1302. }
  1303. return L.length - R.length;
  1304. }
  1305. function dirname(p) {
  1306. if(p.charAt(p.length - 1) == "/") return (p.slice(0,-1).indexOf("/") === -1) ? p : dirname(p.slice(0, -1));
  1307. var c = p.lastIndexOf("/");
  1308. return (c === -1) ? p : p.slice(0, c+1);
  1309. }
  1310. function filename(p) {
  1311. if(p.charAt(p.length - 1) == "/") return filename(p.slice(0, -1));
  1312. var c = p.lastIndexOf("/");
  1313. return (c === -1) ? p : p.slice(c+1);
  1314. }
  1315. /* -------------------------------------------------------------------------- */
  1316. /* DOS Date format:
  1317. high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
  1318. add 1980 to stored year
  1319. stored second should be doubled
  1320. */
  1321. /* write JS date to buf as a DOS date */
  1322. function write_dos_date(buf, date) {
  1323. if(typeof date === "string") date = new Date(date);
  1324. var hms = date.getHours();
  1325. hms = hms << 6 | date.getMinutes();
  1326. hms = hms << 5 | (date.getSeconds()>>>1);
  1327. buf.write_shift(2, hms);
  1328. var ymd = (date.getFullYear() - 1980);
  1329. ymd = ymd << 4 | (date.getMonth()+1);
  1330. ymd = ymd << 5 | date.getDate();
  1331. buf.write_shift(2, ymd);
  1332. }
  1333. /* read four bytes from buf and interpret as a DOS date */
  1334. function parse_dos_date(buf) {
  1335. var hms = buf.read_shift(2) & 0xFFFF;
  1336. var ymd = buf.read_shift(2) & 0xFFFF;
  1337. var val = new Date();
  1338. var d = ymd & 0x1F; ymd >>>= 5;
  1339. var m = ymd & 0x0F; ymd >>>= 4;
  1340. val.setMilliseconds(0);
  1341. val.setFullYear(ymd + 1980);
  1342. val.setMonth(m-1);
  1343. val.setDate(d);
  1344. var S = hms & 0x1F; hms >>>= 5;
  1345. var M = hms & 0x3F; hms >>>= 6;
  1346. val.setHours(hms);
  1347. val.setMinutes(M);
  1348. val.setSeconds(S<<1);
  1349. return val;
  1350. }
  1351. function parse_extra_field(blob) {
  1352. prep_blob(blob, 0);
  1353. var o = {};
  1354. var flags = 0;
  1355. while(blob.l <= blob.length - 4) {
  1356. var type = blob.read_shift(2);
  1357. var sz = blob.read_shift(2), tgt = blob.l + sz;
  1358. var p = {};
  1359. switch(type) {
  1360. /* UNIX-style Timestamps */
  1361. case 0x5455: {
  1362. flags = blob.read_shift(1);
  1363. if(flags & 1) p.mtime = blob.read_shift(4);
  1364. /* for some reason, CD flag corresponds to LFH */
  1365. if(sz > 5) {
  1366. if(flags & 2) p.atime = blob.read_shift(4);
  1367. if(flags & 4) p.ctime = blob.read_shift(4);
  1368. }
  1369. if(p.mtime) p.mt = new Date(p.mtime*1000);
  1370. }
  1371. break;
  1372. }
  1373. blob.l = tgt;
  1374. o[type] = p;
  1375. }
  1376. return o;
  1377. }
  1378. var fs;
  1379. function get_fs() { return fs || (fs = require('fs')); }
  1380. function parse(file, options) {
  1381. if(file[0] == 0x50 && file[1] == 0x4b) return parse_zip(file, options);
  1382. if(file.length < 512) throw new Error("CFB file size " + file.length + " < 512");
  1383. var mver = 3;
  1384. var ssz = 512;
  1385. var nmfs = 0; // number of mini FAT sectors
  1386. var difat_sec_cnt = 0;
  1387. var dir_start = 0;
  1388. var minifat_start = 0;
  1389. var difat_start = 0;
  1390. var fat_addrs = []; // locations of FAT sectors
  1391. /* [MS-CFB] 2.2 Compound File Header */
  1392. var blob = file.slice(0,512);
  1393. prep_blob(blob, 0);
  1394. /* major version */
  1395. var mv = check_get_mver(blob);
  1396. mver = mv[0];
  1397. switch(mver) {
  1398. case 3: ssz = 512; break; case 4: ssz = 4096; break;
  1399. case 0: if(mv[1] == 0) return parse_zip(file, options);
  1400. /* falls through */
  1401. default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
  1402. }
  1403. /* reprocess header */
  1404. if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
  1405. /* Save header for final object */
  1406. var header = file.slice(0,ssz);
  1407. check_shifts(blob, mver);
  1408. // Number of Directory Sectors
  1409. var dir_cnt = blob.read_shift(4, 'i');
  1410. if(mver === 3 && dir_cnt !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + dir_cnt);
  1411. // Number of FAT Sectors
  1412. blob.l += 4;
  1413. // First Directory Sector Location
  1414. dir_start = blob.read_shift(4, 'i');
  1415. // Transaction Signature
  1416. blob.l += 4;
  1417. // Mini Stream Cutoff Size
  1418. blob.chk('00100000', 'Mini Stream Cutoff Size: ');
  1419. // First Mini FAT Sector Location
  1420. minifat_start = blob.read_shift(4, 'i');
  1421. // Number of Mini FAT Sectors
  1422. nmfs = blob.read_shift(4, 'i');
  1423. // First DIFAT sector location
  1424. difat_start = blob.read_shift(4, 'i');
  1425. // Number of DIFAT Sectors
  1426. difat_sec_cnt = blob.read_shift(4, 'i');
  1427. // Grab FAT Sector Locations
  1428. for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
  1429. q = blob.read_shift(4, 'i');
  1430. if(q<0) break;
  1431. fat_addrs[j] = q;
  1432. }
  1433. /** Break the file up into sectors */
  1434. var sectors = sectorify(file, ssz);
  1435. sleuth_fat(difat_start, difat_sec_cnt, sectors, ssz, fat_addrs);
  1436. /** Chains */
  1437. var sector_list = make_sector_list(sectors, dir_start, fat_addrs, ssz);
  1438. sector_list[dir_start].name = "!Directory";
  1439. if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
  1440. sector_list[fat_addrs[0]].name = "!FAT";
  1441. sector_list.fat_addrs = fat_addrs;
  1442. sector_list.ssz = ssz;
  1443. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  1444. var files = {}, Paths = [], FileIndex = [], FullPaths = [];
  1445. read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, minifat_start);
  1446. build_full_paths(FileIndex, FullPaths, Paths);
  1447. Paths.shift();
  1448. var o = {
  1449. FileIndex: FileIndex,
  1450. FullPaths: FullPaths
  1451. };
  1452. // $FlowIgnore
  1453. if(options && options.raw) o.raw = {header: header, sectors: sectors};
  1454. return o;
  1455. } // parse
  1456. /* [MS-CFB] 2.2 Compound File Header -- read up to major version */
  1457. function check_get_mver(blob) {
  1458. if(blob[blob.l] == 0x50 && blob[blob.l + 1] == 0x4b) return [0, 0];
  1459. // header signature 8
  1460. blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
  1461. // clsid 16
  1462. //blob.chk(HEADER_CLSID, 'CLSID: ');
  1463. blob.l += 16;
  1464. // minor version 2
  1465. var mver = blob.read_shift(2, 'u');
  1466. return [blob.read_shift(2,'u'), mver];
  1467. }
  1468. function check_shifts(blob, mver) {
  1469. var shift = 0x09;
  1470. // Byte Order
  1471. //blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
  1472. blob.l += 2;
  1473. // Sector Shift
  1474. switch((shift = blob.read_shift(2))) {
  1475. case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
  1476. case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
  1477. default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
  1478. }
  1479. // Mini Sector Shift
  1480. blob.chk('0600', 'Mini Sector Shift: ');
  1481. // Reserved
  1482. blob.chk('000000000000', 'Reserved: ');
  1483. }
  1484. /** Break the file up into sectors */
  1485. function sectorify(file, ssz) {
  1486. var nsectors = Math.ceil(file.length/ssz)-1;
  1487. var sectors = [];
  1488. for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
  1489. sectors[nsectors-1] = file.slice(nsectors*ssz);
  1490. return sectors;
  1491. }
  1492. /* [MS-CFB] 2.6.4 Red-Black Tree */
  1493. function build_full_paths(FI, FP, Paths) {
  1494. var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
  1495. var dad = [], q = [];
  1496. for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
  1497. for(; j < q.length; ++j) {
  1498. i = q[j];
  1499. L = FI[i].L; R = FI[i].R; C = FI[i].C;
  1500. if(dad[i] === i) {
  1501. if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
  1502. if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
  1503. }
  1504. if(C !== -1 /*NOSTREAM*/) dad[C] = i;
  1505. if(L !== -1 && i != dad[i]) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); }
  1506. if(R !== -1 && i != dad[i]) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); }
  1507. }
  1508. for(i=1; i < pl; ++i) if(dad[i] === i) {
  1509. if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
  1510. else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
  1511. }
  1512. for(i=1; i < pl; ++i) {
  1513. if(FI[i].type === 0 /* unknown */) continue;
  1514. j = i;
  1515. if(j != dad[j]) do {
  1516. j = dad[j];
  1517. FP[i] = FP[j] + "/" + FP[i];
  1518. } while (j !== 0 && -1 !== dad[j] && j != dad[j]);
  1519. dad[i] = -1;
  1520. }
  1521. FP[0] += "/";
  1522. for(i=1; i < pl; ++i) {
  1523. if(FI[i].type !== 2 /* stream */) FP[i] += "/";
  1524. }
  1525. }
  1526. function get_mfat_entry(entry, payload, mini) {
  1527. var start = entry.start, size = entry.size;
  1528. //return (payload.slice(start*MSSZ, start*MSSZ + size));
  1529. var o = [];
  1530. var idx = start;
  1531. while(mini && size > 0 && idx >= 0) {
  1532. o.push(payload.slice(idx * MSSZ, idx * MSSZ + MSSZ));
  1533. size -= MSSZ;
  1534. idx = __readInt32LE(mini, idx * 4);
  1535. }
  1536. if(o.length === 0) return (new_buf(0));
  1537. return (bconcat(o).slice(0, entry.size));
  1538. }
  1539. /** Chase down the rest of the DIFAT chain to build a comprehensive list
  1540. DIFAT chains by storing the next sector number as the last 32 bits */
  1541. function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
  1542. var q = ENDOFCHAIN;
  1543. if(idx === ENDOFCHAIN) {
  1544. if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
  1545. } else if(idx !== -1 /*FREESECT*/) {
  1546. var sector = sectors[idx], m = (ssz>>>2)-1;
  1547. if(!sector) return;
  1548. for(var i = 0; i < m; ++i) {
  1549. if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
  1550. fat_addrs.push(q);
  1551. }
  1552. sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs);
  1553. }
  1554. }
  1555. /** Follow the linked list of sectors for a given starting point */
  1556. function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
  1557. var buf = [], buf_chain = [];
  1558. if(!chkd) chkd = [];
  1559. var modulus = ssz - 1, j = 0, jj = 0;
  1560. for(j=start; j>=0;) {
  1561. chkd[j] = true;
  1562. buf[buf.length] = j;
  1563. buf_chain.push(sectors[j]);
  1564. var addr = fat_addrs[Math.floor(j*4/ssz)];
  1565. jj = ((j*4) & modulus);
  1566. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  1567. if(!sectors[addr]) break;
  1568. j = __readInt32LE(sectors[addr], jj);
  1569. }
  1570. return {nodes: buf, data:__toBuffer([buf_chain])};
  1571. }
  1572. /** Chase down the sector linked lists */
  1573. function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
  1574. var sl = sectors.length, sector_list = ([]);
  1575. var chkd = [], buf = [], buf_chain = [];
  1576. var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
  1577. for(i=0; i < sl; ++i) {
  1578. buf = ([]);
  1579. k = (i + dir_start); if(k >= sl) k-=sl;
  1580. if(chkd[k]) continue;
  1581. buf_chain = [];
  1582. var seen = [];
  1583. for(j=k; j>=0;) {
  1584. seen[j] = true;
  1585. chkd[j] = true;
  1586. buf[buf.length] = j;
  1587. buf_chain.push(sectors[j]);
  1588. var addr = fat_addrs[Math.floor(j*4/ssz)];
  1589. jj = ((j*4) & modulus);
  1590. if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
  1591. if(!sectors[addr]) break;
  1592. j = __readInt32LE(sectors[addr], jj);
  1593. if(seen[j]) break;
  1594. }
  1595. sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
  1596. }
  1597. return sector_list;
  1598. }
  1599. /* [MS-CFB] 2.6.1 Compound File Directory Entry */
  1600. function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, mini) {
  1601. var minifat_store = 0, pl = (Paths.length?2:0);
  1602. var sector = sector_list[dir_start].data;
  1603. var i = 0, namelen = 0, name;
  1604. for(; i < sector.length; i+= 128) {
  1605. var blob = sector.slice(i, i+128);
  1606. prep_blob(blob, 64);
  1607. namelen = blob.read_shift(2);
  1608. name = __utf16le(blob,0,namelen-pl);
  1609. Paths.push(name);
  1610. var o = ({
  1611. name: name,
  1612. type: blob.read_shift(1),
  1613. color: blob.read_shift(1),
  1614. L: blob.read_shift(4, 'i'),
  1615. R: blob.read_shift(4, 'i'),
  1616. C: blob.read_shift(4, 'i'),
  1617. clsid: blob.read_shift(16),
  1618. state: blob.read_shift(4, 'i'),
  1619. start: 0,
  1620. size: 0
  1621. });
  1622. var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  1623. if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
  1624. var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
  1625. if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
  1626. o.start = blob.read_shift(4, 'i');
  1627. o.size = blob.read_shift(4, 'i');
  1628. if(o.size < 0 && o.start < 0) { o.size = o.type = 0; o.start = ENDOFCHAIN; o.name = ""; }
  1629. if(o.type === 5) { /* root */
  1630. minifat_store = o.start;
  1631. if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
  1632. /*minifat_size = o.size;*/
  1633. } else if(o.size >= 4096 /* MSCSZ */) {
  1634. o.storage = 'fat';
  1635. if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
  1636. sector_list[o.start].name = o.name;
  1637. o.content = (sector_list[o.start].data.slice(0,o.size));
  1638. } else {
  1639. o.storage = 'minifat';
  1640. if(o.size < 0) o.size = 0;
  1641. else if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN && sector_list[minifat_store]) {
  1642. o.content = get_mfat_entry(o, sector_list[minifat_store].data, (sector_list[mini]||{}).data);
  1643. }
  1644. }
  1645. if(o.content) prep_blob(o.content, 0);
  1646. files[name] = o;
  1647. FileIndex.push(o);
  1648. }
  1649. }
  1650. function read_date(blob, offset) {
  1651. return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
  1652. }
  1653. function read_file(filename, options) {
  1654. get_fs();
  1655. return parse(fs.readFileSync(filename), options);
  1656. }
  1657. function read(blob, options) {
  1658. switch(options && options.type || "base64") {
  1659. case "file": return read_file(blob, options);
  1660. case "base64": return parse(s2a(Base64.decode(blob)), options);
  1661. case "binary": return parse(s2a(blob), options);
  1662. }
  1663. return parse(blob, options);
  1664. }
  1665. function init_cfb(cfb, opts) {
  1666. var o = opts || {}, root = o.root || "Root Entry";
  1667. if(!cfb.FullPaths) cfb.FullPaths = [];
  1668. if(!cfb.FileIndex) cfb.FileIndex = [];
  1669. if(cfb.FullPaths.length !== cfb.FileIndex.length) throw new Error("inconsistent CFB structure");
  1670. if(cfb.FullPaths.length === 0) {
  1671. cfb.FullPaths[0] = root + "/";
  1672. cfb.FileIndex[0] = ({ name: root, type: 5 });
  1673. }
  1674. if(o.CLSID) cfb.FileIndex[0].clsid = o.CLSID;
  1675. seed_cfb(cfb);
  1676. }
  1677. function seed_cfb(cfb) {
  1678. var nm = "\u0001Sh33tJ5";
  1679. if(CFB.find(cfb, "/" + nm)) return;
  1680. var p = new_buf(4); p[0] = 55; p[1] = p[3] = 50; p[2] = 54;
  1681. cfb.FileIndex.push(({ name: nm, type: 2, content:p, size:4, L:69, R:69, C:69 }));
  1682. cfb.FullPaths.push(cfb.FullPaths[0] + nm);
  1683. rebuild_cfb(cfb);
  1684. }
  1685. function rebuild_cfb(cfb, f) {
  1686. init_cfb(cfb);
  1687. var gc = false, s = false;
  1688. for(var i = cfb.FullPaths.length - 1; i >= 0; --i) {
  1689. var _file = cfb.FileIndex[i];
  1690. switch(_file.type) {
  1691. case 0:
  1692. if(s) gc = true;
  1693. else { cfb.FileIndex.pop(); cfb.FullPaths.pop(); }
  1694. break;
  1695. case 1: case 2: case 5:
  1696. s = true;
  1697. if(isNaN(_file.R * _file.L * _file.C)) gc = true;
  1698. if(_file.R > -1 && _file.L > -1 && _file.R == _file.L) gc = true;
  1699. break;
  1700. default: gc = true; break;
  1701. }
  1702. }
  1703. if(!gc && !f) return;
  1704. var now = new Date(1987, 1, 19), j = 0;
  1705. var data = [];
  1706. for(i = 0; i < cfb.FullPaths.length; ++i) {
  1707. if(cfb.FileIndex[i].type === 0) continue;
  1708. data.push([cfb.FullPaths[i], cfb.FileIndex[i]]);
  1709. }
  1710. for(i = 0; i < data.length; ++i) {
  1711. var dad = dirname(data[i][0]);
  1712. s = false;
  1713. for(j = 0; j < data.length; ++j) if(data[j][0] === dad) s = true;
  1714. if(!s) data.push([dad, ({
  1715. name: filename(dad).replace("/",""),
  1716. type: 1,
  1717. clsid: HEADER_CLSID,
  1718. ct: now, mt: now,
  1719. content: null
  1720. })]);
  1721. }
  1722. data.sort(function(x,y) { return namecmp(x[0], y[0]); });
  1723. cfb.FullPaths = []; cfb.FileIndex = [];
  1724. for(i = 0; i < data.length; ++i) { cfb.FullPaths[i] = data[i][0]; cfb.FileIndex[i] = data[i][1]; }
  1725. for(i = 0; i < data.length; ++i) {
  1726. var elt = cfb.FileIndex[i];
  1727. var nm = cfb.FullPaths[i];
  1728. elt.name = filename(nm).replace("/","");
  1729. elt.L = elt.R = elt.C = -(elt.color = 1);
  1730. elt.size = elt.content ? elt.content.length : 0;
  1731. elt.start = 0;
  1732. elt.clsid = (elt.clsid || HEADER_CLSID);
  1733. if(i === 0) {
  1734. elt.C = data.length > 1 ? 1 : -1;
  1735. elt.size = 0;
  1736. elt.type = 5;
  1737. } else if(nm.slice(-1) == "/") {
  1738. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==nm) break;
  1739. elt.C = j >= data.length ? -1 : j;
  1740. for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==dirname(nm)) break;
  1741. elt.R = j >= data.length ? -1 : j;
  1742. elt.type = 1;
  1743. } else {
  1744. if(dirname(cfb.FullPaths[i+1]||"") == dirname(nm)) elt.R = i + 1;
  1745. elt.type = 2;
  1746. }
  1747. }
  1748. }
  1749. function _write(cfb, options) {
  1750. var _opts = options || {};
  1751. rebuild_cfb(cfb);
  1752. if(_opts.fileType == 'zip') return write_zip(cfb, _opts);
  1753. var L = (function(cfb){
  1754. var mini_size = 0, fat_size = 0;
  1755. for(var i = 0; i < cfb.FileIndex.length; ++i) {
  1756. var file = cfb.FileIndex[i];
  1757. if(!file.content) continue;
  1758. var flen = file.content.length;
  1759. if(flen > 0){
  1760. if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6;
  1761. else fat_size += (flen + 0x01FF) >> 9;
  1762. }
  1763. }
  1764. var dir_cnt = (cfb.FullPaths.length +3) >> 2;
  1765. var mini_cnt = (mini_size + 7) >> 3;
  1766. var mfat_cnt = (mini_size + 0x7F) >> 7;
  1767. var fat_base = mini_cnt + fat_size + dir_cnt + mfat_cnt;
  1768. var fat_cnt = (fat_base + 0x7F) >> 7;
  1769. var difat_cnt = fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  1770. while(((fat_base + fat_cnt + difat_cnt + 0x7F) >> 7) > fat_cnt) difat_cnt = ++fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
  1771. var L = [1, difat_cnt, fat_cnt, mfat_cnt, dir_cnt, fat_size, mini_size, 0];
  1772. cfb.FileIndex[0].size = mini_size << 6;
  1773. L[7] = (cfb.FileIndex[0].start=L[0]+L[1]+L[2]+L[3]+L[4]+L[5])+((L[6]+7) >> 3);
  1774. return L;
  1775. })(cfb);
  1776. var o = new_buf(L[7] << 9);
  1777. var i = 0, T = 0;
  1778. {
  1779. for(i = 0; i < 8; ++i) o.write_shift(1, HEADER_SIG[i]);
  1780. for(i = 0; i < 8; ++i) o.write_shift(2, 0);
  1781. o.write_shift(2, 0x003E);
  1782. o.write_shift(2, 0x0003);
  1783. o.write_shift(2, 0xFFFE);
  1784. o.write_shift(2, 0x0009);
  1785. o.write_shift(2, 0x0006);
  1786. for(i = 0; i < 3; ++i) o.write_shift(2, 0);
  1787. o.write_shift(4, 0);
  1788. o.write_shift(4, L[2]);
  1789. o.write_shift(4, L[0] + L[1] + L[2] + L[3] - 1);
  1790. o.write_shift(4, 0);
  1791. o.write_shift(4, 1<<12);
  1792. o.write_shift(4, L[3] ? L[0] + L[1] + L[2] - 1: ENDOFCHAIN);
  1793. o.write_shift(4, L[3]);
  1794. o.write_shift(-4, L[1] ? L[0] - 1: ENDOFCHAIN);
  1795. o.write_shift(4, L[1]);
  1796. for(i = 0; i < 109; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  1797. }
  1798. if(L[1]) {
  1799. for(T = 0; T < L[1]; ++T) {
  1800. for(; i < 236 + T * 127; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
  1801. o.write_shift(-4, T === L[1] - 1 ? ENDOFCHAIN : T + 1);
  1802. }
  1803. }
  1804. var chainit = function(w) {
  1805. for(T += w; i<T-1; ++i) o.write_shift(-4, i+1);
  1806. if(w) { ++i; o.write_shift(-4, ENDOFCHAIN); }
  1807. };
  1808. T = i = 0;
  1809. for(T+=L[1]; i<T; ++i) o.write_shift(-4, consts.DIFSECT);
  1810. for(T+=L[2]; i<T; ++i) o.write_shift(-4, consts.FATSECT);
  1811. chainit(L[3]);
  1812. chainit(L[4]);
  1813. var j = 0, flen = 0;
  1814. var file = cfb.FileIndex[0];
  1815. for(; j < cfb.FileIndex.length; ++j) {
  1816. file = cfb.FileIndex[j];
  1817. if(!file.content) continue;
  1818. flen = file.content.length;
  1819. if(flen < 0x1000) continue;
  1820. file.start = T;
  1821. chainit((flen + 0x01FF) >> 9);
  1822. }
  1823. chainit((L[6] + 7) >> 3);
  1824. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  1825. T = i = 0;
  1826. for(j = 0; j < cfb.FileIndex.length; ++j) {
  1827. file = cfb.FileIndex[j];
  1828. if(!file.content) continue;
  1829. flen = file.content.length;
  1830. if(!flen || flen >= 0x1000) continue;
  1831. file.start = T;
  1832. chainit((flen + 0x3F) >> 6);
  1833. }
  1834. while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
  1835. for(i = 0; i < L[4]<<2; ++i) {
  1836. var nm = cfb.FullPaths[i];
  1837. if(!nm || nm.length === 0) {
  1838. for(j = 0; j < 17; ++j) o.write_shift(4, 0);
  1839. for(j = 0; j < 3; ++j) o.write_shift(4, -1);
  1840. for(j = 0; j < 12; ++j) o.write_shift(4, 0);
  1841. continue;
  1842. }
  1843. file = cfb.FileIndex[i];
  1844. if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN;
  1845. var _nm = (i === 0 && _opts.root) || file.name;
  1846. flen = 2*(_nm.length+1);
  1847. o.write_shift(64, _nm, "utf16le");
  1848. o.write_shift(2, flen);
  1849. o.write_shift(1, file.type);
  1850. o.write_shift(1, file.color);
  1851. o.write_shift(-4, file.L);
  1852. o.write_shift(-4, file.R);
  1853. o.write_shift(-4, file.C);
  1854. if(!file.clsid) for(j = 0; j < 4; ++j) o.write_shift(4, 0);
  1855. else o.write_shift(16, file.clsid, "hex");
  1856. o.write_shift(4, file.state || 0);
  1857. o.write_shift(4, 0); o.write_shift(4, 0);
  1858. o.write_shift(4, 0); o.write_shift(4, 0);
  1859. o.write_shift(4, file.start);
  1860. o.write_shift(4, file.size); o.write_shift(4, 0);
  1861. }
  1862. for(i = 1; i < cfb.FileIndex.length; ++i) {
  1863. file = cfb.FileIndex[i];
  1864. if(file.size >= 0x1000) {
  1865. o.l = (file.start+1) << 9;
  1866. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  1867. for(; j & 0x1FF; ++j) o.write_shift(1, 0);
  1868. }
  1869. }
  1870. for(i = 1; i < cfb.FileIndex.length; ++i) {
  1871. file = cfb.FileIndex[i];
  1872. if(file.size > 0 && file.size < 0x1000) {
  1873. for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
  1874. for(; j & 0x3F; ++j) o.write_shift(1, 0);
  1875. }
  1876. }
  1877. while(o.l < o.length) o.write_shift(1, 0);
  1878. return o;
  1879. }
  1880. /* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
  1881. function find(cfb, path) {
  1882. var UCFullPaths = cfb.FullPaths.map(function(x) { return x.toUpperCase(); });
  1883. var UCPaths = UCFullPaths.map(function(x) { var y = x.split("/"); return y[y.length - (x.slice(-1) == "/" ? 2 : 1)]; });
  1884. var k = false;
  1885. if(path.charCodeAt(0) === 47 /* "/" */) { k = true; path = UCFullPaths[0].slice(0, -1) + path; }
  1886. else k = path.indexOf("/") !== -1;
  1887. var UCPath = path.toUpperCase();
  1888. var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
  1889. if(w !== -1) return cfb.FileIndex[w];
  1890. var m = !UCPath.match(chr1);
  1891. UCPath = UCPath.replace(chr0,'');
  1892. if(m) UCPath = UCPath.replace(chr1,'!');
  1893. for(w = 0; w < UCFullPaths.length; ++w) {
  1894. if((m ? UCFullPaths[w].replace(chr1,'!') : UCFullPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  1895. if((m ? UCPaths[w].replace(chr1,'!') : UCPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
  1896. }
  1897. return null;
  1898. }
  1899. /** CFB Constants */
  1900. var MSSZ = 64; /* Mini Sector Size = 1<<6 */
  1901. //var MSCSZ = 4096; /* Mini Stream Cutoff Size */
  1902. /* 2.1 Compound File Sector Numbers and Types */
  1903. var ENDOFCHAIN = -2;
  1904. /* 2.2 Compound File Header */
  1905. var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
  1906. var HEADER_SIG = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
  1907. var HEADER_CLSID = '00000000000000000000000000000000';
  1908. var consts = {
  1909. /* 2.1 Compund File Sector Numbers and Types */
  1910. MAXREGSECT: -6,
  1911. DIFSECT: -4,
  1912. FATSECT: -3,
  1913. ENDOFCHAIN: ENDOFCHAIN,
  1914. FREESECT: -1,
  1915. /* 2.2 Compound File Header */
  1916. HEADER_SIGNATURE: HEADER_SIGNATURE,
  1917. HEADER_MINOR_VERSION: '3e00',
  1918. MAXREGSID: -6,
  1919. NOSTREAM: -1,
  1920. HEADER_CLSID: HEADER_CLSID,
  1921. /* 2.6.1 Compound File Directory Entry */
  1922. EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
  1923. };
  1924. function write_file(cfb, filename, options) {
  1925. get_fs();
  1926. var o = _write(cfb, options);
  1927. fs.writeFileSync(filename, o);
  1928. }
  1929. function a2s(o) {
  1930. var out = new Array(o.length);
  1931. for(var i = 0; i < o.length; ++i) out[i] = String.fromCharCode(o[i]);
  1932. return out.join("");
  1933. }
  1934. function write(cfb, options) {
  1935. var o = _write(cfb, options);
  1936. switch(options && options.type) {
  1937. case "file": get_fs(); fs.writeFileSync(options.filename, (o)); return o;
  1938. case "binary": return a2s(o);
  1939. case "base64": return Base64.encode(a2s(o));
  1940. }
  1941. return o;
  1942. }
  1943. /* node < 8.1 zlib does not expose bytesRead, so default to pure JS */
  1944. var _zlib;
  1945. function use_zlib(zlib) { try {
  1946. var InflateRaw = zlib.InflateRaw;
  1947. var InflRaw = new InflateRaw();
  1948. InflRaw._processChunk(new Uint8Array([3, 0]), InflRaw._finishFlushFlag);
  1949. if(InflRaw.bytesRead) _zlib = zlib;
  1950. else throw new Error("zlib does not expose bytesRead");
  1951. } catch(e) {console.error("cannot use native zlib: " + (e.message || e)); } }
  1952. function _inflateRawSync(payload, usz) {
  1953. if(!_zlib) return _inflate(payload, usz);
  1954. var InflateRaw = _zlib.InflateRaw;
  1955. var InflRaw = new InflateRaw();
  1956. var out = InflRaw._processChunk(payload.slice(payload.l), InflRaw._finishFlushFlag);
  1957. payload.l += InflRaw.bytesRead;
  1958. return out;
  1959. }
  1960. function _deflateRawSync(payload) {
  1961. return _zlib ? _zlib.deflateRawSync(payload) : _deflate(payload);
  1962. }
  1963. var CLEN_ORDER = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
  1964. /* LEN_ID = [ 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285 ]; */
  1965. var LEN_LN = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13 , 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 ];
  1966. /* DST_ID = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]; */
  1967. var DST_LN = [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
  1968. function bit_swap_8(n) { var t = (((((n<<1)|(n<<11)) & 0x22110) | (((n<<5)|(n<<15)) & 0x88440))); return ((t>>16) | (t>>8) |t)&0xFF; }
  1969. var use_typed_arrays = typeof Uint8Array !== 'undefined';
  1970. var bitswap8 = use_typed_arrays ? new Uint8Array(1<<8) : [];
  1971. for(var q = 0; q < (1<<8); ++q) bitswap8[q] = bit_swap_8(q);
  1972. function bit_swap_n(n, b) {
  1973. var rev = bitswap8[n & 0xFF];
  1974. if(b <= 8) return rev >>> (8-b);
  1975. rev = (rev << 8) | bitswap8[(n>>8)&0xFF];
  1976. if(b <= 16) return rev >>> (16-b);
  1977. rev = (rev << 8) | bitswap8[(n>>16)&0xFF];
  1978. return rev >>> (24-b);
  1979. }
  1980. /* helpers for unaligned bit reads */
  1981. function read_bits_2(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 6 ? 0 : buf[h+1]<<8))>>>w)& 0x03; }
  1982. function read_bits_3(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 5 ? 0 : buf[h+1]<<8))>>>w)& 0x07; }
  1983. function read_bits_4(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 4 ? 0 : buf[h+1]<<8))>>>w)& 0x0F; }
  1984. function read_bits_5(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 3 ? 0 : buf[h+1]<<8))>>>w)& 0x1F; }
  1985. function read_bits_7(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 1 ? 0 : buf[h+1]<<8))>>>w)& 0x7F; }
  1986. /* works up to n = 3 * 8 + 1 = 25 */
  1987. function read_bits_n(buf, bl, n) {
  1988. var w = (bl&7), h = (bl>>>3), f = ((1<<n)-1);
  1989. var v = buf[h] >>> w;
  1990. if(n < 8 - w) return v & f;
  1991. v |= buf[h+1]<<(8-w);
  1992. if(n < 16 - w) return v & f;
  1993. v |= buf[h+2]<<(16-w);
  1994. if(n < 24 - w) return v & f;
  1995. v |= buf[h+3]<<(24-w);
  1996. return v & f;
  1997. }
  1998. /* until ArrayBuffer#realloc is a thing, fake a realloc */
  1999. function realloc(b, sz) {
  2000. var L = b.length, M = 2*L > sz ? 2*L : sz + 5, i = 0;
  2001. if(L >= sz) return b;
  2002. if(has_buf) {
  2003. var o = new_unsafe_buf(M);
  2004. // $FlowIgnore
  2005. if(b.copy) b.copy(o);
  2006. else for(; i < b.length; ++i) o[i] = b[i];
  2007. return o;
  2008. } else if(use_typed_arrays) {
  2009. var a = new Uint8Array(M);
  2010. if(a.set) a.set(b);
  2011. else for(; i < b.length; ++i) a[i] = b[i];
  2012. return a;
  2013. }
  2014. b.length = M;
  2015. return b;
  2016. }
  2017. /* zero-filled arrays for older browsers */
  2018. function zero_fill_array(n) {
  2019. var o = new Array(n);
  2020. for(var i = 0; i < n; ++i) o[i] = 0;
  2021. return o;
  2022. }var _deflate = (function() {
  2023. var _deflateRaw = (function() {
  2024. return function deflateRaw(data, out) {
  2025. var boff = 0;
  2026. while(boff < data.length) {
  2027. var L = Math.min(0xFFFF, data.length - boff);
  2028. var h = boff + L == data.length;
  2029. /* TODO: this is only type 0 stored */
  2030. out.write_shift(1, +h);
  2031. out.write_shift(2, L);
  2032. out.write_shift(2, (~L) & 0xFFFF);
  2033. while(L-- > 0) out[out.l++] = data[boff++];
  2034. }
  2035. return out.l;
  2036. };
  2037. })();
  2038. return function(data) {
  2039. var buf = new_buf(50+Math.floor(data.length*1.1));
  2040. var off = _deflateRaw(data, buf);
  2041. return buf.slice(0, off);
  2042. };
  2043. })();
  2044. /* modified inflate function also moves original read head */
  2045. /* build tree (used for literals and lengths) */
  2046. function build_tree(clens, cmap, MAX) {
  2047. var maxlen = 1, w = 0, i = 0, j = 0, ccode = 0, L = clens.length;
  2048. var bl_count = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  2049. for(i = 0; i < 32; ++i) bl_count[i] = 0;
  2050. for(i = L; i < MAX; ++i) clens[i] = 0;
  2051. L = clens.length;
  2052. var ctree = use_typed_arrays ? new Uint16Array(L) : zero_fill_array(L); // []
  2053. /* build code tree */
  2054. for(i = 0; i < L; ++i) {
  2055. bl_count[(w = clens[i])]++;
  2056. if(maxlen < w) maxlen = w;
  2057. ctree[i] = 0;
  2058. }
  2059. bl_count[0] = 0;
  2060. for(i = 1; i <= maxlen; ++i) bl_count[i+16] = (ccode = (ccode + bl_count[i-1])<<1);
  2061. for(i = 0; i < L; ++i) {
  2062. ccode = clens[i];
  2063. if(ccode != 0) ctree[i] = bl_count[ccode+16]++;
  2064. }
  2065. /* cmap[maxlen + 4 bits] = (off&15) + (lit<<4) reverse mapping */
  2066. var cleni = 0;
  2067. for(i = 0; i < L; ++i) {
  2068. cleni = clens[i];
  2069. if(cleni != 0) {
  2070. ccode = bit_swap_n(ctree[i], maxlen)>>(maxlen-cleni);
  2071. for(j = (1<<(maxlen + 4 - cleni)) - 1; j>=0; --j)
  2072. cmap[ccode|(j<<cleni)] = (cleni&15) | (i<<4);
  2073. }
  2074. }
  2075. return maxlen;
  2076. }
  2077. var fix_lmap = use_typed_arrays ? new Uint16Array(512) : zero_fill_array(512);
  2078. var fix_dmap = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
  2079. if(!use_typed_arrays) {
  2080. for(var i = 0; i < 512; ++i) fix_lmap[i] = 0;
  2081. for(i = 0; i < 32; ++i) fix_dmap[i] = 0;
  2082. }
  2083. (function() {
  2084. var dlens = [];
  2085. var i = 0;
  2086. for(;i<32; i++) dlens.push(5);
  2087. build_tree(dlens, fix_dmap, 32);
  2088. var clens = [];
  2089. i = 0;
  2090. for(; i<=143; i++) clens.push(8);
  2091. for(; i<=255; i++) clens.push(9);
  2092. for(; i<=279; i++) clens.push(7);
  2093. for(; i<=287; i++) clens.push(8);
  2094. build_tree(clens, fix_lmap, 288);
  2095. })();
  2096. var dyn_lmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  2097. var dyn_dmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
  2098. var dyn_cmap = use_typed_arrays ? new Uint16Array(128) : zero_fill_array(128);
  2099. var dyn_len_1 = 1, dyn_len_2 = 1;
  2100. /* 5.5.3 Expanding Huffman Codes */
  2101. function dyn(data, boff) {
  2102. /* nomenclature from RFC1951 refers to bit values; these are offset by the implicit constant */
  2103. var _HLIT = read_bits_5(data, boff) + 257; boff += 5;
  2104. var _HDIST = read_bits_5(data, boff) + 1; boff += 5;
  2105. var _HCLEN = read_bits_4(data, boff) + 4; boff += 4;
  2106. var w = 0;
  2107. /* grab and store code lengths */
  2108. var clens = use_typed_arrays ? new Uint8Array(19) : zero_fill_array(19);
  2109. var ctree = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  2110. var maxlen = 1;
  2111. var bl_count = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  2112. var next_code = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
  2113. var L = clens.length; /* 19 */
  2114. for(var i = 0; i < _HCLEN; ++i) {
  2115. clens[CLEN_ORDER[i]] = w = read_bits_3(data, boff);
  2116. if(maxlen < w) maxlen = w;
  2117. bl_count[w]++;
  2118. boff += 3;
  2119. }
  2120. /* build code tree */
  2121. var ccode = 0;
  2122. bl_count[0] = 0;
  2123. for(i = 1; i <= maxlen; ++i) next_code[i] = ccode = (ccode + bl_count[i-1])<<1;
  2124. for(i = 0; i < L; ++i) if((ccode = clens[i]) != 0) ctree[i] = next_code[ccode]++;
  2125. /* cmap[7 bits from stream] = (off&7) + (lit<<3) */
  2126. var cleni = 0;
  2127. for(i = 0; i < L; ++i) {
  2128. cleni = clens[i];
  2129. if(cleni != 0) {
  2130. ccode = bitswap8[ctree[i]]>>(8-cleni);
  2131. for(var j = (1<<(7-cleni))-1; j>=0; --j) dyn_cmap[ccode|(j<<cleni)] = (cleni&7) | (i<<3);
  2132. }
  2133. }
  2134. /* read literal and dist codes at once */
  2135. var hcodes = [];
  2136. maxlen = 1;
  2137. for(; hcodes.length < _HLIT + _HDIST;) {
  2138. ccode = dyn_cmap[read_bits_7(data, boff)];
  2139. boff += ccode & 7;
  2140. switch((ccode >>>= 3)) {
  2141. case 16:
  2142. w = 3 + read_bits_2(data, boff); boff += 2;
  2143. ccode = hcodes[hcodes.length - 1];
  2144. while(w-- > 0) hcodes.push(ccode);
  2145. break;
  2146. case 17:
  2147. w = 3 + read_bits_3(data, boff); boff += 3;
  2148. while(w-- > 0) hcodes.push(0);
  2149. break;
  2150. case 18:
  2151. w = 11 + read_bits_7(data, boff); boff += 7;
  2152. while(w -- > 0) hcodes.push(0);
  2153. break;
  2154. default:
  2155. hcodes.push(ccode);
  2156. if(maxlen < ccode) maxlen = ccode;
  2157. break;
  2158. }
  2159. }
  2160. /* build literal / length trees */
  2161. var h1 = hcodes.slice(0, _HLIT), h2 = hcodes.slice(_HLIT);
  2162. for(i = _HLIT; i < 286; ++i) h1[i] = 0;
  2163. for(i = _HDIST; i < 30; ++i) h2[i] = 0;
  2164. dyn_len_1 = build_tree(h1, dyn_lmap, 286);
  2165. dyn_len_2 = build_tree(h2, dyn_dmap, 30);
  2166. return boff;
  2167. }
  2168. /* return [ data, bytesRead ] */
  2169. function inflate(data, usz) {
  2170. /* shortcircuit for empty buffer [0x03, 0x00] */
  2171. if(data[0] == 3 && !(data[1] & 0x3)) { return [new_raw_buf(usz), 2]; }
  2172. /* bit offset */
  2173. var boff = 0;
  2174. /* header includes final bit and type bits */
  2175. var header = 0;
  2176. var outbuf = new_unsafe_buf(usz ? usz : (1<<18));
  2177. var woff = 0;
  2178. var OL = outbuf.length>>>0;
  2179. var max_len_1 = 0, max_len_2 = 0;
  2180. while((header&1) == 0) {
  2181. header = read_bits_3(data, boff); boff += 3;
  2182. if((header >>> 1) == 0) {
  2183. /* Stored block */
  2184. if(boff & 7) boff += 8 - (boff&7);
  2185. /* 2 bytes sz, 2 bytes bit inverse */
  2186. var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8;
  2187. boff += 32;
  2188. /* push sz bytes */
  2189. if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; }
  2190. if(typeof data.copy === 'function') {
  2191. // $FlowIgnore
  2192. data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz);
  2193. woff += sz; boff += 8*sz;
  2194. } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; }
  2195. continue;
  2196. } else if((header >>> 1) == 1) {
  2197. /* Fixed Huffman */
  2198. max_len_1 = 9; max_len_2 = 5;
  2199. } else {
  2200. /* Dynamic Huffman */
  2201. boff = dyn(data, boff);
  2202. max_len_1 = dyn_len_1; max_len_2 = dyn_len_2;
  2203. }
  2204. if(!usz && (OL < woff + 32767)) { outbuf = realloc(outbuf, woff + 32767); OL = outbuf.length; }
  2205. for(;;) { // while(true) is apparently out of vogue in modern JS circles
  2206. /* ingest code and move read head */
  2207. var bits = read_bits_n(data, boff, max_len_1);
  2208. var code = (header>>>1) == 1 ? fix_lmap[bits] : dyn_lmap[bits];
  2209. boff += code & 15; code >>>= 4;
  2210. /* 0-255 are literals, 256 is end of block token, 257+ are copy tokens */
  2211. if(((code>>>8)&0xFF) === 0) outbuf[woff++] = code;
  2212. else if(code == 256) break;
  2213. else {
  2214. code -= 257;
  2215. var len_eb = (code < 8) ? 0 : ((code-4)>>2); if(len_eb > 5) len_eb = 0;
  2216. var tgt = woff + LEN_LN[code];
  2217. /* length extra bits */
  2218. if(len_eb > 0) {
  2219. tgt += read_bits_n(data, boff, len_eb);
  2220. boff += len_eb;
  2221. }
  2222. /* dist code */
  2223. bits = read_bits_n(data, boff, max_len_2);
  2224. code = (header>>>1) == 1 ? fix_dmap[bits] : dyn_dmap[bits];
  2225. boff += code & 15; code >>>= 4;
  2226. var dst_eb = (code < 4 ? 0 : (code-2)>>1);
  2227. var dst = DST_LN[code];
  2228. /* dist extra bits */
  2229. if(dst_eb > 0) {
  2230. dst += read_bits_n(data, boff, dst_eb);
  2231. boff += dst_eb;
  2232. }
  2233. /* in the common case, manual byte copy is faster than TA set / Buffer copy */
  2234. if(!usz && OL < tgt) { outbuf = realloc(outbuf, tgt); OL = outbuf.length; }
  2235. while(woff < tgt) { outbuf[woff] = outbuf[woff - dst]; ++woff; }
  2236. }
  2237. }
  2238. }
  2239. return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3];
  2240. }
  2241. function _inflate(payload, usz) {
  2242. var data = payload.slice(payload.l||0);
  2243. var out = inflate(data, usz);
  2244. payload.l += out[1];
  2245. return out[0];
  2246. }
  2247. function warn_or_throw(wrn, msg) {
  2248. if(wrn) { if(typeof console !== 'undefined') console.error(msg); }
  2249. else throw new Error(msg);
  2250. }
  2251. function parse_zip(file, options) {
  2252. var blob = file;
  2253. prep_blob(blob, 0);
  2254. var FileIndex = [], FullPaths = [];
  2255. var o = {
  2256. FileIndex: FileIndex,
  2257. FullPaths: FullPaths
  2258. };
  2259. init_cfb(o, { root: options.root });
  2260. /* find end of central directory, start just after signature */
  2261. var i = blob.length - 4;
  2262. while((blob[i] != 0x50 || blob[i+1] != 0x4b || blob[i+2] != 0x05 || blob[i+3] != 0x06) && i >= 0) --i;
  2263. blob.l = i + 4;
  2264. /* parse end of central directory */
  2265. blob.l += 4;
  2266. var fcnt = blob.read_shift(2);
  2267. blob.l += 6;
  2268. var start_cd = blob.read_shift(4);
  2269. /* parse central directory */
  2270. blob.l = start_cd;
  2271. for(i = 0; i < fcnt; ++i) {
  2272. /* trust local file header instead of CD entry */
  2273. blob.l += 20;
  2274. var csz = blob.read_shift(4);
  2275. var usz = blob.read_shift(4);
  2276. var namelen = blob.read_shift(2);
  2277. var efsz = blob.read_shift(2);
  2278. var fcsz = blob.read_shift(2);
  2279. blob.l += 8;
  2280. var offset = blob.read_shift(4);
  2281. var EF = parse_extra_field(blob.slice(blob.l+namelen, blob.l+namelen+efsz));
  2282. blob.l += namelen + efsz + fcsz;
  2283. var L = blob.l;
  2284. blob.l = offset + 4;
  2285. parse_local_file(blob, csz, usz, o, EF);
  2286. blob.l = L;
  2287. }
  2288. return o;
  2289. }
  2290. /* head starts just after local file header signature */
  2291. function parse_local_file(blob, csz, usz, o, EF) {
  2292. /* [local file header] */
  2293. blob.l += 2;
  2294. var flags = blob.read_shift(2);
  2295. var meth = blob.read_shift(2);
  2296. var date = parse_dos_date(blob);
  2297. if(flags & 0x2041) throw new Error("Unsupported ZIP encryption");
  2298. var crc32 = blob.read_shift(4);
  2299. var _csz = blob.read_shift(4);
  2300. var _usz = blob.read_shift(4);
  2301. var namelen = blob.read_shift(2);
  2302. var efsz = blob.read_shift(2);
  2303. // TODO: flags & (1<<11) // UTF8
  2304. var name = ""; for(var i = 0; i < namelen; ++i) name += String.fromCharCode(blob[blob.l++]);
  2305. if(efsz) {
  2306. var ef = parse_extra_field(blob.slice(blob.l, blob.l + efsz));
  2307. if((ef[0x5455]||{}).mt) date = ef[0x5455].mt;
  2308. if(((EF||{})[0x5455]||{}).mt) date = EF[0x5455].mt;
  2309. }
  2310. blob.l += efsz;
  2311. /* [encryption header] */
  2312. /* [file data] */
  2313. var data = blob.slice(blob.l, blob.l + _csz);
  2314. switch(meth) {
  2315. case 8: data = _inflateRawSync(blob, _usz); break;
  2316. case 0: break;
  2317. default: throw new Error("Unsupported ZIP Compression method " + meth);
  2318. }
  2319. /* [data descriptor] */
  2320. var wrn = false;
  2321. if(flags & 8) {
  2322. crc32 = blob.read_shift(4);
  2323. if(crc32 == 0x08074b50) { crc32 = blob.read_shift(4); wrn = true; }
  2324. _csz = blob.read_shift(4);
  2325. _usz = blob.read_shift(4);
  2326. }
  2327. if(_csz != csz) warn_or_throw(wrn, "Bad compressed size: " + csz + " != " + _csz);
  2328. if(_usz != usz) warn_or_throw(wrn, "Bad uncompressed size: " + usz + " != " + _usz);
  2329. var _crc32 = CRC32.buf(data, 0);
  2330. if((crc32>>0) != (_crc32>>0)) warn_or_throw(wrn, "Bad CRC32 checksum: " + crc32 + " != " + _crc32);
  2331. cfb_add(o, name, data, {unsafe: true, mt: date});
  2332. }
  2333. function write_zip(cfb, options) {
  2334. var _opts = options || {};
  2335. var out = [], cdirs = [];
  2336. var o = new_buf(1);
  2337. var method = (_opts.compression ? 8 : 0), flags = 0;
  2338. var desc = false;
  2339. if(desc) flags |= 8;
  2340. var i = 0, j = 0;
  2341. var start_cd = 0, fcnt = 0;
  2342. var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
  2343. var crcs = [];
  2344. var sz_cd = 0;
  2345. for(i = 1; i < cfb.FullPaths.length; ++i) {
  2346. fp = cfb.FullPaths[i].slice(root.length); fi = cfb.FileIndex[i];
  2347. if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
  2348. var start = start_cd;
  2349. /* TODO: CP437 filename */
  2350. var namebuf = new_buf(fp.length);
  2351. for(j = 0; j < fp.length; ++j) namebuf.write_shift(1, fp.charCodeAt(j) & 0x7F);
  2352. namebuf = namebuf.slice(0, namebuf.l);
  2353. crcs[fcnt] = CRC32.buf(fi.content, 0);
  2354. var outbuf = fi.content;
  2355. if(method == 8) outbuf = _deflateRawSync(outbuf);
  2356. /* local file header */
  2357. o = new_buf(30);
  2358. o.write_shift(4, 0x04034b50);
  2359. o.write_shift(2, 20);
  2360. o.write_shift(2, flags);
  2361. o.write_shift(2, method);
  2362. /* TODO: last mod file time/date */
  2363. if(fi.mt) write_dos_date(o, fi.mt);
  2364. else o.write_shift(4, 0);
  2365. o.write_shift(-4, (flags & 8) ? 0 : crcs[fcnt]);
  2366. o.write_shift(4, (flags & 8) ? 0 : outbuf.length);
  2367. o.write_shift(4, (flags & 8) ? 0 : fi.content.length);
  2368. o.write_shift(2, namebuf.length);
  2369. o.write_shift(2, 0);
  2370. start_cd += o.length;
  2371. out.push(o);
  2372. start_cd += namebuf.length;
  2373. out.push(namebuf);
  2374. /* TODO: encryption header ? */
  2375. start_cd += outbuf.length;
  2376. out.push(outbuf);
  2377. /* data descriptor */
  2378. if(flags & 8) {
  2379. o = new_buf(12);
  2380. o.write_shift(-4, crcs[fcnt]);
  2381. o.write_shift(4, outbuf.length);
  2382. o.write_shift(4, fi.content.length);
  2383. start_cd += o.l;
  2384. out.push(o);
  2385. }
  2386. /* central directory */
  2387. o = new_buf(46);
  2388. o.write_shift(4, 0x02014b50);
  2389. o.write_shift(2, 0);
  2390. o.write_shift(2, 20);
  2391. o.write_shift(2, flags);
  2392. o.write_shift(2, method);
  2393. o.write_shift(4, 0); /* TODO: last mod file time/date */
  2394. o.write_shift(-4, crcs[fcnt]);
  2395. o.write_shift(4, outbuf.length);
  2396. o.write_shift(4, fi.content.length);
  2397. o.write_shift(2, namebuf.length);
  2398. o.write_shift(2, 0);
  2399. o.write_shift(2, 0);
  2400. o.write_shift(2, 0);
  2401. o.write_shift(2, 0);
  2402. o.write_shift(4, 0);
  2403. o.write_shift(4, start);
  2404. sz_cd += o.l;
  2405. cdirs.push(o);
  2406. sz_cd += namebuf.length;
  2407. cdirs.push(namebuf);
  2408. ++fcnt;
  2409. }
  2410. /* end of central directory */
  2411. o = new_buf(22);
  2412. o.write_shift(4, 0x06054b50);
  2413. o.write_shift(2, 0);
  2414. o.write_shift(2, 0);
  2415. o.write_shift(2, fcnt);
  2416. o.write_shift(2, fcnt);
  2417. o.write_shift(4, sz_cd);
  2418. o.write_shift(4, start_cd);
  2419. o.write_shift(2, 0);
  2420. return bconcat(([bconcat((out)), bconcat(cdirs), o]));
  2421. }
  2422. function cfb_new(opts) {
  2423. var o = ({});
  2424. init_cfb(o, opts);
  2425. return o;
  2426. }
  2427. function cfb_add(cfb, name, content, opts) {
  2428. var unsafe = opts && opts.unsafe;
  2429. if(!unsafe) init_cfb(cfb);
  2430. var file = !unsafe && CFB.find(cfb, name);
  2431. if(!file) {
  2432. var fpath = cfb.FullPaths[0];
  2433. if(name.slice(0, fpath.length) == fpath) fpath = name;
  2434. else {
  2435. if(fpath.slice(-1) != "/") fpath += "/";
  2436. fpath = (fpath + name).replace("//","/");
  2437. }
  2438. file = ({name: filename(name), type: 2});
  2439. cfb.FileIndex.push(file);
  2440. cfb.FullPaths.push(fpath);
  2441. if(!unsafe) CFB.utils.cfb_gc(cfb);
  2442. }
  2443. file.content = (content);
  2444. file.size = content ? content.length : 0;
  2445. if(opts) {
  2446. if(opts.CLSID) file.clsid = opts.CLSID;
  2447. if(opts.mt) file.mt = opts.mt;
  2448. if(opts.ct) file.ct = opts.ct;
  2449. }
  2450. return file;
  2451. }
  2452. function cfb_del(cfb, name) {
  2453. init_cfb(cfb);
  2454. var file = CFB.find(cfb, name);
  2455. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  2456. cfb.FileIndex.splice(j, 1);
  2457. cfb.FullPaths.splice(j, 1);
  2458. return true;
  2459. }
  2460. return false;
  2461. }
  2462. function cfb_mov(cfb, old_name, new_name) {
  2463. init_cfb(cfb);
  2464. var file = CFB.find(cfb, old_name);
  2465. if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
  2466. cfb.FileIndex[j].name = filename(new_name);
  2467. cfb.FullPaths[j] = new_name;
  2468. return true;
  2469. }
  2470. return false;
  2471. }
  2472. function cfb_gc(cfb) { rebuild_cfb(cfb, true); }
  2473. exports.find = find;
  2474. exports.read = read;
  2475. exports.parse = parse;
  2476. exports.write = write;
  2477. exports.writeFile = write_file;
  2478. exports.utils = {
  2479. cfb_new: cfb_new,
  2480. cfb_add: cfb_add,
  2481. cfb_del: cfb_del,
  2482. cfb_mov: cfb_mov,
  2483. cfb_gc: cfb_gc,
  2484. ReadShift: ReadShift,
  2485. CheckField: CheckField,
  2486. prep_blob: prep_blob,
  2487. bconcat: bconcat,
  2488. use_zlib: use_zlib,
  2489. _deflateRaw: _deflate,
  2490. _inflateRaw: _inflate,
  2491. consts: consts
  2492. };
  2493. return exports;
  2494. })();
  2495. if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
  2496. var _fs;
  2497. if(typeof require !== 'undefined') try { _fs = require('fs'); } catch(e) {}
  2498. /* normalize data for blob ctor */
  2499. function blobify(data) {
  2500. if(typeof data === "string") return s2ab(data);
  2501. if(Array.isArray(data)) return a2u(data);
  2502. return data;
  2503. }
  2504. /* write or download file */
  2505. function write_dl(fname, payload, enc) {
  2506. /*global IE_SaveFile, Blob, navigator, saveAs, document, File, chrome */
  2507. if(typeof _fs !== 'undefined' && _fs.writeFileSync) return enc ? _fs.writeFileSync(fname, payload, enc) : _fs.writeFileSync(fname, payload);
  2508. var data = (enc == "utf8") ? utf8write(payload) : payload;
  2509. if(typeof IE_SaveFile !== 'undefined') return IE_SaveFile(data, fname);
  2510. if(typeof Blob !== 'undefined') {
  2511. var blob = new Blob([blobify(data)], {type:"application/octet-stream"});
  2512. if(typeof navigator !== 'undefined' && navigator.msSaveBlob) return navigator.msSaveBlob(blob, fname);
  2513. if(typeof saveAs !== 'undefined') return saveAs(blob, fname);
  2514. if(typeof URL !== 'undefined' && typeof document !== 'undefined' && document.createElement && URL.createObjectURL) {
  2515. var url = URL.createObjectURL(blob);
  2516. if(typeof chrome === 'object' && typeof (chrome.downloads||{}).download == "function") {
  2517. if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
  2518. return chrome.downloads.download({ url: url, filename: fname, saveAs: true});
  2519. }
  2520. var a = document.createElement("a");
  2521. if(a.download != null) {
  2522. a.download = fname; a.href = url; document.body.appendChild(a); a.click();
  2523. document.body.removeChild(a);
  2524. if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
  2525. return url;
  2526. }
  2527. }
  2528. }
  2529. // $FlowIgnore
  2530. if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
  2531. // $FlowIgnore
  2532. var out = File(fname); out.open("w"); out.encoding = "binary";
  2533. if(Array.isArray(payload)) payload = a2s(payload);
  2534. out.write(payload); out.close(); return payload;
  2535. } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
  2536. throw new Error("cannot save file " + fname);
  2537. }
  2538. /* read binary data from file */
  2539. function read_binary(path) {
  2540. if(typeof _fs !== 'undefined') return _fs.readFileSync(path);
  2541. // $FlowIgnore
  2542. if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
  2543. // $FlowIgnore
  2544. var infile = File(path); infile.open("r"); infile.encoding = "binary";
  2545. var data = infile.read(); infile.close();
  2546. return data;
  2547. } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
  2548. throw new Error("Cannot access file " + path);
  2549. }
  2550. function keys(o) {
  2551. var ks = Object.keys(o), o2 = [];
  2552. for(var i = 0; i < ks.length; ++i) if(Object.prototype.hasOwnProperty.call(o, ks[i])) o2.push(ks[i]);
  2553. return o2;
  2554. }
  2555. function evert_key(obj, key) {
  2556. var o = ([]), K = keys(obj);
  2557. for(var i = 0; i !== K.length; ++i) if(o[obj[K[i]][key]] == null) o[obj[K[i]][key]] = K[i];
  2558. return o;
  2559. }
  2560. function evert(obj) {
  2561. var o = ([]), K = keys(obj);
  2562. for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = K[i];
  2563. return o;
  2564. }
  2565. function evert_num(obj) {
  2566. var o = ([]), K = keys(obj);
  2567. for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = parseInt(K[i],10);
  2568. return o;
  2569. }
  2570. function evert_arr(obj) {
  2571. var o = ([]), K = keys(obj);
  2572. for(var i = 0; i !== K.length; ++i) {
  2573. if(o[obj[K[i]]] == null) o[obj[K[i]]] = [];
  2574. o[obj[K[i]]].push(K[i]);
  2575. }
  2576. return o;
  2577. }
  2578. var basedate = new Date(1899, 11, 30, 0, 0, 0); // 2209161600000
  2579. function datenum(v, date1904) {
  2580. var epoch = v.getTime();
  2581. if(date1904) epoch -= 1462*24*60*60*1000;
  2582. var dnthresh = basedate.getTime() + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
  2583. return (epoch - dnthresh) / (24 * 60 * 60 * 1000);
  2584. }
  2585. var refdate = new Date();
  2586. var dnthresh = basedate.getTime() + (refdate.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
  2587. var refoffset = refdate.getTimezoneOffset();
  2588. function numdate(v) {
  2589. var out = new Date();
  2590. out.setTime(v * 24 * 60 * 60 * 1000 + dnthresh);
  2591. if (out.getTimezoneOffset() !== refoffset) {
  2592. out.setTime(out.getTime() + (out.getTimezoneOffset() - refoffset) * 60000);
  2593. }
  2594. return out;
  2595. }
  2596. /* ISO 8601 Duration */
  2597. function parse_isodur(s) {
  2598. var sec = 0, mt = 0, time = false;
  2599. var m = s.match(/P([0-9\.]+Y)?([0-9\.]+M)?([0-9\.]+D)?T([0-9\.]+H)?([0-9\.]+M)?([0-9\.]+S)?/);
  2600. if(!m) throw new Error("|" + s + "| is not an ISO8601 Duration");
  2601. for(var i = 1; i != m.length; ++i) {
  2602. if(!m[i]) continue;
  2603. mt = 1;
  2604. if(i > 3) time = true;
  2605. switch(m[i].slice(m[i].length-1)) {
  2606. case 'Y':
  2607. throw new Error("Unsupported ISO Duration Field: " + m[i].slice(m[i].length-1));
  2608. case 'D': mt *= 24;
  2609. /* falls through */
  2610. case 'H': mt *= 60;
  2611. /* falls through */
  2612. case 'M':
  2613. if(!time) throw new Error("Unsupported ISO Duration Field: M");
  2614. else mt *= 60;
  2615. /* falls through */
  2616. case 'S': break;
  2617. }
  2618. sec += mt * parseInt(m[i], 10);
  2619. }
  2620. return sec;
  2621. }
  2622. var good_pd_date = new Date('2017-02-19T19:06:09.000Z');
  2623. if(isNaN(good_pd_date.getFullYear())) good_pd_date = new Date('2/19/17');
  2624. var good_pd = good_pd_date.getFullYear() == 2017;
  2625. /* parses a date as a local date */
  2626. function parseDate(str, fixdate) {
  2627. var d = new Date(str);
  2628. if(good_pd) {
  2629. if(fixdate > 0) d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
  2630. else if(fixdate < 0) d.setTime(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
  2631. return d;
  2632. }
  2633. if(str instanceof Date) return str;
  2634. if(good_pd_date.getFullYear() == 1917 && !isNaN(d.getFullYear())) {
  2635. var s = d.getFullYear();
  2636. if(str.indexOf("" + s) > -1) return d;
  2637. d.setFullYear(d.getFullYear() + 100); return d;
  2638. }
  2639. var n = str.match(/\d+/g)||["2017","2","19","0","0","0"];
  2640. var out = new Date(+n[0], +n[1] - 1, +n[2], (+n[3]||0), (+n[4]||0), (+n[5]||0));
  2641. if(str.indexOf("Z") > -1) out = new Date(out.getTime() - out.getTimezoneOffset() * 60 * 1000);
  2642. return out;
  2643. }
  2644. function cc2str(arr) {
  2645. var o = "";
  2646. for(var i = 0; i != arr.length; ++i) o += String.fromCharCode(arr[i]);
  2647. return o;
  2648. }
  2649. function dup(o) {
  2650. if(typeof JSON != 'undefined' && !Array.isArray(o)) return JSON.parse(JSON.stringify(o));
  2651. if(typeof o != 'object' || o == null) return o;
  2652. if(o instanceof Date) return new Date(o.getTime());
  2653. var out = {};
  2654. for(var k in o) if(Object.prototype.hasOwnProperty.call(o, k)) out[k] = dup(o[k]);
  2655. return out;
  2656. }
  2657. function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
  2658. /* TODO: stress test */
  2659. function fuzzynum(s) {
  2660. var v = Number(s);
  2661. if(!isNaN(v)) return v;
  2662. if(!/\d/.test(s)) return v;
  2663. var wt = 1;
  2664. var ss = s.replace(/([\d]),([\d])/g,"$1$2").replace(/[$]/g,"").replace(/[%]/g, function() { wt *= 100; return "";});
  2665. if(!isNaN(v = Number(ss))) return v / wt;
  2666. ss = ss.replace(/[(](.*)[)]/,function($$, $1) { wt = -wt; return $1;});
  2667. if(!isNaN(v = Number(ss))) return v / wt;
  2668. return v;
  2669. }
  2670. function fuzzydate(s) {
  2671. var o = new Date(s), n = new Date(NaN);
  2672. var y = o.getYear(), m = o.getMonth(), d = o.getDate();
  2673. if(isNaN(d)) return n;
  2674. if(y < 0 || y > 8099) return n;
  2675. if((m > 0 || d > 1) && y != 101) return o;
  2676. if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
  2677. if(s.match(/[^-0-9:,\/\\]/)) return n;
  2678. return o;
  2679. }
  2680. var safe_split_regex = "abacaba".split(/(:?b)/i).length == 5;
  2681. function split_regex(str, re, def) {
  2682. if(safe_split_regex || typeof re == "string") return str.split(re);
  2683. var p = str.split(re), o = [p[0]];
  2684. for(var i = 1; i < p.length; ++i) { o.push(def); o.push(p[i]); }
  2685. return o;
  2686. }
  2687. function getdatastr(data) {
  2688. if(!data) return null;
  2689. if(data.data) return debom(data.data);
  2690. if(data.asNodeBuffer && has_buf) return debom(data.asNodeBuffer().toString('binary'));
  2691. if(data.asBinary) return debom(data.asBinary());
  2692. if(data._data && data._data.getContent) return debom(cc2str(Array.prototype.slice.call(data._data.getContent(),0)));
  2693. if(data.content && data.type) return debom(cc2str(data.content));
  2694. return null;
  2695. }
  2696. function getdatabin(data) {
  2697. if(!data) return null;
  2698. if(data.data) return char_codes(data.data);
  2699. if(data.asNodeBuffer && has_buf) return data.asNodeBuffer();
  2700. if(data._data && data._data.getContent) {
  2701. var o = data._data.getContent();
  2702. if(typeof o == "string") return char_codes(o);
  2703. return Array.prototype.slice.call(o);
  2704. }
  2705. if(data.content && data.type) return data.content;
  2706. return null;
  2707. }
  2708. function getdata(data) { return (data && data.name.slice(-4) === ".bin") ? getdatabin(data) : getdatastr(data); }
  2709. /* Part 2 Section 10.1.2 "Mapping Content Types" Names are case-insensitive */
  2710. /* OASIS does not comment on filename case sensitivity */
  2711. function safegetzipfile(zip, file) {
  2712. var k = zip.FullPaths || keys(zip.files);
  2713. var f = file.toLowerCase(), g = f.replace(/\//g,'\\');
  2714. for(var i=0; i<k.length; ++i) {
  2715. var n = k[i].toLowerCase();
  2716. if(f == n || g == n) return zip.files[k[i]];
  2717. }
  2718. return null;
  2719. }
  2720. function getzipfile(zip, file) {
  2721. var o = safegetzipfile(zip, file);
  2722. if(o == null) throw new Error("Cannot find file " + file + " in zip");
  2723. return o;
  2724. }
  2725. function getzipdata(zip, file, safe) {
  2726. if(!safe) return getdata(getzipfile(zip, file));
  2727. if(!file) return null;
  2728. try { return getzipdata(zip, file); } catch(e) { return null; }
  2729. }
  2730. function getzipstr(zip, file, safe) {
  2731. if(!safe) return getdatastr(getzipfile(zip, file));
  2732. if(!file) return null;
  2733. try { return getzipstr(zip, file); } catch(e) { return null; }
  2734. }
  2735. function zipentries(zip) {
  2736. var k = zip.FullPaths || keys(zip.files), o = [];
  2737. for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i]);
  2738. return o.sort();
  2739. }
  2740. function zip_add_file(zip, path, content) {
  2741. if(zip.FullPaths) CFB.utils.cfb_add(zip, path, content);
  2742. else zip.file(path, content);
  2743. }
  2744. var jszip;
  2745. /*global JSZipSync:true */
  2746. if(typeof JSZipSync !== 'undefined') jszip = JSZipSync;
  2747. if(typeof exports !== 'undefined') {
  2748. if(typeof module !== 'undefined' && module.exports) {
  2749. if(typeof jszip === 'undefined') jszip = require('./jszip.js');
  2750. }
  2751. }
  2752. function zip_new() {
  2753. if(!jszip) return CFB.utils.cfb_new();
  2754. return new jszip();
  2755. }
  2756. function zip_read(d, o) {
  2757. var zip;
  2758. if(jszip) switch(o.type) {
  2759. case "base64": zip = new jszip(d, { base64:true }); break;
  2760. case "binary": case "array": zip = new jszip(d, { base64:false }); break;
  2761. case "buffer": zip = new jszip(d); break;
  2762. default: throw new Error("Unrecognized type " + o.type);
  2763. }
  2764. else switch(o.type) {
  2765. case "base64": zip = CFB.read(d, { type: "base64" }); break;
  2766. case "binary": zip = CFB.read(d, { type: "binary" }); break;
  2767. case "buffer": case "array": zip = CFB.read(d, { type: "buffer" }); break;
  2768. default: throw new Error("Unrecognized type " + o.type);
  2769. }
  2770. return zip;
  2771. }
  2772. function resolve_path(path, base) {
  2773. if(path.charAt(0) == "/") return path.slice(1);
  2774. var result = base.split('/');
  2775. if(base.slice(-1) != "/") result.pop(); // folder path
  2776. var target = path.split('/');
  2777. while (target.length !== 0) {
  2778. var step = target.shift();
  2779. if (step === '..') result.pop();
  2780. else if (step !== '.') result.push(step);
  2781. }
  2782. return result.join('/');
  2783. }
  2784. var XML_HEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n';
  2785. var attregexg=/([^"\s?>\/]+)\s*=\s*((?:")([^"]*)(?:")|(?:')([^']*)(?:')|([^'">\s]+))/g;
  2786. var tagregex=/<[\/\?]?[a-zA-Z0-9:_-]+(?:\s+[^"\s?>\/]+\s*=\s*(?:"[^"]*"|'[^']*'|[^'">\s=]+))*\s?[\/\?]?>/mg;
  2787. if(!(XML_HEADER.match(tagregex))) tagregex = /<[^>]*>/g;
  2788. var nsregex=/<\w*:/, nsregex2 = /<(\/?)\w+:/;
  2789. function parsexmltag(tag, skip_root, skip_LC) {
  2790. var z = ({});
  2791. var eq = 0, c = 0;
  2792. for(; eq !== tag.length; ++eq) if((c = tag.charCodeAt(eq)) === 32 || c === 10 || c === 13) break;
  2793. if(!skip_root) z[0] = tag.slice(0, eq);
  2794. if(eq === tag.length) return z;
  2795. var m = tag.match(attregexg), j=0, v="", i=0, q="", cc="", quot = 1;
  2796. if(m) for(i = 0; i != m.length; ++i) {
  2797. cc = m[i];
  2798. for(c=0; c != cc.length; ++c) if(cc.charCodeAt(c) === 61) break;
  2799. q = cc.slice(0,c).trim();
  2800. while(cc.charCodeAt(c+1) == 32) ++c;
  2801. quot = ((eq=cc.charCodeAt(c+1)) == 34 || eq == 39) ? 1 : 0;
  2802. v = cc.slice(c+1+quot, cc.length-quot);
  2803. for(j=0;j!=q.length;++j) if(q.charCodeAt(j) === 58) break;
  2804. if(j===q.length) {
  2805. if(q.indexOf("_") > 0) q = q.slice(0, q.indexOf("_")); // from ods
  2806. z[q] = v;
  2807. if(!skip_LC) z[q.toLowerCase()] = v;
  2808. }
  2809. else {
  2810. var k = (j===5 && q.slice(0,5)==="xmlns"?"xmlns":"")+q.slice(j+1);
  2811. if(z[k] && q.slice(j-3,j) == "ext") continue; // from ods
  2812. z[k] = v;
  2813. if(!skip_LC) z[k.toLowerCase()] = v;
  2814. }
  2815. }
  2816. return z;
  2817. }
  2818. function strip_ns(x) { return x.replace(nsregex2, "<$1"); }
  2819. var encodings = {
  2820. '&quot;': '"',
  2821. '&apos;': "'",
  2822. '&gt;': '>',
  2823. '&lt;': '<',
  2824. '&amp;': '&'
  2825. };
  2826. var rencoding = evert(encodings);
  2827. //var rencstr = "&<>'\"".split("");
  2828. // TODO: CP remap (need to read file version to determine OS)
  2829. var unescapexml = (function() {
  2830. /* 22.4.2.4 bstr (Basic String) */
  2831. var encregex = /&(?:quot|apos|gt|lt|amp|#x?([\da-fA-F]+));/ig, coderegex = /_x([\da-fA-F]{4})_/ig;
  2832. return function unescapexml(text) {
  2833. var s = text + '', i = s.indexOf("<![CDATA[");
  2834. if(i == -1) return s.replace(encregex, function($$, $1) { return encodings[$$]||String.fromCharCode(parseInt($1,$$.indexOf("x")>-1?16:10))||$$; }).replace(coderegex,function(m,c) {return String.fromCharCode(parseInt(c,16));});
  2835. var j = s.indexOf("]]>");
  2836. return unescapexml(s.slice(0, i)) + s.slice(i+9,j) + unescapexml(s.slice(j+3));
  2837. };
  2838. })();
  2839. var decregex=/[&<>'"]/g, charegex = /[\u0000-\u0008\u000b-\u001f]/g;
  2840. function escapexml(text){
  2841. var s = text + '';
  2842. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(charegex,function(s) { return "_x" + ("000"+s.charCodeAt(0).toString(16)).slice(-4) + "_";});
  2843. }
  2844. function escapexmltag(text){ return escapexml(text).replace(/ /g,"_x0020_"); }
  2845. var htmlcharegex = /[\u0000-\u001f]/g;
  2846. function escapehtml(text){
  2847. var s = text + '';
  2848. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(/\n/g, "<br/>").replace(htmlcharegex,function(s) { return "&#x" + ("000"+s.charCodeAt(0).toString(16)).slice(-4) + ";"; });
  2849. }
  2850. function escapexlml(text){
  2851. var s = text + '';
  2852. return s.replace(decregex, function(y) { return rencoding[y]; }).replace(htmlcharegex,function(s) { return "&#x" + (s.charCodeAt(0).toString(16)).toUpperCase() + ";"; });
  2853. }
  2854. /* TODO: handle codepages */
  2855. var xlml_fixstr = (function() {
  2856. var entregex = /&#(\d+);/g;
  2857. function entrepl($$,$1) { return String.fromCharCode(parseInt($1,10)); }
  2858. return function xlml_fixstr(str) { return str.replace(entregex,entrepl); };
  2859. })();
  2860. var xlml_unfixstr = (function() {
  2861. return function xlml_unfixstr(str) { return str.replace(/(\r\n|[\r\n])/g,"\&#10;"); };
  2862. })();
  2863. function parsexmlbool(value) {
  2864. switch(value) {
  2865. case 1: case true: case '1': case 'true': case 'TRUE': return true;
  2866. /* case '0': case 'false': case 'FALSE':*/
  2867. default: return false;
  2868. }
  2869. }
  2870. var utf8read = function utf8reada(orig) {
  2871. var out = "", i = 0, c = 0, d = 0, e = 0, f = 0, w = 0;
  2872. while (i < orig.length) {
  2873. c = orig.charCodeAt(i++);
  2874. if (c < 128) { out += String.fromCharCode(c); continue; }
  2875. d = orig.charCodeAt(i++);
  2876. if (c>191 && c<224) { f = ((c & 31) << 6); f |= (d & 63); out += String.fromCharCode(f); continue; }
  2877. e = orig.charCodeAt(i++);
  2878. if (c < 240) { out += String.fromCharCode(((c & 15) << 12) | ((d & 63) << 6) | (e & 63)); continue; }
  2879. f = orig.charCodeAt(i++);
  2880. w = (((c & 7) << 18) | ((d & 63) << 12) | ((e & 63) << 6) | (f & 63))-65536;
  2881. out += String.fromCharCode(0xD800 + ((w>>>10)&1023));
  2882. out += String.fromCharCode(0xDC00 + (w&1023));
  2883. }
  2884. return out;
  2885. };
  2886. var utf8write = function(orig) {
  2887. var out = [], i = 0, c = 0, d = 0;
  2888. while(i < orig.length) {
  2889. c = orig.charCodeAt(i++);
  2890. switch(true) {
  2891. case c < 128: out.push(String.fromCharCode(c)); break;
  2892. case c < 2048:
  2893. out.push(String.fromCharCode(192 + (c >> 6)));
  2894. out.push(String.fromCharCode(128 + (c & 63)));
  2895. break;
  2896. case c >= 55296 && c < 57344:
  2897. c -= 55296; d = orig.charCodeAt(i++) - 56320 + (c<<10);
  2898. out.push(String.fromCharCode(240 + ((d >>18) & 7)));
  2899. out.push(String.fromCharCode(144 + ((d >>12) & 63)));
  2900. out.push(String.fromCharCode(128 + ((d >> 6) & 63)));
  2901. out.push(String.fromCharCode(128 + (d & 63)));
  2902. break;
  2903. default:
  2904. out.push(String.fromCharCode(224 + (c >> 12)));
  2905. out.push(String.fromCharCode(128 + ((c >> 6) & 63)));
  2906. out.push(String.fromCharCode(128 + (c & 63)));
  2907. }
  2908. }
  2909. return out.join("");
  2910. };
  2911. if(has_buf) {
  2912. var utf8readb = function utf8readb(data) {
  2913. var out = Buffer.alloc(2*data.length), w, i, j = 1, k = 0, ww=0, c;
  2914. for(i = 0; i < data.length; i+=j) {
  2915. j = 1;
  2916. if((c=data.charCodeAt(i)) < 128) w = c;
  2917. else if(c < 224) { w = (c&31)*64+(data.charCodeAt(i+1)&63); j=2; }
  2918. else if(c < 240) { w=(c&15)*4096+(data.charCodeAt(i+1)&63)*64+(data.charCodeAt(i+2)&63); j=3; }
  2919. else { j = 4;
  2920. w = (c & 7)*262144+(data.charCodeAt(i+1)&63)*4096+(data.charCodeAt(i+2)&63)*64+(data.charCodeAt(i+3)&63);
  2921. w -= 65536; ww = 0xD800 + ((w>>>10)&1023); w = 0xDC00 + (w&1023);
  2922. }
  2923. if(ww !== 0) { out[k++] = ww&255; out[k++] = ww>>>8; ww = 0; }
  2924. out[k++] = w%256; out[k++] = w>>>8;
  2925. }
  2926. return out.slice(0,k).toString('ucs2');
  2927. };
  2928. var corpus = "foo bar baz\u00e2\u0098\u0083\u00f0\u009f\u008d\u00a3";
  2929. if(utf8read(corpus) == utf8readb(corpus)) utf8read = utf8readb;
  2930. var utf8readc = function utf8readc(data) { return Buffer_from(data, 'binary').toString('utf8'); };
  2931. if(utf8read(corpus) == utf8readc(corpus)) utf8read = utf8readc;
  2932. utf8write = function(data) { return Buffer_from(data, 'utf8').toString("binary"); };
  2933. }
  2934. // matches <foo>...</foo> extracts content
  2935. var matchtag = (function() {
  2936. var mtcache = ({});
  2937. return function matchtag(f,g) {
  2938. var t = f+"|"+(g||"");
  2939. if(mtcache[t]) return mtcache[t];
  2940. return (mtcache[t] = new RegExp('<(?:\\w+:)?'+f+'(?: xml:space="preserve")?(?:[^>]*)>([\\s\\S]*?)</(?:\\w+:)?'+f+'>',((g||""))));
  2941. };
  2942. })();
  2943. var htmldecode = (function() {
  2944. var entities = [
  2945. ['nbsp', ' '], ['middot', '·'],
  2946. ['quot', '"'], ['apos', "'"], ['gt', '>'], ['lt', '<'], ['amp', '&']
  2947. ].map(function(x) { return [new RegExp('&' + x[0] + ';', "ig"), x[1]]; });
  2948. return function htmldecode(str) {
  2949. var o = str
  2950. // Remove new lines and spaces from start of content
  2951. .replace(/^[\t\n\r ]+/, "")
  2952. // Remove new lines and spaces from end of content
  2953. .replace(/[\t\n\r ]+$/,"")
  2954. // Added line which removes any white space characters after and before html tags
  2955. .replace(/>\s+/g,">").replace(/\s+</g,"<")
  2956. // Replace remaining new lines and spaces with space
  2957. .replace(/[\t\n\r ]+/g, " ")
  2958. // Replace <br> tags with new lines
  2959. .replace(/<\s*[bB][rR]\s*\/?>/g,"\n")
  2960. // Strip HTML elements
  2961. .replace(/<[^>]*>/g,"");
  2962. for(var i = 0; i < entities.length; ++i) o = o.replace(entities[i][0], entities[i][1]);
  2963. return o;
  2964. };
  2965. })();
  2966. var vtregex = (function(){ var vt_cache = {};
  2967. return function vt_regex(bt) {
  2968. if(vt_cache[bt] !== undefined) return vt_cache[bt];
  2969. return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
  2970. };})();
  2971. var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
  2972. function parseVector(data, opts) {
  2973. var h = parsexmltag(data);
  2974. var matches = data.match(vtregex(h.baseType))||[];
  2975. var res = [];
  2976. if(matches.length != h.size) {
  2977. if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
  2978. return res;
  2979. }
  2980. matches.forEach(function(x) {
  2981. var v = x.replace(vtvregex,"").match(vtmregex);
  2982. if(v) res.push({v:utf8read(v[2]), t:v[1]});
  2983. });
  2984. return res;
  2985. }
  2986. var wtregex = /(^\s|\s$|\n)/;
  2987. function writetag(f,g) { return '<' + f + (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f + '>'; }
  2988. function wxt_helper(h) { return keys(h).map(function(k) { return " " + k + '="' + h[k] + '"';}).join(""); }
  2989. function writextag(f,g,h) { return '<' + f + ((h != null) ? wxt_helper(h) : "") + ((g != null) ? (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f : "/") + '>';}
  2990. function write_w3cdtf(d, t) { try { return d.toISOString().replace(/\.\d*/,""); } catch(e) { if(t) throw e; } return ""; }
  2991. function write_vt(s, xlsx) {
  2992. switch(typeof s) {
  2993. case 'string':
  2994. var o = writextag('vt:lpwstr', escapexml(s));
  2995. if(xlsx) o = o.replace(/&quot;/g, "_x0022_");
  2996. return o;
  2997. case 'number': return writextag((s|0)==s?'vt:i4':'vt:r8', escapexml(String(s)));
  2998. case 'boolean': return writextag('vt:bool',s?'true':'false');
  2999. }
  3000. if(s instanceof Date) return writextag('vt:filetime', write_w3cdtf(s));
  3001. throw new Error("Unable to serialize " + s);
  3002. }
  3003. var XMLNS = ({
  3004. 'dc': 'http://purl.org/dc/elements/1.1/',
  3005. 'dcterms': 'http://purl.org/dc/terms/',
  3006. 'dcmitype': 'http://purl.org/dc/dcmitype/',
  3007. 'mx': 'http://schemas.microsoft.com/office/mac/excel/2008/main',
  3008. 'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
  3009. 'sjs': 'http://schemas.openxmlformats.org/package/2006/sheetjs/core-properties',
  3010. 'vt': 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes',
  3011. 'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
  3012. 'xsd': 'http://www.w3.org/2001/XMLSchema'
  3013. });
  3014. XMLNS.main = [
  3015. 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
  3016. 'http://purl.oclc.org/ooxml/spreadsheetml/main',
  3017. 'http://schemas.microsoft.com/office/excel/2006/main',
  3018. 'http://schemas.microsoft.com/office/excel/2006/2'
  3019. ];
  3020. var XLMLNS = ({
  3021. 'o': 'urn:schemas-microsoft-com:office:office',
  3022. 'x': 'urn:schemas-microsoft-com:office:excel',
  3023. 'ss': 'urn:schemas-microsoft-com:office:spreadsheet',
  3024. 'dt': 'uuid:C2F41010-65B3-11d1-A29F-00AA00C14882',
  3025. 'mv': 'http://macVmlSchemaUri',
  3026. 'v': 'urn:schemas-microsoft-com:vml',
  3027. 'html': 'http://www.w3.org/TR/REC-html40'
  3028. });
  3029. function read_double_le(b, idx) {
  3030. var s = 1 - 2 * (b[idx + 7] >>> 7);
  3031. var e = ((b[idx + 7] & 0x7f) << 4) + ((b[idx + 6] >>> 4) & 0x0f);
  3032. var m = (b[idx+6]&0x0f);
  3033. for(var i = 5; i >= 0; --i) m = m * 256 + b[idx + i];
  3034. if(e == 0x7ff) return m == 0 ? (s * Infinity) : NaN;
  3035. if(e == 0) e = -1022;
  3036. else { e -= 1023; m += Math.pow(2,52); }
  3037. return s * Math.pow(2, e - 52) * m;
  3038. }
  3039. function write_double_le(b, v, idx) {
  3040. var bs = ((((v < 0) || (1/v == -Infinity)) ? 1 : 0) << 7), e = 0, m = 0;
  3041. var av = bs ? (-v) : v;
  3042. if(!isFinite(av)) { e = 0x7ff; m = isNaN(v) ? 0x6969 : 0; }
  3043. else if(av == 0) e = m = 0;
  3044. else {
  3045. e = Math.floor(Math.log(av) / Math.LN2);
  3046. m = av * Math.pow(2, 52 - e);
  3047. if((e <= -1023) && (!isFinite(m) || (m < Math.pow(2,52)))) { e = -1022; }
  3048. else { m -= Math.pow(2,52); e+=1023; }
  3049. }
  3050. for(var i = 0; i <= 5; ++i, m/=256) b[idx + i] = m & 0xff;
  3051. b[idx + 6] = ((e & 0x0f) << 4) | (m & 0xf);
  3052. b[idx + 7] = (e >> 4) | bs;
  3053. }
  3054. var __toBuffer = function(bufs) { var x=[],w=10240; for(var i=0;i<bufs[0].length;++i) if(bufs[0][i]) for(var j=0,L=bufs[0][i].length;j<L;j+=w) x.push.apply(x, bufs[0][i].slice(j,j+w)); return x; };
  3055. var ___toBuffer = __toBuffer;
  3056. var __utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,''); };
  3057. var ___utf16le = __utf16le;
  3058. var __hexlify = function(b,s,l) { var ss=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
  3059. var ___hexlify = __hexlify;
  3060. var __utf8 = function(b,s,e) { var ss=[]; for(var i=s; i<e; i++) ss.push(String.fromCharCode(__readUInt8(b,i))); return ss.join(""); };
  3061. var ___utf8 = __utf8;
  3062. var __lpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  3063. var ___lpstr = __lpstr;
  3064. var __cpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  3065. var ___cpstr = __cpstr;
  3066. var __lpwstr = function(b,i) { var len = 2*__readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
  3067. var ___lpwstr = __lpwstr;
  3068. var __lpp4, ___lpp4;
  3069. __lpp4 = ___lpp4 = function lpp4_(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf16le(b, i+4,i+4+len) : "";};
  3070. var __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len) : "";};
  3071. var ___8lpp4 = __8lpp4;
  3072. var __double, ___double;
  3073. __double = ___double = function(b, idx) { return read_double_le(b, idx);};
  3074. var is_buf = function is_buf_a(a) { return Array.isArray(a); };
  3075. if(has_buf) {
  3076. __utf16le = function(b,s,e) { if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e); return b.toString('utf16le',s,e).replace(chr0,'')/*.replace(chr1,'!')*/; };
  3077. __hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
  3078. __lpstr = function lpstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpstr(b, i); var len = b.readUInt32LE(i); return len > 0 ? b.toString('utf8',i+4,i+4+len-1) : "";};
  3079. __cpstr = function cpstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___cpstr(b, i); var len = b.readUInt32LE(i); return len > 0 ? b.toString('utf8',i+4,i+4+len-1) : "";};
  3080. __lpwstr = function lpwstr_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpwstr(b, i); var len = 2*b.readUInt32LE(i); return b.toString('utf16le',i+4,i+4+len-1);};
  3081. __lpp4 = function lpp4_b(b, i) { if(!Buffer.isBuffer(b)) return ___lpp4(b, i); var len = b.readUInt32LE(i); return b.toString('utf16le',i+4,i+4+len);};
  3082. __8lpp4 = function lpp4_8b(b, i) { if(!Buffer.isBuffer(b)) return ___8lpp4(b, i); var len = b.readUInt32LE(i); return b.toString('utf8',i+4,i+4+len);};
  3083. __utf8 = function utf8_b(b, s, e) { return (Buffer.isBuffer(b)) ? b.toString('utf8',s,e) : ___utf8(b,s,e); };
  3084. __toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
  3085. bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
  3086. __double = function double_(b, i) { if(Buffer.isBuffer(b)) return b.readDoubleLE(i); return ___double(b,i); };
  3087. is_buf = function is_buf_b(a) { return Buffer.isBuffer(a) || Array.isArray(a); };
  3088. }
  3089. /* from js-xls */
  3090. if(typeof cptable !== 'undefined') {
  3091. __utf16le = function(b,s,e) { return cptable.utils.decode(1200, b.slice(s,e)).replace(chr0, ''); };
  3092. __utf8 = function(b,s,e) { return cptable.utils.decode(65001, b.slice(s,e)); };
  3093. __lpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(current_ansi, b.slice(i+4, i+4+len-1)) : "";};
  3094. __cpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(current_codepage, b.slice(i+4, i+4+len-1)) : "";};
  3095. __lpwstr = function(b,i) { var len = 2*__readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len-1)) : "";};
  3096. __lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len)) : "";};
  3097. __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(65001, b.slice(i+4,i+4+len)) : "";};
  3098. }
  3099. var __readUInt8 = function(b, idx) { return b[idx]; };
  3100. var __readUInt16LE = function(b, idx) { return (b[idx+1]*(1<<8))+b[idx]; };
  3101. var __readInt16LE = function(b, idx) { var u = (b[idx+1]*(1<<8))+b[idx]; return (u < 0x8000) ? u : ((0xffff - u + 1) * -1); };
  3102. var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
  3103. var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)|(b[idx+2]<<16)|(b[idx+1]<<8)|b[idx]; };
  3104. var __readInt32BE = function(b, idx) { return (b[idx]<<24)|(b[idx+1]<<16)|(b[idx+2]<<8)|b[idx+3]; };
  3105. function ReadShift(size, t) {
  3106. var o="", oI, oR, oo=[], w, vv, i, loc;
  3107. switch(t) {
  3108. case 'dbcs':
  3109. loc = this.l;
  3110. if(has_buf && Buffer.isBuffer(this)) o = this.slice(this.l, this.l+2*size).toString("utf16le");
  3111. else for(i = 0; i < size; ++i) { o+=String.fromCharCode(__readUInt16LE(this, loc)); loc+=2; }
  3112. size *= 2;
  3113. break;
  3114. case 'utf8': o = __utf8(this, this.l, this.l + size); break;
  3115. case 'utf16le': size *= 2; o = __utf16le(this, this.l, this.l + size); break;
  3116. case 'wstr':
  3117. if(typeof cptable !== 'undefined') o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l+2*size));
  3118. else return ReadShift.call(this, size, 'dbcs');
  3119. size = 2 * size; break;
  3120. /* [MS-OLEDS] 2.1.4 LengthPrefixedAnsiString */
  3121. case 'lpstr-ansi': o = __lpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
  3122. case 'lpstr-cp': o = __cpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
  3123. /* [MS-OLEDS] 2.1.5 LengthPrefixedUnicodeString */
  3124. case 'lpwstr': o = __lpwstr(this, this.l); size = 4 + 2 * __readUInt32LE(this, this.l); break;
  3125. /* [MS-OFFCRYPTO] 2.1.2 Length-Prefixed Padded Unicode String (UNICODE-LP-P4) */
  3126. case 'lpp4': size = 4 + __readUInt32LE(this, this.l); o = __lpp4(this, this.l); if(size & 0x02) size += 2; break;
  3127. /* [MS-OFFCRYPTO] 2.1.3 Length-Prefixed UTF-8 String (UTF-8-LP-P4) */
  3128. case '8lpp4': size = 4 + __readUInt32LE(this, this.l); o = __8lpp4(this, this.l); if(size & 0x03) size += 4 - (size & 0x03); break;
  3129. case 'cstr': size = 0; o = "";
  3130. while((w=__readUInt8(this, this.l + size++))!==0) oo.push(_getchar(w));
  3131. o = oo.join(""); break;
  3132. case '_wstr': size = 0; o = "";
  3133. while((w=__readUInt16LE(this,this.l +size))!==0){oo.push(_getchar(w));size+=2;}
  3134. size+=2; o = oo.join(""); break;
  3135. /* sbcs and dbcs support continue records in the SST way TODO codepages */
  3136. case 'dbcs-cont': o = ""; loc = this.l;
  3137. for(i = 0; i < size; ++i) {
  3138. if(this.lens && this.lens.indexOf(loc) !== -1) {
  3139. w = __readUInt8(this, loc);
  3140. this.l = loc + 1;
  3141. vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
  3142. return oo.join("") + vv;
  3143. }
  3144. oo.push(_getchar(__readUInt16LE(this, loc)));
  3145. loc+=2;
  3146. } o = oo.join(""); size *= 2; break;
  3147. case 'cpstr':
  3148. if(typeof cptable !== 'undefined') {
  3149. o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l + size));
  3150. break;
  3151. }
  3152. /* falls through */
  3153. case 'sbcs-cont': o = ""; loc = this.l;
  3154. for(i = 0; i != size; ++i) {
  3155. if(this.lens && this.lens.indexOf(loc) !== -1) {
  3156. w = __readUInt8(this, loc);
  3157. this.l = loc + 1;
  3158. vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
  3159. return oo.join("") + vv;
  3160. }
  3161. oo.push(_getchar(__readUInt8(this, loc)));
  3162. loc+=1;
  3163. } o = oo.join(""); break;
  3164. default:
  3165. switch(size) {
  3166. case 1: oI = __readUInt8(this, this.l); this.l++; return oI;
  3167. case 2: oI = (t === 'i' ? __readInt16LE : __readUInt16LE)(this, this.l); this.l += 2; return oI;
  3168. case 4: case -4:
  3169. if(t === 'i' || ((this[this.l+3] & 0x80)===0)) { oI = ((size > 0) ? __readInt32LE : __readInt32BE)(this, this.l); this.l += 4; return oI; }
  3170. else { oR = __readUInt32LE(this, this.l); this.l += 4; } return oR;
  3171. case 8: case -8:
  3172. if(t === 'f') {
  3173. if(size == 8) oR = __double(this, this.l);
  3174. else oR = __double([this[this.l+7],this[this.l+6],this[this.l+5],this[this.l+4],this[this.l+3],this[this.l+2],this[this.l+1],this[this.l+0]], 0);
  3175. this.l += 8; return oR;
  3176. } else size = 8;
  3177. /* falls through */
  3178. case 16: o = __hexlify(this, this.l, size); break;
  3179. }}
  3180. this.l+=size; return o;
  3181. }
  3182. var __writeUInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); b[idx+2] = ((val >>> 16) & 0xFF); b[idx+3] = ((val >>> 24) & 0xFF); };
  3183. var __writeInt32LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >> 8) & 0xFF); b[idx+2] = ((val >> 16) & 0xFF); b[idx+3] = ((val >> 24) & 0xFF); };
  3184. var __writeUInt16LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); };
  3185. function WriteShift(t, val, f) {
  3186. var size = 0, i = 0;
  3187. if(f === 'dbcs') {
  3188. for(i = 0; i != val.length; ++i) __writeUInt16LE(this, val.charCodeAt(i), this.l + 2 * i);
  3189. size = 2 * val.length;
  3190. } else if(f === 'sbcs') {
  3191. if(typeof cptable !== 'undefined' && current_ansi == 874) {
  3192. /* TODO: use tables directly, don't encode */
  3193. for(i = 0; i != val.length; ++i) {
  3194. var cppayload = cptable.utils.encode(current_ansi, val.charAt(i));
  3195. this[this.l + i] = cppayload[0];
  3196. }
  3197. } else {
  3198. val = val.replace(/[^\x00-\x7F]/g, "_");
  3199. for(i = 0; i != val.length; ++i) this[this.l + i] = (val.charCodeAt(i) & 0xFF);
  3200. }
  3201. size = val.length;
  3202. } else if(f === 'hex') {
  3203. for(; i < t; ++i) {
  3204. this[this.l++] = (parseInt(val.slice(2*i, 2*i+2), 16)||0);
  3205. } return this;
  3206. } else if(f === 'utf16le') {
  3207. var end = Math.min(this.l + t, this.length);
  3208. for(i = 0; i < Math.min(val.length, t); ++i) {
  3209. var cc = val.charCodeAt(i);
  3210. this[this.l++] = (cc & 0xff);
  3211. this[this.l++] = (cc >> 8);
  3212. }
  3213. while(this.l < end) this[this.l++] = 0;
  3214. return this;
  3215. } else switch(t) {
  3216. case 1: size = 1; this[this.l] = val&0xFF; break;
  3217. case 2: size = 2; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; break;
  3218. case 3: size = 3; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; val >>>= 8; this[this.l+2] = val&0xFF; break;
  3219. case 4: size = 4; __writeUInt32LE(this, val, this.l); break;
  3220. case 8: size = 8; if(f === 'f') { write_double_le(this, val, this.l); break; }
  3221. /* falls through */
  3222. case 16: break;
  3223. case -4: size = 4; __writeInt32LE(this, val, this.l); break;
  3224. }
  3225. this.l += size; return this;
  3226. }
  3227. function CheckField(hexstr, fld) {
  3228. var m = __hexlify(this,this.l,hexstr.length>>1);
  3229. if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
  3230. this.l += hexstr.length>>1;
  3231. }
  3232. function prep_blob(blob, pos) {
  3233. blob.l = pos;
  3234. blob.read_shift = ReadShift;
  3235. blob.chk = CheckField;
  3236. blob.write_shift = WriteShift;
  3237. }
  3238. function parsenoop(blob, length) { blob.l += length; }
  3239. function new_buf(sz) {
  3240. var o = new_raw_buf(sz);
  3241. prep_blob(o, 0);
  3242. return o;
  3243. }
  3244. /* [MS-XLSB] 2.1.4 Record */
  3245. function recordhopper(data, cb, opts) {
  3246. if(!data) return;
  3247. var tmpbyte, cntbyte, length;
  3248. prep_blob(data, data.l || 0);
  3249. var L = data.length, RT = 0, tgt = 0;
  3250. while(data.l < L) {
  3251. RT = data.read_shift(1);
  3252. if(RT & 0x80) RT = (RT & 0x7F) + ((data.read_shift(1) & 0x7F)<<7);
  3253. var R = XLSBRecordEnum[RT] || XLSBRecordEnum[0xFFFF];
  3254. tmpbyte = data.read_shift(1);
  3255. length = tmpbyte & 0x7F;
  3256. for(cntbyte = 1; cntbyte <4 && (tmpbyte & 0x80); ++cntbyte) length += ((tmpbyte = data.read_shift(1)) & 0x7F)<<(7*cntbyte);
  3257. tgt = data.l + length;
  3258. var d = (R.f||parsenoop)(data, length, opts);
  3259. data.l = tgt;
  3260. if(cb(d, R.n, RT)) return;
  3261. }
  3262. }
  3263. /* control buffer usage for fixed-length buffers */
  3264. function buf_array() {
  3265. var bufs = [], blksz = has_buf ? 256 : 2048;
  3266. var newblk = function ba_newblk(sz) {
  3267. var o = (new_buf(sz));
  3268. prep_blob(o, 0);
  3269. return o;
  3270. };
  3271. var curbuf = newblk(blksz);
  3272. var endbuf = function ba_endbuf() {
  3273. if(!curbuf) return;
  3274. if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
  3275. if(curbuf.length > 0) bufs.push(curbuf);
  3276. curbuf = null;
  3277. };
  3278. var next = function ba_next(sz) {
  3279. if(curbuf && (sz < (curbuf.length - curbuf.l))) return curbuf;
  3280. endbuf();
  3281. return (curbuf = newblk(Math.max(sz+1, blksz)));
  3282. };
  3283. var end = function ba_end() {
  3284. endbuf();
  3285. return __toBuffer([bufs]);
  3286. };
  3287. var push = function ba_push(buf) { endbuf(); curbuf = buf; if(curbuf.l == null) curbuf.l = curbuf.length; next(blksz); };
  3288. return ({ next:next, push:push, end:end, _bufs:bufs });
  3289. }
  3290. function write_record(ba, type, payload, length) {
  3291. var t = +XLSBRE[type], l;
  3292. if(isNaN(t)) return; // TODO: throw something here?
  3293. if(!length) length = XLSBRecordEnum[t].p || (payload||[]).length || 0;
  3294. l = 1 + (t >= 0x80 ? 1 : 0) + 1/* + length*/;
  3295. if(length >= 0x80) ++l; if(length >= 0x4000) ++l; if(length >= 0x200000) ++l;
  3296. var o = ba.next(l);
  3297. if(t <= 0x7F) o.write_shift(1, t);
  3298. else {
  3299. o.write_shift(1, (t & 0x7F) + 0x80);
  3300. o.write_shift(1, (t >> 7));
  3301. }
  3302. for(var i = 0; i != 4; ++i) {
  3303. if(length >= 0x80) { o.write_shift(1, (length & 0x7F)+0x80); length >>= 7; }
  3304. else { o.write_shift(1, length); break; }
  3305. }
  3306. if(length > 0 && is_buf(payload)) ba.push(payload);
  3307. }
  3308. /* XLS ranges enforced */
  3309. function shift_cell_xls(cell, tgt, opts) {
  3310. var out = dup(cell);
  3311. if(tgt.s) {
  3312. if(out.cRel) out.c += tgt.s.c;
  3313. if(out.rRel) out.r += tgt.s.r;
  3314. } else {
  3315. if(out.cRel) out.c += tgt.c;
  3316. if(out.rRel) out.r += tgt.r;
  3317. }
  3318. if(!opts || opts.biff < 12) {
  3319. while(out.c >= 0x100) out.c -= 0x100;
  3320. while(out.r >= 0x10000) out.r -= 0x10000;
  3321. }
  3322. return out;
  3323. }
  3324. function shift_range_xls(cell, range, opts) {
  3325. var out = dup(cell);
  3326. out.s = shift_cell_xls(out.s, range.s, opts);
  3327. out.e = shift_cell_xls(out.e, range.s, opts);
  3328. return out;
  3329. }
  3330. function encode_cell_xls(c, biff) {
  3331. if(c.cRel && c.c < 0) { c = dup(c); while(c.c < 0) c.c += (biff > 8) ? 0x4000 : 0x100; }
  3332. if(c.rRel && c.r < 0) { c = dup(c); while(c.r < 0) c.r += (biff > 8) ? 0x100000 : ((biff > 5) ? 0x10000 : 0x4000); }
  3333. var s = encode_cell(c);
  3334. if(!c.cRel && c.cRel != null) s = fix_col(s);
  3335. if(!c.rRel && c.rRel != null) s = fix_row(s);
  3336. return s;
  3337. }
  3338. function encode_range_xls(r, opts) {
  3339. if(r.s.r == 0 && !r.s.rRel) {
  3340. if(r.e.r == (opts.biff >= 12 ? 0xFFFFF : (opts.biff >= 8 ? 0x10000 : 0x4000)) && !r.e.rRel) {
  3341. return (r.s.cRel ? "" : "$") + encode_col(r.s.c) + ":" + (r.e.cRel ? "" : "$") + encode_col(r.e.c);
  3342. }
  3343. }
  3344. if(r.s.c == 0 && !r.s.cRel) {
  3345. if(r.e.c == (opts.biff >= 12 ? 0x3FFF : 0xFF) && !r.e.cRel) {
  3346. return (r.s.rRel ? "" : "$") + encode_row(r.s.r) + ":" + (r.e.rRel ? "" : "$") + encode_row(r.e.r);
  3347. }
  3348. }
  3349. return encode_cell_xls(r.s, opts.biff) + ":" + encode_cell_xls(r.e, opts.biff);
  3350. }
  3351. var OFFCRYPTO = {};
  3352. var make_offcrypto = function(O, _crypto) {
  3353. var crypto;
  3354. if(typeof _crypto !== 'undefined') crypto = _crypto;
  3355. else if(typeof require !== 'undefined') {
  3356. try { crypto = require('crypto'); }
  3357. catch(e) { crypto = null; }
  3358. }
  3359. O.rc4 = function(key, data) {
  3360. var S = new Array(256);
  3361. var c = 0, i = 0, j = 0, t = 0;
  3362. for(i = 0; i != 256; ++i) S[i] = i;
  3363. for(i = 0; i != 256; ++i) {
  3364. j = (j + S[i] + (key[i%key.length]).charCodeAt(0))&255;
  3365. t = S[i]; S[i] = S[j]; S[j] = t;
  3366. }
  3367. // $FlowIgnore
  3368. i = j = 0; var out = new_raw_buf(data.length);
  3369. for(c = 0; c != data.length; ++c) {
  3370. i = (i + 1)&255;
  3371. j = (j + S[i])%256;
  3372. t = S[i]; S[i] = S[j]; S[j] = t;
  3373. out[c] = (data[c] ^ S[(S[i]+S[j])&255]);
  3374. }
  3375. return out;
  3376. };
  3377. O.md5 = function(hex) {
  3378. if(!crypto) throw new Error("Unsupported crypto");
  3379. return crypto.createHash('md5').update(hex).digest('hex');
  3380. };
  3381. };
  3382. /*global crypto:true */
  3383. make_offcrypto(OFFCRYPTO, typeof crypto !== "undefined" ? crypto : undefined);
  3384. function decode_row(rowstr) { return parseInt(unfix_row(rowstr),10) - 1; }
  3385. function encode_row(row) { return "" + (row + 1); }
  3386. function fix_row(cstr) { return cstr.replace(/([A-Z]|^)(\d+)$/,"$1$$$2"); }
  3387. function unfix_row(cstr) { return cstr.replace(/\$(\d+)$/,"$1"); }
  3388. function decode_col(colstr) { var c = unfix_col(colstr), d = 0, i = 0; for(; i !== c.length; ++i) d = 26*d + c.charCodeAt(i) - 64; return d - 1; }
  3389. function encode_col(col) { if(col < 0) throw new Error("invalid column " + col); var s=""; for(++col; col; col=Math.floor((col-1)/26)) s = String.fromCharCode(((col-1)%26) + 65) + s; return s; }
  3390. function fix_col(cstr) { return cstr.replace(/^([A-Z])/,"$$$1"); }
  3391. function unfix_col(cstr) { return cstr.replace(/^\$([A-Z])/,"$1"); }
  3392. function split_cell(cstr) { return cstr.replace(/(\$?[A-Z]*)(\$?\d*)/,"$1,$2").split(","); }
  3393. //function decode_cell(cstr) { var splt = split_cell(cstr); return { c:decode_col(splt[0]), r:decode_row(splt[1]) }; }
  3394. function decode_cell(cstr) {
  3395. var R = 0, C = 0;
  3396. for(var i = 0; i < cstr.length; ++i) {
  3397. var cc = cstr.charCodeAt(i);
  3398. if(cc >= 48 && cc <= 57) R = 10 * R + (cc - 48);
  3399. else if(cc >= 65 && cc <= 90) C = 26 * C + (cc - 64);
  3400. }
  3401. return { c: C - 1, r:R - 1 };
  3402. }
  3403. //function encode_cell(cell) { return encode_col(cell.c) + encode_row(cell.r); }
  3404. function encode_cell(cell) {
  3405. var col = cell.c + 1;
  3406. var s="";
  3407. for(; col; col=((col-1)/26)|0) s = String.fromCharCode(((col-1)%26) + 65) + s;
  3408. return s + (cell.r + 1);
  3409. }
  3410. function decode_range(range) {
  3411. var idx = range.indexOf(":");
  3412. if(idx == -1) return { s: decode_cell(range), e: decode_cell(range) };
  3413. return { s: decode_cell(range.slice(0, idx)), e: decode_cell(range.slice(idx + 1)) };
  3414. }
  3415. function encode_range(cs,ce) {
  3416. if(typeof ce === 'undefined' || typeof ce === 'number') {
  3417. return encode_range(cs.s, cs.e);
  3418. }
  3419. if(typeof cs !== 'string') cs = encode_cell((cs));
  3420. if(typeof ce !== 'string') ce = encode_cell((ce));
  3421. return cs == ce ? cs : cs + ":" + ce;
  3422. }
  3423. function safe_decode_range(range) {
  3424. var o = {s:{c:0,r:0},e:{c:0,r:0}};
  3425. var idx = 0, i = 0, cc = 0;
  3426. var len = range.length;
  3427. for(idx = 0; i < len; ++i) {
  3428. if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
  3429. idx = 26*idx + cc;
  3430. }
  3431. o.s.c = --idx;
  3432. for(idx = 0; i < len; ++i) {
  3433. if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
  3434. idx = 10*idx + cc;
  3435. }
  3436. o.s.r = --idx;
  3437. if(i === len || range.charCodeAt(++i) === 58) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }
  3438. for(idx = 0; i != len; ++i) {
  3439. if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
  3440. idx = 26*idx + cc;
  3441. }
  3442. o.e.c = --idx;
  3443. for(idx = 0; i != len; ++i) {
  3444. if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
  3445. idx = 10*idx + cc;
  3446. }
  3447. o.e.r = --idx;
  3448. return o;
  3449. }
  3450. function safe_format_cell(cell, v) {
  3451. var q = (cell.t == 'd' && v instanceof Date);
  3452. if(cell.z != null) try { return (cell.w = SSF.format(cell.z, q ? datenum(v) : v)); } catch(e) { }
  3453. try { return (cell.w = SSF.format((cell.XF||{}).numFmtId||(q ? 14 : 0), q ? datenum(v) : v)); } catch(e) { return ''+v; }
  3454. }
  3455. function format_cell(cell, v, o) {
  3456. if(cell == null || cell.t == null || cell.t == 'z') return "";
  3457. if(cell.w !== undefined) return cell.w;
  3458. if(cell.t == 'd' && !cell.z && o && o.dateNF) cell.z = o.dateNF;
  3459. if(v == undefined) return safe_format_cell(cell, cell.v);
  3460. return safe_format_cell(cell, v);
  3461. }
  3462. function sheet_to_workbook(sheet, opts) {
  3463. var n = opts && opts.sheet ? opts.sheet : "Sheet1";
  3464. var sheets = {}; sheets[n] = sheet;
  3465. return { SheetNames: [n], Sheets: sheets };
  3466. }
  3467. function sheet_add_aoa(_ws, data, opts) {
  3468. var o = opts || {};
  3469. var dense = _ws ? Array.isArray(_ws) : o.dense;
  3470. if(DENSE != null && dense == null) dense = DENSE;
  3471. var ws = _ws || (dense ? ([]) : ({}));
  3472. var _R = 0, _C = 0;
  3473. if(ws && o.origin != null) {
  3474. if(typeof o.origin == 'number') _R = o.origin;
  3475. else {
  3476. var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
  3477. _R = _origin.r; _C = _origin.c;
  3478. }
  3479. if(!ws["!ref"]) ws["!ref"] = "A1:A1";
  3480. }
  3481. var range = ({s: {c:10000000, r:10000000}, e: {c:0, r:0}});
  3482. if(ws['!ref']) {
  3483. var _range = safe_decode_range(ws['!ref']);
  3484. range.s.c = _range.s.c;
  3485. range.s.r = _range.s.r;
  3486. range.e.c = Math.max(range.e.c, _range.e.c);
  3487. range.e.r = Math.max(range.e.r, _range.e.r);
  3488. if(_R == -1) range.e.r = _R = _range.e.r + 1;
  3489. }
  3490. for(var R = 0; R != data.length; ++R) {
  3491. if(!data[R]) continue;
  3492. if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays");
  3493. for(var C = 0; C != data[R].length; ++C) {
  3494. if(typeof data[R][C] === 'undefined') continue;
  3495. var cell = ({v: data[R][C] });
  3496. var __R = _R + R, __C = _C + C;
  3497. if(range.s.r > __R) range.s.r = __R;
  3498. if(range.s.c > __C) range.s.c = __C;
  3499. if(range.e.r < __R) range.e.r = __R;
  3500. if(range.e.c < __C) range.e.c = __C;
  3501. if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C];
  3502. else {
  3503. if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; }
  3504. if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.sheetStubs) continue; else cell.t = 'z'; }
  3505. else if(typeof cell.v === 'number') cell.t = 'n';
  3506. else if(typeof cell.v === 'boolean') cell.t = 'b';
  3507. else if(cell.v instanceof Date) {
  3508. cell.z = o.dateNF || SSF._table[14];
  3509. if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); }
  3510. else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); }
  3511. }
  3512. else cell.t = 's';
  3513. }
  3514. if(dense) {
  3515. if(!ws[__R]) ws[__R] = [];
  3516. if(ws[__R][__C] && ws[__R][__C].z) cell.z = ws[__R][__C].z;
  3517. ws[__R][__C] = cell;
  3518. } else {
  3519. var cell_ref = encode_cell(({c:__C,r:__R}));
  3520. if(ws[cell_ref] && ws[cell_ref].z) cell.z = ws[cell_ref].z;
  3521. ws[cell_ref] = cell;
  3522. }
  3523. }
  3524. }
  3525. if(range.s.c < 10000000) ws['!ref'] = encode_range(range);
  3526. return ws;
  3527. }
  3528. function aoa_to_sheet(data, opts) { return sheet_add_aoa(null, data, opts); }
  3529. function write_UInt32LE(x, o) {
  3530. if (!o) o = new_buf(4);
  3531. o.write_shift(4, x);
  3532. return o;
  3533. }
  3534. /* [MS-XLSB] 2.5.168 */
  3535. function parse_XLWideString(data) {
  3536. var cchCharacters = data.read_shift(4);
  3537. return cchCharacters === 0 ? "" : data.read_shift(cchCharacters, 'dbcs');
  3538. }
  3539. function write_XLWideString(data, o) {
  3540. var _null = false; if (o == null) { _null = true; o = new_buf(4 + 2 * data.length); }
  3541. o.write_shift(4, data.length);
  3542. if (data.length > 0) o.write_shift(0, data, 'dbcs');
  3543. return _null ? o.slice(0, o.l) : o;
  3544. }
  3545. /* [MS-XLSB] 2.5.91 */
  3546. //function parse_LPWideString(data) {
  3547. // var cchCharacters = data.read_shift(2);
  3548. // return cchCharacters === 0 ? "" : data.read_shift(cchCharacters, "utf16le");
  3549. //}
  3550. /* [MS-XLSB] 2.5.143 */
  3551. function parse_StrRun(data) {
  3552. return { ich: data.read_shift(2), ifnt: data.read_shift(2) };
  3553. }
  3554. function write_StrRun(run, o) {
  3555. if (!o) o = new_buf(4);
  3556. o.write_shift(2, run.ich || 0);
  3557. o.write_shift(2, run.ifnt || 0);
  3558. return o;
  3559. }
  3560. /* [MS-XLSB] 2.5.121 */
  3561. function parse_RichStr(data, length) {
  3562. var start = data.l;
  3563. var flags = data.read_shift(1);
  3564. var str = parse_XLWideString(data);
  3565. var rgsStrRun = [];
  3566. var z = ({ t: str, h: str });
  3567. if ((flags & 1) !== 0) { /* fRichStr */
  3568. /* TODO: formatted string */
  3569. var dwSizeStrRun = data.read_shift(4);
  3570. for (var i = 0; i != dwSizeStrRun; ++i) rgsStrRun.push(parse_StrRun(data));
  3571. z.r = rgsStrRun;
  3572. }
  3573. else z.r = [{ ich: 0, ifnt: 0 }];
  3574. //if((flags & 2) !== 0) { /* fExtStr */
  3575. // /* TODO: phonetic string */
  3576. //}
  3577. data.l = start + length;
  3578. return z;
  3579. }
  3580. function write_RichStr(str, o) {
  3581. /* TODO: formatted string */
  3582. var _null = false; if (o == null) { _null = true; o = new_buf(15 + 4 * str.t.length); }
  3583. o.write_shift(1, 0);
  3584. write_XLWideString(str.t, o);
  3585. return _null ? o.slice(0, o.l) : o;
  3586. }
  3587. /* [MS-XLSB] 2.4.328 BrtCommentText (RichStr w/1 run) */
  3588. var parse_BrtCommentText = parse_RichStr;
  3589. function write_BrtCommentText(str, o) {
  3590. /* TODO: formatted string */
  3591. var _null = false; if (o == null) { _null = true; o = new_buf(23 + 4 * str.t.length); }
  3592. o.write_shift(1, 1);
  3593. write_XLWideString(str.t, o);
  3594. o.write_shift(4, 1);
  3595. write_StrRun({ ich: 0, ifnt: 0 }, o);
  3596. return _null ? o.slice(0, o.l) : o;
  3597. }
  3598. /* [MS-XLSB] 2.5.9 */
  3599. function parse_XLSBCell(data) {
  3600. var col = data.read_shift(4);
  3601. var iStyleRef = data.read_shift(2);
  3602. iStyleRef += data.read_shift(1) << 16;
  3603. data.l++; //var fPhShow = data.read_shift(1);
  3604. return { c: col, iStyleRef: iStyleRef };
  3605. }
  3606. function write_XLSBCell(cell, o) {
  3607. if (o == null) o = new_buf(8);
  3608. o.write_shift(-4, cell.c);
  3609. o.write_shift(3, cell.iStyleRef || cell.s);
  3610. o.write_shift(1, 0); /* fPhShow */
  3611. return o;
  3612. }
  3613. /* [MS-XLSB] 2.5.21 */
  3614. var parse_XLSBCodeName = parse_XLWideString;
  3615. var write_XLSBCodeName = write_XLWideString;
  3616. /* [MS-XLSB] 2.5.166 */
  3617. function parse_XLNullableWideString(data) {
  3618. var cchCharacters = data.read_shift(4);
  3619. return cchCharacters === 0 || cchCharacters === 0xFFFFFFFF ? "" : data.read_shift(cchCharacters, 'dbcs');
  3620. }
  3621. function write_XLNullableWideString(data, o) {
  3622. var _null = false; if (o == null) { _null = true; o = new_buf(127); }
  3623. o.write_shift(4, data.length > 0 ? data.length : 0xFFFFFFFF);
  3624. if (data.length > 0) o.write_shift(0, data, 'dbcs');
  3625. return _null ? o.slice(0, o.l) : o;
  3626. }
  3627. /* [MS-XLSB] 2.5.165 */
  3628. var parse_XLNameWideString = parse_XLWideString;
  3629. //var write_XLNameWideString = write_XLWideString;
  3630. /* [MS-XLSB] 2.5.114 */
  3631. var parse_RelID = parse_XLNullableWideString;
  3632. var write_RelID = write_XLNullableWideString;
  3633. /* [MS-XLS] 2.5.217 ; [MS-XLSB] 2.5.122 */
  3634. function parse_RkNumber(data) {
  3635. var b = data.slice(data.l, data.l + 4);
  3636. var fX100 = (b[0] & 1), fInt = (b[0] & 2);
  3637. data.l += 4;
  3638. b[0] &= 0xFC; // b[0] &= ~3;
  3639. var RK = fInt === 0 ? __double([0, 0, 0, 0, b[0], b[1], b[2], b[3]], 0) : __readInt32LE(b, 0) >> 2;
  3640. return fX100 ? (RK / 100) : RK;
  3641. }
  3642. function write_RkNumber(data, o) {
  3643. if (o == null) o = new_buf(4);
  3644. var fX100 = 0, fInt = 0, d100 = data * 100;
  3645. if ((data == (data | 0)) && (data >= -(1 << 29)) && (data < (1 << 29))) { fInt = 1; }
  3646. else if ((d100 == (d100 | 0)) && (d100 >= -(1 << 29)) && (d100 < (1 << 29))) { fInt = 1; fX100 = 1; }
  3647. if (fInt) o.write_shift(-4, ((fX100 ? d100 : data) << 2) + (fX100 + 2));
  3648. else throw new Error("unsupported RkNumber " + data); // TODO
  3649. }
  3650. /* [MS-XLSB] 2.5.117 RfX */
  3651. function parse_RfX(data ) {
  3652. var cell = ({ s: {}, e: {} });
  3653. cell.s.r = data.read_shift(4);
  3654. cell.e.r = data.read_shift(4);
  3655. cell.s.c = data.read_shift(4);
  3656. cell.e.c = data.read_shift(4);
  3657. return cell;
  3658. }
  3659. function write_RfX(r, o) {
  3660. if (!o) o = new_buf(16);
  3661. o.write_shift(4, r.s.r);
  3662. o.write_shift(4, r.e.r);
  3663. o.write_shift(4, r.s.c);
  3664. o.write_shift(4, r.e.c);
  3665. return o;
  3666. }
  3667. /* [MS-XLSB] 2.5.153 UncheckedRfX */
  3668. var parse_UncheckedRfX = parse_RfX;
  3669. var write_UncheckedRfX = write_RfX;
  3670. /* [MS-XLSB] 2.5.155 UncheckedSqRfX */
  3671. //function parse_UncheckedSqRfX(data) {
  3672. // var cnt = data.read_shift(4);
  3673. // var out = [];
  3674. // for(var i = 0; i < cnt; ++i) {
  3675. // var rng = parse_UncheckedRfX(data);
  3676. // out.push(encode_range(rng));
  3677. // }
  3678. // return out.join(",");
  3679. //}
  3680. //function write_UncheckedSqRfX(sqrfx) {
  3681. // var parts = sqrfx.split(/\s*,\s*/);
  3682. // var o = new_buf(4); o.write_shift(4, parts.length);
  3683. // var out = [o];
  3684. // parts.forEach(function(rng) {
  3685. // out.push(write_UncheckedRfX(safe_decode_range(rng)));
  3686. // });
  3687. // return bconcat(out);
  3688. //}
  3689. /* [MS-XLS] 2.5.342 ; [MS-XLSB] 2.5.171 */
  3690. /* TODO: error checking, NaN and Infinity values are not valid Xnum */
  3691. function parse_Xnum(data) { return data.read_shift(8, 'f'); }
  3692. function write_Xnum(data, o) { return (o || new_buf(8)).write_shift(8, data, 'f'); }
  3693. /* [MS-XLSB] 2.4.324 BrtColor */
  3694. function parse_BrtColor(data) {
  3695. var out = {};
  3696. var d = data.read_shift(1);
  3697. //var fValidRGB = d & 1;
  3698. var xColorType = d >>> 1;
  3699. var index = data.read_shift(1);
  3700. var nTS = data.read_shift(2, 'i');
  3701. var bR = data.read_shift(1);
  3702. var bG = data.read_shift(1);
  3703. var bB = data.read_shift(1);
  3704. data.l++; //var bAlpha = data.read_shift(1);
  3705. switch (xColorType) {
  3706. case 0: out.auto = 1; break;
  3707. case 1:
  3708. out.index = index;
  3709. var icv = XLSIcv[index];
  3710. /* automatic pseudo index 81 */
  3711. if (icv) out.rgb = rgb2Hex(icv);
  3712. break;
  3713. case 2:
  3714. /* if(!fValidRGB) throw new Error("invalid"); */
  3715. out.rgb = rgb2Hex([bR, bG, bB]);
  3716. break;
  3717. case 3: out.theme = index; break;
  3718. }
  3719. if (nTS != 0) out.tint = nTS > 0 ? nTS / 32767 : nTS / 32768;
  3720. return out;
  3721. }
  3722. function write_BrtColor(color, o) {
  3723. if (!o) o = new_buf(8);
  3724. if (!color || color.auto) { o.write_shift(4, 0); o.write_shift(4, 0); return o; }
  3725. if (color.index != null) {
  3726. o.write_shift(1, 0x02);
  3727. o.write_shift(1, color.index);
  3728. } else if (color.theme != null) {
  3729. o.write_shift(1, 0x06);
  3730. o.write_shift(1, color.theme);
  3731. } else {
  3732. o.write_shift(1, 0x05);
  3733. o.write_shift(1, 0);
  3734. }
  3735. var nTS = color.tint || 0;
  3736. if (nTS > 0) nTS *= 32767;
  3737. else if (nTS < 0) nTS *= 32768;
  3738. o.write_shift(2, nTS);
  3739. if (!color.rgb || color.theme != null) {
  3740. o.write_shift(2, 0);
  3741. o.write_shift(1, 0);
  3742. o.write_shift(1, 0);
  3743. } else {
  3744. var rgb = (color.rgb || 'FFFFFF');
  3745. if (typeof rgb == 'number') rgb = ("000000" + rgb.toString(16)).slice(-6);
  3746. o.write_shift(1, parseInt(rgb.slice(0, 2), 16));
  3747. o.write_shift(1, parseInt(rgb.slice(2, 4), 16));
  3748. o.write_shift(1, parseInt(rgb.slice(4, 6), 16));
  3749. o.write_shift(1, 0xFF);
  3750. }
  3751. return o;
  3752. }
  3753. /* [MS-XLSB] 2.5.52 */
  3754. function parse_FontFlags(data) {
  3755. var d = data.read_shift(1);
  3756. data.l++;
  3757. var out = {
  3758. fBold: d & 0x01,
  3759. fItalic: d & 0x02,
  3760. fUnderline: d & 0x04,
  3761. fStrikeout: d & 0x08,
  3762. fOutline: d & 0x10,
  3763. fShadow: d & 0x20,
  3764. fCondense: d & 0x40,
  3765. fExtend: d & 0x80
  3766. };
  3767. return out;
  3768. }
  3769. function write_FontFlags(font, o) {
  3770. if (!o) o = new_buf(2);
  3771. var grbit =
  3772. (font.italic ? 0x02 : 0) |
  3773. (font.strike ? 0x08 : 0) |
  3774. (font.outline ? 0x10 : 0) |
  3775. (font.shadow ? 0x20 : 0) |
  3776. (font.condense ? 0x40 : 0) |
  3777. (font.extend ? 0x80 : 0);
  3778. o.write_shift(1, grbit);
  3779. o.write_shift(1, 0);
  3780. return o;
  3781. }
  3782. /* [MS-OLEDS] 2.3.1 and 2.3.2 */
  3783. function parse_ClipboardFormatOrString(o, w) {
  3784. // $FlowIgnore
  3785. var ClipFmt = { 2: "BITMAP", 3: "METAFILEPICT", 8: "DIB", 14: "ENHMETAFILE" };
  3786. var m = o.read_shift(4);
  3787. switch (m) {
  3788. case 0x00000000: return "";
  3789. case 0xffffffff: case 0xfffffffe: return ClipFmt[o.read_shift(4)] || "";
  3790. }
  3791. if (m > 0x190) throw new Error("Unsupported Clipboard: " + m.toString(16));
  3792. o.l -= 4;
  3793. return o.read_shift(0, w == 1 ? "lpstr" : "lpwstr");
  3794. }
  3795. function parse_ClipboardFormatOrAnsiString(o) { return parse_ClipboardFormatOrString(o, 1); }
  3796. function parse_ClipboardFormatOrUnicodeString(o) { return parse_ClipboardFormatOrString(o, 2); }
  3797. /* [MS-OLEPS] 2.2 PropertyType */
  3798. //var VT_EMPTY = 0x0000;
  3799. //var VT_NULL = 0x0001;
  3800. var VT_I2 = 0x0002;
  3801. var VT_I4 = 0x0003;
  3802. //var VT_R4 = 0x0004;
  3803. //var VT_R8 = 0x0005;
  3804. //var VT_CY = 0x0006;
  3805. //var VT_DATE = 0x0007;
  3806. //var VT_BSTR = 0x0008;
  3807. //var VT_ERROR = 0x000A;
  3808. var VT_BOOL = 0x000B;
  3809. var VT_VARIANT = 0x000C;
  3810. //var VT_DECIMAL = 0x000E;
  3811. //var VT_I1 = 0x0010;
  3812. //var VT_UI1 = 0x0011;
  3813. //var VT_UI2 = 0x0012;
  3814. var VT_UI4 = 0x0013;
  3815. //var VT_I8 = 0x0014;
  3816. //var VT_UI8 = 0x0015;
  3817. //var VT_INT = 0x0016;
  3818. //var VT_UINT = 0x0017;
  3819. var VT_LPSTR = 0x001E;
  3820. //var VT_LPWSTR = 0x001F;
  3821. var VT_FILETIME = 0x0040;
  3822. var VT_BLOB = 0x0041;
  3823. //var VT_STREAM = 0x0042;
  3824. //var VT_STORAGE = 0x0043;
  3825. //var VT_STREAMED_Object = 0x0044;
  3826. //var VT_STORED_Object = 0x0045;
  3827. //var VT_BLOB_Object = 0x0046;
  3828. var VT_CF = 0x0047;
  3829. //var VT_CLSID = 0x0048;
  3830. //var VT_VERSIONED_STREAM = 0x0049;
  3831. var VT_VECTOR = 0x1000;
  3832. //var VT_ARRAY = 0x2000;
  3833. var VT_STRING = 0x0050; // 2.3.3.1.11 VtString
  3834. var VT_USTR = 0x0051; // 2.3.3.1.12 VtUnalignedString
  3835. var VT_CUSTOM = [VT_STRING, VT_USTR];
  3836. /* [MS-OSHARED] 2.3.3.2.2.1 Document Summary Information PIDDSI */
  3837. var DocSummaryPIDDSI = {
  3838. 0x01: { n: 'CodePage', t: VT_I2 },
  3839. 0x02: { n: 'Category', t: VT_STRING },
  3840. 0x03: { n: 'PresentationFormat', t: VT_STRING },
  3841. 0x04: { n: 'ByteCount', t: VT_I4 },
  3842. 0x05: { n: 'LineCount', t: VT_I4 },
  3843. 0x06: { n: 'ParagraphCount', t: VT_I4 },
  3844. 0x07: { n: 'SlideCount', t: VT_I4 },
  3845. 0x08: { n: 'NoteCount', t: VT_I4 },
  3846. 0x09: { n: 'HiddenCount', t: VT_I4 },
  3847. 0x0a: { n: 'MultimediaClipCount', t: VT_I4 },
  3848. 0x0b: { n: 'ScaleCrop', t: VT_BOOL },
  3849. 0x0c: { n: 'HeadingPairs', t: VT_VECTOR | VT_VARIANT },
  3850. 0x0d: { n: 'TitlesOfParts', t: VT_VECTOR | VT_LPSTR },
  3851. 0x0e: { n: 'Manager', t: VT_STRING },
  3852. 0x0f: { n: 'Company', t: VT_STRING },
  3853. 0x10: { n: 'LinksUpToDate', t: VT_BOOL },
  3854. 0x11: { n: 'CharacterCount', t: VT_I4 },
  3855. 0x13: { n: 'SharedDoc', t: VT_BOOL },
  3856. 0x16: { n: 'HyperlinksChanged', t: VT_BOOL },
  3857. 0x17: { n: 'AppVersion', t: VT_I4, p: 'version' },
  3858. 0x18: { n: 'DigSig', t: VT_BLOB },
  3859. 0x1A: { n: 'ContentType', t: VT_STRING },
  3860. 0x1B: { n: 'ContentStatus', t: VT_STRING },
  3861. 0x1C: { n: 'Language', t: VT_STRING },
  3862. 0x1D: { n: 'Version', t: VT_STRING },
  3863. 0xFF: {}
  3864. };
  3865. /* [MS-OSHARED] 2.3.3.2.1.1 Summary Information Property Set PIDSI */
  3866. var SummaryPIDSI = {
  3867. 0x01: { n: 'CodePage', t: VT_I2 },
  3868. 0x02: { n: 'Title', t: VT_STRING },
  3869. 0x03: { n: 'Subject', t: VT_STRING },
  3870. 0x04: { n: 'Author', t: VT_STRING },
  3871. 0x05: { n: 'Keywords', t: VT_STRING },
  3872. 0x06: { n: 'Comments', t: VT_STRING },
  3873. 0x07: { n: 'Template', t: VT_STRING },
  3874. 0x08: { n: 'LastAuthor', t: VT_STRING },
  3875. 0x09: { n: 'RevNumber', t: VT_STRING },
  3876. 0x0A: { n: 'EditTime', t: VT_FILETIME },
  3877. 0x0B: { n: 'LastPrinted', t: VT_FILETIME },
  3878. 0x0C: { n: 'CreatedDate', t: VT_FILETIME },
  3879. 0x0D: { n: 'ModifiedDate', t: VT_FILETIME },
  3880. 0x0E: { n: 'PageCount', t: VT_I4 },
  3881. 0x0F: { n: 'WordCount', t: VT_I4 },
  3882. 0x10: { n: 'CharCount', t: VT_I4 },
  3883. 0x11: { n: 'Thumbnail', t: VT_CF },
  3884. 0x12: { n: 'Application', t: VT_STRING },
  3885. 0x13: { n: 'DocSecurity', t: VT_I4 },
  3886. 0xFF: {}
  3887. };
  3888. /* [MS-OLEPS] 2.18 */
  3889. var SpecialProperties = {
  3890. 0x80000000: { n: 'Locale', t: VT_UI4 },
  3891. 0x80000003: { n: 'Behavior', t: VT_UI4 },
  3892. 0x72627262: {}
  3893. };
  3894. (function() {
  3895. for(var y in SpecialProperties) if(Object.prototype.hasOwnProperty.call(SpecialProperties, y))
  3896. DocSummaryPIDDSI[y] = SummaryPIDSI[y] = SpecialProperties[y];
  3897. })();
  3898. var DocSummaryRE = evert_key(DocSummaryPIDDSI, "n");
  3899. var SummaryRE = evert_key(SummaryPIDSI, "n");
  3900. /* [MS-XLS] 2.4.63 Country/Region codes */
  3901. var CountryEnum = {
  3902. 0x0001: "US", // United States
  3903. 0x0002: "CA", // Canada
  3904. 0x0003: "", // Latin America (except Brazil)
  3905. 0x0007: "RU", // Russia
  3906. 0x0014: "EG", // Egypt
  3907. 0x001E: "GR", // Greece
  3908. 0x001F: "NL", // Netherlands
  3909. 0x0020: "BE", // Belgium
  3910. 0x0021: "FR", // France
  3911. 0x0022: "ES", // Spain
  3912. 0x0024: "HU", // Hungary
  3913. 0x0027: "IT", // Italy
  3914. 0x0029: "CH", // Switzerland
  3915. 0x002B: "AT", // Austria
  3916. 0x002C: "GB", // United Kingdom
  3917. 0x002D: "DK", // Denmark
  3918. 0x002E: "SE", // Sweden
  3919. 0x002F: "NO", // Norway
  3920. 0x0030: "PL", // Poland
  3921. 0x0031: "DE", // Germany
  3922. 0x0034: "MX", // Mexico
  3923. 0x0037: "BR", // Brazil
  3924. 0x003d: "AU", // Australia
  3925. 0x0040: "NZ", // New Zealand
  3926. 0x0042: "TH", // Thailand
  3927. 0x0051: "JP", // Japan
  3928. 0x0052: "KR", // Korea
  3929. 0x0054: "VN", // Viet Nam
  3930. 0x0056: "CN", // China
  3931. 0x005A: "TR", // Turkey
  3932. 0x0069: "JS", // Ramastan
  3933. 0x00D5: "DZ", // Algeria
  3934. 0x00D8: "MA", // Morocco
  3935. 0x00DA: "LY", // Libya
  3936. 0x015F: "PT", // Portugal
  3937. 0x0162: "IS", // Iceland
  3938. 0x0166: "FI", // Finland
  3939. 0x01A4: "CZ", // Czech Republic
  3940. 0x0376: "TW", // Taiwan
  3941. 0x03C1: "LB", // Lebanon
  3942. 0x03C2: "JO", // Jordan
  3943. 0x03C3: "SY", // Syria
  3944. 0x03C4: "IQ", // Iraq
  3945. 0x03C5: "KW", // Kuwait
  3946. 0x03C6: "SA", // Saudi Arabia
  3947. 0x03CB: "AE", // United Arab Emirates
  3948. 0x03CC: "IL", // Israel
  3949. 0x03CE: "QA", // Qatar
  3950. 0x03D5: "IR", // Iran
  3951. 0xFFFF: "US" // United States
  3952. };
  3953. /* [MS-XLS] 2.5.127 */
  3954. var XLSFillPattern = [
  3955. null,
  3956. 'solid',
  3957. 'mediumGray',
  3958. 'darkGray',
  3959. 'lightGray',
  3960. 'darkHorizontal',
  3961. 'darkVertical',
  3962. 'darkDown',
  3963. 'darkUp',
  3964. 'darkGrid',
  3965. 'darkTrellis',
  3966. 'lightHorizontal',
  3967. 'lightVertical',
  3968. 'lightDown',
  3969. 'lightUp',
  3970. 'lightGrid',
  3971. 'lightTrellis',
  3972. 'gray125',
  3973. 'gray0625'
  3974. ];
  3975. function rgbify(arr) { return arr.map(function(x) { return [(x>>16)&255,(x>>8)&255,x&255]; }); }
  3976. /* [MS-XLS] 2.5.161 */
  3977. /* [MS-XLSB] 2.5.75 Icv */
  3978. var _XLSIcv = rgbify([
  3979. /* Color Constants */
  3980. 0x000000,
  3981. 0xFFFFFF,
  3982. 0xFF0000,
  3983. 0x00FF00,
  3984. 0x0000FF,
  3985. 0xFFFF00,
  3986. 0xFF00FF,
  3987. 0x00FFFF,
  3988. /* Overridable Defaults */
  3989. 0x000000,
  3990. 0xFFFFFF,
  3991. 0xFF0000,
  3992. 0x00FF00,
  3993. 0x0000FF,
  3994. 0xFFFF00,
  3995. 0xFF00FF,
  3996. 0x00FFFF,
  3997. 0x800000,
  3998. 0x008000,
  3999. 0x000080,
  4000. 0x808000,
  4001. 0x800080,
  4002. 0x008080,
  4003. 0xC0C0C0,
  4004. 0x808080,
  4005. 0x9999FF,
  4006. 0x993366,
  4007. 0xFFFFCC,
  4008. 0xCCFFFF,
  4009. 0x660066,
  4010. 0xFF8080,
  4011. 0x0066CC,
  4012. 0xCCCCFF,
  4013. 0x000080,
  4014. 0xFF00FF,
  4015. 0xFFFF00,
  4016. 0x00FFFF,
  4017. 0x800080,
  4018. 0x800000,
  4019. 0x008080,
  4020. 0x0000FF,
  4021. 0x00CCFF,
  4022. 0xCCFFFF,
  4023. 0xCCFFCC,
  4024. 0xFFFF99,
  4025. 0x99CCFF,
  4026. 0xFF99CC,
  4027. 0xCC99FF,
  4028. 0xFFCC99,
  4029. 0x3366FF,
  4030. 0x33CCCC,
  4031. 0x99CC00,
  4032. 0xFFCC00,
  4033. 0xFF9900,
  4034. 0xFF6600,
  4035. 0x666699,
  4036. 0x969696,
  4037. 0x003366,
  4038. 0x339966,
  4039. 0x003300,
  4040. 0x333300,
  4041. 0x993300,
  4042. 0x993366,
  4043. 0x333399,
  4044. 0x333333,
  4045. /* Other entries to appease BIFF8/12 */
  4046. 0xFFFFFF, /* 0x40 icvForeground ?? */
  4047. 0x000000, /* 0x41 icvBackground ?? */
  4048. 0x000000, /* 0x42 icvFrame ?? */
  4049. 0x000000, /* 0x43 icv3D ?? */
  4050. 0x000000, /* 0x44 icv3DText ?? */
  4051. 0x000000, /* 0x45 icv3DHilite ?? */
  4052. 0x000000, /* 0x46 icv3DShadow ?? */
  4053. 0x000000, /* 0x47 icvHilite ?? */
  4054. 0x000000, /* 0x48 icvCtlText ?? */
  4055. 0x000000, /* 0x49 icvCtlScrl ?? */
  4056. 0x000000, /* 0x4A icvCtlInv ?? */
  4057. 0x000000, /* 0x4B icvCtlBody ?? */
  4058. 0x000000, /* 0x4C icvCtlFrame ?? */
  4059. 0x000000, /* 0x4D icvCtlFore ?? */
  4060. 0x000000, /* 0x4E icvCtlBack ?? */
  4061. 0x000000, /* 0x4F icvCtlNeutral */
  4062. 0x000000, /* 0x50 icvInfoBk ?? */
  4063. 0x000000 /* 0x51 icvInfoText ?? */
  4064. ]);
  4065. var XLSIcv = dup(_XLSIcv);
  4066. /* [MS-XLSB] 2.5.97.2 */
  4067. var BErr = {
  4068. 0x00: "#NULL!",
  4069. 0x07: "#DIV/0!",
  4070. 0x0F: "#VALUE!",
  4071. 0x17: "#REF!",
  4072. 0x1D: "#NAME?",
  4073. 0x24: "#NUM!",
  4074. 0x2A: "#N/A",
  4075. 0x2B: "#GETTING_DATA",
  4076. 0xFF: "#WTF?"
  4077. };
  4078. var RBErr = evert_num(BErr);
  4079. /* Parts enumerated in OPC spec, MS-XLSB and MS-XLSX */
  4080. /* 12.3 Part Summary <SpreadsheetML> */
  4081. /* 14.2 Part Summary <DrawingML> */
  4082. /* [MS-XLSX] 2.1 Part Enumerations ; [MS-XLSB] 2.1.7 Part Enumeration */
  4083. var ct2type/*{[string]:string}*/ = ({
  4084. /* Workbook */
  4085. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml": "workbooks",
  4086. /* Worksheet */
  4087. "application/vnd.ms-excel.binIndexWs": "TODO", /* Binary Index */
  4088. /* Macrosheet */
  4089. "application/vnd.ms-excel.intlmacrosheet": "TODO",
  4090. "application/vnd.ms-excel.binIndexMs": "TODO", /* Binary Index */
  4091. /* File Properties */
  4092. "application/vnd.openxmlformats-package.core-properties+xml": "coreprops",
  4093. "application/vnd.openxmlformats-officedocument.custom-properties+xml": "custprops",
  4094. "application/vnd.openxmlformats-officedocument.extended-properties+xml": "extprops",
  4095. /* Custom Data Properties */
  4096. "application/vnd.openxmlformats-officedocument.customXmlProperties+xml": "TODO",
  4097. "application/vnd.openxmlformats-officedocument.spreadsheetml.customProperty": "TODO",
  4098. /* PivotTable */
  4099. "application/vnd.ms-excel.pivotTable": "TODO",
  4100. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml": "TODO",
  4101. /* Chart Objects */
  4102. "application/vnd.openxmlformats-officedocument.drawingml.chart+xml": "TODO",
  4103. /* Chart Colors */
  4104. "application/vnd.ms-office.chartcolorstyle+xml": "TODO",
  4105. /* Chart Style */
  4106. "application/vnd.ms-office.chartstyle+xml": "TODO",
  4107. /* Chart Advanced */
  4108. "application/vnd.ms-office.chartex+xml": "TODO",
  4109. /* Calculation Chain */
  4110. "application/vnd.ms-excel.calcChain": "calcchains",
  4111. "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml": "calcchains",
  4112. /* Printer Settings */
  4113. "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings": "TODO",
  4114. /* ActiveX */
  4115. "application/vnd.ms-office.activeX": "TODO",
  4116. "application/vnd.ms-office.activeX+xml": "TODO",
  4117. /* Custom Toolbars */
  4118. "application/vnd.ms-excel.attachedToolbars": "TODO",
  4119. /* External Data Connections */
  4120. "application/vnd.ms-excel.connections": "TODO",
  4121. "application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml": "TODO",
  4122. /* External Links */
  4123. "application/vnd.ms-excel.externalLink": "links",
  4124. "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml": "links",
  4125. /* Metadata */
  4126. "application/vnd.ms-excel.sheetMetadata": "TODO",
  4127. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml": "TODO",
  4128. /* PivotCache */
  4129. "application/vnd.ms-excel.pivotCacheDefinition": "TODO",
  4130. "application/vnd.ms-excel.pivotCacheRecords": "TODO",
  4131. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml": "TODO",
  4132. "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml": "TODO",
  4133. /* Query Table */
  4134. "application/vnd.ms-excel.queryTable": "TODO",
  4135. "application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml": "TODO",
  4136. /* Shared Workbook */
  4137. "application/vnd.ms-excel.userNames": "TODO",
  4138. "application/vnd.ms-excel.revisionHeaders": "TODO",
  4139. "application/vnd.ms-excel.revisionLog": "TODO",
  4140. "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml": "TODO",
  4141. "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml": "TODO",
  4142. "application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml": "TODO",
  4143. /* Single Cell Table */
  4144. "application/vnd.ms-excel.tableSingleCells": "TODO",
  4145. "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml": "TODO",
  4146. /* Slicer */
  4147. "application/vnd.ms-excel.slicer": "TODO",
  4148. "application/vnd.ms-excel.slicerCache": "TODO",
  4149. "application/vnd.ms-excel.slicer+xml": "TODO",
  4150. "application/vnd.ms-excel.slicerCache+xml": "TODO",
  4151. /* Sort Map */
  4152. "application/vnd.ms-excel.wsSortMap": "TODO",
  4153. /* Table */
  4154. "application/vnd.ms-excel.table": "TODO",
  4155. "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml": "TODO",
  4156. /* Themes */
  4157. "application/vnd.openxmlformats-officedocument.theme+xml": "themes",
  4158. /* Theme Override */
  4159. "application/vnd.openxmlformats-officedocument.themeOverride+xml": "TODO",
  4160. /* Timeline */
  4161. "application/vnd.ms-excel.Timeline+xml": "TODO", /* verify */
  4162. "application/vnd.ms-excel.TimelineCache+xml": "TODO", /* verify */
  4163. /* VBA */
  4164. "application/vnd.ms-office.vbaProject": "vba",
  4165. "application/vnd.ms-office.vbaProjectSignature": "vba",
  4166. /* Volatile Dependencies */
  4167. "application/vnd.ms-office.volatileDependencies": "TODO",
  4168. "application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml": "TODO",
  4169. /* Control Properties */
  4170. "application/vnd.ms-excel.controlproperties+xml": "TODO",
  4171. /* Data Model */
  4172. "application/vnd.openxmlformats-officedocument.model+data": "TODO",
  4173. /* Survey */
  4174. "application/vnd.ms-excel.Survey+xml": "TODO",
  4175. /* Drawing */
  4176. "application/vnd.openxmlformats-officedocument.drawing+xml": "drawings",
  4177. "application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml": "TODO",
  4178. "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml": "TODO",
  4179. "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml": "TODO",
  4180. "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml": "TODO",
  4181. "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml": "TODO",
  4182. /* VML */
  4183. "application/vnd.openxmlformats-officedocument.vmlDrawing": "TODO",
  4184. "application/vnd.openxmlformats-package.relationships+xml": "rels",
  4185. "application/vnd.openxmlformats-officedocument.oleObject": "TODO",
  4186. /* Image */
  4187. "image/png": "TODO",
  4188. "sheet": "js"
  4189. });
  4190. var CT_LIST = (function(){
  4191. var o = {
  4192. workbooks: {
  4193. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
  4194. xlsm: "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  4195. xlsb: "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
  4196. xlam: "application/vnd.ms-excel.addin.macroEnabled.main+xml",
  4197. xltx: "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"
  4198. },
  4199. strs: { /* Shared Strings */
  4200. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
  4201. xlsb: "application/vnd.ms-excel.sharedStrings"
  4202. },
  4203. comments: { /* Comments */
  4204. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  4205. xlsb: "application/vnd.ms-excel.comments"
  4206. },
  4207. sheets: { /* Worksheet */
  4208. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
  4209. xlsb: "application/vnd.ms-excel.worksheet"
  4210. },
  4211. charts: { /* Chartsheet */
  4212. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
  4213. xlsb: "application/vnd.ms-excel.chartsheet"
  4214. },
  4215. dialogs: { /* Dialogsheet */
  4216. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml",
  4217. xlsb: "application/vnd.ms-excel.dialogsheet"
  4218. },
  4219. macros: { /* Macrosheet (Excel 4.0 Macros) */
  4220. xlsx: "application/vnd.ms-excel.macrosheet+xml",
  4221. xlsb: "application/vnd.ms-excel.macrosheet"
  4222. },
  4223. styles: { /* Styles */
  4224. xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
  4225. xlsb: "application/vnd.ms-excel.styles"
  4226. }
  4227. };
  4228. keys(o).forEach(function(k) { ["xlsm", "xlam"].forEach(function(v) { if(!o[k][v]) o[k][v] = o[k].xlsx; }); });
  4229. keys(o).forEach(function(k){ keys(o[k]).forEach(function(v) { ct2type[o[k][v]] = k; }); });
  4230. return o;
  4231. })();
  4232. var type2ct/*{[string]:Array<string>}*/ = evert_arr(ct2type);
  4233. XMLNS.CT = 'http://schemas.openxmlformats.org/package/2006/content-types';
  4234. function new_ct() {
  4235. return ({
  4236. workbooks:[], sheets:[], charts:[], dialogs:[], macros:[],
  4237. rels:[], strs:[], comments:[], links:[],
  4238. coreprops:[], extprops:[], custprops:[], themes:[], styles:[],
  4239. calcchains:[], vba: [], drawings: [],
  4240. TODO:[], xmlns: "" });
  4241. }
  4242. function parse_ct(data) {
  4243. var ct = new_ct();
  4244. if(!data || !data.match) return ct;
  4245. var ctext = {};
  4246. (data.match(tagregex)||[]).forEach(function(x) {
  4247. var y = parsexmltag(x);
  4248. switch(y[0].replace(nsregex,"<")) {
  4249. case '<?xml': break;
  4250. case '<Types': ct.xmlns = y['xmlns' + (y[0].match(/<(\w+):/)||["",""])[1] ]; break;
  4251. case '<Default': ctext[y.Extension] = y.ContentType; break;
  4252. case '<Override':
  4253. if(ct[ct2type[y.ContentType]] !== undefined) ct[ct2type[y.ContentType]].push(y.PartName);
  4254. break;
  4255. }
  4256. });
  4257. if(ct.xmlns !== XMLNS.CT) throw new Error("Unknown Namespace: " + ct.xmlns);
  4258. ct.calcchain = ct.calcchains.length > 0 ? ct.calcchains[0] : "";
  4259. ct.sst = ct.strs.length > 0 ? ct.strs[0] : "";
  4260. ct.style = ct.styles.length > 0 ? ct.styles[0] : "";
  4261. ct.defaults = ctext;
  4262. delete ct.calcchains;
  4263. return ct;
  4264. }
  4265. var CTYPE_XML_ROOT = writextag('Types', null, {
  4266. 'xmlns': XMLNS.CT,
  4267. 'xmlns:xsd': XMLNS.xsd,
  4268. 'xmlns:xsi': XMLNS.xsi
  4269. });
  4270. var CTYPE_DEFAULTS = [
  4271. ['xml', 'application/xml'],
  4272. ['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
  4273. ['vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'],
  4274. ['data', 'application/vnd.openxmlformats-officedocument.model+data'],
  4275. /* from test files */
  4276. ['bmp', 'image/bmp'],
  4277. ['png', 'image/png'],
  4278. ['gif', 'image/gif'],
  4279. ['emf', 'image/x-emf'],
  4280. ['wmf', 'image/x-wmf'],
  4281. ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
  4282. ['tif', 'image/tiff'], ['tiff', 'image/tiff'],
  4283. ['pdf', 'application/pdf'],
  4284. ['rels', type2ct.rels[0]]
  4285. ].map(function(x) {
  4286. return writextag('Default', null, {'Extension':x[0], 'ContentType': x[1]});
  4287. });
  4288. function write_ct(ct, opts) {
  4289. var o = [], v;
  4290. o[o.length] = (XML_HEADER);
  4291. o[o.length] = (CTYPE_XML_ROOT);
  4292. o = o.concat(CTYPE_DEFAULTS);
  4293. /* only write first instance */
  4294. var f1 = function(w) {
  4295. if(ct[w] && ct[w].length > 0) {
  4296. v = ct[w][0];
  4297. o[o.length] = (writextag('Override', null, {
  4298. 'PartName': (v[0] == '/' ? "":"/") + v,
  4299. 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
  4300. }));
  4301. }
  4302. };
  4303. /* book type-specific */
  4304. var f2 = function(w) {
  4305. (ct[w]||[]).forEach(function(v) {
  4306. o[o.length] = (writextag('Override', null, {
  4307. 'PartName': (v[0] == '/' ? "":"/") + v,
  4308. 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
  4309. }));
  4310. });
  4311. };
  4312. /* standard type */
  4313. var f3 = function(t) {
  4314. (ct[t]||[]).forEach(function(v) {
  4315. o[o.length] = (writextag('Override', null, {
  4316. 'PartName': (v[0] == '/' ? "":"/") + v,
  4317. 'ContentType': type2ct[t][0]
  4318. }));
  4319. });
  4320. };
  4321. f1('workbooks');
  4322. f2('sheets');
  4323. f2('charts');
  4324. f3('themes');
  4325. ['strs', 'styles'].forEach(f1);
  4326. ['coreprops', 'extprops', 'custprops'].forEach(f3);
  4327. f3('vba');
  4328. f3('comments');
  4329. f3('drawings');
  4330. if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
  4331. return o.join("");
  4332. }
  4333. /* 9.3 Relationships */
  4334. var RELS = ({
  4335. WB: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  4336. SHEET: "http://sheetjs.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  4337. HLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
  4338. VML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
  4339. XPATH: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath",
  4340. XMISS: "http://schemas.microsoft.com/office/2006/relationships/xlExternalLinkPath/xlPathMissing",
  4341. XLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
  4342. CXML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml",
  4343. CXMLP: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps",
  4344. VBA: "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
  4345. });
  4346. /* 9.3.3 Representing Relationships */
  4347. function get_rels_path(file) {
  4348. var n = file.lastIndexOf("/");
  4349. return file.slice(0,n+1) + '_rels/' + file.slice(n+1) + ".rels";
  4350. }
  4351. function parse_rels(data, currentFilePath) {
  4352. var rels = {"!id":{}};
  4353. if (!data) return rels;
  4354. if (currentFilePath.charAt(0) !== '/') {
  4355. currentFilePath = '/'+currentFilePath;
  4356. }
  4357. var hash = {};
  4358. (data.match(tagregex)||[]).forEach(function(x) {
  4359. var y = parsexmltag(x);
  4360. /* 9.3.2.2 OPC_Relationships */
  4361. if (y[0] === '<Relationship') {
  4362. var rel = {}; rel.Type = y.Type; rel.Target = y.Target; rel.Id = y.Id; rel.TargetMode = y.TargetMode;
  4363. var canonictarget = y.TargetMode === 'External' ? y.Target : resolve_path(y.Target, currentFilePath);
  4364. rels[canonictarget] = rel;
  4365. hash[y.Id] = rel;
  4366. }
  4367. });
  4368. rels["!id"] = hash;
  4369. return rels;
  4370. }
  4371. XMLNS.RELS = 'http://schemas.openxmlformats.org/package/2006/relationships';
  4372. var RELS_ROOT = writextag('Relationships', null, {
  4373. //'xmlns:ns0': XMLNS.RELS,
  4374. 'xmlns': XMLNS.RELS
  4375. });
  4376. /* TODO */
  4377. function write_rels(rels) {
  4378. var o = [XML_HEADER, RELS_ROOT];
  4379. keys(rels['!id']).forEach(function(rid) {
  4380. o[o.length] = (writextag('Relationship', null, rels['!id'][rid]));
  4381. });
  4382. if(o.length>2){ o[o.length] = ('</Relationships>'); o[1]=o[1].replace("/>",">"); }
  4383. return o.join("");
  4384. }
  4385. var RELS_EXTERN = [RELS.HLINK, RELS.XPATH, RELS.XMISS];
  4386. function add_rels(rels, rId, f, type, relobj, targetmode) {
  4387. if(!relobj) relobj = {};
  4388. if(!rels['!id']) rels['!id'] = {};
  4389. if(rId < 0) for(rId = 1; rels['!id']['rId' + rId]; ++rId){/* empty */}
  4390. relobj.Id = 'rId' + rId;
  4391. relobj.Type = type;
  4392. relobj.Target = f;
  4393. if(targetmode) relobj.TargetMode = targetmode;
  4394. else if(RELS_EXTERN.indexOf(relobj.Type) > -1) relobj.TargetMode = "External";
  4395. if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
  4396. rels['!id'][relobj.Id] = relobj;
  4397. rels[('/' + relobj.Target).replace("//","/")] = relobj;
  4398. return rId;
  4399. }
  4400. /* Open Document Format for Office Applications (OpenDocument) Version 1.2 */
  4401. /* Part 3 Section 4 Manifest File */
  4402. var CT_ODS = "application/vnd.oasis.opendocument.spreadsheet";
  4403. function parse_manifest(d, opts) {
  4404. var str = xlml_normalize(d);
  4405. var Rn;
  4406. var FEtag;
  4407. while((Rn = xlmlregex.exec(str))) switch(Rn[3]) {
  4408. case 'manifest': break; // 4.2 <manifest:manifest>
  4409. case 'file-entry': // 4.3 <manifest:file-entry>
  4410. FEtag = parsexmltag(Rn[0], false);
  4411. if(FEtag.path == '/' && FEtag.type !== CT_ODS) throw new Error("This OpenDocument is not a spreadsheet");
  4412. break;
  4413. case 'encryption-data': // 4.4 <manifest:encryption-data>
  4414. case 'algorithm': // 4.5 <manifest:algorithm>
  4415. case 'start-key-generation': // 4.6 <manifest:start-key-generation>
  4416. case 'key-derivation': // 4.7 <manifest:key-derivation>
  4417. throw new Error("Unsupported ODS Encryption");
  4418. default: if(opts && opts.WTF) throw Rn;
  4419. }
  4420. }
  4421. function write_manifest(manifest) {
  4422. var o = [XML_HEADER];
  4423. o.push('<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">\n');
  4424. o.push(' <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>\n');
  4425. for(var i = 0; i < manifest.length; ++i) o.push(' <manifest:file-entry manifest:full-path="' + manifest[i][0] + '" manifest:media-type="' + manifest[i][1] + '"/>\n');
  4426. o.push('</manifest:manifest>');
  4427. return o.join("");
  4428. }
  4429. /* Part 3 Section 6 Metadata Manifest File */
  4430. function write_rdf_type(file, res, tag) {
  4431. return [
  4432. ' <rdf:Description rdf:about="' + file + '">\n',
  4433. ' <rdf:type rdf:resource="http://docs.oasis-open.org/ns/office/1.2/meta/' + (tag || "odf") + '#' + res + '"/>\n',
  4434. ' </rdf:Description>\n'
  4435. ].join("");
  4436. }
  4437. function write_rdf_has(base, file) {
  4438. return [
  4439. ' <rdf:Description rdf:about="' + base + '">\n',
  4440. ' <ns0:hasPart xmlns:ns0="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#" rdf:resource="' + file + '"/>\n',
  4441. ' </rdf:Description>\n'
  4442. ].join("");
  4443. }
  4444. function write_rdf(rdf) {
  4445. var o = [XML_HEADER];
  4446. o.push('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n');
  4447. for(var i = 0; i != rdf.length; ++i) {
  4448. o.push(write_rdf_type(rdf[i][0], rdf[i][1]));
  4449. o.push(write_rdf_has("",rdf[i][0]));
  4450. }
  4451. o.push(write_rdf_type("","Document", "pkg"));
  4452. o.push('</rdf:RDF>');
  4453. return o.join("");
  4454. }
  4455. /* TODO: pull properties */
  4456. var write_meta_ods = (function() {
  4457. var payload = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" office:version="1.2"><office:meta><meta:generator>Sheet' + 'JS ' + XLSX.version + '</meta:generator></office:meta></office:document-meta>';
  4458. return function wmo() {
  4459. return payload;
  4460. };
  4461. })();
  4462. /* ECMA-376 Part II 11.1 Core Properties Part */
  4463. /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
  4464. var CORE_PROPS = [
  4465. ["cp:category", "Category"],
  4466. ["cp:contentStatus", "ContentStatus"],
  4467. ["cp:keywords", "Keywords"],
  4468. ["cp:lastModifiedBy", "LastAuthor"],
  4469. ["cp:lastPrinted", "LastPrinted"],
  4470. ["cp:revision", "RevNumber"],
  4471. ["cp:version", "Version"],
  4472. ["dc:creator", "Author"],
  4473. ["dc:description", "Comments"],
  4474. ["dc:identifier", "Identifier"],
  4475. ["dc:language", "Language"],
  4476. ["dc:subject", "Subject"],
  4477. ["dc:title", "Title"],
  4478. ["dcterms:created", "CreatedDate", 'date'],
  4479. ["dcterms:modified", "ModifiedDate", 'date']
  4480. ];
  4481. XMLNS.CORE_PROPS = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
  4482. RELS.CORE_PROPS = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
  4483. var CORE_PROPS_REGEX = (function() {
  4484. var r = new Array(CORE_PROPS.length);
  4485. for(var i = 0; i < CORE_PROPS.length; ++i) {
  4486. var f = CORE_PROPS[i];
  4487. var g = "(?:"+ f[0].slice(0,f[0].indexOf(":")) +":)"+ f[0].slice(f[0].indexOf(":")+1);
  4488. r[i] = new RegExp("<" + g + "[^>]*>([\\s\\S]*?)<\/" + g + ">");
  4489. }
  4490. return r;
  4491. })();
  4492. function parse_core_props(data) {
  4493. var p = {};
  4494. data = utf8read(data);
  4495. for(var i = 0; i < CORE_PROPS.length; ++i) {
  4496. var f = CORE_PROPS[i], cur = data.match(CORE_PROPS_REGEX[i]);
  4497. if(cur != null && cur.length > 0) p[f[1]] = unescapexml(cur[1]);
  4498. if(f[2] === 'date' && p[f[1]]) p[f[1]] = parseDate(p[f[1]]);
  4499. }
  4500. return p;
  4501. }
  4502. var CORE_PROPS_XML_ROOT = writextag('cp:coreProperties', null, {
  4503. //'xmlns': XMLNS.CORE_PROPS,
  4504. 'xmlns:cp': XMLNS.CORE_PROPS,
  4505. 'xmlns:dc': XMLNS.dc,
  4506. 'xmlns:dcterms': XMLNS.dcterms,
  4507. 'xmlns:dcmitype': XMLNS.dcmitype,
  4508. 'xmlns:xsi': XMLNS.xsi
  4509. });
  4510. function cp_doit(f, g, h, o, p) {
  4511. if(p[f] != null || g == null || g === "") return;
  4512. p[f] = g;
  4513. g = escapexml(g);
  4514. o[o.length] = (h ? writextag(f,g,h) : writetag(f,g));
  4515. }
  4516. function write_core_props(cp, _opts) {
  4517. var opts = _opts || {};
  4518. var o = [XML_HEADER, CORE_PROPS_XML_ROOT], p = {};
  4519. if(!cp && !opts.Props) return o.join("");
  4520. if(cp) {
  4521. if(cp.CreatedDate != null) cp_doit("dcterms:created", typeof cp.CreatedDate === "string" ? cp.CreatedDate : write_w3cdtf(cp.CreatedDate, opts.WTF), {"xsi:type":"dcterms:W3CDTF"}, o, p);
  4522. if(cp.ModifiedDate != null) cp_doit("dcterms:modified", typeof cp.ModifiedDate === "string" ? cp.ModifiedDate : write_w3cdtf(cp.ModifiedDate, opts.WTF), {"xsi:type":"dcterms:W3CDTF"}, o, p);
  4523. }
  4524. for(var i = 0; i != CORE_PROPS.length; ++i) {
  4525. var f = CORE_PROPS[i];
  4526. var v = opts.Props && opts.Props[f[1]] != null ? opts.Props[f[1]] : cp ? cp[f[1]] : null;
  4527. if(v === true) v = "1";
  4528. else if(v === false) v = "0";
  4529. else if(typeof v == "number") v = String(v);
  4530. if(v != null) cp_doit(f[0], v, null, o, p);
  4531. }
  4532. if(o.length>2){ o[o.length] = ('</cp:coreProperties>'); o[1]=o[1].replace("/>",">"); }
  4533. return o.join("");
  4534. }
  4535. /* 15.2.12.3 Extended File Properties Part */
  4536. /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
  4537. var EXT_PROPS = [
  4538. ["Application", "Application", "string"],
  4539. ["AppVersion", "AppVersion", "string"],
  4540. ["Company", "Company", "string"],
  4541. ["DocSecurity", "DocSecurity", "string"],
  4542. ["Manager", "Manager", "string"],
  4543. ["HyperlinksChanged", "HyperlinksChanged", "bool"],
  4544. ["SharedDoc", "SharedDoc", "bool"],
  4545. ["LinksUpToDate", "LinksUpToDate", "bool"],
  4546. ["ScaleCrop", "ScaleCrop", "bool"],
  4547. ["HeadingPairs", "HeadingPairs", "raw"],
  4548. ["TitlesOfParts", "TitlesOfParts", "raw"]
  4549. ];
  4550. XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
  4551. RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
  4552. var PseudoPropsPairs = [
  4553. "Worksheets", "SheetNames",
  4554. "NamedRanges", "DefinedNames",
  4555. "Chartsheets", "ChartNames"
  4556. ];
  4557. function load_props_pairs(HP, TOP, props, opts) {
  4558. var v = [];
  4559. if(typeof HP == "string") v = parseVector(HP, opts);
  4560. else for(var j = 0; j < HP.length; ++j) v = v.concat(HP[j].map(function(hp) { return {v:hp}; }));
  4561. var parts = (typeof TOP == "string") ? parseVector(TOP, opts).map(function (x) { return x.v; }) : TOP;
  4562. var idx = 0, len = 0;
  4563. if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
  4564. len = +(v[i+1].v);
  4565. switch(v[i].v) {
  4566. case "Worksheets":
  4567. case "工作表":
  4568. case "Листы":
  4569. case "أوراق العمل":
  4570. case "ワークシート":
  4571. case "גליונות עבודה":
  4572. case "Arbeitsblätter":
  4573. case "Çalışma Sayfaları":
  4574. case "Feuilles de calcul":
  4575. case "Fogli di lavoro":
  4576. case "Folhas de cálculo":
  4577. case "Planilhas":
  4578. case "Regneark":
  4579. case "Hojas de cálculo":
  4580. case "Werkbladen":
  4581. props.Worksheets = len;
  4582. props.SheetNames = parts.slice(idx, idx + len);
  4583. break;
  4584. case "Named Ranges":
  4585. case "Rangos con nombre":
  4586. case "名前付き一覧":
  4587. case "Benannte Bereiche":
  4588. case "Navngivne områder":
  4589. props.NamedRanges = len;
  4590. props.DefinedNames = parts.slice(idx, idx + len);
  4591. break;
  4592. case "Charts":
  4593. case "Diagramme":
  4594. props.Chartsheets = len;
  4595. props.ChartNames = parts.slice(idx, idx + len);
  4596. break;
  4597. }
  4598. idx += len;
  4599. }
  4600. }
  4601. function parse_ext_props(data, p, opts) {
  4602. var q = {}; if(!p) p = {};
  4603. data = utf8read(data);
  4604. EXT_PROPS.forEach(function(f) {
  4605. var xml = (data.match(matchtag(f[0]))||[])[1];
  4606. switch(f[2]) {
  4607. case "string": if(xml) p[f[1]] = unescapexml(xml); break;
  4608. case "bool": p[f[1]] = xml === "true"; break;
  4609. case "raw":
  4610. var cur = data.match(new RegExp("<" + f[0] + "[^>]*>([\\s\\S]*?)<\/" + f[0] + ">"));
  4611. if(cur && cur.length > 0) q[f[1]] = cur[1];
  4612. break;
  4613. }
  4614. });
  4615. if(q.HeadingPairs && q.TitlesOfParts) load_props_pairs(q.HeadingPairs, q.TitlesOfParts, p, opts);
  4616. return p;
  4617. }
  4618. var EXT_PROPS_XML_ROOT = writextag('Properties', null, {
  4619. 'xmlns': XMLNS.EXT_PROPS,
  4620. 'xmlns:vt': XMLNS.vt
  4621. });
  4622. function write_ext_props(cp) {
  4623. var o = [], W = writextag;
  4624. if(!cp) cp = {};
  4625. cp.Application = "SheetJS";
  4626. o[o.length] = (XML_HEADER);
  4627. o[o.length] = (EXT_PROPS_XML_ROOT);
  4628. EXT_PROPS.forEach(function(f) {
  4629. if(cp[f[1]] === undefined) return;
  4630. var v;
  4631. switch(f[2]) {
  4632. case 'string': v = escapexml(String(cp[f[1]])); break;
  4633. case 'bool': v = cp[f[1]] ? 'true' : 'false'; break;
  4634. }
  4635. if(v !== undefined) o[o.length] = (W(f[0], v));
  4636. });
  4637. /* TODO: HeadingPairs, TitlesOfParts */
  4638. o[o.length] = (W('HeadingPairs', W('vt:vector', W('vt:variant', '<vt:lpstr>Worksheets</vt:lpstr>')+W('vt:variant', W('vt:i4', String(cp.Worksheets))), {size:2, baseType:"variant"})));
  4639. o[o.length] = (W('TitlesOfParts', W('vt:vector', cp.SheetNames.map(function(s) { return "<vt:lpstr>" + escapexml(s) + "</vt:lpstr>"; }).join(""), {size: cp.Worksheets, baseType:"lpstr"})));
  4640. if(o.length>2){ o[o.length] = ('</Properties>'); o[1]=o[1].replace("/>",">"); }
  4641. return o.join("");
  4642. }
  4643. /* 15.2.12.2 Custom File Properties Part */
  4644. XMLNS.CUST_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
  4645. RELS.CUST_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties';
  4646. var custregex = /<[^>]+>[^<]*/g;
  4647. function parse_cust_props(data, opts) {
  4648. var p = {}, name = "";
  4649. var m = data.match(custregex);
  4650. if(m) for(var i = 0; i != m.length; ++i) {
  4651. var x = m[i], y = parsexmltag(x);
  4652. switch(y[0]) {
  4653. case '<?xml': break;
  4654. case '<Properties': break;
  4655. case '<property': name = unescapexml(y.name); break;
  4656. case '</property>': name = null; break;
  4657. default: if (x.indexOf('<vt:') === 0) {
  4658. var toks = x.split('>');
  4659. var type = toks[0].slice(4), text = toks[1];
  4660. /* 22.4.2.32 (CT_Variant). Omit the binary types from 22.4 (Variant Types) */
  4661. switch(type) {
  4662. case 'lpstr': case 'bstr': case 'lpwstr':
  4663. p[name] = unescapexml(text);
  4664. break;
  4665. case 'bool':
  4666. p[name] = parsexmlbool(text);
  4667. break;
  4668. case 'i1': case 'i2': case 'i4': case 'i8': case 'int': case 'uint':
  4669. p[name] = parseInt(text, 10);
  4670. break;
  4671. case 'r4': case 'r8': case 'decimal':
  4672. p[name] = parseFloat(text);
  4673. break;
  4674. case 'filetime': case 'date':
  4675. p[name] = parseDate(text);
  4676. break;
  4677. case 'cy': case 'error':
  4678. p[name] = unescapexml(text);
  4679. break;
  4680. default:
  4681. if(type.slice(-1) == '/') break;
  4682. if(opts.WTF && typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
  4683. }
  4684. } else if(x.slice(0,2) === "</") {/* empty */
  4685. } else if(opts.WTF) throw new Error(x);
  4686. }
  4687. }
  4688. return p;
  4689. }
  4690. var CUST_PROPS_XML_ROOT = writextag('Properties', null, {
  4691. 'xmlns': XMLNS.CUST_PROPS,
  4692. 'xmlns:vt': XMLNS.vt
  4693. });
  4694. function write_cust_props(cp) {
  4695. var o = [XML_HEADER, CUST_PROPS_XML_ROOT];
  4696. if(!cp) return o.join("");
  4697. var pid = 1;
  4698. keys(cp).forEach(function custprop(k) { ++pid;
  4699. o[o.length] = (writextag('property', write_vt(cp[k], true), {
  4700. 'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
  4701. 'pid': pid,
  4702. 'name': escapexml(k)
  4703. }));
  4704. });
  4705. if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
  4706. return o.join("");
  4707. }
  4708. /* Common Name -> XLML Name */
  4709. var XLMLDocPropsMap = {
  4710. Title: 'Title',
  4711. Subject: 'Subject',
  4712. Author: 'Author',
  4713. Keywords: 'Keywords',
  4714. Comments: 'Description',
  4715. LastAuthor: 'LastAuthor',
  4716. RevNumber: 'Revision',
  4717. Application: 'AppName',
  4718. /* TotalTime: 'TotalTime', */
  4719. LastPrinted: 'LastPrinted',
  4720. CreatedDate: 'Created',
  4721. ModifiedDate: 'LastSaved',
  4722. /* Pages */
  4723. /* Words */
  4724. /* Characters */
  4725. Category: 'Category',
  4726. /* PresentationFormat */
  4727. Manager: 'Manager',
  4728. Company: 'Company',
  4729. /* Guid */
  4730. /* HyperlinkBase */
  4731. /* Bytes */
  4732. /* Lines */
  4733. /* Paragraphs */
  4734. /* CharactersWithSpaces */
  4735. AppVersion: 'Version',
  4736. ContentStatus: 'ContentStatus', /* NOTE: missing from schema */
  4737. Identifier: 'Identifier', /* NOTE: missing from schema */
  4738. Language: 'Language' /* NOTE: missing from schema */
  4739. };
  4740. var evert_XLMLDPM = evert(XLMLDocPropsMap);
  4741. function xlml_set_prop(Props, tag, val) {
  4742. tag = evert_XLMLDPM[tag] || tag;
  4743. Props[tag] = val;
  4744. }
  4745. function xlml_write_docprops(Props, opts) {
  4746. var o = [];
  4747. keys(XLMLDocPropsMap).map(function(m) {
  4748. for(var i = 0; i < CORE_PROPS.length; ++i) if(CORE_PROPS[i][1] == m) return CORE_PROPS[i];
  4749. for(i = 0; i < EXT_PROPS.length; ++i) if(EXT_PROPS[i][1] == m) return EXT_PROPS[i];
  4750. throw m;
  4751. }).forEach(function(p) {
  4752. if(Props[p[1]] == null) return;
  4753. var m = opts && opts.Props && opts.Props[p[1]] != null ? opts.Props[p[1]] : Props[p[1]];
  4754. switch(p[2]) {
  4755. case 'date': m = new Date(m).toISOString().replace(/\.\d*Z/,"Z"); break;
  4756. }
  4757. if(typeof m == 'number') m = String(m);
  4758. else if(m === true || m === false) { m = m ? "1" : "0"; }
  4759. else if(m instanceof Date) m = new Date(m).toISOString().replace(/\.\d*Z/,"");
  4760. o.push(writetag(XLMLDocPropsMap[p[1]] || p[1], m));
  4761. });
  4762. return writextag('DocumentProperties', o.join(""), {xmlns:XLMLNS.o });
  4763. }
  4764. function xlml_write_custprops(Props, Custprops) {
  4765. var BLACKLIST = ["Worksheets","SheetNames"];
  4766. var T = 'CustomDocumentProperties';
  4767. var o = [];
  4768. if(Props) keys(Props).forEach(function(k) {
  4769. if(!Object.prototype.hasOwnProperty.call(Props, k)) return;
  4770. for(var i = 0; i < CORE_PROPS.length; ++i) if(k == CORE_PROPS[i][1]) return;
  4771. for(i = 0; i < EXT_PROPS.length; ++i) if(k == EXT_PROPS[i][1]) return;
  4772. for(i = 0; i < BLACKLIST.length; ++i) if(k == BLACKLIST[i]) return;
  4773. var m = Props[k];
  4774. var t = "string";
  4775. if(typeof m == 'number') { t = "float"; m = String(m); }
  4776. else if(m === true || m === false) { t = "boolean"; m = m ? "1" : "0"; }
  4777. else m = String(m);
  4778. o.push(writextag(escapexmltag(k), m, {"dt:dt":t}));
  4779. });
  4780. if(Custprops) keys(Custprops).forEach(function(k) {
  4781. if(!Object.prototype.hasOwnProperty.call(Custprops, k)) return;
  4782. if(Props && Object.prototype.hasOwnProperty.call(Props, k)) return;
  4783. var m = Custprops[k];
  4784. var t = "string";
  4785. if(typeof m == 'number') { t = "float"; m = String(m); }
  4786. else if(m === true || m === false) { t = "boolean"; m = m ? "1" : "0"; }
  4787. else if(m instanceof Date) { t = "dateTime.tz"; m = m.toISOString(); }
  4788. else m = String(m);
  4789. o.push(writextag(escapexmltag(k), m, {"dt:dt":t}));
  4790. });
  4791. return '<' + T + ' xmlns="' + XLMLNS.o + '">' + o.join("") + '</' + T + '>';
  4792. }
  4793. /* [MS-DTYP] 2.3.3 FILETIME */
  4794. /* [MS-OLEDS] 2.1.3 FILETIME (Packet Version) */
  4795. /* [MS-OLEPS] 2.8 FILETIME (Packet Version) */
  4796. function parse_FILETIME(blob) {
  4797. var dwLowDateTime = blob.read_shift(4), dwHighDateTime = blob.read_shift(4);
  4798. return new Date(((dwHighDateTime/1e7*Math.pow(2,32) + dwLowDateTime/1e7) - 11644473600)*1000).toISOString().replace(/\.000/,"");
  4799. }
  4800. function write_FILETIME(time) {
  4801. var date = (typeof time == "string") ? new Date(Date.parse(time)) : time;
  4802. var t = date.getTime() / 1000 + 11644473600;
  4803. var l = t % Math.pow(2,32), h = (t - l) / Math.pow(2,32);
  4804. l *= 1e7; h *= 1e7;
  4805. var w = (l / Math.pow(2,32)) | 0;
  4806. if(w > 0) { l = l % Math.pow(2,32); h += w; }
  4807. var o = new_buf(8); o.write_shift(4, l); o.write_shift(4, h); return o;
  4808. }
  4809. /* [MS-OSHARED] 2.3.3.1.4 Lpstr */
  4810. function parse_lpstr(blob, type, pad) {
  4811. var start = blob.l;
  4812. var str = blob.read_shift(0, 'lpstr-cp');
  4813. if(pad) while((blob.l - start) & 3) ++blob.l;
  4814. return str;
  4815. }
  4816. /* [MS-OSHARED] 2.3.3.1.6 Lpwstr */
  4817. function parse_lpwstr(blob, type, pad) {
  4818. var str = blob.read_shift(0, 'lpwstr');
  4819. if(pad) blob.l += (4 - ((str.length+1) & 3)) & 3;
  4820. return str;
  4821. }
  4822. /* [MS-OSHARED] 2.3.3.1.11 VtString */
  4823. /* [MS-OSHARED] 2.3.3.1.12 VtUnalignedString */
  4824. function parse_VtStringBase(blob, stringType, pad) {
  4825. if(stringType === 0x1F /*VT_LPWSTR*/) return parse_lpwstr(blob);
  4826. return parse_lpstr(blob, stringType, pad);
  4827. }
  4828. function parse_VtString(blob, t, pad) { return parse_VtStringBase(blob, t, pad === false ? 0: 4); }
  4829. function parse_VtUnalignedString(blob, t) { if(!t) throw new Error("VtUnalignedString must have positive length"); return parse_VtStringBase(blob, t, 0); }
  4830. /* [MS-OSHARED] 2.3.3.1.9 VtVecUnalignedLpstrValue */
  4831. function parse_VtVecUnalignedLpstrValue(blob) {
  4832. var length = blob.read_shift(4);
  4833. var ret = [];
  4834. for(var i = 0; i != length; ++i) ret[i] = blob.read_shift(0, 'lpstr-cp').replace(chr0,'');
  4835. return ret;
  4836. }
  4837. /* [MS-OSHARED] 2.3.3.1.10 VtVecUnalignedLpstr */
  4838. function parse_VtVecUnalignedLpstr(blob) {
  4839. return parse_VtVecUnalignedLpstrValue(blob);
  4840. }
  4841. /* [MS-OSHARED] 2.3.3.1.13 VtHeadingPair */
  4842. function parse_VtHeadingPair(blob) {
  4843. var headingString = parse_TypedPropertyValue(blob, VT_USTR);
  4844. var headerParts = parse_TypedPropertyValue(blob, VT_I4);
  4845. return [headingString, headerParts];
  4846. }
  4847. /* [MS-OSHARED] 2.3.3.1.14 VtVecHeadingPairValue */
  4848. function parse_VtVecHeadingPairValue(blob) {
  4849. var cElements = blob.read_shift(4);
  4850. var out = [];
  4851. for(var i = 0; i != cElements / 2; ++i) out.push(parse_VtHeadingPair(blob));
  4852. return out;
  4853. }
  4854. /* [MS-OSHARED] 2.3.3.1.15 VtVecHeadingPair */
  4855. function parse_VtVecHeadingPair(blob) {
  4856. // NOTE: When invoked, wType & padding were already consumed
  4857. return parse_VtVecHeadingPairValue(blob);
  4858. }
  4859. /* [MS-OLEPS] 2.18.1 Dictionary (uses 2.17, 2.16) */
  4860. function parse_dictionary(blob,CodePage) {
  4861. var cnt = blob.read_shift(4);
  4862. var dict = ({});
  4863. for(var j = 0; j != cnt; ++j) {
  4864. var pid = blob.read_shift(4);
  4865. var len = blob.read_shift(4);
  4866. dict[pid] = blob.read_shift(len, (CodePage === 0x4B0 ?'utf16le':'utf8')).replace(chr0,'').replace(chr1,'!');
  4867. if(CodePage === 0x4B0 && (len % 2)) blob.l += 2;
  4868. }
  4869. if(blob.l & 3) blob.l = (blob.l>>2+1)<<2;
  4870. return dict;
  4871. }
  4872. /* [MS-OLEPS] 2.9 BLOB */
  4873. function parse_BLOB(blob) {
  4874. var size = blob.read_shift(4);
  4875. var bytes = blob.slice(blob.l,blob.l+size);
  4876. blob.l += size;
  4877. if((size & 3) > 0) blob.l += (4 - (size & 3)) & 3;
  4878. return bytes;
  4879. }
  4880. /* [MS-OLEPS] 2.11 ClipboardData */
  4881. function parse_ClipboardData(blob) {
  4882. // TODO
  4883. var o = {};
  4884. o.Size = blob.read_shift(4);
  4885. //o.Format = blob.read_shift(4);
  4886. blob.l += o.Size + 3 - (o.Size - 1) % 4;
  4887. return o;
  4888. }
  4889. /* [MS-OLEPS] 2.15 TypedPropertyValue */
  4890. function parse_TypedPropertyValue(blob, type, _opts) {
  4891. var t = blob.read_shift(2), ret, opts = _opts||{};
  4892. blob.l += 2;
  4893. if(type !== VT_VARIANT)
  4894. if(t !== type && VT_CUSTOM.indexOf(type)===-1) throw new Error('Expected type ' + type + ' saw ' + t);
  4895. switch(type === VT_VARIANT ? t : type) {
  4896. case 0x02 /*VT_I2*/: ret = blob.read_shift(2, 'i'); if(!opts.raw) blob.l += 2; return ret;
  4897. case 0x03 /*VT_I4*/: ret = blob.read_shift(4, 'i'); return ret;
  4898. case 0x0B /*VT_BOOL*/: return blob.read_shift(4) !== 0x0;
  4899. case 0x13 /*VT_UI4*/: ret = blob.read_shift(4); return ret;
  4900. case 0x1E /*VT_LPSTR*/: return parse_lpstr(blob, t, 4).replace(chr0,'');
  4901. case 0x1F /*VT_LPWSTR*/: return parse_lpwstr(blob);
  4902. case 0x40 /*VT_FILETIME*/: return parse_FILETIME(blob);
  4903. case 0x41 /*VT_BLOB*/: return parse_BLOB(blob);
  4904. case 0x47 /*VT_CF*/: return parse_ClipboardData(blob);
  4905. case 0x50 /*VT_STRING*/: return parse_VtString(blob, t, !opts.raw).replace(chr0,'');
  4906. case 0x51 /*VT_USTR*/: return parse_VtUnalignedString(blob, t/*, 4*/).replace(chr0,'');
  4907. case 0x100C /*VT_VECTOR|VT_VARIANT*/: return parse_VtVecHeadingPair(blob);
  4908. case 0x101E /*VT_LPSTR*/: return parse_VtVecUnalignedLpstr(blob);
  4909. default: throw new Error("TypedPropertyValue unrecognized type " + type + " " + t);
  4910. }
  4911. }
  4912. function write_TypedPropertyValue(type, value) {
  4913. var o = new_buf(4), p = new_buf(4);
  4914. o.write_shift(4, type == 0x50 ? 0x1F : type);
  4915. switch(type) {
  4916. case 0x03 /*VT_I4*/: p.write_shift(-4, value); break;
  4917. case 0x05 /*VT_I4*/: p = new_buf(8); p.write_shift(8, value, 'f'); break;
  4918. case 0x0B /*VT_BOOL*/: p.write_shift(4, value ? 0x01 : 0x00); break;
  4919. case 0x40 /*VT_FILETIME*/: p = write_FILETIME(value); break;
  4920. case 0x1F /*VT_LPWSTR*/:
  4921. case 0x50 /*VT_STRING*/:
  4922. p = new_buf(4 + 2 * (value.length + 1) + (value.length % 2 ? 0 : 2));
  4923. p.write_shift(4, value.length + 1);
  4924. p.write_shift(0, value, "dbcs");
  4925. while(p.l != p.length) p.write_shift(1, 0);
  4926. break;
  4927. default: throw new Error("TypedPropertyValue unrecognized type " + type + " " + value);
  4928. }
  4929. return bconcat([o, p]);
  4930. }
  4931. /* [MS-OLEPS] 2.20 PropertySet */
  4932. function parse_PropertySet(blob, PIDSI) {
  4933. var start_addr = blob.l;
  4934. var size = blob.read_shift(4);
  4935. var NumProps = blob.read_shift(4);
  4936. var Props = [], i = 0;
  4937. var CodePage = 0;
  4938. var Dictionary = -1, DictObj = ({});
  4939. for(i = 0; i != NumProps; ++i) {
  4940. var PropID = blob.read_shift(4);
  4941. var Offset = blob.read_shift(4);
  4942. Props[i] = [PropID, Offset + start_addr];
  4943. }
  4944. Props.sort(function(x,y) { return x[1] - y[1]; });
  4945. var PropH = {};
  4946. for(i = 0; i != NumProps; ++i) {
  4947. if(blob.l !== Props[i][1]) {
  4948. var fail = true;
  4949. if(i>0 && PIDSI) switch(PIDSI[Props[i-1][0]].t) {
  4950. case 0x02 /*VT_I2*/: if(blob.l+2 === Props[i][1]) { blob.l+=2; fail = false; } break;
  4951. case 0x50 /*VT_STRING*/: if(blob.l <= Props[i][1]) { blob.l=Props[i][1]; fail = false; } break;
  4952. case 0x100C /*VT_VECTOR|VT_VARIANT*/: if(blob.l <= Props[i][1]) { blob.l=Props[i][1]; fail = false; } break;
  4953. }
  4954. if((!PIDSI||i==0) && blob.l <= Props[i][1]) { fail=false; blob.l = Props[i][1]; }
  4955. if(fail) throw new Error("Read Error: Expected address " + Props[i][1] + ' at ' + blob.l + ' :' + i);
  4956. }
  4957. if(PIDSI) {
  4958. var piddsi = PIDSI[Props[i][0]];
  4959. PropH[piddsi.n] = parse_TypedPropertyValue(blob, piddsi.t, {raw:true});
  4960. if(piddsi.p === 'version') PropH[piddsi.n] = String(PropH[piddsi.n] >> 16) + "." + ("0000" + String(PropH[piddsi.n] & 0xFFFF)).slice(-4);
  4961. if(piddsi.n == "CodePage") switch(PropH[piddsi.n]) {
  4962. case 0: PropH[piddsi.n] = 1252;
  4963. /* falls through */
  4964. case 874:
  4965. case 932:
  4966. case 936:
  4967. case 949:
  4968. case 950:
  4969. case 1250:
  4970. case 1251:
  4971. case 1253:
  4972. case 1254:
  4973. case 1255:
  4974. case 1256:
  4975. case 1257:
  4976. case 1258:
  4977. case 10000:
  4978. case 1200:
  4979. case 1201:
  4980. case 1252:
  4981. case 65000: case -536:
  4982. case 65001: case -535:
  4983. set_cp(CodePage = (PropH[piddsi.n]>>>0) & 0xFFFF); break;
  4984. default: throw new Error("Unsupported CodePage: " + PropH[piddsi.n]);
  4985. }
  4986. } else {
  4987. if(Props[i][0] === 0x1) {
  4988. CodePage = PropH.CodePage = (parse_TypedPropertyValue(blob, VT_I2));
  4989. set_cp(CodePage);
  4990. if(Dictionary !== -1) {
  4991. var oldpos = blob.l;
  4992. blob.l = Props[Dictionary][1];
  4993. DictObj = parse_dictionary(blob,CodePage);
  4994. blob.l = oldpos;
  4995. }
  4996. } else if(Props[i][0] === 0) {
  4997. if(CodePage === 0) { Dictionary = i; blob.l = Props[i+1][1]; continue; }
  4998. DictObj = parse_dictionary(blob,CodePage);
  4999. } else {
  5000. var name = DictObj[Props[i][0]];
  5001. var val;
  5002. /* [MS-OSHARED] 2.3.3.2.3.1.2 + PROPVARIANT */
  5003. switch(blob[blob.l]) {
  5004. case 0x41 /*VT_BLOB*/: blob.l += 4; val = parse_BLOB(blob); break;
  5005. case 0x1E /*VT_LPSTR*/: blob.l += 4; val = parse_VtString(blob, blob[blob.l-4]).replace(/\u0000+$/,""); break;
  5006. case 0x1F /*VT_LPWSTR*/: blob.l += 4; val = parse_VtString(blob, blob[blob.l-4]).replace(/\u0000+$/,""); break;
  5007. case 0x03 /*VT_I4*/: blob.l += 4; val = blob.read_shift(4, 'i'); break;
  5008. case 0x13 /*VT_UI4*/: blob.l += 4; val = blob.read_shift(4); break;
  5009. case 0x05 /*VT_R8*/: blob.l += 4; val = blob.read_shift(8, 'f'); break;
  5010. case 0x0B /*VT_BOOL*/: blob.l += 4; val = parsebool(blob, 4); break;
  5011. case 0x40 /*VT_FILETIME*/: blob.l += 4; val = parseDate(parse_FILETIME(blob)); break;
  5012. default: throw new Error("unparsed value: " + blob[blob.l]);
  5013. }
  5014. PropH[name] = val;
  5015. }
  5016. }
  5017. }
  5018. blob.l = start_addr + size; /* step ahead to skip padding */
  5019. return PropH;
  5020. }
  5021. var XLSPSSkip = [ "CodePage", "Thumbnail", "_PID_LINKBASE", "_PID_HLINKS", "SystemIdentifier", "FMTID" ].concat(PseudoPropsPairs);
  5022. function guess_property_type(val) {
  5023. switch(typeof val) {
  5024. case "boolean": return 0x0B;
  5025. case "number": return ((val|0)==val) ? 0x03 : 0x05;
  5026. case "string": return 0x1F;
  5027. case "object": if(val instanceof Date) return 0x40; break;
  5028. }
  5029. return -1;
  5030. }
  5031. function write_PropertySet(entries, RE, PIDSI) {
  5032. var hdr = new_buf(8), piao = [], prop = [];
  5033. var sz = 8, i = 0;
  5034. var pr = new_buf(8), pio = new_buf(8);
  5035. pr.write_shift(4, 0x0002);
  5036. pr.write_shift(4, 0x04B0);
  5037. pio.write_shift(4, 0x0001);
  5038. prop.push(pr); piao.push(pio);
  5039. sz += 8 + pr.length;
  5040. if(!RE) {
  5041. pio = new_buf(8);
  5042. pio.write_shift(4, 0);
  5043. piao.unshift(pio);
  5044. var bufs = [new_buf(4)];
  5045. bufs[0].write_shift(4, entries.length);
  5046. for(i = 0; i < entries.length; ++i) {
  5047. var value = entries[i][0];
  5048. pr = new_buf(4 + 4 + 2 * (value.length + 1) + (value.length % 2 ? 0 : 2));
  5049. pr.write_shift(4, i+2);
  5050. pr.write_shift(4, value.length + 1);
  5051. pr.write_shift(0, value, "dbcs");
  5052. while(pr.l != pr.length) pr.write_shift(1, 0);
  5053. bufs.push(pr);
  5054. }
  5055. pr = bconcat(bufs);
  5056. prop.unshift(pr);
  5057. sz += 8 + pr.length;
  5058. }
  5059. for(i = 0; i < entries.length; ++i) {
  5060. if(RE && !RE[entries[i][0]]) continue;
  5061. if(XLSPSSkip.indexOf(entries[i][0]) > -1) continue;
  5062. if(entries[i][1] == null) continue;
  5063. var val = entries[i][1], idx = 0;
  5064. if(RE) {
  5065. idx = +RE[entries[i][0]];
  5066. var pinfo = (PIDSI)[idx];
  5067. if(pinfo.p == "version" && typeof val == "string") {
  5068. var arr = val.split(".");
  5069. val = ((+arr[0])<<16) + ((+arr[1])||0);
  5070. }
  5071. pr = write_TypedPropertyValue(pinfo.t, val);
  5072. } else {
  5073. var T = guess_property_type(val);
  5074. if(T == -1) { T = 0x1F; val = String(val); }
  5075. pr = write_TypedPropertyValue(T, val);
  5076. }
  5077. prop.push(pr);
  5078. pio = new_buf(8);
  5079. pio.write_shift(4, !RE ? 2+i : idx);
  5080. piao.push(pio);
  5081. sz += 8 + pr.length;
  5082. }
  5083. var w = 8 * (prop.length + 1);
  5084. for(i = 0; i < prop.length; ++i) { piao[i].write_shift(4, w); w += prop[i].length; }
  5085. hdr.write_shift(4, sz);
  5086. hdr.write_shift(4, prop.length);
  5087. return bconcat([hdr].concat(piao).concat(prop));
  5088. }
  5089. /* [MS-OLEPS] 2.21 PropertySetStream */
  5090. function parse_PropertySetStream(file, PIDSI, clsid) {
  5091. var blob = file.content;
  5092. if(!blob) return ({});
  5093. prep_blob(blob, 0);
  5094. var NumSets, FMTID0, FMTID1, Offset0, Offset1 = 0;
  5095. blob.chk('feff', 'Byte Order: ');
  5096. /*var vers = */blob.read_shift(2); // TODO: check version
  5097. var SystemIdentifier = blob.read_shift(4);
  5098. var CLSID = blob.read_shift(16);
  5099. if(CLSID !== CFB.utils.consts.HEADER_CLSID && CLSID !== clsid) throw new Error("Bad PropertySet CLSID " + CLSID);
  5100. NumSets = blob.read_shift(4);
  5101. if(NumSets !== 1 && NumSets !== 2) throw new Error("Unrecognized #Sets: " + NumSets);
  5102. FMTID0 = blob.read_shift(16); Offset0 = blob.read_shift(4);
  5103. if(NumSets === 1 && Offset0 !== blob.l) throw new Error("Length mismatch: " + Offset0 + " !== " + blob.l);
  5104. else if(NumSets === 2) { FMTID1 = blob.read_shift(16); Offset1 = blob.read_shift(4); }
  5105. var PSet0 = parse_PropertySet(blob, PIDSI);
  5106. var rval = ({ SystemIdentifier: SystemIdentifier });
  5107. for(var y in PSet0) rval[y] = PSet0[y];
  5108. //rval.blob = blob;
  5109. rval.FMTID = FMTID0;
  5110. //rval.PSet0 = PSet0;
  5111. if(NumSets === 1) return rval;
  5112. if(Offset1 - blob.l == 2) blob.l += 2;
  5113. if(blob.l !== Offset1) throw new Error("Length mismatch 2: " + blob.l + " !== " + Offset1);
  5114. var PSet1;
  5115. try { PSet1 = parse_PropertySet(blob, null); } catch(e) {/* empty */}
  5116. for(y in PSet1) rval[y] = PSet1[y];
  5117. rval.FMTID = [FMTID0, FMTID1]; // TODO: verify FMTID0/1
  5118. return rval;
  5119. }
  5120. function write_PropertySetStream(entries, clsid, RE, PIDSI, entries2, clsid2) {
  5121. var hdr = new_buf(entries2 ? 68 : 48);
  5122. var bufs = [hdr];
  5123. hdr.write_shift(2, 0xFFFE);
  5124. hdr.write_shift(2, 0x0000); /* TODO: type 1 props */
  5125. hdr.write_shift(4, 0x32363237);
  5126. hdr.write_shift(16, CFB.utils.consts.HEADER_CLSID, "hex");
  5127. hdr.write_shift(4, (entries2 ? 2 : 1));
  5128. hdr.write_shift(16, clsid, "hex");
  5129. hdr.write_shift(4, (entries2 ? 68 : 48));
  5130. var ps0 = write_PropertySet(entries, RE, PIDSI);
  5131. bufs.push(ps0);
  5132. if(entries2) {
  5133. var ps1 = write_PropertySet(entries2, null, null);
  5134. hdr.write_shift(16, clsid2, "hex");
  5135. hdr.write_shift(4, 68 + ps0.length);
  5136. bufs.push(ps1);
  5137. }
  5138. return bconcat(bufs);
  5139. }
  5140. function parsenoop2(blob, length) { blob.read_shift(length); return null; }
  5141. function writezeroes(n, o) { if(!o) o=new_buf(n); for(var j=0; j<n; ++j) o.write_shift(1, 0); return o; }
  5142. function parslurp(blob, length, cb) {
  5143. var arr = [], target = blob.l + length;
  5144. while(blob.l < target) arr.push(cb(blob, target - blob.l));
  5145. if(target !== blob.l) throw new Error("Slurp error");
  5146. return arr;
  5147. }
  5148. function parsebool(blob, length) { return blob.read_shift(length) === 0x1; }
  5149. function writebool(v, o) { if(!o) o=new_buf(2); o.write_shift(2, +!!v); return o; }
  5150. function parseuint16(blob) { return blob.read_shift(2, 'u'); }
  5151. function writeuint16(v, o) { if(!o) o=new_buf(2); o.write_shift(2, v); return o; }
  5152. function parseuint16a(blob, length) { return parslurp(blob,length,parseuint16);}
  5153. /* --- 2.5 Structures --- */
  5154. /* [MS-XLS] 2.5.10 Bes (boolean or error) */
  5155. function parse_Bes(blob) {
  5156. var v = blob.read_shift(1), t = blob.read_shift(1);
  5157. return t === 0x01 ? v : v === 0x01;
  5158. }
  5159. function write_Bes(v, t, o) {
  5160. if(!o) o = new_buf(2);
  5161. o.write_shift(1, +v);
  5162. o.write_shift(1, ((t == 'e') ? 1 : 0));
  5163. return o;
  5164. }
  5165. /* [MS-XLS] 2.5.240 ShortXLUnicodeString */
  5166. function parse_ShortXLUnicodeString(blob, length, opts) {
  5167. var cch = blob.read_shift(opts && opts.biff >= 12 ? 2 : 1);
  5168. var encoding = 'sbcs-cont';
  5169. var cp = current_codepage;
  5170. if(opts && opts.biff >= 8) current_codepage = 1200;
  5171. if(!opts || opts.biff == 8 ) {
  5172. var fHighByte = blob.read_shift(1);
  5173. if(fHighByte) { encoding = 'dbcs-cont'; }
  5174. } else if(opts.biff == 12) {
  5175. encoding = 'wstr';
  5176. }
  5177. if(opts.biff >= 2 && opts.biff <= 5) encoding = 'cpstr';
  5178. var o = cch ? blob.read_shift(cch, encoding) : "";
  5179. current_codepage = cp;
  5180. return o;
  5181. }
  5182. /* 2.5.293 XLUnicodeRichExtendedString */
  5183. function parse_XLUnicodeRichExtendedString(blob) {
  5184. var cp = current_codepage;
  5185. current_codepage = 1200;
  5186. var cch = blob.read_shift(2), flags = blob.read_shift(1);
  5187. var /*fHighByte = flags & 0x1,*/ fExtSt = flags & 0x4, fRichSt = flags & 0x8;
  5188. var width = 1 + (flags & 0x1); // 0x0 -> utf8, 0x1 -> dbcs
  5189. var cRun = 0, cbExtRst;
  5190. var z = {};
  5191. if(fRichSt) cRun = blob.read_shift(2);
  5192. if(fExtSt) cbExtRst = blob.read_shift(4);
  5193. var encoding = width == 2 ? 'dbcs-cont' : 'sbcs-cont';
  5194. var msg = cch === 0 ? "" : blob.read_shift(cch, encoding);
  5195. if(fRichSt) blob.l += 4 * cRun; //TODO: parse this
  5196. if(fExtSt) blob.l += cbExtRst; //TODO: parse this
  5197. z.t = msg;
  5198. if(!fRichSt) { z.raw = "<t>" + z.t + "</t>"; z.r = z.t; }
  5199. current_codepage = cp;
  5200. return z;
  5201. }
  5202. function write_XLUnicodeRichExtendedString(xlstr) {
  5203. var str = (xlstr.t||""), nfmts = 1;
  5204. var hdr = new_buf(3 + (nfmts > 1 ? 2 : 0));
  5205. hdr.write_shift(2, str.length);
  5206. hdr.write_shift(1, (nfmts > 1 ? 0x08 : 0x00) | 0x01);
  5207. if(nfmts > 1) hdr.write_shift(2, nfmts);
  5208. var otext = new_buf(2 * str.length);
  5209. otext.write_shift(2 * str.length, str, 'utf16le');
  5210. var out = [hdr, otext];
  5211. return bconcat(out);
  5212. }
  5213. /* 2.5.296 XLUnicodeStringNoCch */
  5214. function parse_XLUnicodeStringNoCch(blob, cch, opts) {
  5215. var retval;
  5216. if(opts) {
  5217. if(opts.biff >= 2 && opts.biff <= 5) return blob.read_shift(cch, 'cpstr');
  5218. if(opts.biff >= 12) return blob.read_shift(cch, 'dbcs-cont');
  5219. }
  5220. var fHighByte = blob.read_shift(1);
  5221. if(fHighByte===0) { retval = blob.read_shift(cch, 'sbcs-cont'); }
  5222. else { retval = blob.read_shift(cch, 'dbcs-cont'); }
  5223. return retval;
  5224. }
  5225. /* 2.5.294 XLUnicodeString */
  5226. function parse_XLUnicodeString(blob, length, opts) {
  5227. var cch = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  5228. if(cch === 0) { blob.l++; return ""; }
  5229. return parse_XLUnicodeStringNoCch(blob, cch, opts);
  5230. }
  5231. /* BIFF5 override */
  5232. function parse_XLUnicodeString2(blob, length, opts) {
  5233. if(opts.biff > 5) return parse_XLUnicodeString(blob, length, opts);
  5234. var cch = blob.read_shift(1);
  5235. if(cch === 0) { blob.l++; return ""; }
  5236. return blob.read_shift(cch, (opts.biff <= 4 || !blob.lens ) ? 'cpstr' : 'sbcs-cont');
  5237. }
  5238. /* TODO: BIFF5 and lower, codepage awareness */
  5239. function write_XLUnicodeString(str, opts, o) {
  5240. if(!o) o = new_buf(3 + 2 * str.length);
  5241. o.write_shift(2, str.length);
  5242. o.write_shift(1, 1);
  5243. o.write_shift(31, str, 'utf16le');
  5244. return o;
  5245. }
  5246. /* [MS-XLS] 2.5.61 ControlInfo */
  5247. function parse_ControlInfo(blob) {
  5248. var flags = blob.read_shift(1);
  5249. blob.l++;
  5250. var accel = blob.read_shift(2);
  5251. blob.l += 2;
  5252. return [flags, accel];
  5253. }
  5254. /* [MS-OSHARED] 2.3.7.6 URLMoniker TODO: flags */
  5255. function parse_URLMoniker(blob) {
  5256. var len = blob.read_shift(4), start = blob.l;
  5257. var extra = false;
  5258. if(len > 24) {
  5259. /* look ahead */
  5260. blob.l += len - 24;
  5261. if(blob.read_shift(16) === "795881f43b1d7f48af2c825dc4852763") extra = true;
  5262. blob.l = start;
  5263. }
  5264. var url = blob.read_shift((extra?len-24:len)>>1, 'utf16le').replace(chr0,"");
  5265. if(extra) blob.l += 24;
  5266. return url;
  5267. }
  5268. /* [MS-OSHARED] 2.3.7.8 FileMoniker TODO: all fields */
  5269. function parse_FileMoniker(blob) {
  5270. blob.l += 2; //var cAnti = blob.read_shift(2);
  5271. var ansiPath = blob.read_shift(0, 'lpstr-ansi');
  5272. blob.l += 2; //var endServer = blob.read_shift(2);
  5273. if(blob.read_shift(2) != 0xDEAD) throw new Error("Bad FileMoniker");
  5274. var sz = blob.read_shift(4);
  5275. if(sz === 0) return ansiPath.replace(/\\/g,"/");
  5276. var bytes = blob.read_shift(4);
  5277. if(blob.read_shift(2) != 3) throw new Error("Bad FileMoniker");
  5278. var unicodePath = blob.read_shift(bytes>>1, 'utf16le').replace(chr0,"");
  5279. return unicodePath;
  5280. }
  5281. /* [MS-OSHARED] 2.3.7.2 HyperlinkMoniker TODO: all the monikers */
  5282. function parse_HyperlinkMoniker(blob, length) {
  5283. var clsid = blob.read_shift(16); length -= 16;
  5284. switch(clsid) {
  5285. case "e0c9ea79f9bace118c8200aa004ba90b": return parse_URLMoniker(blob, length);
  5286. case "0303000000000000c000000000000046": return parse_FileMoniker(blob, length);
  5287. default: throw new Error("Unsupported Moniker " + clsid);
  5288. }
  5289. }
  5290. /* [MS-OSHARED] 2.3.7.9 HyperlinkString */
  5291. function parse_HyperlinkString(blob) {
  5292. var len = blob.read_shift(4);
  5293. var o = len > 0 ? blob.read_shift(len, 'utf16le').replace(chr0, "") : "";
  5294. return o;
  5295. }
  5296. /* [MS-OSHARED] 2.3.7.1 Hyperlink Object */
  5297. function parse_Hyperlink(blob, length) {
  5298. var end = blob.l + length;
  5299. var sVer = blob.read_shift(4);
  5300. if(sVer !== 2) throw new Error("Unrecognized streamVersion: " + sVer);
  5301. var flags = blob.read_shift(2);
  5302. blob.l += 2;
  5303. var displayName, targetFrameName, moniker, oleMoniker, Loc="", guid, fileTime;
  5304. if(flags & 0x0010) displayName = parse_HyperlinkString(blob, end - blob.l);
  5305. if(flags & 0x0080) targetFrameName = parse_HyperlinkString(blob, end - blob.l);
  5306. if((flags & 0x0101) === 0x0101) moniker = parse_HyperlinkString(blob, end - blob.l);
  5307. if((flags & 0x0101) === 0x0001) oleMoniker = parse_HyperlinkMoniker(blob, end - blob.l);
  5308. if(flags & 0x0008) Loc = parse_HyperlinkString(blob, end - blob.l);
  5309. if(flags & 0x0020) guid = blob.read_shift(16);
  5310. if(flags & 0x0040) fileTime = parse_FILETIME(blob/*, 8*/);
  5311. blob.l = end;
  5312. var target = targetFrameName||moniker||oleMoniker||"";
  5313. if(target && Loc) target+="#"+Loc;
  5314. if(!target) target = "#" + Loc;
  5315. var out = ({Target:target});
  5316. if(guid) out.guid = guid;
  5317. if(fileTime) out.time = fileTime;
  5318. if(displayName) out.Tooltip = displayName;
  5319. return out;
  5320. }
  5321. function write_Hyperlink(hl) {
  5322. var out = new_buf(512), i = 0;
  5323. var Target = hl.Target;
  5324. var F = Target.indexOf("#") > -1 ? 0x1f : 0x17;
  5325. switch(Target.charAt(0)) { case "#": F=0x1c; break; case ".": F&=~2; break; }
  5326. out.write_shift(4,2); out.write_shift(4, F);
  5327. var data = [8,6815827,6619237,4849780,83]; for(i = 0; i < data.length; ++i) out.write_shift(4, data[i]);
  5328. if(F == 0x1C) {
  5329. Target = Target.slice(1);
  5330. out.write_shift(4, Target.length + 1);
  5331. for(i = 0; i < Target.length; ++i) out.write_shift(2, Target.charCodeAt(i));
  5332. out.write_shift(2, 0);
  5333. } else if(F & 0x02) {
  5334. data = "e0 c9 ea 79 f9 ba ce 11 8c 82 00 aa 00 4b a9 0b".split(" ");
  5335. for(i = 0; i < data.length; ++i) out.write_shift(1, parseInt(data[i], 16));
  5336. out.write_shift(4, 2*(Target.length + 1));
  5337. for(i = 0; i < Target.length; ++i) out.write_shift(2, Target.charCodeAt(i));
  5338. out.write_shift(2, 0);
  5339. } else {
  5340. data = "03 03 00 00 00 00 00 00 c0 00 00 00 00 00 00 46".split(" ");
  5341. for(i = 0; i < data.length; ++i) out.write_shift(1, parseInt(data[i], 16));
  5342. var P = 0;
  5343. while(Target.slice(P*3,P*3+3)=="../"||Target.slice(P*3,P*3+3)=="..\\") ++P;
  5344. out.write_shift(2, P);
  5345. out.write_shift(4, Target.length + 1);
  5346. for(i = 0; i < Target.length; ++i) out.write_shift(1, Target.charCodeAt(i) & 0xFF);
  5347. out.write_shift(1, 0);
  5348. out.write_shift(2, 0xFFFF);
  5349. out.write_shift(2, 0xDEAD);
  5350. for(i = 0; i < 6; ++i) out.write_shift(4, 0);
  5351. }
  5352. return out.slice(0, out.l);
  5353. }
  5354. /* 2.5.178 LongRGBA */
  5355. function parse_LongRGBA(blob) { 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]; }
  5356. /* 2.5.177 LongRGB */
  5357. function parse_LongRGB(blob, length) { var x = parse_LongRGBA(blob, length); x[3] = 0; return x; }
  5358. /* [MS-XLS] 2.5.19 */
  5359. function parse_XLSCell(blob) {
  5360. var rw = blob.read_shift(2); // 0-indexed
  5361. var col = blob.read_shift(2);
  5362. var ixfe = blob.read_shift(2);
  5363. return ({r:rw, c:col, ixfe:ixfe});
  5364. }
  5365. function write_XLSCell(R, C, ixfe, o) {
  5366. if(!o) o = new_buf(6);
  5367. o.write_shift(2, R);
  5368. o.write_shift(2, C);
  5369. o.write_shift(2, ixfe||0);
  5370. return o;
  5371. }
  5372. /* [MS-XLS] 2.5.134 */
  5373. function parse_frtHeader(blob) {
  5374. var rt = blob.read_shift(2);
  5375. var flags = blob.read_shift(2); // TODO: parse these flags
  5376. blob.l += 8;
  5377. return {type: rt, flags: flags};
  5378. }
  5379. function parse_OptXLUnicodeString(blob, length, opts) { return length === 0 ? "" : parse_XLUnicodeString2(blob, length, opts); }
  5380. /* [MS-XLS] 2.5.344 */
  5381. function parse_XTI(blob, length, opts) {
  5382. var w = opts.biff > 8 ? 4 : 2;
  5383. var iSupBook = blob.read_shift(w), itabFirst = blob.read_shift(w,'i'), itabLast = blob.read_shift(w,'i');
  5384. return [iSupBook, itabFirst, itabLast];
  5385. }
  5386. /* [MS-XLS] 2.5.218 */
  5387. function parse_RkRec(blob) {
  5388. var ixfe = blob.read_shift(2);
  5389. var RK = parse_RkNumber(blob);
  5390. return [ixfe, RK];
  5391. }
  5392. /* [MS-XLS] 2.5.1 */
  5393. function parse_AddinUdf(blob, length, opts) {
  5394. blob.l += 4; length -= 4;
  5395. var l = blob.l + length;
  5396. var udfName = parse_ShortXLUnicodeString(blob, length, opts);
  5397. var cb = blob.read_shift(2);
  5398. l -= blob.l;
  5399. if(cb !== l) throw new Error("Malformed AddinUdf: padding = " + l + " != " + cb);
  5400. blob.l += cb;
  5401. return udfName;
  5402. }
  5403. /* [MS-XLS] 2.5.209 TODO: Check sizes */
  5404. function parse_Ref8U(blob) {
  5405. var rwFirst = blob.read_shift(2);
  5406. var rwLast = blob.read_shift(2);
  5407. var colFirst = blob.read_shift(2);
  5408. var colLast = blob.read_shift(2);
  5409. return {s:{c:colFirst, r:rwFirst}, e:{c:colLast,r:rwLast}};
  5410. }
  5411. function write_Ref8U(r, o) {
  5412. if(!o) o = new_buf(8);
  5413. o.write_shift(2, r.s.r);
  5414. o.write_shift(2, r.e.r);
  5415. o.write_shift(2, r.s.c);
  5416. o.write_shift(2, r.e.c);
  5417. return o;
  5418. }
  5419. /* [MS-XLS] 2.5.211 */
  5420. function parse_RefU(blob) {
  5421. var rwFirst = blob.read_shift(2);
  5422. var rwLast = blob.read_shift(2);
  5423. var colFirst = blob.read_shift(1);
  5424. var colLast = blob.read_shift(1);
  5425. return {s:{c:colFirst, r:rwFirst}, e:{c:colLast,r:rwLast}};
  5426. }
  5427. /* [MS-XLS] 2.5.207 */
  5428. var parse_Ref = parse_RefU;
  5429. /* [MS-XLS] 2.5.143 */
  5430. function parse_FtCmo(blob) {
  5431. blob.l += 4;
  5432. var ot = blob.read_shift(2);
  5433. var id = blob.read_shift(2);
  5434. var flags = blob.read_shift(2);
  5435. blob.l+=12;
  5436. return [id, ot, flags];
  5437. }
  5438. /* [MS-XLS] 2.5.149 */
  5439. function parse_FtNts(blob) {
  5440. var out = {};
  5441. blob.l += 4;
  5442. blob.l += 16; // GUID TODO
  5443. out.fSharedNote = blob.read_shift(2);
  5444. blob.l += 4;
  5445. return out;
  5446. }
  5447. /* [MS-XLS] 2.5.142 */
  5448. function parse_FtCf(blob) {
  5449. var out = {};
  5450. blob.l += 4;
  5451. blob.cf = blob.read_shift(2);
  5452. return out;
  5453. }
  5454. /* [MS-XLS] 2.5.140 - 2.5.154 and friends */
  5455. function parse_FtSkip(blob) { blob.l += 2; blob.l += blob.read_shift(2); }
  5456. var FtTab = {
  5457. 0x00: parse_FtSkip, /* FtEnd */
  5458. 0x04: parse_FtSkip, /* FtMacro */
  5459. 0x05: parse_FtSkip, /* FtButton */
  5460. 0x06: parse_FtSkip, /* FtGmo */
  5461. 0x07: parse_FtCf, /* FtCf */
  5462. 0x08: parse_FtSkip, /* FtPioGrbit */
  5463. 0x09: parse_FtSkip, /* FtPictFmla */
  5464. 0x0A: parse_FtSkip, /* FtCbls */
  5465. 0x0B: parse_FtSkip, /* FtRbo */
  5466. 0x0C: parse_FtSkip, /* FtSbs */
  5467. 0x0D: parse_FtNts, /* FtNts */
  5468. 0x0E: parse_FtSkip, /* FtSbsFmla */
  5469. 0x0F: parse_FtSkip, /* FtGboData */
  5470. 0x10: parse_FtSkip, /* FtEdoData */
  5471. 0x11: parse_FtSkip, /* FtRboData */
  5472. 0x12: parse_FtSkip, /* FtCblsData */
  5473. 0x13: parse_FtSkip, /* FtLbsData */
  5474. 0x14: parse_FtSkip, /* FtCblsFmla */
  5475. 0x15: parse_FtCmo
  5476. };
  5477. function parse_FtArray(blob, length) {
  5478. var tgt = blob.l + length;
  5479. var fts = [];
  5480. while(blob.l < tgt) {
  5481. var ft = blob.read_shift(2);
  5482. blob.l-=2;
  5483. try {
  5484. fts.push(FtTab[ft](blob, tgt - blob.l));
  5485. } catch(e) { blob.l = tgt; return fts; }
  5486. }
  5487. if(blob.l != tgt) blob.l = tgt; //throw new Error("bad Object Ft-sequence");
  5488. return fts;
  5489. }
  5490. /* --- 2.4 Records --- */
  5491. /* [MS-XLS] 2.4.21 */
  5492. function parse_BOF(blob, length) {
  5493. var o = {BIFFVer:0, dt:0};
  5494. o.BIFFVer = blob.read_shift(2); length -= 2;
  5495. if(length >= 2) { o.dt = blob.read_shift(2); blob.l -= 2; }
  5496. switch(o.BIFFVer) {
  5497. case 0x0600: /* BIFF8 */
  5498. case 0x0500: /* BIFF5 */
  5499. case 0x0400: /* BIFF4 */
  5500. case 0x0300: /* BIFF3 */
  5501. case 0x0200: /* BIFF2 */
  5502. case 0x0002: case 0x0007: /* BIFF2 */
  5503. break;
  5504. default: if(length > 6) throw new Error("Unexpected BIFF Ver " + o.BIFFVer);
  5505. }
  5506. blob.read_shift(length);
  5507. return o;
  5508. }
  5509. function write_BOF(wb, t, o) {
  5510. var h = 0x0600, w = 16;
  5511. switch(o.bookType) {
  5512. case 'biff8': break;
  5513. case 'biff5': h = 0x0500; w = 8; break;
  5514. case 'biff4': h = 0x0004; w = 6; break;
  5515. case 'biff3': h = 0x0003; w = 6; break;
  5516. case 'biff2': h = 0x0002; w = 4; break;
  5517. case 'xla': break;
  5518. default: throw new Error("unsupported BIFF version");
  5519. }
  5520. var out = new_buf(w);
  5521. out.write_shift(2, h);
  5522. out.write_shift(2, t);
  5523. if(w > 4) out.write_shift(2, 0x7262);
  5524. if(w > 6) out.write_shift(2, 0x07CD);
  5525. if(w > 8) {
  5526. out.write_shift(2, 0xC009);
  5527. out.write_shift(2, 0x0001);
  5528. out.write_shift(2, 0x0706);
  5529. out.write_shift(2, 0x0000);
  5530. }
  5531. return out;
  5532. }
  5533. /* [MS-XLS] 2.4.146 */
  5534. function parse_InterfaceHdr(blob, length) {
  5535. if(length === 0) return 0x04b0;
  5536. if((blob.read_shift(2))!==0x04b0){/* empty */}
  5537. return 0x04b0;
  5538. }
  5539. /* [MS-XLS] 2.4.349 */
  5540. function parse_WriteAccess(blob, length, opts) {
  5541. if(opts.enc) { blob.l += length; return ""; }
  5542. var l = blob.l;
  5543. // TODO: make sure XLUnicodeString doesnt overrun
  5544. var UserName = parse_XLUnicodeString2(blob, 0, opts);
  5545. blob.read_shift(length + l - blob.l);
  5546. return UserName;
  5547. }
  5548. function write_WriteAccess(s, opts) {
  5549. var b8 = !opts || opts.biff == 8;
  5550. var o = new_buf(b8 ? 112 : 54);
  5551. o.write_shift(opts.biff == 8 ? 2 : 1, 7);
  5552. if(b8) o.write_shift(1, 0);
  5553. o.write_shift(4, 0x33336853);
  5554. o.write_shift(4, (0x00534A74 | (b8 ? 0 : 0x20000000)));
  5555. while(o.l < o.length) o.write_shift(1, (b8 ? 0 : 32));
  5556. return o;
  5557. }
  5558. /* [MS-XLS] 2.4.351 */
  5559. function parse_WsBool(blob, length, opts) {
  5560. var flags = opts && opts.biff == 8 || length == 2 ? blob.read_shift(2) : (blob.l += length, 0);
  5561. return { fDialog: flags & 0x10 };
  5562. }
  5563. /* [MS-XLS] 2.4.28 */
  5564. function parse_BoundSheet8(blob, length, opts) {
  5565. var pos = blob.read_shift(4);
  5566. var hidden = blob.read_shift(1) & 0x03;
  5567. var dt = blob.read_shift(1);
  5568. switch(dt) {
  5569. case 0: dt = 'Worksheet'; break;
  5570. case 1: dt = 'Macrosheet'; break;
  5571. case 2: dt = 'Chartsheet'; break;
  5572. case 6: dt = 'VBAModule'; break;
  5573. }
  5574. var name = parse_ShortXLUnicodeString(blob, 0, opts);
  5575. if(name.length === 0) name = "Sheet1";
  5576. return { pos:pos, hs:hidden, dt:dt, name:name };
  5577. }
  5578. function write_BoundSheet8(data, opts) {
  5579. var w = (!opts || opts.biff >= 8 ? 2 : 1);
  5580. var o = new_buf(8 + w * data.name.length);
  5581. o.write_shift(4, data.pos);
  5582. o.write_shift(1, data.hs || 0);
  5583. o.write_shift(1, data.dt);
  5584. o.write_shift(1, data.name.length);
  5585. if(opts.biff >= 8) o.write_shift(1, 1);
  5586. o.write_shift(w * data.name.length, data.name, opts.biff < 8 ? 'sbcs' : 'utf16le');
  5587. var out = o.slice(0, o.l);
  5588. out.l = o.l; return out;
  5589. }
  5590. /* [MS-XLS] 2.4.265 TODO */
  5591. function parse_SST(blob, length) {
  5592. var end = blob.l + length;
  5593. var cnt = blob.read_shift(4);
  5594. var ucnt = blob.read_shift(4);
  5595. var strs = ([]);
  5596. for(var i = 0; i != ucnt && blob.l < end; ++i) {
  5597. strs.push(parse_XLUnicodeRichExtendedString(blob));
  5598. }
  5599. strs.Count = cnt; strs.Unique = ucnt;
  5600. return strs;
  5601. }
  5602. function write_SST(sst, opts) {
  5603. var header = new_buf(8);
  5604. header.write_shift(4, sst.Count);
  5605. header.write_shift(4, sst.Unique);
  5606. var strs = [];
  5607. for(var j = 0; j < sst.length; ++j) strs[j] = write_XLUnicodeRichExtendedString(sst[j], opts);
  5608. var o = bconcat([header].concat(strs));
  5609. o.parts = [header.length].concat(strs.map(function(str) { return str.length; }));
  5610. return o;
  5611. }
  5612. /* [MS-XLS] 2.4.107 */
  5613. function parse_ExtSST(blob, length) {
  5614. var extsst = {};
  5615. extsst.dsst = blob.read_shift(2);
  5616. blob.l += length-2;
  5617. return extsst;
  5618. }
  5619. /* [MS-XLS] 2.4.221 TODO: check BIFF2-4 */
  5620. function parse_Row(blob) {
  5621. var z = ({});
  5622. z.r = blob.read_shift(2);
  5623. z.c = blob.read_shift(2);
  5624. z.cnt = blob.read_shift(2) - z.c;
  5625. var miyRw = blob.read_shift(2);
  5626. blob.l += 4; // reserved(2), unused(2)
  5627. var flags = blob.read_shift(1); // various flags
  5628. blob.l += 3; // reserved(8), ixfe(12), flags(4)
  5629. if(flags & 0x07) z.level = flags & 0x07;
  5630. // collapsed: flags & 0x10
  5631. if(flags & 0x20) z.hidden = true;
  5632. if(flags & 0x40) z.hpt = miyRw / 20;
  5633. return z;
  5634. }
  5635. /* [MS-XLS] 2.4.125 */
  5636. function parse_ForceFullCalculation(blob) {
  5637. var header = parse_frtHeader(blob);
  5638. if(header.type != 0x08A3) throw new Error("Invalid Future Record " + header.type);
  5639. var fullcalc = blob.read_shift(4);
  5640. return fullcalc !== 0x0;
  5641. }
  5642. /* [MS-XLS] 2.4.215 rt */
  5643. function parse_RecalcId(blob) {
  5644. blob.read_shift(2);
  5645. return blob.read_shift(4);
  5646. }
  5647. /* [MS-XLS] 2.4.87 */
  5648. function parse_DefaultRowHeight(blob, length, opts) {
  5649. var f = 0;
  5650. if(!(opts && opts.biff == 2)) {
  5651. f = blob.read_shift(2);
  5652. }
  5653. var miyRw = blob.read_shift(2);
  5654. if((opts && opts.biff == 2)) {
  5655. f = 1 - (miyRw >> 15); miyRw &= 0x7fff;
  5656. }
  5657. var fl = {Unsynced:f&1,DyZero:(f&2)>>1,ExAsc:(f&4)>>2,ExDsc:(f&8)>>3};
  5658. return [fl, miyRw];
  5659. }
  5660. /* [MS-XLS] 2.4.345 TODO */
  5661. function parse_Window1(blob) {
  5662. var xWn = blob.read_shift(2), yWn = blob.read_shift(2), dxWn = blob.read_shift(2), dyWn = blob.read_shift(2);
  5663. var flags = blob.read_shift(2), iTabCur = blob.read_shift(2), iTabFirst = blob.read_shift(2);
  5664. var ctabSel = blob.read_shift(2), wTabRatio = blob.read_shift(2);
  5665. return { Pos: [xWn, yWn], Dim: [dxWn, dyWn], Flags: flags, CurTab: iTabCur,
  5666. FirstTab: iTabFirst, Selected: ctabSel, TabRatio: wTabRatio };
  5667. }
  5668. function write_Window1() {
  5669. var o = new_buf(18);
  5670. o.write_shift(2, 0);
  5671. o.write_shift(2, 0);
  5672. o.write_shift(2, 0x7260);
  5673. o.write_shift(2, 0x44c0);
  5674. o.write_shift(2, 0x38);
  5675. o.write_shift(2, 0);
  5676. o.write_shift(2, 0);
  5677. o.write_shift(2, 1);
  5678. o.write_shift(2, 0x01f4);
  5679. return o;
  5680. }
  5681. /* [MS-XLS] 2.4.346 TODO */
  5682. function parse_Window2(blob, length, opts) {
  5683. if(opts && opts.biff >= 2 && opts.biff < 5) return {};
  5684. var f = blob.read_shift(2);
  5685. return { RTL: f & 0x40 };
  5686. }
  5687. function write_Window2(view) {
  5688. var o = new_buf(18), f = 0x6b6;
  5689. if(view && view.RTL) f |= 0x40;
  5690. o.write_shift(2, f);
  5691. o.write_shift(4, 0);
  5692. o.write_shift(4, 64);
  5693. o.write_shift(4, 0);
  5694. o.write_shift(4, 0);
  5695. return o;
  5696. }
  5697. /* [MS-XLS] 2.4.189 TODO */
  5698. function parse_Pane(/*blob, length, opts*/) {
  5699. }
  5700. /* [MS-XLS] 2.4.122 TODO */
  5701. function parse_Font(blob, length, opts) {
  5702. var o = {
  5703. dyHeight: blob.read_shift(2),
  5704. fl: blob.read_shift(2)
  5705. };
  5706. switch((opts && opts.biff) || 8) {
  5707. case 2: break;
  5708. case 3: case 4: blob.l += 2; break;
  5709. default: blob.l += 10; break;
  5710. }
  5711. o.name = parse_ShortXLUnicodeString(blob, 0, opts);
  5712. return o;
  5713. }
  5714. function write_Font(data, opts) {
  5715. var name = data.name || "Arial";
  5716. var b5 = (opts && (opts.biff == 5)), w = (b5 ? (15 + name.length) : (16 + 2 * name.length));
  5717. var o = new_buf(w);
  5718. o.write_shift(2, (data.sz || 12) * 20);
  5719. o.write_shift(4, 0);
  5720. o.write_shift(2, 400);
  5721. o.write_shift(4, 0);
  5722. o.write_shift(2, 0);
  5723. o.write_shift(1, name.length);
  5724. if(!b5) o.write_shift(1, 1);
  5725. o.write_shift((b5 ? 1 : 2) * name.length, name, (b5 ? "sbcs" : "utf16le"));
  5726. return o;
  5727. }
  5728. /* [MS-XLS] 2.4.149 */
  5729. function parse_LabelSst(blob) {
  5730. var cell = parse_XLSCell(blob);
  5731. cell.isst = blob.read_shift(4);
  5732. return cell;
  5733. }
  5734. function write_LabelSst(R, C, v, os ) {
  5735. var o = new_buf(10);
  5736. write_XLSCell(R, C, os, o);
  5737. o.write_shift(4, v);
  5738. return o;
  5739. }
  5740. /* [MS-XLS] 2.4.148 */
  5741. function parse_Label(blob, length, opts) {
  5742. var target = blob.l + length;
  5743. var cell = parse_XLSCell(blob, 6);
  5744. if(opts.biff == 2) blob.l++;
  5745. var str = parse_XLUnicodeString(blob, target - blob.l, opts);
  5746. cell.val = str;
  5747. return cell;
  5748. }
  5749. function write_Label(R, C, v, os, opts) {
  5750. var b8 = !opts || opts.biff == 8;
  5751. var o = new_buf(6 + 2 + (+b8) + (1 + b8) * v.length);
  5752. write_XLSCell(R, C, os, o);
  5753. o.write_shift(2, v.length);
  5754. if(b8) o.write_shift(1, 1);
  5755. o.write_shift((1 + b8) * v.length, v, b8 ? 'utf16le' : 'sbcs');
  5756. return o;
  5757. }
  5758. /* [MS-XLS] 2.4.126 Number Formats */
  5759. function parse_Format(blob, length, opts) {
  5760. var numFmtId = blob.read_shift(2);
  5761. var fmtstr = parse_XLUnicodeString2(blob, 0, opts);
  5762. return [numFmtId, fmtstr];
  5763. }
  5764. function write_Format(i, f, opts, o) {
  5765. var b5 = (opts && (opts.biff == 5));
  5766. if(!o) o = new_buf(b5 ? (3 + f.length) : (5 + 2 * f.length));
  5767. o.write_shift(2, i);
  5768. o.write_shift((b5 ? 1 : 2), f.length);
  5769. if(!b5) o.write_shift(1, 1);
  5770. o.write_shift((b5 ? 1 : 2) * f.length, f, (b5 ? 'sbcs' : 'utf16le'));
  5771. var out = (o.length > o.l) ? o.slice(0, o.l) : o;
  5772. if(out.l == null) out.l = out.length;
  5773. return out;
  5774. }
  5775. var parse_BIFF2Format = parse_XLUnicodeString2;
  5776. /* [MS-XLS] 2.4.90 */
  5777. function parse_Dimensions(blob, length, opts) {
  5778. var end = blob.l + length;
  5779. var w = opts.biff == 8 || !opts.biff ? 4 : 2;
  5780. var r = blob.read_shift(w), R = blob.read_shift(w);
  5781. var c = blob.read_shift(2), C = blob.read_shift(2);
  5782. blob.l = end;
  5783. return {s: {r:r, c:c}, e: {r:R, c:C}};
  5784. }
  5785. function write_Dimensions(range, opts) {
  5786. var w = opts.biff == 8 || !opts.biff ? 4 : 2;
  5787. var o = new_buf(2*w + 6);
  5788. o.write_shift(w, range.s.r);
  5789. o.write_shift(w, range.e.r + 1);
  5790. o.write_shift(2, range.s.c);
  5791. o.write_shift(2, range.e.c + 1);
  5792. o.write_shift(2, 0);
  5793. return o;
  5794. }
  5795. /* [MS-XLS] 2.4.220 */
  5796. function parse_RK(blob) {
  5797. var rw = blob.read_shift(2), col = blob.read_shift(2);
  5798. var rkrec = parse_RkRec(blob);
  5799. return {r:rw, c:col, ixfe:rkrec[0], rknum:rkrec[1]};
  5800. }
  5801. /* [MS-XLS] 2.4.175 */
  5802. function parse_MulRk(blob, length) {
  5803. var target = blob.l + length - 2;
  5804. var rw = blob.read_shift(2), col = blob.read_shift(2);
  5805. var rkrecs = [];
  5806. while(blob.l < target) rkrecs.push(parse_RkRec(blob));
  5807. if(blob.l !== target) throw new Error("MulRK read error");
  5808. var lastcol = blob.read_shift(2);
  5809. if(rkrecs.length != lastcol - col + 1) throw new Error("MulRK length mismatch");
  5810. return {r:rw, c:col, C:lastcol, rkrec:rkrecs};
  5811. }
  5812. /* [MS-XLS] 2.4.174 */
  5813. function parse_MulBlank(blob, length) {
  5814. var target = blob.l + length - 2;
  5815. var rw = blob.read_shift(2), col = blob.read_shift(2);
  5816. var ixfes = [];
  5817. while(blob.l < target) ixfes.push(blob.read_shift(2));
  5818. if(blob.l !== target) throw new Error("MulBlank read error");
  5819. var lastcol = blob.read_shift(2);
  5820. if(ixfes.length != lastcol - col + 1) throw new Error("MulBlank length mismatch");
  5821. return {r:rw, c:col, C:lastcol, ixfe:ixfes};
  5822. }
  5823. /* [MS-XLS] 2.5.20 2.5.249 TODO: interpret values here */
  5824. function parse_CellStyleXF(blob, length, style, opts) {
  5825. var o = {};
  5826. var a = blob.read_shift(4), b = blob.read_shift(4);
  5827. var c = blob.read_shift(4), d = blob.read_shift(2);
  5828. o.patternType = XLSFillPattern[c >> 26];
  5829. if(!opts.cellStyles) return o;
  5830. o.alc = a & 0x07;
  5831. o.fWrap = (a >> 3) & 0x01;
  5832. o.alcV = (a >> 4) & 0x07;
  5833. o.fJustLast = (a >> 7) & 0x01;
  5834. o.trot = (a >> 8) & 0xFF;
  5835. o.cIndent = (a >> 16) & 0x0F;
  5836. o.fShrinkToFit = (a >> 20) & 0x01;
  5837. o.iReadOrder = (a >> 22) & 0x02;
  5838. o.fAtrNum = (a >> 26) & 0x01;
  5839. o.fAtrFnt = (a >> 27) & 0x01;
  5840. o.fAtrAlc = (a >> 28) & 0x01;
  5841. o.fAtrBdr = (a >> 29) & 0x01;
  5842. o.fAtrPat = (a >> 30) & 0x01;
  5843. o.fAtrProt = (a >> 31) & 0x01;
  5844. o.dgLeft = b & 0x0F;
  5845. o.dgRight = (b >> 4) & 0x0F;
  5846. o.dgTop = (b >> 8) & 0x0F;
  5847. o.dgBottom = (b >> 12) & 0x0F;
  5848. o.icvLeft = (b >> 16) & 0x7F;
  5849. o.icvRight = (b >> 23) & 0x7F;
  5850. o.grbitDiag = (b >> 30) & 0x03;
  5851. o.icvTop = c & 0x7F;
  5852. o.icvBottom = (c >> 7) & 0x7F;
  5853. o.icvDiag = (c >> 14) & 0x7F;
  5854. o.dgDiag = (c >> 21) & 0x0F;
  5855. o.icvFore = d & 0x7F;
  5856. o.icvBack = (d >> 7) & 0x7F;
  5857. o.fsxButton = (d >> 14) & 0x01;
  5858. return o;
  5859. }
  5860. //function parse_CellXF(blob, length, opts) {return parse_CellStyleXF(blob,length,0, opts);}
  5861. //function parse_StyleXF(blob, length, opts) {return parse_CellStyleXF(blob,length,1, opts);}
  5862. /* [MS-XLS] 2.4.353 TODO: actually do this right */
  5863. function parse_XF(blob, length, opts) {
  5864. var o = {};
  5865. o.ifnt = blob.read_shift(2); o.numFmtId = blob.read_shift(2); o.flags = blob.read_shift(2);
  5866. o.fStyle = (o.flags >> 2) & 0x01;
  5867. length -= 6;
  5868. o.data = parse_CellStyleXF(blob, length, o.fStyle, opts);
  5869. return o;
  5870. }
  5871. function write_XF(data, ixfeP, opts, o) {
  5872. var b5 = (opts && (opts.biff == 5));
  5873. if(!o) o = new_buf(b5 ? 16 : 20);
  5874. o.write_shift(2, 0);
  5875. if(data.style) {
  5876. o.write_shift(2, (data.numFmtId||0));
  5877. o.write_shift(2, 0xFFF4);
  5878. } else {
  5879. o.write_shift(2, (data.numFmtId||0));
  5880. o.write_shift(2, (ixfeP<<4));
  5881. }
  5882. o.write_shift(4, 0);
  5883. o.write_shift(4, 0);
  5884. if(!b5) o.write_shift(4, 0);
  5885. o.write_shift(2, 0);
  5886. return o;
  5887. }
  5888. /* [MS-XLS] 2.4.134 */
  5889. function parse_Guts(blob) {
  5890. blob.l += 4;
  5891. var out = [blob.read_shift(2), blob.read_shift(2)];
  5892. if(out[0] !== 0) out[0]--;
  5893. if(out[1] !== 0) out[1]--;
  5894. if(out[0] > 7 || out[1] > 7) throw new Error("Bad Gutters: " + out.join("|"));
  5895. return out;
  5896. }
  5897. function write_Guts(guts) {
  5898. var o = new_buf(8);
  5899. o.write_shift(4, 0);
  5900. o.write_shift(2, guts[0] ? guts[0] + 1 : 0);
  5901. o.write_shift(2, guts[1] ? guts[1] + 1 : 0);
  5902. return o;
  5903. }
  5904. /* [MS-XLS] 2.4.24 */
  5905. function parse_BoolErr(blob, length, opts) {
  5906. var cell = parse_XLSCell(blob, 6);
  5907. if(opts.biff == 2) ++blob.l;
  5908. var val = parse_Bes(blob, 2);
  5909. cell.val = val;
  5910. cell.t = (val === true || val === false) ? 'b' : 'e';
  5911. return cell;
  5912. }
  5913. function write_BoolErr(R, C, v, os, opts, t) {
  5914. var o = new_buf(8);
  5915. write_XLSCell(R, C, os, o);
  5916. write_Bes(v, t, o);
  5917. return o;
  5918. }
  5919. /* [MS-XLS] 2.4.180 Number */
  5920. function parse_Number(blob) {
  5921. var cell = parse_XLSCell(blob, 6);
  5922. var xnum = parse_Xnum(blob, 8);
  5923. cell.val = xnum;
  5924. return cell;
  5925. }
  5926. function write_Number(R, C, v, os) {
  5927. var o = new_buf(14);
  5928. write_XLSCell(R, C, os, o);
  5929. write_Xnum(v, o);
  5930. return o;
  5931. }
  5932. var parse_XLHeaderFooter = parse_OptXLUnicodeString; // TODO: parse 2.4.136
  5933. /* [MS-XLS] 2.4.271 */
  5934. function parse_SupBook(blob, length, opts) {
  5935. var end = blob.l + length;
  5936. var ctab = blob.read_shift(2);
  5937. var cch = blob.read_shift(2);
  5938. opts.sbcch = cch;
  5939. if(cch == 0x0401 || cch == 0x3A01) return [cch, ctab];
  5940. if(cch < 0x01 || cch >0xff) throw new Error("Unexpected SupBook type: "+cch);
  5941. var virtPath = parse_XLUnicodeStringNoCch(blob, cch);
  5942. /* TODO: 2.5.277 Virtual Path */
  5943. var rgst = [];
  5944. while(end > blob.l) rgst.push(parse_XLUnicodeString(blob));
  5945. return [cch, ctab, virtPath, rgst];
  5946. }
  5947. /* [MS-XLS] 2.4.105 TODO */
  5948. function parse_ExternName(blob, length, opts) {
  5949. var flags = blob.read_shift(2);
  5950. var body;
  5951. var o = ({
  5952. fBuiltIn: flags & 0x01,
  5953. fWantAdvise: (flags >>> 1) & 0x01,
  5954. fWantPict: (flags >>> 2) & 0x01,
  5955. fOle: (flags >>> 3) & 0x01,
  5956. fOleLink: (flags >>> 4) & 0x01,
  5957. cf: (flags >>> 5) & 0x3FF,
  5958. fIcon: flags >>> 15 & 0x01
  5959. });
  5960. if(opts.sbcch === 0x3A01) body = parse_AddinUdf(blob, length-2, opts);
  5961. //else throw new Error("unsupported SupBook cch: " + opts.sbcch);
  5962. o.body = body || blob.read_shift(length-2);
  5963. if(typeof body === "string") o.Name = body;
  5964. return o;
  5965. }
  5966. /* [MS-XLS] 2.4.150 TODO */
  5967. var XLSLblBuiltIn = [
  5968. "_xlnm.Consolidate_Area",
  5969. "_xlnm.Auto_Open",
  5970. "_xlnm.Auto_Close",
  5971. "_xlnm.Extract",
  5972. "_xlnm.Database",
  5973. "_xlnm.Criteria",
  5974. "_xlnm.Print_Area",
  5975. "_xlnm.Print_Titles",
  5976. "_xlnm.Recorder",
  5977. "_xlnm.Data_Form",
  5978. "_xlnm.Auto_Activate",
  5979. "_xlnm.Auto_Deactivate",
  5980. "_xlnm.Sheet_Title",
  5981. "_xlnm._FilterDatabase"
  5982. ];
  5983. function parse_Lbl(blob, length, opts) {
  5984. var target = blob.l + length;
  5985. var flags = blob.read_shift(2);
  5986. var chKey = blob.read_shift(1);
  5987. var cch = blob.read_shift(1);
  5988. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  5989. var itab = 0;
  5990. if(!opts || opts.biff >= 5) {
  5991. if(opts.biff != 5) blob.l += 2;
  5992. itab = blob.read_shift(2);
  5993. if(opts.biff == 5) blob.l += 2;
  5994. blob.l += 4;
  5995. }
  5996. var name = parse_XLUnicodeStringNoCch(blob, cch, opts);
  5997. if(flags & 0x20) name = XLSLblBuiltIn[name.charCodeAt(0)];
  5998. var npflen = target - blob.l; if(opts && opts.biff == 2) --npflen;
  5999. var rgce = target == blob.l || cce === 0 ? [] : parse_NameParsedFormula(blob, npflen, opts, cce);
  6000. return {
  6001. chKey: chKey,
  6002. Name: name,
  6003. itab: itab,
  6004. rgce: rgce
  6005. };
  6006. }
  6007. /* [MS-XLS] 2.4.106 TODO: verify filename encoding */
  6008. function parse_ExternSheet(blob, length, opts) {
  6009. if(opts.biff < 8) return parse_BIFF5ExternSheet(blob, length, opts);
  6010. var o = [], target = blob.l + length, len = blob.read_shift(opts.biff > 8 ? 4 : 2);
  6011. while(len-- !== 0) o.push(parse_XTI(blob, opts.biff > 8 ? 12 : 6, opts));
  6012. // [iSupBook, itabFirst, itabLast];
  6013. if(blob.l != target) throw new Error("Bad ExternSheet: " + blob.l + " != " + target);
  6014. return o;
  6015. }
  6016. function parse_BIFF5ExternSheet(blob, length, opts) {
  6017. if(blob[blob.l + 1] == 0x03) blob[blob.l]++;
  6018. var o = parse_ShortXLUnicodeString(blob, length, opts);
  6019. return o.charCodeAt(0) == 0x03 ? o.slice(1) : o;
  6020. }
  6021. /* [MS-XLS] 2.4.176 TODO: check older biff */
  6022. function parse_NameCmt(blob, length, opts) {
  6023. if(opts.biff < 8) { blob.l += length; return; }
  6024. var cchName = blob.read_shift(2);
  6025. var cchComment = blob.read_shift(2);
  6026. var name = parse_XLUnicodeStringNoCch(blob, cchName, opts);
  6027. var comment = parse_XLUnicodeStringNoCch(blob, cchComment, opts);
  6028. return [name, comment];
  6029. }
  6030. /* [MS-XLS] 2.4.260 */
  6031. function parse_ShrFmla(blob, length, opts) {
  6032. var ref = parse_RefU(blob, 6);
  6033. blob.l++;
  6034. var cUse = blob.read_shift(1);
  6035. length -= 8;
  6036. return [parse_SharedParsedFormula(blob, length, opts), cUse, ref];
  6037. }
  6038. /* [MS-XLS] 2.4.4 TODO */
  6039. function parse_Array(blob, length, opts) {
  6040. var ref = parse_Ref(blob, 6);
  6041. /* TODO: fAlwaysCalc */
  6042. switch(opts.biff) {
  6043. case 2: blob.l ++; length -= 7; break;
  6044. case 3: case 4: blob.l += 2; length -= 8; break;
  6045. default: blob.l += 6; length -= 12;
  6046. }
  6047. return [ref, parse_ArrayParsedFormula(blob, length, opts, ref)];
  6048. }
  6049. /* [MS-XLS] 2.4.173 */
  6050. function parse_MTRSettings(blob) {
  6051. var fMTREnabled = blob.read_shift(4) !== 0x00;
  6052. var fUserSetThreadCount = blob.read_shift(4) !== 0x00;
  6053. var cUserThreadCount = blob.read_shift(4);
  6054. return [fMTREnabled, fUserSetThreadCount, cUserThreadCount];
  6055. }
  6056. /* [MS-XLS] 2.5.186 TODO: BIFF5 */
  6057. function parse_NoteSh(blob, length, opts) {
  6058. if(opts.biff < 8) return;
  6059. var row = blob.read_shift(2), col = blob.read_shift(2);
  6060. var flags = blob.read_shift(2), idObj = blob.read_shift(2);
  6061. var stAuthor = parse_XLUnicodeString2(blob, 0, opts);
  6062. if(opts.biff < 8) blob.read_shift(1);
  6063. return [{r:row,c:col}, stAuthor, idObj, flags];
  6064. }
  6065. /* [MS-XLS] 2.4.179 */
  6066. function parse_Note(blob, length, opts) {
  6067. /* TODO: Support revisions */
  6068. return parse_NoteSh(blob, length, opts);
  6069. }
  6070. /* [MS-XLS] 2.4.168 */
  6071. function parse_MergeCells(blob, length) {
  6072. var merges = [];
  6073. var cmcs = blob.read_shift(2);
  6074. while (cmcs--) merges.push(parse_Ref8U(blob,length));
  6075. return merges;
  6076. }
  6077. function write_MergeCells(merges) {
  6078. var o = new_buf(2 + merges.length * 8);
  6079. o.write_shift(2, merges.length);
  6080. for(var i = 0; i < merges.length; ++i) write_Ref8U(merges[i], o);
  6081. return o;
  6082. }
  6083. /* [MS-XLS] 2.4.181 TODO: parse all the things! */
  6084. function parse_Obj(blob, length, opts) {
  6085. if(opts && opts.biff < 8) return parse_BIFF5Obj(blob, length, opts);
  6086. var cmo = parse_FtCmo(blob, 22); // id, ot, flags
  6087. var fts = parse_FtArray(blob, length-22, cmo[1]);
  6088. return { cmo: cmo, ft:fts };
  6089. }
  6090. /* from older spec */
  6091. var parse_BIFF5OT = [];
  6092. parse_BIFF5OT[0x08] = function(blob, length) {
  6093. var tgt = blob.l + length;
  6094. blob.l += 10; // todo
  6095. var cf = blob.read_shift(2);
  6096. blob.l += 4;
  6097. blob.l += 2; //var cbPictFmla = blob.read_shift(2);
  6098. blob.l += 2;
  6099. blob.l += 2; //var grbit = blob.read_shift(2);
  6100. blob.l += 4;
  6101. var cchName = blob.read_shift(1);
  6102. blob.l += cchName; // TODO: stName
  6103. blob.l = tgt; // TODO: fmla
  6104. return { fmt:cf };
  6105. };
  6106. function parse_BIFF5Obj(blob, length, opts) {
  6107. blob.l += 4; //var cnt = blob.read_shift(4);
  6108. var ot = blob.read_shift(2);
  6109. var id = blob.read_shift(2);
  6110. var grbit = blob.read_shift(2);
  6111. blob.l += 2; //var colL = blob.read_shift(2);
  6112. blob.l += 2; //var dxL = blob.read_shift(2);
  6113. blob.l += 2; //var rwT = blob.read_shift(2);
  6114. blob.l += 2; //var dyT = blob.read_shift(2);
  6115. blob.l += 2; //var colR = blob.read_shift(2);
  6116. blob.l += 2; //var dxR = blob.read_shift(2);
  6117. blob.l += 2; //var rwB = blob.read_shift(2);
  6118. blob.l += 2; //var dyB = blob.read_shift(2);
  6119. blob.l += 2; //var cbMacro = blob.read_shift(2);
  6120. blob.l += 6;
  6121. length -= 36;
  6122. var fts = [];
  6123. fts.push((parse_BIFF5OT[ot]||parsenoop)(blob, length, opts));
  6124. return { cmo: [id, ot, grbit], ft:fts };
  6125. }
  6126. /* [MS-XLS] 2.4.329 TODO: parse properly */
  6127. function parse_TxO(blob, length, opts) {
  6128. var s = blob.l;
  6129. var texts = "";
  6130. try {
  6131. blob.l += 4;
  6132. var ot = (opts.lastobj||{cmo:[0,0]}).cmo[1];
  6133. var controlInfo; // eslint-disable-line no-unused-vars
  6134. if([0,5,7,11,12,14].indexOf(ot) == -1) blob.l += 6;
  6135. else controlInfo = parse_ControlInfo(blob, 6, opts);
  6136. var cchText = blob.read_shift(2);
  6137. /*var cbRuns = */blob.read_shift(2);
  6138. /*var ifntEmpty = */parseuint16(blob, 2);
  6139. var len = blob.read_shift(2);
  6140. blob.l += len;
  6141. //var fmla = parse_ObjFmla(blob, s + length - blob.l);
  6142. for(var i = 1; i < blob.lens.length-1; ++i) {
  6143. if(blob.l-s != blob.lens[i]) throw new Error("TxO: bad continue record");
  6144. var hdr = blob[blob.l];
  6145. var t = parse_XLUnicodeStringNoCch(blob, blob.lens[i+1]-blob.lens[i]-1);
  6146. texts += t;
  6147. if(texts.length >= (hdr ? cchText : 2*cchText)) break;
  6148. }
  6149. if(texts.length !== cchText && texts.length !== cchText*2) {
  6150. throw new Error("cchText: " + cchText + " != " + texts.length);
  6151. }
  6152. blob.l = s + length;
  6153. /* [MS-XLS] 2.5.272 TxORuns */
  6154. // var rgTxoRuns = [];
  6155. // for(var j = 0; j != cbRuns/8-1; ++j) blob.l += 8;
  6156. // var cchText2 = blob.read_shift(2);
  6157. // if(cchText2 !== cchText) throw new Error("TxOLastRun mismatch: " + cchText2 + " " + cchText);
  6158. // blob.l += 6;
  6159. // if(s + length != blob.l) throw new Error("TxO " + (s + length) + ", at " + blob.l);
  6160. return { t: texts };
  6161. } catch(e) { blob.l = s + length; return { t: texts }; }
  6162. }
  6163. /* [MS-XLS] 2.4.140 */
  6164. function parse_HLink(blob, length) {
  6165. var ref = parse_Ref8U(blob, 8);
  6166. blob.l += 16; /* CLSID */
  6167. var hlink = parse_Hyperlink(blob, length-24);
  6168. return [ref, hlink];
  6169. }
  6170. function write_HLink(hl) {
  6171. var O = new_buf(24);
  6172. var ref = decode_cell(hl[0]);
  6173. O.write_shift(2, ref.r); O.write_shift(2, ref.r);
  6174. O.write_shift(2, ref.c); O.write_shift(2, ref.c);
  6175. var clsid = "d0 c9 ea 79 f9 ba ce 11 8c 82 00 aa 00 4b a9 0b".split(" ");
  6176. for(var i = 0; i < 16; ++i) O.write_shift(1, parseInt(clsid[i], 16));
  6177. return bconcat([O, write_Hyperlink(hl[1])]);
  6178. }
  6179. /* [MS-XLS] 2.4.141 */
  6180. function parse_HLinkTooltip(blob, length) {
  6181. blob.read_shift(2);
  6182. var ref = parse_Ref8U(blob, 8);
  6183. var wzTooltip = blob.read_shift((length-10)/2, 'dbcs-cont');
  6184. wzTooltip = wzTooltip.replace(chr0,"");
  6185. return [ref, wzTooltip];
  6186. }
  6187. function write_HLinkTooltip(hl) {
  6188. var TT = hl[1].Tooltip;
  6189. var O = new_buf(10 + 2 * (TT.length + 1));
  6190. O.write_shift(2, 0x0800);
  6191. var ref = decode_cell(hl[0]);
  6192. O.write_shift(2, ref.r); O.write_shift(2, ref.r);
  6193. O.write_shift(2, ref.c); O.write_shift(2, ref.c);
  6194. for(var i = 0; i < TT.length; ++i) O.write_shift(2, TT.charCodeAt(i));
  6195. O.write_shift(2, 0);
  6196. return O;
  6197. }
  6198. /* [MS-XLS] 2.4.63 */
  6199. function parse_Country(blob) {
  6200. var o = [0,0], d;
  6201. d = blob.read_shift(2); o[0] = CountryEnum[d] || d;
  6202. d = blob.read_shift(2); o[1] = CountryEnum[d] || d;
  6203. return o;
  6204. }
  6205. function write_Country(o) {
  6206. if(!o) o = new_buf(4);
  6207. o.write_shift(2, 0x01);
  6208. o.write_shift(2, 0x01);
  6209. return o;
  6210. }
  6211. /* [MS-XLS] 2.4.50 ClrtClient */
  6212. function parse_ClrtClient(blob) {
  6213. var ccv = blob.read_shift(2);
  6214. var o = [];
  6215. while(ccv-->0) o.push(parse_LongRGB(blob, 8));
  6216. return o;
  6217. }
  6218. /* [MS-XLS] 2.4.188 */
  6219. function parse_Palette(blob) {
  6220. var ccv = blob.read_shift(2);
  6221. var o = [];
  6222. while(ccv-->0) o.push(parse_LongRGB(blob, 8));
  6223. return o;
  6224. }
  6225. /* [MS-XLS] 2.4.354 */
  6226. function parse_XFCRC(blob) {
  6227. blob.l += 2;
  6228. var o = {cxfs:0, crc:0};
  6229. o.cxfs = blob.read_shift(2);
  6230. o.crc = blob.read_shift(4);
  6231. return o;
  6232. }
  6233. /* [MS-XLS] 2.4.53 TODO: parse flags */
  6234. /* [MS-XLSB] 2.4.323 TODO: parse flags */
  6235. function parse_ColInfo(blob, length, opts) {
  6236. if(!opts.cellStyles) return parsenoop(blob, length);
  6237. var w = opts && opts.biff >= 12 ? 4 : 2;
  6238. var colFirst = blob.read_shift(w);
  6239. var colLast = blob.read_shift(w);
  6240. var coldx = blob.read_shift(w);
  6241. var ixfe = blob.read_shift(w);
  6242. var flags = blob.read_shift(2);
  6243. if(w == 2) blob.l += 2;
  6244. var o = ({s:colFirst, e:colLast, w:coldx, ixfe:ixfe, flags:flags});
  6245. if(opts.biff >= 5 || !opts.biff) o.level = (flags >> 8) & 0x7;
  6246. return o;
  6247. }
  6248. /* [MS-XLS] 2.4.257 */
  6249. function parse_Setup(blob, length) {
  6250. var o = {};
  6251. if(length < 32) return o;
  6252. blob.l += 16;
  6253. o.header = parse_Xnum(blob, 8);
  6254. o.footer = parse_Xnum(blob, 8);
  6255. blob.l += 2;
  6256. return o;
  6257. }
  6258. /* [MS-XLS] 2.4.261 */
  6259. function parse_ShtProps(blob, length, opts) {
  6260. var def = {area:false};
  6261. if(opts.biff != 5) { blob.l += length; return def; }
  6262. var d = blob.read_shift(1); blob.l += 3;
  6263. if((d & 0x10)) def.area = true;
  6264. return def;
  6265. }
  6266. /* [MS-XLS] 2.4.241 */
  6267. function write_RRTabId(n) {
  6268. var out = new_buf(2 * n);
  6269. for(var i = 0; i < n; ++i) out.write_shift(2, i+1);
  6270. return out;
  6271. }
  6272. var parse_Blank = parse_XLSCell; /* [MS-XLS] 2.4.20 Just the cell */
  6273. var parse_Scl = parseuint16a; /* [MS-XLS] 2.4.247 num, den */
  6274. var parse_String = parse_XLUnicodeString; /* [MS-XLS] 2.4.268 */
  6275. /* --- Specific to versions before BIFF8 --- */
  6276. function parse_ImData(blob) {
  6277. var cf = blob.read_shift(2);
  6278. var env = blob.read_shift(2);
  6279. var lcb = blob.read_shift(4);
  6280. var o = {fmt:cf, env:env, len:lcb, data:blob.slice(blob.l,blob.l+lcb)};
  6281. blob.l += lcb;
  6282. return o;
  6283. }
  6284. /* BIFF2_??? where ??? is the name from [XLS] */
  6285. function parse_BIFF2STR(blob, length, opts) {
  6286. var cell = parse_XLSCell(blob, 6);
  6287. ++blob.l;
  6288. var str = parse_XLUnicodeString2(blob, length-7, opts);
  6289. cell.t = 'str';
  6290. cell.val = str;
  6291. return cell;
  6292. }
  6293. function parse_BIFF2NUM(blob) {
  6294. var cell = parse_XLSCell(blob, 6);
  6295. ++blob.l;
  6296. var num = parse_Xnum(blob, 8);
  6297. cell.t = 'n';
  6298. cell.val = num;
  6299. return cell;
  6300. }
  6301. function write_BIFF2NUM(r, c, val) {
  6302. var out = new_buf(15);
  6303. write_BIFF2Cell(out, r, c);
  6304. out.write_shift(8, val, 'f');
  6305. return out;
  6306. }
  6307. function parse_BIFF2INT(blob) {
  6308. var cell = parse_XLSCell(blob, 6);
  6309. ++blob.l;
  6310. var num = blob.read_shift(2);
  6311. cell.t = 'n';
  6312. cell.val = num;
  6313. return cell;
  6314. }
  6315. function write_BIFF2INT(r, c, val) {
  6316. var out = new_buf(9);
  6317. write_BIFF2Cell(out, r, c);
  6318. out.write_shift(2, val);
  6319. return out;
  6320. }
  6321. function parse_BIFF2STRING(blob) {
  6322. var cch = blob.read_shift(1);
  6323. if(cch === 0) { blob.l++; return ""; }
  6324. return blob.read_shift(cch, 'sbcs-cont');
  6325. }
  6326. /* TODO: convert to BIFF8 font struct */
  6327. function parse_BIFF2FONTXTRA(blob, length) {
  6328. blob.l += 6; // unknown
  6329. blob.l += 2; // font weight "bls"
  6330. blob.l += 1; // charset
  6331. blob.l += 3; // unknown
  6332. blob.l += 1; // font family
  6333. blob.l += length - 13;
  6334. }
  6335. /* TODO: parse rich text runs */
  6336. function parse_RString(blob, length, opts) {
  6337. var end = blob.l + length;
  6338. var cell = parse_XLSCell(blob, 6);
  6339. var cch = blob.read_shift(2);
  6340. var str = parse_XLUnicodeStringNoCch(blob, cch, opts);
  6341. blob.l = end;
  6342. cell.t = 'str';
  6343. cell.val = str;
  6344. return cell;
  6345. }
  6346. /* from js-harb (C) 2014-present SheetJS */
  6347. var DBF = (function() {
  6348. var dbf_codepage_map = {
  6349. /* Code Pages Supported by Visual FoxPro */
  6350. 0x01: 437, 0x02: 850,
  6351. 0x03: 1252, 0x04: 10000,
  6352. 0x64: 852, 0x65: 866,
  6353. 0x66: 865, 0x67: 861,
  6354. 0x68: 895, 0x69: 620,
  6355. 0x6A: 737, 0x6B: 857,
  6356. 0x78: 950, 0x79: 949,
  6357. 0x7A: 936, 0x7B: 932,
  6358. 0x7C: 874, 0x7D: 1255,
  6359. 0x7E: 1256, 0x96: 10007,
  6360. 0x97: 10029, 0x98: 10006,
  6361. 0xC8: 1250, 0xC9: 1251,
  6362. 0xCA: 1254, 0xCB: 1253,
  6363. /* shapefile DBF extension */
  6364. 0x00: 20127, 0x08: 865,
  6365. 0x09: 437, 0x0A: 850,
  6366. 0x0B: 437, 0x0D: 437,
  6367. 0x0E: 850, 0x0F: 437,
  6368. 0x10: 850, 0x11: 437,
  6369. 0x12: 850, 0x13: 932,
  6370. 0x14: 850, 0x15: 437,
  6371. 0x16: 850, 0x17: 865,
  6372. 0x18: 437, 0x19: 437,
  6373. 0x1A: 850, 0x1B: 437,
  6374. 0x1C: 863, 0x1D: 850,
  6375. 0x1F: 852, 0x22: 852,
  6376. 0x23: 852, 0x24: 860,
  6377. 0x25: 850, 0x26: 866,
  6378. 0x37: 850, 0x40: 852,
  6379. 0x4D: 936, 0x4E: 949,
  6380. 0x4F: 950, 0x50: 874,
  6381. 0x57: 1252, 0x58: 1252,
  6382. 0x59: 1252,
  6383. 0xFF: 16969
  6384. };
  6385. var dbf_reverse_map = evert({
  6386. 0x01: 437, 0x02: 850,
  6387. 0x03: 1252, 0x04: 10000,
  6388. 0x64: 852, 0x65: 866,
  6389. 0x66: 865, 0x67: 861,
  6390. 0x68: 895, 0x69: 620,
  6391. 0x6A: 737, 0x6B: 857,
  6392. 0x78: 950, 0x79: 949,
  6393. 0x7A: 936, 0x7B: 932,
  6394. 0x7C: 874, 0x7D: 1255,
  6395. 0x7E: 1256, 0x96: 10007,
  6396. 0x97: 10029, 0x98: 10006,
  6397. 0xC8: 1250, 0xC9: 1251,
  6398. 0xCA: 1254, 0xCB: 1253,
  6399. 0x00: 20127
  6400. });
  6401. var DBF_SUPPORTED_VERSIONS = [0x02, 0x03, 0x30, 0x31, 0x83, 0x8B, 0x8C, 0xF5];
  6402. /* TODO: find an actual specification */
  6403. function dbf_to_aoa(buf, opts) {
  6404. var out = [];
  6405. /* TODO: browser based */
  6406. var d = (new_raw_buf(1));
  6407. switch(opts.type) {
  6408. case 'base64': d = s2a(Base64.decode(buf)); break;
  6409. case 'binary': d = s2a(buf); break;
  6410. case 'buffer':
  6411. case 'array': d = buf; break;
  6412. }
  6413. prep_blob(d, 0);
  6414. /* header */
  6415. var ft = d.read_shift(1);
  6416. var memo = false;
  6417. var vfp = false, l7 = false;
  6418. switch(ft) {
  6419. case 0x02: case 0x03: break;
  6420. case 0x30: vfp = true; memo = true; break;
  6421. case 0x31: vfp = true; break;
  6422. case 0x83: memo = true; break;
  6423. case 0x8B: memo = true; break;
  6424. case 0x8C: memo = true; l7 = true; break;
  6425. case 0xF5: memo = true; break;
  6426. default: throw new Error("DBF Unsupported Version: " + ft.toString(16));
  6427. }
  6428. var /*filedate = new Date(),*/ nrow = 0, fpos = 0;
  6429. if(ft == 0x02) nrow = d.read_shift(2);
  6430. /*filedate = new Date(d.read_shift(1) + 1900, d.read_shift(1) - 1, d.read_shift(1));*/d.l += 3;
  6431. if(ft != 0x02) nrow = d.read_shift(4);
  6432. if(ft != 0x02) fpos = d.read_shift(2);
  6433. var rlen = d.read_shift(2);
  6434. var /*flags = 0,*/ current_cp = 1252;
  6435. if(ft != 0x02) {
  6436. d.l+=16;
  6437. /*flags = */d.read_shift(1);
  6438. //if(memo && ((flags & 0x02) === 0)) throw new Error("DBF Flags " + flags.toString(16) + " ft " + ft.toString(16));
  6439. /* codepage present in FoxPro */
  6440. if(d[d.l] !== 0) current_cp = dbf_codepage_map[d[d.l]];
  6441. d.l+=1;
  6442. d.l+=2;
  6443. }
  6444. if(l7) d.l += 36;
  6445. var fields = [], field = ({});
  6446. var hend = fpos - 10 - (vfp ? 264 : 0), ww = l7 ? 32 : 11;
  6447. while(ft == 0x02 ? d.l < d.length && d[d.l] != 0x0d: d.l < hend) {
  6448. field = ({});
  6449. field.name = cptable.utils.decode(current_cp, d.slice(d.l, d.l+ww)).replace(/[\u0000\r\n].*$/g,"");
  6450. d.l += ww;
  6451. field.type = String.fromCharCode(d.read_shift(1));
  6452. if(ft != 0x02 && !l7) field.offset = d.read_shift(4);
  6453. field.len = d.read_shift(1);
  6454. if(ft == 0x02) field.offset = d.read_shift(2);
  6455. field.dec = d.read_shift(1);
  6456. if(field.name.length) fields.push(field);
  6457. if(ft != 0x02) d.l += l7 ? 13 : 14;
  6458. switch(field.type) {
  6459. case 'B': // VFP Double
  6460. if((!vfp || field.len != 8) && opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
  6461. break;
  6462. case 'G': // General
  6463. case 'P': // Picture
  6464. if(opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
  6465. break;
  6466. case 'C': // character
  6467. case 'D': // date
  6468. case 'F': // floating point
  6469. case 'I': // long
  6470. case 'L': // boolean
  6471. case 'M': // memo
  6472. case 'N': // number
  6473. case 'O': // double
  6474. case 'T': // datetime
  6475. case 'Y': // currency
  6476. case '0': // VFP _NullFlags
  6477. case '@': // timestamp
  6478. case '+': // autoincrement
  6479. break;
  6480. default: throw new Error('Unknown Field Type: ' + field.type);
  6481. }
  6482. }
  6483. if(d[d.l] !== 0x0D) d.l = fpos-1;
  6484. else if(ft == 0x02) d.l = 0x209;
  6485. if(ft != 0x02) {
  6486. if(d.read_shift(1) !== 0x0D) throw new Error("DBF Terminator not found " + d.l + " " + d[d.l]);
  6487. d.l = fpos;
  6488. }
  6489. /* data */
  6490. var R = 0, C = 0;
  6491. out[0] = [];
  6492. for(C = 0; C != fields.length; ++C) out[0][C] = fields[C].name;
  6493. while(nrow-- > 0) {
  6494. if(d[d.l] === 0x2A) { d.l+=rlen; continue; }
  6495. ++d.l;
  6496. out[++R] = []; C = 0;
  6497. for(C = 0; C != fields.length; ++C) {
  6498. var dd = d.slice(d.l, d.l+fields[C].len); d.l+=fields[C].len;
  6499. prep_blob(dd, 0);
  6500. var s = cptable.utils.decode(current_cp, dd);
  6501. switch(fields[C].type) {
  6502. case 'C':
  6503. out[R][C] = cptable.utils.decode(current_cp, dd);
  6504. out[R][C] = out[R][C].trim();
  6505. break;
  6506. case 'D':
  6507. if(s.length === 8) out[R][C] = new Date(+s.slice(0,4), +s.slice(4,6)-1, +s.slice(6,8));
  6508. else out[R][C] = s;
  6509. break;
  6510. case 'F': out[R][C] = parseFloat(s.trim()); break;
  6511. case '+': case 'I': out[R][C] = l7 ? dd.read_shift(-4, 'i') ^ 0x80000000 : dd.read_shift(4, 'i'); break;
  6512. case 'L': switch(s.toUpperCase()) {
  6513. case 'Y': case 'T': out[R][C] = true; break;
  6514. case 'N': case 'F': out[R][C] = false; break;
  6515. case ' ': case '?': out[R][C] = false; break; /* NOTE: technically uninitialized */
  6516. default: throw new Error("DBF Unrecognized L:|" + s + "|");
  6517. } break;
  6518. case 'M': /* TODO: handle memo files */
  6519. if(!memo) throw new Error("DBF Unexpected MEMO for type " + ft.toString(16));
  6520. out[R][C] = "##MEMO##" + (l7 ? parseInt(s.trim(), 10): dd.read_shift(4));
  6521. break;
  6522. case 'N': out[R][C] = +s.replace(/\u0000/g,"").trim(); break;
  6523. case '@': out[R][C] = new Date(dd.read_shift(-8, 'f') - 0x388317533400); break;
  6524. case 'T': out[R][C] = new Date((dd.read_shift(4) - 0x253D8C) * 0x5265C00 + dd.read_shift(4)); break;
  6525. case 'Y': out[R][C] = dd.read_shift(4,'i')/1e4; break;
  6526. case 'O': out[R][C] = -dd.read_shift(-8, 'f'); break;
  6527. case 'B': if(vfp && fields[C].len == 8) { out[R][C] = dd.read_shift(8,'f'); break; }
  6528. /* falls through */
  6529. case 'G': case 'P': dd.l += fields[C].len; break;
  6530. case '0':
  6531. if(fields[C].name === '_NullFlags') break;
  6532. /* falls through */
  6533. default: throw new Error("DBF Unsupported data type " + fields[C].type);
  6534. }
  6535. }
  6536. }
  6537. if(ft != 0x02) if(d.l < d.length && d[d.l++] != 0x1A) throw new Error("DBF EOF Marker missing " + (d.l-1) + " of " + d.length + " " + d[d.l-1].toString(16));
  6538. if(opts && opts.sheetRows) out = out.slice(0, opts.sheetRows);
  6539. return out;
  6540. }
  6541. function dbf_to_sheet(buf, opts) {
  6542. var o = opts || {};
  6543. if(!o.dateNF) o.dateNF = "yyyymmdd";
  6544. return aoa_to_sheet(dbf_to_aoa(buf, o), o);
  6545. }
  6546. function dbf_to_workbook(buf, opts) {
  6547. try { return sheet_to_workbook(dbf_to_sheet(buf, opts), opts); }
  6548. catch(e) { if(opts && opts.WTF) throw e; }
  6549. return ({SheetNames:[],Sheets:{}});
  6550. }
  6551. var _RLEN = { 'B': 8, 'C': 250, 'L': 1, 'D': 8, '?': 0, '': 0 };
  6552. function sheet_to_dbf(ws, opts) {
  6553. var o = opts || {};
  6554. if(+o.codepage >= 0) set_cp(+o.codepage);
  6555. if(o.type == "string") throw new Error("Cannot write DBF to JS string");
  6556. var ba = buf_array();
  6557. var aoa = sheet_to_json(ws, {header:1, raw:true, cellDates:true});
  6558. var headers = aoa[0], data = aoa.slice(1);
  6559. var i = 0, j = 0, hcnt = 0, rlen = 1;
  6560. for(i = 0; i < headers.length; ++i) {
  6561. if(i == null) continue;
  6562. ++hcnt;
  6563. if(typeof headers[i] === 'number') headers[i] = headers[i].toString(10);
  6564. if(typeof headers[i] !== 'string') throw new Error("DBF Invalid column name " + headers[i] + " |" + (typeof headers[i]) + "|");
  6565. if(headers.indexOf(headers[i]) !== i) for(j=0; j<1024;++j)
  6566. if(headers.indexOf(headers[i] + "_" + j) == -1) { headers[i] += "_" + j; break; }
  6567. }
  6568. var range = safe_decode_range(ws['!ref']);
  6569. var coltypes = [];
  6570. for(i = 0; i <= range.e.c - range.s.c; ++i) {
  6571. var col = [];
  6572. for(j=0; j < data.length; ++j) {
  6573. if(data[j][i] != null) col.push(data[j][i]);
  6574. }
  6575. if(col.length == 0 || headers[i] == null) { coltypes[i] = '?'; continue; }
  6576. var guess = '', _guess = '';
  6577. for(j = 0; j < col.length; ++j) {
  6578. switch(typeof col[j]) {
  6579. /* TODO: check if L2 compat is desired */
  6580. case 'number': _guess = 'B'; break;
  6581. case 'string': _guess = 'C'; break;
  6582. case 'boolean': _guess = 'L'; break;
  6583. case 'object': _guess = col[j] instanceof Date ? 'D' : 'C'; break;
  6584. default: _guess = 'C';
  6585. }
  6586. guess = guess && guess != _guess ? 'C' : _guess;
  6587. if(guess == 'C') break;
  6588. }
  6589. rlen += _RLEN[guess] || 0;
  6590. coltypes[i] = guess;
  6591. }
  6592. var h = ba.next(32);
  6593. h.write_shift(4, 0x13021130);
  6594. h.write_shift(4, data.length);
  6595. h.write_shift(2, 296 + 32 * hcnt);
  6596. h.write_shift(2, rlen);
  6597. for(i=0; i < 4; ++i) h.write_shift(4, 0);
  6598. h.write_shift(4, 0x00000000 | ((+dbf_reverse_map[current_ansi] || 0x03)<<8));
  6599. for(i = 0, j = 0; i < headers.length; ++i) {
  6600. if(headers[i] == null) continue;
  6601. var hf = ba.next(32);
  6602. var _f = (headers[i].slice(-10) + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00").slice(0, 11);
  6603. hf.write_shift(1, _f, "sbcs");
  6604. hf.write_shift(1, coltypes[i] == '?' ? 'C' : coltypes[i], "sbcs");
  6605. hf.write_shift(4, j);
  6606. hf.write_shift(1, _RLEN[coltypes[i]] || 0);
  6607. hf.write_shift(1, 0);
  6608. hf.write_shift(1, 0x02);
  6609. hf.write_shift(4, 0);
  6610. hf.write_shift(1, 0);
  6611. hf.write_shift(4, 0);
  6612. hf.write_shift(4, 0);
  6613. j += _RLEN[coltypes[i]] || 0;
  6614. }
  6615. var hb = ba.next(264);
  6616. hb.write_shift(4, 0x0000000D);
  6617. for(i=0; i < 65;++i) hb.write_shift(4, 0x00000000);
  6618. for(i=0; i < data.length; ++i) {
  6619. var rout = ba.next(rlen);
  6620. rout.write_shift(1, 0);
  6621. for(j=0; j<headers.length; ++j) {
  6622. if(headers[j] == null) continue;
  6623. switch(coltypes[j]) {
  6624. case 'L': rout.write_shift(1, data[i][j] == null ? 0x3F : data[i][j] ? 0x54 : 0x46); break;
  6625. case 'B': rout.write_shift(8, data[i][j]||0, 'f'); break;
  6626. case 'D':
  6627. if(!data[i][j]) rout.write_shift(8, "00000000", "sbcs");
  6628. else {
  6629. rout.write_shift(4, ("0000"+data[i][j].getFullYear()).slice(-4), "sbcs");
  6630. rout.write_shift(2, ("00"+(data[i][j].getMonth()+1)).slice(-2), "sbcs");
  6631. rout.write_shift(2, ("00"+data[i][j].getDate()).slice(-2), "sbcs");
  6632. } break;
  6633. case 'C':
  6634. var _s = String(data[i][j]||"");
  6635. rout.write_shift(1, _s, "sbcs");
  6636. for(hcnt=0; hcnt < 250-_s.length; ++hcnt) rout.write_shift(1, 0x20); break;
  6637. }
  6638. }
  6639. // data
  6640. }
  6641. ba.next(1).write_shift(1, 0x1A);
  6642. return ba.end();
  6643. }
  6644. return {
  6645. versions: DBF_SUPPORTED_VERSIONS,
  6646. to_workbook: dbf_to_workbook,
  6647. to_sheet: dbf_to_sheet,
  6648. from_sheet: sheet_to_dbf
  6649. };
  6650. })();
  6651. var SYLK = (function() {
  6652. /* TODO: stress test sequences */
  6653. var sylk_escapes = ({
  6654. AA:'À', BA:'Á', CA:'Â', DA:195, HA:'Ä', JA:197,
  6655. AE:'È', BE:'É', CE:'Ê', HE:'Ë',
  6656. AI:'Ì', BI:'Í', CI:'Î', HI:'Ï',
  6657. AO:'Ò', BO:'Ó', CO:'Ô', DO:213, HO:'Ö',
  6658. AU:'Ù', BU:'Ú', CU:'Û', HU:'Ü',
  6659. Aa:'à', Ba:'á', Ca:'â', Da:227, Ha:'ä', Ja:229,
  6660. Ae:'è', Be:'é', Ce:'ê', He:'ë',
  6661. Ai:'ì', Bi:'í', Ci:'î', Hi:'ï',
  6662. Ao:'ò', Bo:'ó', Co:'ô', Do:245, Ho:'ö',
  6663. Au:'ù', Bu:'ú', Cu:'û', Hu:'ü',
  6664. KC:'Ç', Kc:'ç', q:'æ', z:'œ', a:'Æ', j:'Œ',
  6665. DN:209, Dn:241, Hy:255,
  6666. S:169, c:170, R:174, B:180,
  6667. 0:176, 1:177, 2:178,
  6668. 3:179, 5:181, 6:182,
  6669. 7:183, Q:185, k:186, b:208, i:216, l:222, s:240, y:248,
  6670. "!":161, '"':162, "#":163, "(":164, "%":165, "'":167, "H ":168,
  6671. "+":171, ";":187, "<":188, "=":189, ">":190, "?":191, "{":223
  6672. });
  6673. var sylk_char_regex = new RegExp("\u001BN(" + keys(sylk_escapes).join("|").replace(/\|\|\|/, "|\\||").replace(/([?()+])/g,"\\$1") + "|\\|)", "gm");
  6674. var sylk_char_fn = function(_, $1){ var o = sylk_escapes[$1]; return typeof o == "number" ? _getansi(o) : o; };
  6675. var decode_sylk_char = function($$, $1, $2) { var newcc = (($1.charCodeAt(0) - 0x20)<<4) | ($2.charCodeAt(0) - 0x30); return newcc == 59 ? $$ : _getansi(newcc); };
  6676. sylk_escapes["|"] = 254;
  6677. /* TODO: find an actual specification */
  6678. function sylk_to_aoa(d, opts) {
  6679. switch(opts.type) {
  6680. case 'base64': return sylk_to_aoa_str(Base64.decode(d), opts);
  6681. case 'binary': return sylk_to_aoa_str(d, opts);
  6682. case 'buffer': return sylk_to_aoa_str(d.toString('binary'), opts);
  6683. case 'array': return sylk_to_aoa_str(cc2str(d), opts);
  6684. }
  6685. throw new Error("Unrecognized type " + opts.type);
  6686. }
  6687. function sylk_to_aoa_str(str, opts) {
  6688. var records = str.split(/[\n\r]+/), R = -1, C = -1, ri = 0, rj = 0, arr = [];
  6689. var formats = [];
  6690. var next_cell_format = null;
  6691. var sht = {}, rowinfo = [], colinfo = [], cw = [];
  6692. var Mval = 0, j;
  6693. if(+opts.codepage >= 0) set_cp(+opts.codepage);
  6694. for (; ri !== records.length; ++ri) {
  6695. Mval = 0;
  6696. var rstr=records[ri].trim().replace(/\x1B([\x20-\x2F])([\x30-\x3F])/g, decode_sylk_char).replace(sylk_char_regex, sylk_char_fn);
  6697. var record=rstr.replace(/;;/g, "\u0000").split(";").map(function(x) { return x.replace(/\u0000/g, ";"); });
  6698. var RT=record[0], val;
  6699. if(rstr.length > 0) switch(RT) {
  6700. case 'ID': break; /* header */
  6701. case 'E': break; /* EOF */
  6702. case 'B': break; /* dimensions */
  6703. case 'O': break; /* options? */
  6704. case 'P':
  6705. if(record[1].charAt(0) == 'P')
  6706. formats.push(rstr.slice(3).replace(/;;/g, ";"));
  6707. break;
  6708. case 'C':
  6709. var C_seen_K = false, C_seen_X = false;
  6710. for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
  6711. case 'X': C = parseInt(record[rj].slice(1))-1; C_seen_X = true; break;
  6712. case 'Y':
  6713. R = parseInt(record[rj].slice(1))-1; if(!C_seen_X) C = 0;
  6714. for(j = arr.length; j <= R; ++j) arr[j] = [];
  6715. break;
  6716. case 'K':
  6717. val = record[rj].slice(1);
  6718. if(val.charAt(0) === '"') val = val.slice(1,val.length - 1);
  6719. else if(val === 'TRUE') val = true;
  6720. else if(val === 'FALSE') val = false;
  6721. else if(!isNaN(fuzzynum(val))) {
  6722. val = fuzzynum(val);
  6723. if(next_cell_format !== null && SSF.is_date(next_cell_format)) val = numdate(val);
  6724. } else if(!isNaN(fuzzydate(val).getDate())) {
  6725. val = parseDate(val);
  6726. }
  6727. if(typeof cptable !== 'undefined' && typeof val == "string" && ((opts||{}).type != "string") && (opts||{}).codepage) val = cptable.utils.decode(opts.codepage, val);
  6728. C_seen_K = true;
  6729. break;
  6730. case 'E':
  6731. var formula = rc_to_a1(record[rj].slice(1), {r:R,c:C});
  6732. arr[R][C] = [arr[R][C], formula];
  6733. break;
  6734. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  6735. }
  6736. if(C_seen_K) { arr[R][C] = val; next_cell_format = null; }
  6737. break;
  6738. case 'F':
  6739. var F_seen = 0;
  6740. for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
  6741. case 'X': C = parseInt(record[rj].slice(1))-1; ++F_seen; break;
  6742. case 'Y':
  6743. R = parseInt(record[rj].slice(1))-1; /*C = 0;*/
  6744. for(j = arr.length; j <= R; ++j) arr[j] = [];
  6745. break;
  6746. case 'M': Mval = parseInt(record[rj].slice(1)) / 20; break;
  6747. case 'F': break; /* ??? */
  6748. case 'G': break; /* hide grid */
  6749. case 'P':
  6750. next_cell_format = formats[parseInt(record[rj].slice(1))];
  6751. break;
  6752. case 'S': break; /* cell style */
  6753. case 'D': break; /* column */
  6754. case 'N': break; /* font */
  6755. case 'W':
  6756. cw = record[rj].slice(1).split(" ");
  6757. for(j = parseInt(cw[0], 10); j <= parseInt(cw[1], 10); ++j) {
  6758. Mval = parseInt(cw[2], 10);
  6759. colinfo[j-1] = Mval === 0 ? {hidden:true}: {wch:Mval}; process_col(colinfo[j-1]);
  6760. } break;
  6761. case 'C': /* default column format */
  6762. C = parseInt(record[rj].slice(1))-1;
  6763. if(!colinfo[C]) colinfo[C] = {};
  6764. break;
  6765. case 'R': /* row properties */
  6766. R = parseInt(record[rj].slice(1))-1;
  6767. if(!rowinfo[R]) rowinfo[R] = {};
  6768. if(Mval > 0) { rowinfo[R].hpt = Mval; rowinfo[R].hpx = pt2px(Mval); }
  6769. else if(Mval === 0) rowinfo[R].hidden = true;
  6770. break;
  6771. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  6772. }
  6773. if(F_seen < 1) next_cell_format = null; break;
  6774. default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
  6775. }
  6776. }
  6777. if(rowinfo.length > 0) sht['!rows'] = rowinfo;
  6778. if(colinfo.length > 0) sht['!cols'] = colinfo;
  6779. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  6780. return [arr, sht];
  6781. }
  6782. function sylk_to_sheet(d, opts) {
  6783. var aoasht = sylk_to_aoa(d, opts);
  6784. var aoa = aoasht[0], ws = aoasht[1];
  6785. var o = aoa_to_sheet(aoa, opts);
  6786. keys(ws).forEach(function(k) { o[k] = ws[k]; });
  6787. return o;
  6788. }
  6789. function sylk_to_workbook(d, opts) { return sheet_to_workbook(sylk_to_sheet(d, opts), opts); }
  6790. function write_ws_cell_sylk(cell, ws, R, C) {
  6791. var o = "C;Y" + (R+1) + ";X" + (C+1) + ";K";
  6792. switch(cell.t) {
  6793. case 'n':
  6794. o += (cell.v||0);
  6795. if(cell.f && !cell.F) o += ";E" + a1_to_rc(cell.f, {r:R, c:C}); break;
  6796. case 'b': o += cell.v ? "TRUE" : "FALSE"; break;
  6797. case 'e': o += cell.w || cell.v; break;
  6798. case 'd': o += '"' + (cell.w || cell.v) + '"'; break;
  6799. case 's': o += '"' + cell.v.replace(/"/g,"") + '"'; break;
  6800. }
  6801. return o;
  6802. }
  6803. function write_ws_cols_sylk(out, cols) {
  6804. cols.forEach(function(col, i) {
  6805. var rec = "F;W" + (i+1) + " " + (i+1) + " ";
  6806. if(col.hidden) rec += "0";
  6807. else {
  6808. if(typeof col.width == 'number') col.wpx = width2px(col.width);
  6809. if(typeof col.wpx == 'number') col.wch = px2char(col.wpx);
  6810. if(typeof col.wch == 'number') rec += Math.round(col.wch);
  6811. }
  6812. if(rec.charAt(rec.length - 1) != " ") out.push(rec);
  6813. });
  6814. }
  6815. function write_ws_rows_sylk(out, rows) {
  6816. rows.forEach(function(row, i) {
  6817. var rec = "F;";
  6818. if(row.hidden) rec += "M0;";
  6819. else if(row.hpt) rec += "M" + 20 * row.hpt + ";";
  6820. else if(row.hpx) rec += "M" + 20 * px2pt(row.hpx) + ";";
  6821. if(rec.length > 2) out.push(rec + "R" + (i+1));
  6822. });
  6823. }
  6824. function sheet_to_sylk(ws, opts) {
  6825. var preamble = ["ID;PWXL;N;E"], o = [];
  6826. var r = safe_decode_range(ws['!ref']), cell;
  6827. var dense = Array.isArray(ws);
  6828. var RS = "\r\n";
  6829. preamble.push("P;PGeneral");
  6830. preamble.push("F;P0;DG0G8;M255");
  6831. if(ws['!cols']) write_ws_cols_sylk(preamble, ws['!cols']);
  6832. if(ws['!rows']) write_ws_rows_sylk(preamble, ws['!rows']);
  6833. preamble.push("B;Y" + (r.e.r - r.s.r + 1) + ";X" + (r.e.c - r.s.c + 1) + ";D" + [r.s.c,r.s.r,r.e.c,r.e.r].join(" "));
  6834. for(var R = r.s.r; R <= r.e.r; ++R) {
  6835. for(var C = r.s.c; C <= r.e.c; ++C) {
  6836. var coord = encode_cell({r:R,c:C});
  6837. cell = dense ? (ws[R]||[])[C]: ws[coord];
  6838. if(!cell || (cell.v == null && (!cell.f || cell.F))) continue;
  6839. o.push(write_ws_cell_sylk(cell, ws, R, C, opts));
  6840. }
  6841. }
  6842. return preamble.join(RS) + RS + o.join(RS) + RS + "E" + RS;
  6843. }
  6844. return {
  6845. to_workbook: sylk_to_workbook,
  6846. to_sheet: sylk_to_sheet,
  6847. from_sheet: sheet_to_sylk
  6848. };
  6849. })();
  6850. var DIF = (function() {
  6851. function dif_to_aoa(d, opts) {
  6852. switch(opts.type) {
  6853. case 'base64': return dif_to_aoa_str(Base64.decode(d), opts);
  6854. case 'binary': return dif_to_aoa_str(d, opts);
  6855. case 'buffer': return dif_to_aoa_str(d.toString('binary'), opts);
  6856. case 'array': return dif_to_aoa_str(cc2str(d), opts);
  6857. }
  6858. throw new Error("Unrecognized type " + opts.type);
  6859. }
  6860. function dif_to_aoa_str(str, opts) {
  6861. var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
  6862. for (; ri !== records.length; ++ri) {
  6863. if (records[ri].trim() === 'BOT') { arr[++R] = []; C = 0; continue; }
  6864. if (R < 0) continue;
  6865. var metadata = records[ri].trim().split(",");
  6866. var type = metadata[0], value = metadata[1];
  6867. ++ri;
  6868. var data = records[ri].trim();
  6869. switch (+type) {
  6870. case -1:
  6871. if (data === 'BOT') { arr[++R] = []; C = 0; continue; }
  6872. else if (data !== 'EOD') throw new Error("Unrecognized DIF special command " + data);
  6873. break;
  6874. case 0:
  6875. if(data === 'TRUE') arr[R][C] = true;
  6876. else if(data === 'FALSE') arr[R][C] = false;
  6877. else if(!isNaN(fuzzynum(value))) arr[R][C] = fuzzynum(value);
  6878. else if(!isNaN(fuzzydate(value).getDate())) arr[R][C] = parseDate(value);
  6879. else arr[R][C] = value;
  6880. ++C; break;
  6881. case 1:
  6882. data = data.slice(1,data.length-1);
  6883. arr[R][C++] = data !== '' ? data : null;
  6884. break;
  6885. }
  6886. if (data === 'EOD') break;
  6887. }
  6888. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  6889. return arr;
  6890. }
  6891. function dif_to_sheet(str, opts) { return aoa_to_sheet(dif_to_aoa(str, opts), opts); }
  6892. function dif_to_workbook(str, opts) { return sheet_to_workbook(dif_to_sheet(str, opts), opts); }
  6893. var sheet_to_dif = (function() {
  6894. var push_field = function pf(o, topic, v, n, s) {
  6895. o.push(topic);
  6896. o.push(v + "," + n);
  6897. o.push('"' + s.replace(/"/g,'""') + '"');
  6898. };
  6899. var push_value = function po(o, type, v, s) {
  6900. o.push(type + "," + v);
  6901. o.push(type == 1 ? '"' + s.replace(/"/g,'""') + '"' : s);
  6902. };
  6903. return function sheet_to_dif(ws) {
  6904. var o = [];
  6905. var r = safe_decode_range(ws['!ref']), cell;
  6906. var dense = Array.isArray(ws);
  6907. push_field(o, "TABLE", 0, 1, "sheetjs");
  6908. push_field(o, "VECTORS", 0, r.e.r - r.s.r + 1,"");
  6909. push_field(o, "TUPLES", 0, r.e.c - r.s.c + 1,"");
  6910. push_field(o, "DATA", 0, 0,"");
  6911. for(var R = r.s.r; R <= r.e.r; ++R) {
  6912. push_value(o, -1, 0, "BOT");
  6913. for(var C = r.s.c; C <= r.e.c; ++C) {
  6914. var coord = encode_cell({r:R,c:C});
  6915. cell = dense ? (ws[R]||[])[C] : ws[coord];
  6916. if(!cell) { push_value(o, 1, 0, ""); continue;}
  6917. switch(cell.t) {
  6918. case 'n':
  6919. var val = DIF_XL ? cell.w : cell.v;
  6920. if(!val && cell.v != null) val = cell.v;
  6921. if(val == null) {
  6922. if(DIF_XL && cell.f && !cell.F) push_value(o, 1, 0, "=" + cell.f);
  6923. else push_value(o, 1, 0, "");
  6924. }
  6925. else push_value(o, 0, val, "V");
  6926. break;
  6927. case 'b':
  6928. push_value(o, 0, cell.v ? 1 : 0, cell.v ? "TRUE" : "FALSE");
  6929. break;
  6930. case 's':
  6931. push_value(o, 1, 0, (!DIF_XL || isNaN(cell.v)) ? cell.v : '="' + cell.v + '"');
  6932. break;
  6933. case 'd':
  6934. if(!cell.w) cell.w = SSF.format(cell.z || SSF._table[14], datenum(parseDate(cell.v)));
  6935. if(DIF_XL) push_value(o, 0, cell.w, "V");
  6936. else push_value(o, 1, 0, cell.w);
  6937. break;
  6938. default: push_value(o, 1, 0, "");
  6939. }
  6940. }
  6941. }
  6942. push_value(o, -1, 0, "EOD");
  6943. var RS = "\r\n";
  6944. var oo = o.join(RS);
  6945. //while((oo.length & 0x7F) != 0) oo += "\0";
  6946. return oo;
  6947. };
  6948. })();
  6949. return {
  6950. to_workbook: dif_to_workbook,
  6951. to_sheet: dif_to_sheet,
  6952. from_sheet: sheet_to_dif
  6953. };
  6954. })();
  6955. var ETH = (function() {
  6956. function decode(s) { return s.replace(/\\b/g,"\\").replace(/\\c/g,":").replace(/\\n/g,"\n"); }
  6957. function encode(s) { return s.replace(/\\/g, "\\b").replace(/:/g, "\\c").replace(/\n/g,"\\n"); }
  6958. function eth_to_aoa(str, opts) {
  6959. var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
  6960. for (; ri !== records.length; ++ri) {
  6961. var record = records[ri].trim().split(":");
  6962. if(record[0] !== 'cell') continue;
  6963. var addr = decode_cell(record[1]);
  6964. if(arr.length <= addr.r) for(R = arr.length; R <= addr.r; ++R) if(!arr[R]) arr[R] = [];
  6965. R = addr.r; C = addr.c;
  6966. switch(record[2]) {
  6967. case 't': arr[R][C] = decode(record[3]); break;
  6968. case 'v': arr[R][C] = +record[3]; break;
  6969. case 'vtf': var _f = record[record.length - 1];
  6970. /* falls through */
  6971. case 'vtc':
  6972. switch(record[3]) {
  6973. case 'nl': arr[R][C] = +record[4] ? true : false; break;
  6974. default: arr[R][C] = +record[4]; break;
  6975. }
  6976. if(record[2] == 'vtf') arr[R][C] = [arr[R][C], _f];
  6977. }
  6978. }
  6979. if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
  6980. return arr;
  6981. }
  6982. function eth_to_sheet(d, opts) { return aoa_to_sheet(eth_to_aoa(d, opts), opts); }
  6983. function eth_to_workbook(d, opts) { return sheet_to_workbook(eth_to_sheet(d, opts), opts); }
  6984. var header = [
  6985. "socialcalc:version:1.5",
  6986. "MIME-Version: 1.0",
  6987. "Content-Type: multipart/mixed; boundary=SocialCalcSpreadsheetControlSave"
  6988. ].join("\n");
  6989. var sep = [
  6990. "--SocialCalcSpreadsheetControlSave",
  6991. "Content-type: text/plain; charset=UTF-8"
  6992. ].join("\n") + "\n";
  6993. /* TODO: the other parts */
  6994. var meta = [
  6995. "# SocialCalc Spreadsheet Control Save",
  6996. "part:sheet"
  6997. ].join("\n");
  6998. var end = "--SocialCalcSpreadsheetControlSave--";
  6999. function sheet_to_eth_data(ws) {
  7000. if(!ws || !ws['!ref']) return "";
  7001. var o = [], oo = [], cell, coord = "";
  7002. var r = decode_range(ws['!ref']);
  7003. var dense = Array.isArray(ws);
  7004. for(var R = r.s.r; R <= r.e.r; ++R) {
  7005. for(var C = r.s.c; C <= r.e.c; ++C) {
  7006. coord = encode_cell({r:R,c:C});
  7007. cell = dense ? (ws[R]||[])[C] : ws[coord];
  7008. if(!cell || cell.v == null || cell.t === 'z') continue;
  7009. oo = ["cell", coord, 't'];
  7010. switch(cell.t) {
  7011. case 's': case 'str': oo.push(encode(cell.v)); break;
  7012. case 'n':
  7013. if(!cell.f) { oo[2]='v'; oo[3]=cell.v; }
  7014. else { oo[2]='vtf'; oo[3]='n'; oo[4]=cell.v; oo[5]=encode(cell.f); }
  7015. break;
  7016. case 'b':
  7017. oo[2] = 'vt'+(cell.f?'f':'c'); oo[3]='nl'; oo[4]=cell.v?"1":"0";
  7018. oo[5] = encode(cell.f||(cell.v?'TRUE':'FALSE'));
  7019. break;
  7020. case 'd':
  7021. var t = datenum(parseDate(cell.v));
  7022. oo[2] = 'vtc'; oo[3] = 'nd'; oo[4] = ""+t;
  7023. oo[5] = cell.w || SSF.format(cell.z || SSF._table[14], t);
  7024. break;
  7025. case 'e': continue;
  7026. }
  7027. o.push(oo.join(":"));
  7028. }
  7029. }
  7030. o.push("sheet:c:" + (r.e.c-r.s.c+1) + ":r:" + (r.e.r-r.s.r+1) + ":tvf:1");
  7031. o.push("valueformat:1:text-wiki");
  7032. //o.push("copiedfrom:" + ws['!ref']); // clipboard only
  7033. return o.join("\n");
  7034. }
  7035. function sheet_to_eth(ws) {
  7036. return [header, sep, meta, sep, sheet_to_eth_data(ws), end].join("\n");
  7037. // return ["version:1.5", sheet_to_eth_data(ws)].join("\n"); // clipboard form
  7038. }
  7039. return {
  7040. to_workbook: eth_to_workbook,
  7041. to_sheet: eth_to_sheet,
  7042. from_sheet: sheet_to_eth
  7043. };
  7044. })();
  7045. var PRN = (function() {
  7046. function set_text_arr(data, arr, R, C, o) {
  7047. if(o.raw) arr[R][C] = data;
  7048. else if(data === 'TRUE') arr[R][C] = true;
  7049. else if(data === 'FALSE') arr[R][C] = false;
  7050. else if(data === ""){/* empty */}
  7051. else if(!isNaN(fuzzynum(data))) arr[R][C] = fuzzynum(data);
  7052. else if(!isNaN(fuzzydate(data).getDate())) arr[R][C] = parseDate(data);
  7053. else arr[R][C] = data;
  7054. }
  7055. function prn_to_aoa_str(f, opts) {
  7056. var o = opts || {};
  7057. var arr = ([]);
  7058. if(!f || f.length === 0) return arr;
  7059. var lines = f.split(/[\r\n]/);
  7060. var L = lines.length - 1;
  7061. while(L >= 0 && lines[L].length === 0) --L;
  7062. var start = 10, idx = 0;
  7063. var R = 0;
  7064. for(; R <= L; ++R) {
  7065. idx = lines[R].indexOf(" ");
  7066. if(idx == -1) idx = lines[R].length; else idx++;
  7067. start = Math.max(start, idx);
  7068. }
  7069. for(R = 0; R <= L; ++R) {
  7070. arr[R] = [];
  7071. /* TODO: confirm that widths are always 10 */
  7072. var C = 0;
  7073. set_text_arr(lines[R].slice(0, start).trim(), arr, R, C, o);
  7074. for(C = 1; C <= (lines[R].length - start)/10 + 1; ++C)
  7075. set_text_arr(lines[R].slice(start+(C-1)*10,start+C*10).trim(),arr,R,C,o);
  7076. }
  7077. if(o.sheetRows) arr = arr.slice(0, o.sheetRows);
  7078. return arr;
  7079. }
  7080. // List of accepted CSV separators
  7081. var guess_seps = {
  7082. 0x2C: ',',
  7083. 0x09: "\t",
  7084. 0x3B: ';'
  7085. };
  7086. // CSV separator weights to be used in case of equal numbers
  7087. var guess_sep_weights = {
  7088. 0x2C: 3,
  7089. 0x09: 2,
  7090. 0x3B: 1
  7091. };
  7092. function guess_sep(str) {
  7093. var cnt = {}, instr = false, end = 0, cc = 0;
  7094. for(;end < str.length;++end) {
  7095. if((cc=str.charCodeAt(end)) == 0x22) instr = !instr;
  7096. else if(!instr && cc in guess_seps) cnt[cc] = (cnt[cc]||0)+1;
  7097. }
  7098. cc = [];
  7099. for(end in cnt) if ( Object.prototype.hasOwnProperty.call(cnt, end) ) {
  7100. cc.push([ cnt[end], end ]);
  7101. }
  7102. if ( !cc.length ) {
  7103. cnt = guess_sep_weights;
  7104. for(end in cnt) if ( Object.prototype.hasOwnProperty.call(cnt, end) ) {
  7105. cc.push([ cnt[end], end ]);
  7106. }
  7107. }
  7108. cc.sort(function(a, b) { return a[0] - b[0] || guess_sep_weights[a[1]] - guess_sep_weights[b[1]]; });
  7109. return guess_seps[cc.pop()[1]];
  7110. }
  7111. function dsv_to_sheet_str(str, opts) {
  7112. var o = opts || {};
  7113. var sep = "";
  7114. if(DENSE != null && o.dense == null) o.dense = DENSE;
  7115. var ws = o.dense ? ([]) : ({});
  7116. var range = ({s: {c:0, r:0}, e: {c:0, r:0}});
  7117. if(str.slice(0,4) == "sep=") {
  7118. // If the line ends in \r\n
  7119. if(str.charCodeAt(5) == 13 && str.charCodeAt(6) == 10 ) {
  7120. sep = str.charAt(4); str = str.slice(7);
  7121. }
  7122. // If line ends in \r OR \n
  7123. else if(str.charCodeAt(5) == 13 || str.charCodeAt(5) == 10 ) {
  7124. //
  7125. sep = str.charAt(4); str = str.slice(6);
  7126. }
  7127. }
  7128. else sep = guess_sep(str.slice(0,1024));
  7129. var R = 0, C = 0, v = 0;
  7130. var start = 0, end = 0, sepcc = sep.charCodeAt(0), instr = false, cc=0;
  7131. str = str.replace(/\r\n/mg, "\n");
  7132. var _re = o.dateNF != null ? dateNF_regex(o.dateNF) : null;
  7133. function finish_cell() {
  7134. var s = str.slice(start, end);
  7135. var cell = ({});
  7136. if(s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') s = s.slice(1,-1).replace(/""/g,'"');
  7137. if(s.length === 0) cell.t = 'z';
  7138. else if(o.raw) { cell.t = 's'; cell.v = s; }
  7139. else if(s.trim().length === 0) { cell.t = 's'; cell.v = s; }
  7140. else if(s.charCodeAt(0) == 0x3D) {
  7141. if(s.charCodeAt(1) == 0x22 && s.charCodeAt(s.length - 1) == 0x22) { cell.t = 's'; cell.v = s.slice(2,-1).replace(/""/g,'"'); }
  7142. else if(fuzzyfmla(s)) { cell.t = 'n'; cell.f = s.slice(1); }
  7143. else { cell.t = 's'; cell.v = s; } }
  7144. else if(s == "TRUE") { cell.t = 'b'; cell.v = true; }
  7145. else if(s == "FALSE") { cell.t = 'b'; cell.v = false; }
  7146. else if(!isNaN(v = fuzzynum(s))) { cell.t = 'n'; if(o.cellText !== false) cell.w = s; cell.v = v; }
  7147. else if(!isNaN(fuzzydate(s).getDate()) || _re && s.match(_re)) {
  7148. cell.z = o.dateNF || SSF._table[14];
  7149. var k = 0;
  7150. if(_re && s.match(_re)){ s=dateNF_fix(s, o.dateNF, (s.match(_re)||[])); k=1; }
  7151. if(o.cellDates) { cell.t = 'd'; cell.v = parseDate(s, k); }
  7152. else { cell.t = 'n'; cell.v = datenum(parseDate(s, k)); }
  7153. if(o.cellText !== false) cell.w = SSF.format(cell.z, cell.v instanceof Date ? datenum(cell.v):cell.v);
  7154. if(!o.cellNF) delete cell.z;
  7155. } else {
  7156. cell.t = 's';
  7157. cell.v = s;
  7158. }
  7159. if(cell.t == 'z'){}
  7160. else if(o.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = cell; }
  7161. else ws[encode_cell({c:C,r:R})] = cell;
  7162. start = end+1;
  7163. if(range.e.c < C) range.e.c = C;
  7164. if(range.e.r < R) range.e.r = R;
  7165. if(cc == sepcc) ++C; else { C = 0; ++R; if(o.sheetRows && o.sheetRows <= R) return true; }
  7166. }
  7167. outer: for(;end < str.length;++end) switch((cc=str.charCodeAt(end))) {
  7168. case 0x22: instr = !instr; break;
  7169. case sepcc: case 0x0a: case 0x0d: if(!instr && finish_cell()) break outer; break;
  7170. default: break;
  7171. }
  7172. if(end - start > 0) finish_cell();
  7173. ws['!ref'] = encode_range(range);
  7174. return ws;
  7175. }
  7176. function prn_to_sheet_str(str, opts) {
  7177. if(!(opts && opts.PRN)) return dsv_to_sheet_str(str, opts);
  7178. if(str.slice(0,4) == "sep=") return dsv_to_sheet_str(str, opts);
  7179. if(str.indexOf("\t") >= 0 || str.indexOf(",") >= 0 || str.indexOf(";") >= 0) return dsv_to_sheet_str(str, opts);
  7180. return aoa_to_sheet(prn_to_aoa_str(str, opts), opts);
  7181. }
  7182. function prn_to_sheet(d, opts) {
  7183. var str = "", bytes = opts.type == 'string' ? [0,0,0,0] : firstbyte(d, opts);
  7184. switch(opts.type) {
  7185. case 'base64': str = Base64.decode(d); break;
  7186. case 'binary': str = d; break;
  7187. case 'buffer':
  7188. if(opts.codepage == 65001) str = d.toString('utf8');
  7189. else if(opts.codepage && typeof cptable !== 'undefined') str = cptable.utils.decode(opts.codepage, d);
  7190. else str = d.toString('binary');
  7191. break;
  7192. case 'array': str = cc2str(d); break;
  7193. case 'string': str = d; break;
  7194. default: throw new Error("Unrecognized type " + opts.type);
  7195. }
  7196. if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str.slice(3));
  7197. else if((opts.type == 'binary') && typeof cptable !== 'undefined' && opts.codepage) str = cptable.utils.decode(opts.codepage, cptable.utils.encode(1252,str));
  7198. if(str.slice(0,19) == "socialcalc:version:") return ETH.to_sheet(opts.type == 'string' ? str : utf8read(str), opts);
  7199. return prn_to_sheet_str(str, opts);
  7200. }
  7201. function prn_to_workbook(d, opts) { return sheet_to_workbook(prn_to_sheet(d, opts), opts); }
  7202. function sheet_to_prn(ws) {
  7203. var o = [];
  7204. var r = safe_decode_range(ws['!ref']), cell;
  7205. var dense = Array.isArray(ws);
  7206. for(var R = r.s.r; R <= r.e.r; ++R) {
  7207. var oo = [];
  7208. for(var C = r.s.c; C <= r.e.c; ++C) {
  7209. var coord = encode_cell({r:R,c:C});
  7210. cell = dense ? (ws[R]||[])[C] : ws[coord];
  7211. if(!cell || cell.v == null) { oo.push(" "); continue; }
  7212. var w = (cell.w || (format_cell(cell), cell.w) || "").slice(0,10);
  7213. while(w.length < 10) w += " ";
  7214. oo.push(w + (C === 0 ? " " : ""));
  7215. }
  7216. o.push(oo.join(""));
  7217. }
  7218. return o.join("\n");
  7219. }
  7220. return {
  7221. to_workbook: prn_to_workbook,
  7222. to_sheet: prn_to_sheet,
  7223. from_sheet: sheet_to_prn
  7224. };
  7225. })();
  7226. /* Excel defaults to SYLK but warns if data is not valid */
  7227. function read_wb_ID(d, opts) {
  7228. var o = opts || {}, OLD_WTF = !!o.WTF; o.WTF = true;
  7229. try {
  7230. var out = SYLK.to_workbook(d, o);
  7231. o.WTF = OLD_WTF;
  7232. return out;
  7233. } catch(e) {
  7234. o.WTF = OLD_WTF;
  7235. if(!e.message.match(/SYLK bad record ID/) && OLD_WTF) throw e;
  7236. return PRN.to_workbook(d, opts);
  7237. }
  7238. }
  7239. var WK_ = (function() {
  7240. function lotushopper(data, cb, opts) {
  7241. if(!data) return;
  7242. prep_blob(data, data.l || 0);
  7243. var Enum = opts.Enum || WK1Enum;
  7244. while(data.l < data.length) {
  7245. var RT = data.read_shift(2);
  7246. var R = Enum[RT] || Enum[0xFF];
  7247. var length = data.read_shift(2);
  7248. var tgt = data.l + length;
  7249. var d = (R.f||parsenoop)(data, length, opts);
  7250. data.l = tgt;
  7251. if(cb(d, R.n, RT)) return;
  7252. }
  7253. }
  7254. function lotus_to_workbook(d, opts) {
  7255. switch(opts.type) {
  7256. case 'base64': return lotus_to_workbook_buf(s2a(Base64.decode(d)), opts);
  7257. case 'binary': return lotus_to_workbook_buf(s2a(d), opts);
  7258. case 'buffer':
  7259. case 'array': return lotus_to_workbook_buf(d, opts);
  7260. }
  7261. throw "Unsupported type " + opts.type;
  7262. }
  7263. function lotus_to_workbook_buf(d, opts) {
  7264. if(!d) return d;
  7265. var o = opts || {};
  7266. if(DENSE != null && o.dense == null) o.dense = DENSE;
  7267. var s = ((o.dense ? [] : {})), n = "Sheet1", sidx = 0;
  7268. var sheets = {}, snames = [n];
  7269. var refguess = {s: {r:0, c:0}, e: {r:0, c:0} };
  7270. var sheetRows = o.sheetRows || 0;
  7271. if(d[2] == 0x02) o.Enum = WK1Enum;
  7272. else if(d[2] == 0x1a) o.Enum = WK3Enum;
  7273. else if(d[2] == 0x0e) { o.Enum = WK3Enum; o.qpro = true; d.l = 0; }
  7274. else throw new Error("Unrecognized LOTUS BOF " + d[2]);
  7275. lotushopper(d, function(val, Rn, RT) {
  7276. if(d[2] == 0x02) switch(RT) {
  7277. case 0x00:
  7278. o.vers = val;
  7279. if(val >= 0x1000) o.qpro = true;
  7280. break;
  7281. case 0x06: refguess = val; break; /* RANGE */
  7282. case 0x0F: /* LABEL */
  7283. if(!o.qpro) val[1].v = val[1].v.slice(1);
  7284. /* falls through */
  7285. case 0x0D: /* INTEGER */
  7286. case 0x0E: /* NUMBER */
  7287. case 0x10: /* FORMULA */
  7288. case 0x33: /* STRING */
  7289. /* TODO: actual translation of the format code */
  7290. if(RT == 0x0E && (val[2] & 0x70) == 0x70 && (val[2] & 0x0F) > 1 && (val[2] & 0x0F) < 15) {
  7291. val[1].z = o.dateNF || SSF._table[14];
  7292. if(o.cellDates) { val[1].t = 'd'; val[1].v = numdate(val[1].v); }
  7293. }
  7294. if(o.dense) {
  7295. if(!s[val[0].r]) s[val[0].r] = [];
  7296. s[val[0].r][val[0].c] = val[1];
  7297. } else s[encode_cell(val[0])] = val[1];
  7298. break;
  7299. } else switch(RT) {
  7300. case 0x16: /* LABEL16 */
  7301. val[1].v = val[1].v.slice(1);
  7302. /* falls through */
  7303. case 0x17: /* NUMBER17 */
  7304. case 0x18: /* NUMBER18 */
  7305. case 0x19: /* FORMULA19 */
  7306. case 0x25: /* NUMBER25 */
  7307. case 0x27: /* NUMBER27 */
  7308. case 0x28: /* FORMULA28 */
  7309. if(val[3] > sidx) {
  7310. s["!ref"] = encode_range(refguess);
  7311. sheets[n] = s;
  7312. s = (o.dense ? [] : {});
  7313. refguess = {s: {r:0, c:0}, e: {r:0, c:0} };
  7314. sidx = val[3]; n = "Sheet" + (sidx + 1);
  7315. snames.push(n);
  7316. }
  7317. if(sheetRows > 0 && val[0].r >= sheetRows) break;
  7318. if(o.dense) {
  7319. if(!s[val[0].r]) s[val[0].r] = [];
  7320. s[val[0].r][val[0].c] = val[1];
  7321. } else s[encode_cell(val[0])] = val[1];
  7322. if(refguess.e.c < val[0].c) refguess.e.c = val[0].c;
  7323. if(refguess.e.r < val[0].r) refguess.e.r = val[0].r;
  7324. break;
  7325. default: break;
  7326. }
  7327. }, o);
  7328. s["!ref"] = encode_range(refguess);
  7329. sheets[n] = s;
  7330. return { SheetNames: snames, Sheets:sheets };
  7331. }
  7332. function parse_RANGE(blob) {
  7333. var o = {s:{c:0,r:0},e:{c:0,r:0}};
  7334. o.s.c = blob.read_shift(2);
  7335. o.s.r = blob.read_shift(2);
  7336. o.e.c = blob.read_shift(2);
  7337. o.e.r = blob.read_shift(2);
  7338. if(o.s.c == 0xFFFF) o.s.c = o.e.c = o.s.r = o.e.r = 0;
  7339. return o;
  7340. }
  7341. function parse_cell(blob, length, opts) {
  7342. var o = [{c:0,r:0}, {t:'n',v:0}, 0];
  7343. if(opts.qpro && opts.vers != 0x5120) {
  7344. o[0].c = blob.read_shift(1);
  7345. blob.l++;
  7346. o[0].r = blob.read_shift(2);
  7347. blob.l+=2;
  7348. } else {
  7349. o[2] = blob.read_shift(1);
  7350. o[0].c = blob.read_shift(2); o[0].r = blob.read_shift(2);
  7351. }
  7352. return o;
  7353. }
  7354. function parse_LABEL(blob, length, opts) {
  7355. var tgt = blob.l + length;
  7356. var o = parse_cell(blob, length, opts);
  7357. o[1].t = 's';
  7358. if(opts.vers == 0x5120) {
  7359. blob.l++;
  7360. var len = blob.read_shift(1);
  7361. o[1].v = blob.read_shift(len, 'utf8');
  7362. return o;
  7363. }
  7364. if(opts.qpro) blob.l++;
  7365. o[1].v = blob.read_shift(tgt - blob.l, 'cstr');
  7366. return o;
  7367. }
  7368. function parse_INTEGER(blob, length, opts) {
  7369. var o = parse_cell(blob, length, opts);
  7370. o[1].v = blob.read_shift(2, 'i');
  7371. return o;
  7372. }
  7373. function parse_NUMBER(blob, length, opts) {
  7374. var o = parse_cell(blob, length, opts);
  7375. o[1].v = blob.read_shift(8, 'f');
  7376. return o;
  7377. }
  7378. function parse_FORMULA(blob, length, opts) {
  7379. var tgt = blob.l + length;
  7380. var o = parse_cell(blob, length, opts);
  7381. /* TODO: formula */
  7382. o[1].v = blob.read_shift(8, 'f');
  7383. if(opts.qpro) blob.l = tgt;
  7384. else {
  7385. var flen = blob.read_shift(2);
  7386. blob.l += flen;
  7387. }
  7388. return o;
  7389. }
  7390. function parse_cell_3(blob) {
  7391. var o = [{c:0,r:0}, {t:'n',v:0}, 0];
  7392. o[0].r = blob.read_shift(2); o[3] = blob[blob.l++]; o[0].c = blob[blob.l++];
  7393. return o;
  7394. }
  7395. function parse_LABEL_16(blob, length) {
  7396. var o = parse_cell_3(blob, length);
  7397. o[1].t = 's';
  7398. o[1].v = blob.read_shift(length - 4, 'cstr');
  7399. return o;
  7400. }
  7401. function parse_NUMBER_18(blob, length) {
  7402. var o = parse_cell_3(blob, length);
  7403. o[1].v = blob.read_shift(2);
  7404. var v = o[1].v >> 1;
  7405. /* TODO: figure out all of the corner cases */
  7406. if(o[1].v & 0x1) {
  7407. switch(v & 0x07) {
  7408. case 1: v = (v >> 3) * 500; break;
  7409. case 2: v = (v >> 3) / 20; break;
  7410. case 4: v = (v >> 3) / 2000; break;
  7411. case 6: v = (v >> 3) / 16; break;
  7412. case 7: v = (v >> 3) / 64; break;
  7413. default: throw "unknown NUMBER_18 encoding " + (v & 0x07);
  7414. }
  7415. }
  7416. o[1].v = v;
  7417. return o;
  7418. }
  7419. function parse_NUMBER_17(blob, length) {
  7420. var o = parse_cell_3(blob, length);
  7421. var v1 = blob.read_shift(4);
  7422. var v2 = blob.read_shift(4);
  7423. var e = blob.read_shift(2);
  7424. if(e == 0xFFFF) { o[1].v = 0; return o; }
  7425. var s = e & 0x8000; e = (e&0x7FFF) - 16446;
  7426. o[1].v = (s*2 - 1) * ((e > 0 ? (v2 << e) : (v2 >>> -e)) + (e > -32 ? (v1 << (e + 32)) : (v1 >>> -(e + 32))));
  7427. return o;
  7428. }
  7429. function parse_FORMULA_19(blob, length) {
  7430. var o = parse_NUMBER_17(blob, 14);
  7431. blob.l += length - 14; /* TODO: formula */
  7432. return o;
  7433. }
  7434. function parse_NUMBER_25(blob, length) {
  7435. var o = parse_cell_3(blob, length);
  7436. var v1 = blob.read_shift(4);
  7437. o[1].v = v1 >> 6;
  7438. return o;
  7439. }
  7440. function parse_NUMBER_27(blob, length) {
  7441. var o = parse_cell_3(blob, length);
  7442. var v1 = blob.read_shift(8,'f');
  7443. o[1].v = v1;
  7444. return o;
  7445. }
  7446. function parse_FORMULA_28(blob, length) {
  7447. var o = parse_NUMBER_27(blob, 14);
  7448. blob.l += length - 10; /* TODO: formula */
  7449. return o;
  7450. }
  7451. var WK1Enum = {
  7452. 0x0000: { n:"BOF", f:parseuint16 },
  7453. 0x0001: { n:"EOF" },
  7454. 0x0002: { n:"CALCMODE" },
  7455. 0x0003: { n:"CALCORDER" },
  7456. 0x0004: { n:"SPLIT" },
  7457. 0x0005: { n:"SYNC" },
  7458. 0x0006: { n:"RANGE", f:parse_RANGE },
  7459. 0x0007: { n:"WINDOW1" },
  7460. 0x0008: { n:"COLW1" },
  7461. 0x0009: { n:"WINTWO" },
  7462. 0x000A: { n:"COLW2" },
  7463. 0x000B: { n:"NAME" },
  7464. 0x000C: { n:"BLANK" },
  7465. 0x000D: { n:"INTEGER", f:parse_INTEGER },
  7466. 0x000E: { n:"NUMBER", f:parse_NUMBER },
  7467. 0x000F: { n:"LABEL", f:parse_LABEL },
  7468. 0x0010: { n:"FORMULA", f:parse_FORMULA },
  7469. 0x0018: { n:"TABLE" },
  7470. 0x0019: { n:"ORANGE" },
  7471. 0x001A: { n:"PRANGE" },
  7472. 0x001B: { n:"SRANGE" },
  7473. 0x001C: { n:"FRANGE" },
  7474. 0x001D: { n:"KRANGE1" },
  7475. 0x0020: { n:"HRANGE" },
  7476. 0x0023: { n:"KRANGE2" },
  7477. 0x0024: { n:"PROTEC" },
  7478. 0x0025: { n:"FOOTER" },
  7479. 0x0026: { n:"HEADER" },
  7480. 0x0027: { n:"SETUP" },
  7481. 0x0028: { n:"MARGINS" },
  7482. 0x0029: { n:"LABELFMT" },
  7483. 0x002A: { n:"TITLES" },
  7484. 0x002B: { n:"SHEETJS" },
  7485. 0x002D: { n:"GRAPH" },
  7486. 0x002E: { n:"NGRAPH" },
  7487. 0x002F: { n:"CALCCOUNT" },
  7488. 0x0030: { n:"UNFORMATTED" },
  7489. 0x0031: { n:"CURSORW12" },
  7490. 0x0032: { n:"WINDOW" },
  7491. 0x0033: { n:"STRING", f:parse_LABEL },
  7492. 0x0037: { n:"PASSWORD" },
  7493. 0x0038: { n:"LOCKED" },
  7494. 0x003C: { n:"QUERY" },
  7495. 0x003D: { n:"QUERYNAME" },
  7496. 0x003E: { n:"PRINT" },
  7497. 0x003F: { n:"PRINTNAME" },
  7498. 0x0040: { n:"GRAPH2" },
  7499. 0x0041: { n:"GRAPHNAME" },
  7500. 0x0042: { n:"ZOOM" },
  7501. 0x0043: { n:"SYMSPLIT" },
  7502. 0x0044: { n:"NSROWS" },
  7503. 0x0045: { n:"NSCOLS" },
  7504. 0x0046: { n:"RULER" },
  7505. 0x0047: { n:"NNAME" },
  7506. 0x0048: { n:"ACOMM" },
  7507. 0x0049: { n:"AMACRO" },
  7508. 0x004A: { n:"PARSE" },
  7509. 0x00FF: { n:"", f:parsenoop }
  7510. };
  7511. var WK3Enum = {
  7512. 0x0000: { n:"BOF" },
  7513. 0x0001: { n:"EOF" },
  7514. 0x0003: { n:"??" },
  7515. 0x0004: { n:"??" },
  7516. 0x0005: { n:"??" },
  7517. 0x0006: { n:"??" },
  7518. 0x0007: { n:"??" },
  7519. 0x0009: { n:"??" },
  7520. 0x000a: { n:"??" },
  7521. 0x000b: { n:"??" },
  7522. 0x000c: { n:"??" },
  7523. 0x000e: { n:"??" },
  7524. 0x000f: { n:"??" },
  7525. 0x0010: { n:"??" },
  7526. 0x0011: { n:"??" },
  7527. 0x0012: { n:"??" },
  7528. 0x0013: { n:"??" },
  7529. 0x0015: { n:"??" },
  7530. 0x0016: { n:"LABEL16", f:parse_LABEL_16},
  7531. 0x0017: { n:"NUMBER17", f:parse_NUMBER_17 },
  7532. 0x0018: { n:"NUMBER18", f:parse_NUMBER_18 },
  7533. 0x0019: { n:"FORMULA19", f:parse_FORMULA_19},
  7534. 0x001a: { n:"??" },
  7535. 0x001b: { n:"??" },
  7536. 0x001c: { n:"??" },
  7537. 0x001d: { n:"??" },
  7538. 0x001e: { n:"??" },
  7539. 0x001f: { n:"??" },
  7540. 0x0021: { n:"??" },
  7541. 0x0025: { n:"NUMBER25", f:parse_NUMBER_25 },
  7542. 0x0027: { n:"NUMBER27", f:parse_NUMBER_27 },
  7543. 0x0028: { n:"FORMULA28", f:parse_FORMULA_28 },
  7544. 0x00FF: { n:"", f:parsenoop }
  7545. };
  7546. return {
  7547. to_workbook: lotus_to_workbook
  7548. };
  7549. })();
  7550. /* 18.4.7 rPr CT_RPrElt */
  7551. function parse_rpr(rpr) {
  7552. var font = {}, m = rpr.match(tagregex), i = 0;
  7553. var pass = false;
  7554. if(m) for(;i!=m.length; ++i) {
  7555. var y = parsexmltag(m[i]);
  7556. switch(y[0].replace(/\w*:/g,"")) {
  7557. /* 18.8.12 condense CT_BooleanProperty */
  7558. /* ** not required . */
  7559. case '<condense': break;
  7560. /* 18.8.17 extend CT_BooleanProperty */
  7561. /* ** not required . */
  7562. case '<extend': break;
  7563. /* 18.8.36 shadow CT_BooleanProperty */
  7564. /* ** not required . */
  7565. case '<shadow':
  7566. if(!y.val) break;
  7567. /* falls through */
  7568. case '<shadow>':
  7569. case '<shadow/>': font.shadow = 1; break;
  7570. case '</shadow>': break;
  7571. /* 18.4.1 charset CT_IntProperty TODO */
  7572. case '<charset':
  7573. if(y.val == '1') break;
  7574. font.cp = CS2CP[parseInt(y.val, 10)];
  7575. break;
  7576. /* 18.4.2 outline CT_BooleanProperty TODO */
  7577. case '<outline':
  7578. if(!y.val) break;
  7579. /* falls through */
  7580. case '<outline>':
  7581. case '<outline/>': font.outline = 1; break;
  7582. case '</outline>': break;
  7583. /* 18.4.5 rFont CT_FontName */
  7584. case '<rFont': font.name = y.val; break;
  7585. /* 18.4.11 sz CT_FontSize */
  7586. case '<sz': font.sz = y.val; break;
  7587. /* 18.4.10 strike CT_BooleanProperty */
  7588. case '<strike':
  7589. if(!y.val) break;
  7590. /* falls through */
  7591. case '<strike>':
  7592. case '<strike/>': font.strike = 1; break;
  7593. case '</strike>': break;
  7594. /* 18.4.13 u CT_UnderlineProperty */
  7595. case '<u':
  7596. if(!y.val) break;
  7597. switch(y.val) {
  7598. case 'double': font.uval = "double"; break;
  7599. case 'singleAccounting': font.uval = "single-accounting"; break;
  7600. case 'doubleAccounting': font.uval = "double-accounting"; break;
  7601. }
  7602. /* falls through */
  7603. case '<u>':
  7604. case '<u/>': font.u = 1; break;
  7605. case '</u>': break;
  7606. /* 18.8.2 b */
  7607. case '<b':
  7608. if(y.val == '0') break;
  7609. /* falls through */
  7610. case '<b>':
  7611. case '<b/>': font.b = 1; break;
  7612. case '</b>': break;
  7613. /* 18.8.26 i */
  7614. case '<i':
  7615. if(y.val == '0') break;
  7616. /* falls through */
  7617. case '<i>':
  7618. case '<i/>': font.i = 1; break;
  7619. case '</i>': break;
  7620. /* 18.3.1.15 color CT_Color TODO: tint, theme, auto, indexed */
  7621. case '<color':
  7622. if(y.rgb) font.color = y.rgb.slice(2,8);
  7623. break;
  7624. /* 18.8.18 family ST_FontFamily */
  7625. case '<family': font.family = y.val; break;
  7626. /* 18.4.14 vertAlign CT_VerticalAlignFontProperty TODO */
  7627. case '<vertAlign': font.valign = y.val; break;
  7628. /* 18.8.35 scheme CT_FontScheme TODO */
  7629. case '<scheme': break;
  7630. /* 18.2.10 extLst CT_ExtensionList ? */
  7631. case '<extLst': case '<extLst>': case '</extLst>': break;
  7632. case '<ext': pass = true; break;
  7633. case '</ext>': pass = false; break;
  7634. default:
  7635. if(y[0].charCodeAt(1) !== 47 && !pass) throw new Error('Unrecognized rich format ' + y[0]);
  7636. }
  7637. }
  7638. return font;
  7639. }
  7640. var parse_rs = (function() {
  7641. var tregex = matchtag("t"), rpregex = matchtag("rPr");
  7642. /* 18.4.4 r CT_RElt */
  7643. function parse_r(r) {
  7644. /* 18.4.12 t ST_Xstring */
  7645. var t = r.match(tregex)/*, cp = 65001*/;
  7646. if(!t) return {t:"s", v:""};
  7647. var o = ({t:'s', v:unescapexml(t[1])});
  7648. var rpr = r.match(rpregex);
  7649. if(rpr) o.s = parse_rpr(rpr[1]);
  7650. return o;
  7651. }
  7652. var rregex = /<(?:\w+:)?r>/g, rend = /<\/(?:\w+:)?r>/;
  7653. return function parse_rs(rs) {
  7654. return rs.replace(rregex,"").split(rend).map(parse_r).filter(function(r) { return r.v; });
  7655. };
  7656. })();
  7657. /* Parse a list of <r> tags */
  7658. var rs_to_html = (function parse_rs_factory() {
  7659. var nlregex = /(\r\n|\n)/g;
  7660. function parse_rpr2(font, intro, outro) {
  7661. var style = [];
  7662. if(font.u) style.push("text-decoration: underline;");
  7663. if(font.uval) style.push("text-underline-style:" + font.uval + ";");
  7664. if(font.sz) style.push("font-size:" + font.sz + "pt;");
  7665. if(font.outline) style.push("text-effect: outline;");
  7666. if(font.shadow) style.push("text-shadow: auto;");
  7667. intro.push('<span style="' + style.join("") + '">');
  7668. if(font.b) { intro.push("<b>"); outro.push("</b>"); }
  7669. if(font.i) { intro.push("<i>"); outro.push("</i>"); }
  7670. if(font.strike) { intro.push("<s>"); outro.push("</s>"); }
  7671. var align = font.valign || "";
  7672. if(align == "superscript" || align == "super") align = "sup";
  7673. else if(align == "subscript") align = "sub";
  7674. if(align != "") { intro.push("<" + align + ">"); outro.push("</" + align + ">"); }
  7675. outro.push("</span>");
  7676. return font;
  7677. }
  7678. /* 18.4.4 r CT_RElt */
  7679. function r_to_html(r) {
  7680. var terms = [[],r.v,[]];
  7681. if(!r.v) return "";
  7682. if(r.s) parse_rpr2(r.s, terms[0], terms[2]);
  7683. return terms[0].join("") + terms[1].replace(nlregex,'<br/>') + terms[2].join("");
  7684. }
  7685. return function parse_rs(rs) {
  7686. return rs.map(r_to_html).join("");
  7687. };
  7688. })();
  7689. /* 18.4.8 si CT_Rst */
  7690. var sitregex = /<(?:\w+:)?t[^>]*>([^<]*)<\/(?:\w+:)?t>/g, sirregex = /<(?:\w+:)?r>/;
  7691. var sirphregex = /<(?:\w+:)?rPh.*?>([\s\S]*?)<\/(?:\w+:)?rPh>/g;
  7692. function parse_si(x, opts) {
  7693. var html = opts ? opts.cellHTML : true;
  7694. var z = {};
  7695. if(!x) return { t: "" };
  7696. //var y;
  7697. /* 18.4.12 t ST_Xstring (Plaintext String) */
  7698. // TODO: is whitespace actually valid here?
  7699. if(x.match(/^\s*<(?:\w+:)?t[^>]*>/)) {
  7700. z.t = unescapexml(utf8read(x.slice(x.indexOf(">")+1).split(/<\/(?:\w+:)?t>/)[0]||""));
  7701. z.r = utf8read(x);
  7702. if(html) z.h = escapehtml(z.t);
  7703. }
  7704. /* 18.4.4 r CT_RElt (Rich Text Run) */
  7705. else if((/*y = */x.match(sirregex))) {
  7706. z.r = utf8read(x);
  7707. z.t = unescapexml(utf8read((x.replace(sirphregex, '').match(sitregex)||[]).join("").replace(tagregex,"")));
  7708. if(html) z.h = rs_to_html(parse_rs(z.r));
  7709. }
  7710. /* 18.4.3 phoneticPr CT_PhoneticPr (TODO: needed for Asian support) */
  7711. /* 18.4.6 rPh CT_PhoneticRun (TODO: needed for Asian support) */
  7712. return z;
  7713. }
  7714. /* 18.4 Shared String Table */
  7715. var sstr0 = /<(?:\w+:)?sst([^>]*)>([\s\S]*)<\/(?:\w+:)?sst>/;
  7716. var sstr1 = /<(?:\w+:)?(?:si|sstItem)>/g;
  7717. var sstr2 = /<\/(?:\w+:)?(?:si|sstItem)>/;
  7718. function parse_sst_xml(data, opts) {
  7719. var s = ([]), ss = "";
  7720. if(!data) return s;
  7721. /* 18.4.9 sst CT_Sst */
  7722. var sst = data.match(sstr0);
  7723. if(sst) {
  7724. ss = sst[2].replace(sstr1,"").split(sstr2);
  7725. for(var i = 0; i != ss.length; ++i) {
  7726. var o = parse_si(ss[i].trim(), opts);
  7727. if(o != null) s[s.length] = o;
  7728. }
  7729. sst = parsexmltag(sst[1]); s.Count = sst.count; s.Unique = sst.uniqueCount;
  7730. }
  7731. return s;
  7732. }
  7733. RELS.SST = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
  7734. var straywsregex = /^\s|\s$|[\t\n\r]/;
  7735. function write_sst_xml(sst, opts) {
  7736. if(!opts.bookSST) return "";
  7737. var o = [XML_HEADER];
  7738. o[o.length] = (writextag('sst', null, {
  7739. xmlns: XMLNS.main[0],
  7740. count: sst.Count,
  7741. uniqueCount: sst.Unique
  7742. }));
  7743. for(var i = 0; i != sst.length; ++i) { if(sst[i] == null) continue;
  7744. var s = sst[i];
  7745. var sitag = "<si>";
  7746. if(s.r) sitag += s.r;
  7747. else {
  7748. sitag += "<t";
  7749. if(!s.t) s.t = "";
  7750. if(s.t.match(straywsregex)) sitag += ' xml:space="preserve"';
  7751. sitag += ">" + escapexml(s.t) + "</t>";
  7752. }
  7753. sitag += "</si>";
  7754. o[o.length] = (sitag);
  7755. }
  7756. if(o.length>2){ o[o.length] = ('</sst>'); o[1]=o[1].replace("/>",">"); }
  7757. return o.join("");
  7758. }
  7759. /* [MS-XLSB] 2.4.221 BrtBeginSst */
  7760. function parse_BrtBeginSst(data) {
  7761. return [data.read_shift(4), data.read_shift(4)];
  7762. }
  7763. /* [MS-XLSB] 2.1.7.45 Shared Strings */
  7764. function parse_sst_bin(data, opts) {
  7765. var s = ([]);
  7766. var pass = false;
  7767. recordhopper(data, function hopper_sst(val, R_n, RT) {
  7768. switch(RT) {
  7769. case 0x009F: /* 'BrtBeginSst' */
  7770. s.Count = val[0]; s.Unique = val[1]; break;
  7771. case 0x0013: /* 'BrtSSTItem' */
  7772. s.push(val); break;
  7773. case 0x00A0: /* 'BrtEndSst' */
  7774. return true;
  7775. case 0x0023: /* 'BrtFRTBegin' */
  7776. pass = true; break;
  7777. case 0x0024: /* 'BrtFRTEnd' */
  7778. pass = false; break;
  7779. default:
  7780. if(R_n.indexOf("Begin") > 0){/* empty */}
  7781. else if(R_n.indexOf("End") > 0){/* empty */}
  7782. if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  7783. }
  7784. });
  7785. return s;
  7786. }
  7787. function write_BrtBeginSst(sst, o) {
  7788. if(!o) o = new_buf(8);
  7789. o.write_shift(4, sst.Count);
  7790. o.write_shift(4, sst.Unique);
  7791. return o;
  7792. }
  7793. var write_BrtSSTItem = write_RichStr;
  7794. function write_sst_bin(sst) {
  7795. var ba = buf_array();
  7796. write_record(ba, "BrtBeginSst", write_BrtBeginSst(sst));
  7797. for(var i = 0; i < sst.length; ++i) write_record(ba, "BrtSSTItem", write_BrtSSTItem(sst[i]));
  7798. /* FRTSST */
  7799. write_record(ba, "BrtEndSst");
  7800. return ba.end();
  7801. }
  7802. function _JS2ANSI(str) {
  7803. if(typeof cptable !== 'undefined') return cptable.utils.encode(current_ansi, str);
  7804. var o = [], oo = str.split("");
  7805. for(var i = 0; i < oo.length; ++i) o[i] = oo[i].charCodeAt(0);
  7806. return o;
  7807. }
  7808. /* [MS-OFFCRYPTO] 2.1.4 Version */
  7809. function parse_CRYPTOVersion(blob, length) {
  7810. var o = {};
  7811. o.Major = blob.read_shift(2);
  7812. o.Minor = blob.read_shift(2);
  7813. if(length >= 4) blob.l += length - 4;
  7814. return o;
  7815. }
  7816. /* [MS-OFFCRYPTO] 2.1.5 DataSpaceVersionInfo */
  7817. function parse_DataSpaceVersionInfo(blob) {
  7818. var o = {};
  7819. o.id = blob.read_shift(0, 'lpp4');
  7820. o.R = parse_CRYPTOVersion(blob, 4);
  7821. o.U = parse_CRYPTOVersion(blob, 4);
  7822. o.W = parse_CRYPTOVersion(blob, 4);
  7823. return o;
  7824. }
  7825. /* [MS-OFFCRYPTO] 2.1.6.1 DataSpaceMapEntry Structure */
  7826. function parse_DataSpaceMapEntry(blob) {
  7827. var len = blob.read_shift(4);
  7828. var end = blob.l + len - 4;
  7829. var o = {};
  7830. var cnt = blob.read_shift(4);
  7831. var comps = [];
  7832. /* [MS-OFFCRYPTO] 2.1.6.2 DataSpaceReferenceComponent Structure */
  7833. while(cnt-- > 0) comps.push({ t: blob.read_shift(4), v: blob.read_shift(0, 'lpp4') });
  7834. o.name = blob.read_shift(0, 'lpp4');
  7835. o.comps = comps;
  7836. if(blob.l != end) throw new Error("Bad DataSpaceMapEntry: " + blob.l + " != " + end);
  7837. return o;
  7838. }
  7839. /* [MS-OFFCRYPTO] 2.1.6 DataSpaceMap */
  7840. function parse_DataSpaceMap(blob) {
  7841. var o = [];
  7842. blob.l += 4; // must be 0x8
  7843. var cnt = blob.read_shift(4);
  7844. while(cnt-- > 0) o.push(parse_DataSpaceMapEntry(blob));
  7845. return o;
  7846. }
  7847. /* [MS-OFFCRYPTO] 2.1.7 DataSpaceDefinition */
  7848. function parse_DataSpaceDefinition(blob) {
  7849. var o = [];
  7850. blob.l += 4; // must be 0x8
  7851. var cnt = blob.read_shift(4);
  7852. while(cnt-- > 0) o.push(blob.read_shift(0, 'lpp4'));
  7853. return o;
  7854. }
  7855. /* [MS-OFFCRYPTO] 2.1.8 DataSpaceDefinition */
  7856. function parse_TransformInfoHeader(blob) {
  7857. var o = {};
  7858. /*var len = */blob.read_shift(4);
  7859. blob.l += 4; // must be 0x1
  7860. o.id = blob.read_shift(0, 'lpp4');
  7861. o.name = blob.read_shift(0, 'lpp4');
  7862. o.R = parse_CRYPTOVersion(blob, 4);
  7863. o.U = parse_CRYPTOVersion(blob, 4);
  7864. o.W = parse_CRYPTOVersion(blob, 4);
  7865. return o;
  7866. }
  7867. function parse_Primary(blob) {
  7868. /* [MS-OFFCRYPTO] 2.2.6 IRMDSTransformInfo */
  7869. var hdr = parse_TransformInfoHeader(blob);
  7870. /* [MS-OFFCRYPTO] 2.1.9 EncryptionTransformInfo */
  7871. hdr.ename = blob.read_shift(0, '8lpp4');
  7872. hdr.blksz = blob.read_shift(4);
  7873. hdr.cmode = blob.read_shift(4);
  7874. if(blob.read_shift(4) != 0x04) throw new Error("Bad !Primary record");
  7875. return hdr;
  7876. }
  7877. /* [MS-OFFCRYPTO] 2.3.2 Encryption Header */
  7878. function parse_EncryptionHeader(blob, length) {
  7879. var tgt = blob.l + length;
  7880. var o = {};
  7881. o.Flags = (blob.read_shift(4) & 0x3F);
  7882. blob.l += 4;
  7883. o.AlgID = blob.read_shift(4);
  7884. var valid = false;
  7885. switch(o.AlgID) {
  7886. case 0x660E: case 0x660F: case 0x6610: valid = (o.Flags == 0x24); break;
  7887. case 0x6801: valid = (o.Flags == 0x04); break;
  7888. case 0: valid = (o.Flags == 0x10 || o.Flags == 0x04 || o.Flags == 0x24); break;
  7889. default: throw 'Unrecognized encryption algorithm: ' + o.AlgID;
  7890. }
  7891. if(!valid) throw new Error("Encryption Flags/AlgID mismatch");
  7892. o.AlgIDHash = blob.read_shift(4);
  7893. o.KeySize = blob.read_shift(4);
  7894. o.ProviderType = blob.read_shift(4);
  7895. blob.l += 8;
  7896. o.CSPName = blob.read_shift((tgt-blob.l)>>1, 'utf16le');
  7897. blob.l = tgt;
  7898. return o;
  7899. }
  7900. /* [MS-OFFCRYPTO] 2.3.3 Encryption Verifier */
  7901. function parse_EncryptionVerifier(blob, length) {
  7902. var o = {}, tgt = blob.l + length;
  7903. blob.l += 4; // SaltSize must be 0x10
  7904. o.Salt = blob.slice(blob.l, blob.l+16); blob.l += 16;
  7905. o.Verifier = blob.slice(blob.l, blob.l+16); blob.l += 16;
  7906. /*var sz = */blob.read_shift(4);
  7907. o.VerifierHash = blob.slice(blob.l, tgt); blob.l = tgt;
  7908. return o;
  7909. }
  7910. /* [MS-OFFCRYPTO] 2.3.4.* EncryptionInfo Stream */
  7911. function parse_EncryptionInfo(blob) {
  7912. var vers = parse_CRYPTOVersion(blob);
  7913. switch(vers.Minor) {
  7914. case 0x02: return [vers.Minor, parse_EncInfoStd(blob, vers)];
  7915. case 0x03: return [vers.Minor, parse_EncInfoExt(blob, vers)];
  7916. case 0x04: return [vers.Minor, parse_EncInfoAgl(blob, vers)];
  7917. }
  7918. throw new Error("ECMA-376 Encrypted file unrecognized Version: " + vers.Minor);
  7919. }
  7920. /* [MS-OFFCRYPTO] 2.3.4.5 EncryptionInfo Stream (Standard Encryption) */
  7921. function parse_EncInfoStd(blob) {
  7922. var flags = blob.read_shift(4);
  7923. if((flags & 0x3F) != 0x24) throw new Error("EncryptionInfo mismatch");
  7924. var sz = blob.read_shift(4);
  7925. //var tgt = blob.l + sz;
  7926. var hdr = parse_EncryptionHeader(blob, sz);
  7927. var verifier = parse_EncryptionVerifier(blob, blob.length - blob.l);
  7928. return { t:"Std", h:hdr, v:verifier };
  7929. }
  7930. /* [MS-OFFCRYPTO] 2.3.4.6 EncryptionInfo Stream (Extensible Encryption) */
  7931. function parse_EncInfoExt() { throw new Error("File is password-protected: ECMA-376 Extensible"); }
  7932. /* [MS-OFFCRYPTO] 2.3.4.10 EncryptionInfo Stream (Agile Encryption) */
  7933. function parse_EncInfoAgl(blob) {
  7934. var KeyData = ["saltSize","blockSize","keyBits","hashSize","cipherAlgorithm","cipherChaining","hashAlgorithm","saltValue"];
  7935. blob.l+=4;
  7936. var xml = blob.read_shift(blob.length - blob.l, 'utf8');
  7937. var o = {};
  7938. xml.replace(tagregex, function xml_agile(x) {
  7939. var y = parsexmltag(x);
  7940. switch(strip_ns(y[0])) {
  7941. case '<?xml': break;
  7942. case '<encryption': case '</encryption>': break;
  7943. case '<keyData': KeyData.forEach(function(k) { o[k] = y[k]; }); break;
  7944. case '<dataIntegrity': o.encryptedHmacKey = y.encryptedHmacKey; o.encryptedHmacValue = y.encryptedHmacValue; break;
  7945. case '<keyEncryptors>': case '<keyEncryptors': o.encs = []; break;
  7946. case '</keyEncryptors>': break;
  7947. case '<keyEncryptor': o.uri = y.uri; break;
  7948. case '</keyEncryptor>': break;
  7949. case '<encryptedKey': o.encs.push(y); break;
  7950. default: throw y[0];
  7951. }
  7952. });
  7953. return o;
  7954. }
  7955. /* [MS-OFFCRYPTO] 2.3.5.1 RC4 CryptoAPI Encryption Header */
  7956. function parse_RC4CryptoHeader(blob, length) {
  7957. var o = {};
  7958. var vers = o.EncryptionVersionInfo = parse_CRYPTOVersion(blob, 4); length -= 4;
  7959. if(vers.Minor != 2) throw new Error('unrecognized minor version code: ' + vers.Minor);
  7960. if(vers.Major > 4 || vers.Major < 2) throw new Error('unrecognized major version code: ' + vers.Major);
  7961. o.Flags = blob.read_shift(4); length -= 4;
  7962. var sz = blob.read_shift(4); length -= 4;
  7963. o.EncryptionHeader = parse_EncryptionHeader(blob, sz); length -= sz;
  7964. o.EncryptionVerifier = parse_EncryptionVerifier(blob, length);
  7965. return o;
  7966. }
  7967. /* [MS-OFFCRYPTO] 2.3.6.1 RC4 Encryption Header */
  7968. function parse_RC4Header(blob) {
  7969. var o = {};
  7970. var vers = o.EncryptionVersionInfo = parse_CRYPTOVersion(blob, 4);
  7971. if(vers.Major != 1 || vers.Minor != 1) throw 'unrecognized version code ' + vers.Major + ' : ' + vers.Minor;
  7972. o.Salt = blob.read_shift(16);
  7973. o.EncryptedVerifier = blob.read_shift(16);
  7974. o.EncryptedVerifierHash = blob.read_shift(16);
  7975. return o;
  7976. }
  7977. /* [MS-OFFCRYPTO] 2.3.7.1 Binary Document Password Verifier Derivation */
  7978. function crypto_CreatePasswordVerifier_Method1(Password) {
  7979. var Verifier = 0x0000, PasswordArray;
  7980. var PasswordDecoded = _JS2ANSI(Password);
  7981. var len = PasswordDecoded.length + 1, i, PasswordByte;
  7982. var Intermediate1, Intermediate2, Intermediate3;
  7983. PasswordArray = new_raw_buf(len);
  7984. PasswordArray[0] = PasswordDecoded.length;
  7985. for(i = 1; i != len; ++i) PasswordArray[i] = PasswordDecoded[i-1];
  7986. for(i = len-1; i >= 0; --i) {
  7987. PasswordByte = PasswordArray[i];
  7988. Intermediate1 = ((Verifier & 0x4000) === 0x0000) ? 0 : 1;
  7989. Intermediate2 = (Verifier << 1) & 0x7FFF;
  7990. Intermediate3 = Intermediate1 | Intermediate2;
  7991. Verifier = Intermediate3 ^ PasswordByte;
  7992. }
  7993. return Verifier ^ 0xCE4B;
  7994. }
  7995. /* [MS-OFFCRYPTO] 2.3.7.2 Binary Document XOR Array Initialization */
  7996. var crypto_CreateXorArray_Method1 = (function() {
  7997. var PadArray = [0xBB, 0xFF, 0xFF, 0xBA, 0xFF, 0xFF, 0xB9, 0x80, 0x00, 0xBE, 0x0F, 0x00, 0xBF, 0x0F, 0x00];
  7998. var InitialCode = [0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, 0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, 0x4EC3];
  7999. var XorMatrix = [0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09, 0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF, 0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0, 0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40, 0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5, 0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A, 0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9, 0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0, 0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC, 0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10, 0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168, 0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C, 0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD, 0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC, 0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4];
  8000. var Ror = function(Byte) { return ((Byte/2) | (Byte*128)) & 0xFF; };
  8001. var XorRor = function(byte1, byte2) { return Ror(byte1 ^ byte2); };
  8002. var CreateXorKey_Method1 = function(Password) {
  8003. var XorKey = InitialCode[Password.length - 1];
  8004. var CurrentElement = 0x68;
  8005. for(var i = Password.length-1; i >= 0; --i) {
  8006. var Char = Password[i];
  8007. for(var j = 0; j != 7; ++j) {
  8008. if(Char & 0x40) XorKey ^= XorMatrix[CurrentElement];
  8009. Char *= 2; --CurrentElement;
  8010. }
  8011. }
  8012. return XorKey;
  8013. };
  8014. return function(password) {
  8015. var Password = _JS2ANSI(password);
  8016. var XorKey = CreateXorKey_Method1(Password);
  8017. var Index = Password.length;
  8018. var ObfuscationArray = new_raw_buf(16);
  8019. for(var i = 0; i != 16; ++i) ObfuscationArray[i] = 0x00;
  8020. var Temp, PasswordLastChar, PadIndex;
  8021. if((Index & 1) === 1) {
  8022. Temp = XorKey >> 8;
  8023. ObfuscationArray[Index] = XorRor(PadArray[0], Temp);
  8024. --Index;
  8025. Temp = XorKey & 0xFF;
  8026. PasswordLastChar = Password[Password.length - 1];
  8027. ObfuscationArray[Index] = XorRor(PasswordLastChar, Temp);
  8028. }
  8029. while(Index > 0) {
  8030. --Index;
  8031. Temp = XorKey >> 8;
  8032. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  8033. --Index;
  8034. Temp = XorKey & 0xFF;
  8035. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  8036. }
  8037. Index = 15;
  8038. PadIndex = 15 - Password.length;
  8039. while(PadIndex > 0) {
  8040. Temp = XorKey >> 8;
  8041. ObfuscationArray[Index] = XorRor(PadArray[PadIndex], Temp);
  8042. --Index;
  8043. --PadIndex;
  8044. Temp = XorKey & 0xFF;
  8045. ObfuscationArray[Index] = XorRor(Password[Index], Temp);
  8046. --Index;
  8047. --PadIndex;
  8048. }
  8049. return ObfuscationArray;
  8050. };
  8051. })();
  8052. /* [MS-OFFCRYPTO] 2.3.7.3 Binary Document XOR Data Transformation Method 1 */
  8053. var crypto_DecryptData_Method1 = function(password, Data, XorArrayIndex, XorArray, O) {
  8054. /* If XorArray is set, use it; if O is not set, make changes in-place */
  8055. if(!O) O = Data;
  8056. if(!XorArray) XorArray = crypto_CreateXorArray_Method1(password);
  8057. var Index, Value;
  8058. for(Index = 0; Index != Data.length; ++Index) {
  8059. Value = Data[Index];
  8060. Value ^= XorArray[XorArrayIndex];
  8061. Value = ((Value>>5) | (Value<<3)) & 0xFF;
  8062. O[Index] = Value;
  8063. ++XorArrayIndex;
  8064. }
  8065. return [O, XorArrayIndex, XorArray];
  8066. };
  8067. var crypto_MakeXorDecryptor = function(password) {
  8068. var XorArrayIndex = 0, XorArray = crypto_CreateXorArray_Method1(password);
  8069. return function(Data) {
  8070. var O = crypto_DecryptData_Method1("", Data, XorArrayIndex, XorArray);
  8071. XorArrayIndex = O[1];
  8072. return O[0];
  8073. };
  8074. };
  8075. /* 2.5.343 */
  8076. function parse_XORObfuscation(blob, length, opts, out) {
  8077. var o = ({ key: parseuint16(blob), verificationBytes: parseuint16(blob) });
  8078. if(opts.password) o.verifier = crypto_CreatePasswordVerifier_Method1(opts.password);
  8079. out.valid = o.verificationBytes === o.verifier;
  8080. if(out.valid) out.insitu = crypto_MakeXorDecryptor(opts.password);
  8081. return o;
  8082. }
  8083. /* 2.4.117 */
  8084. function parse_FilePassHeader(blob, length, oo) {
  8085. var o = oo || {}; o.Info = blob.read_shift(2); blob.l -= 2;
  8086. if(o.Info === 1) o.Data = parse_RC4Header(blob, length);
  8087. else o.Data = parse_RC4CryptoHeader(blob, length);
  8088. return o;
  8089. }
  8090. function parse_FilePass(blob, length, opts) {
  8091. var o = ({ Type: opts.biff >= 8 ? blob.read_shift(2) : 0 }); /* wEncryptionType */
  8092. if(o.Type) parse_FilePassHeader(blob, length-2, o);
  8093. else parse_XORObfuscation(blob, opts.biff >= 8 ? length : length - 2, opts, o);
  8094. return o;
  8095. }
  8096. var RTF = (function() {
  8097. function rtf_to_sheet(d, opts) {
  8098. switch(opts.type) {
  8099. case 'base64': return rtf_to_sheet_str(Base64.decode(d), opts);
  8100. case 'binary': return rtf_to_sheet_str(d, opts);
  8101. case 'buffer': return rtf_to_sheet_str(d.toString('binary'), opts);
  8102. case 'array': return rtf_to_sheet_str(cc2str(d), opts);
  8103. }
  8104. throw new Error("Unrecognized type " + opts.type);
  8105. }
  8106. function rtf_to_sheet_str(str, opts) {
  8107. var o = opts || {};
  8108. var ws = o.dense ? ([]) : ({});
  8109. var range = ({s: {c:0, r:0}, e: {c:0, r:0}});
  8110. // TODO: parse
  8111. if(!str.match(/\\trowd/)) throw new Error("RTF missing table");
  8112. ws['!ref'] = encode_range(range);
  8113. return ws;
  8114. }
  8115. function rtf_to_workbook(d, opts) { return sheet_to_workbook(rtf_to_sheet(d, opts), opts); }
  8116. /* TODO: this is a stub */
  8117. function sheet_to_rtf(ws) {
  8118. var o = ["{\\rtf1\\ansi"];
  8119. var r = safe_decode_range(ws['!ref']), cell;
  8120. var dense = Array.isArray(ws);
  8121. for(var R = r.s.r; R <= r.e.r; ++R) {
  8122. o.push("\\trowd\\trautofit1");
  8123. for(var C = r.s.c; C <= r.e.c; ++C) o.push("\\cellx" + (C+1));
  8124. o.push("\\pard\\intbl");
  8125. for(C = r.s.c; C <= r.e.c; ++C) {
  8126. var coord = encode_cell({r:R,c:C});
  8127. cell = dense ? (ws[R]||[])[C]: ws[coord];
  8128. if(!cell || cell.v == null && (!cell.f || cell.F)) continue;
  8129. o.push(" " + (cell.w || (format_cell(cell), cell.w)));
  8130. o.push("\\cell");
  8131. }
  8132. o.push("\\pard\\intbl\\row");
  8133. }
  8134. return o.join("") + "}";
  8135. }
  8136. return {
  8137. to_workbook: rtf_to_workbook,
  8138. to_sheet: rtf_to_sheet,
  8139. from_sheet: sheet_to_rtf
  8140. };
  8141. })();
  8142. function hex2RGB(h) {
  8143. var o = h.slice(h[0]==="#"?1:0).slice(0,6);
  8144. return [parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)];
  8145. }
  8146. function rgb2Hex(rgb) {
  8147. for(var i=0,o=1; i!=3; ++i) o = o*256 + (rgb[i]>255?255:rgb[i]<0?0:rgb[i]);
  8148. return o.toString(16).toUpperCase().slice(1);
  8149. }
  8150. function rgb2HSL(rgb) {
  8151. var R = rgb[0]/255, G = rgb[1]/255, B=rgb[2]/255;
  8152. var M = Math.max(R, G, B), m = Math.min(R, G, B), C = M - m;
  8153. if(C === 0) return [0, 0, R];
  8154. var H6 = 0, S = 0, L2 = (M + m);
  8155. S = C / (L2 > 1 ? 2 - L2 : L2);
  8156. switch(M){
  8157. case R: H6 = ((G - B) / C + 6)%6; break;
  8158. case G: H6 = ((B - R) / C + 2); break;
  8159. case B: H6 = ((R - G) / C + 4); break;
  8160. }
  8161. return [H6 / 6, S, L2 / 2];
  8162. }
  8163. function hsl2RGB(hsl){
  8164. var H = hsl[0], S = hsl[1], L = hsl[2];
  8165. var C = S * 2 * (L < 0.5 ? L : 1 - L), m = L - C/2;
  8166. var rgb = [m,m,m], h6 = 6*H;
  8167. var X;
  8168. if(S !== 0) switch(h6|0) {
  8169. case 0: case 6: X = C * h6; rgb[0] += C; rgb[1] += X; break;
  8170. case 1: X = C * (2 - h6); rgb[0] += X; rgb[1] += C; break;
  8171. case 2: X = C * (h6 - 2); rgb[1] += C; rgb[2] += X; break;
  8172. case 3: X = C * (4 - h6); rgb[1] += X; rgb[2] += C; break;
  8173. case 4: X = C * (h6 - 4); rgb[2] += C; rgb[0] += X; break;
  8174. case 5: X = C * (6 - h6); rgb[2] += X; rgb[0] += C; break;
  8175. }
  8176. for(var i = 0; i != 3; ++i) rgb[i] = Math.round(rgb[i]*255);
  8177. return rgb;
  8178. }
  8179. /* 18.8.3 bgColor tint algorithm */
  8180. function rgb_tint(hex, tint) {
  8181. if(tint === 0) return hex;
  8182. var hsl = rgb2HSL(hex2RGB(hex));
  8183. if (tint < 0) hsl[2] = hsl[2] * (1 + tint);
  8184. else hsl[2] = 1 - (1 - hsl[2]) * (1 - tint);
  8185. return rgb2Hex(hsl2RGB(hsl));
  8186. }
  8187. /* 18.3.1.13 width calculations */
  8188. /* [MS-OI29500] 2.1.595 Column Width & Formatting */
  8189. var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
  8190. function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
  8191. function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
  8192. function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }
  8193. //function px2char_(px) { return (((px - 5)/MDW * 100 + 0.5))/100; }
  8194. //function char2width_(chr) { return (((chr * MDW + 5)/MDW*256))/256; }
  8195. function cycle_width(collw) { return char2width(px2char(width2px(collw))); }
  8196. /* XLSX/XLSB/XLS specify width in units of MDW */
  8197. function find_mdw_colw(collw) {
  8198. var delta = Math.abs(collw - cycle_width(collw)), _MDW = MDW;
  8199. if(delta > 0.005) 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; }
  8200. MDW = _MDW;
  8201. }
  8202. /* XLML specifies width in terms of pixels */
  8203. /*function find_mdw_wpx(wpx) {
  8204. var delta = Infinity, guess = 0, _MDW = MIN_MDW;
  8205. for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) {
  8206. guess = char2width_(px2char_(wpx))*256;
  8207. guess = (guess) % 1;
  8208. if(guess > 0.5) guess--;
  8209. if(Math.abs(guess) < delta) { delta = Math.abs(guess); _MDW = MDW; }
  8210. }
  8211. MDW = _MDW;
  8212. }*/
  8213. function process_col(coll) {
  8214. if(coll.width) {
  8215. coll.wpx = width2px(coll.width);
  8216. coll.wch = px2char(coll.wpx);
  8217. coll.MDW = MDW;
  8218. } else if(coll.wpx) {
  8219. coll.wch = px2char(coll.wpx);
  8220. coll.width = char2width(coll.wch);
  8221. coll.MDW = MDW;
  8222. } else if(typeof coll.wch == 'number') {
  8223. coll.width = char2width(coll.wch);
  8224. coll.wpx = width2px(coll.width);
  8225. coll.MDW = MDW;
  8226. }
  8227. if(coll.customWidth) delete coll.customWidth;
  8228. }
  8229. var DEF_PPI = 96, PPI = DEF_PPI;
  8230. function px2pt(px) { return px * 96 / PPI; }
  8231. function pt2px(pt) { return pt * PPI / 96; }
  8232. /* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
  8233. var XLMLPatternTypeMap = {
  8234. "None": "none",
  8235. "Solid": "solid",
  8236. "Gray50": "mediumGray",
  8237. "Gray75": "darkGray",
  8238. "Gray25": "lightGray",
  8239. "HorzStripe": "darkHorizontal",
  8240. "VertStripe": "darkVertical",
  8241. "ReverseDiagStripe": "darkDown",
  8242. "DiagStripe": "darkUp",
  8243. "DiagCross": "darkGrid",
  8244. "ThickDiagCross": "darkTrellis",
  8245. "ThinHorzStripe": "lightHorizontal",
  8246. "ThinVertStripe": "lightVertical",
  8247. "ThinReverseDiagStripe": "lightDown",
  8248. "ThinHorzCross": "lightGrid"
  8249. };
  8250. /* 18.8.5 borders CT_Borders */
  8251. function parse_borders(t, styles, themes, opts) {
  8252. styles.Borders = [];
  8253. var border = {};
  8254. var pass = false;
  8255. (t[0].match(tagregex)||[]).forEach(function(x) {
  8256. var y = parsexmltag(x);
  8257. switch(strip_ns(y[0])) {
  8258. case '<borders': case '<borders>': case '</borders>': break;
  8259. /* 18.8.4 border CT_Border */
  8260. case '<border': case '<border>': case '<border/>':
  8261. border = {};
  8262. if(y.diagonalUp) border.diagonalUp = parsexmlbool(y.diagonalUp);
  8263. if(y.diagonalDown) border.diagonalDown = parsexmlbool(y.diagonalDown);
  8264. styles.Borders.push(border);
  8265. break;
  8266. case '</border>': break;
  8267. /* note: not in spec, appears to be CT_BorderPr */
  8268. case '<left/>': break;
  8269. case '<left': case '<left>': break;
  8270. case '</left>': break;
  8271. /* note: not in spec, appears to be CT_BorderPr */
  8272. case '<right/>': break;
  8273. case '<right': case '<right>': break;
  8274. case '</right>': break;
  8275. /* 18.8.43 top CT_BorderPr */
  8276. case '<top/>': break;
  8277. case '<top': case '<top>': break;
  8278. case '</top>': break;
  8279. /* 18.8.6 bottom CT_BorderPr */
  8280. case '<bottom/>': break;
  8281. case '<bottom': case '<bottom>': break;
  8282. case '</bottom>': break;
  8283. /* 18.8.13 diagonal CT_BorderPr */
  8284. case '<diagonal': case '<diagonal>': case '<diagonal/>': break;
  8285. case '</diagonal>': break;
  8286. /* 18.8.25 horizontal CT_BorderPr */
  8287. case '<horizontal': case '<horizontal>': case '<horizontal/>': break;
  8288. case '</horizontal>': break;
  8289. /* 18.8.44 vertical CT_BorderPr */
  8290. case '<vertical': case '<vertical>': case '<vertical/>': break;
  8291. case '</vertical>': break;
  8292. /* 18.8.37 start CT_BorderPr */
  8293. case '<start': case '<start>': case '<start/>': break;
  8294. case '</start>': break;
  8295. /* 18.8.16 end CT_BorderPr */
  8296. case '<end': case '<end>': case '<end/>': break;
  8297. case '</end>': break;
  8298. /* 18.8.? color CT_Color */
  8299. case '<color': case '<color>':
  8300. break;
  8301. case '<color/>': case '</color>': break;
  8302. /* 18.2.10 extLst CT_ExtensionList ? */
  8303. case '<extLst': case '<extLst>': case '</extLst>': break;
  8304. case '<ext': pass = true; break;
  8305. case '</ext>': pass = false; break;
  8306. default: if(opts && opts.WTF) {
  8307. if(!pass) throw new Error('unrecognized ' + y[0] + ' in borders');
  8308. }
  8309. }
  8310. });
  8311. }
  8312. /* 18.8.21 fills CT_Fills */
  8313. function parse_fills(t, styles, themes, opts) {
  8314. styles.Fills = [];
  8315. var fill = {};
  8316. var pass = false;
  8317. (t[0].match(tagregex)||[]).forEach(function(x) {
  8318. var y = parsexmltag(x);
  8319. switch(strip_ns(y[0])) {
  8320. case '<fills': case '<fills>': case '</fills>': break;
  8321. /* 18.8.20 fill CT_Fill */
  8322. case '<fill>': case '<fill': case '<fill/>':
  8323. fill = {}; styles.Fills.push(fill); break;
  8324. case '</fill>': break;
  8325. /* 18.8.24 gradientFill CT_GradientFill */
  8326. case '<gradientFill>': break;
  8327. case '<gradientFill':
  8328. case '</gradientFill>': styles.Fills.push(fill); fill = {}; break;
  8329. /* 18.8.32 patternFill CT_PatternFill */
  8330. case '<patternFill': case '<patternFill>':
  8331. if(y.patternType) fill.patternType = y.patternType;
  8332. break;
  8333. case '<patternFill/>': case '</patternFill>': break;
  8334. /* 18.8.3 bgColor CT_Color */
  8335. case '<bgColor':
  8336. if(!fill.bgColor) fill.bgColor = {};
  8337. if(y.indexed) fill.bgColor.indexed = parseInt(y.indexed, 10);
  8338. if(y.theme) fill.bgColor.theme = parseInt(y.theme, 10);
  8339. if(y.tint) fill.bgColor.tint = parseFloat(y.tint);
  8340. /* Excel uses ARGB strings */
  8341. if(y.rgb) fill.bgColor.rgb = y.rgb.slice(-6);
  8342. break;
  8343. case '<bgColor/>': case '</bgColor>': break;
  8344. /* 18.8.19 fgColor CT_Color */
  8345. case '<fgColor':
  8346. if(!fill.fgColor) fill.fgColor = {};
  8347. if(y.theme) fill.fgColor.theme = parseInt(y.theme, 10);
  8348. if(y.tint) fill.fgColor.tint = parseFloat(y.tint);
  8349. /* Excel uses ARGB strings */
  8350. if(y.rgb != null) fill.fgColor.rgb = y.rgb.slice(-6);
  8351. break;
  8352. case '<fgColor/>': case '</fgColor>': break;
  8353. /* 18.8.38 stop CT_GradientStop */
  8354. case '<stop': case '<stop/>': break;
  8355. case '</stop>': break;
  8356. /* 18.8.? color CT_Color */
  8357. case '<color': case '<color/>': break;
  8358. case '</color>': break;
  8359. /* 18.2.10 extLst CT_ExtensionList ? */
  8360. case '<extLst': case '<extLst>': case '</extLst>': break;
  8361. case '<ext': pass = true; break;
  8362. case '</ext>': pass = false; break;
  8363. default: if(opts && opts.WTF) {
  8364. if(!pass) throw new Error('unrecognized ' + y[0] + ' in fills');
  8365. }
  8366. }
  8367. });
  8368. }
  8369. /* 18.8.23 fonts CT_Fonts */
  8370. function parse_fonts(t, styles, themes, opts) {
  8371. styles.Fonts = [];
  8372. var font = {};
  8373. var pass = false;
  8374. (t[0].match(tagregex)||[]).forEach(function(x) {
  8375. var y = parsexmltag(x);
  8376. switch(strip_ns(y[0])) {
  8377. case '<fonts': case '<fonts>': case '</fonts>': break;
  8378. /* 18.8.22 font CT_Font */
  8379. case '<font': case '<font>': break;
  8380. case '</font>': case '<font/>':
  8381. styles.Fonts.push(font);
  8382. font = {};
  8383. break;
  8384. /* 18.8.29 name CT_FontName */
  8385. case '<name': if(y.val) font.name = utf8read(y.val); break;
  8386. case '<name/>': case '</name>': break;
  8387. /* 18.8.2 b CT_BooleanProperty */
  8388. case '<b': font.bold = y.val ? parsexmlbool(y.val) : 1; break;
  8389. case '<b/>': font.bold = 1; break;
  8390. /* 18.8.26 i CT_BooleanProperty */
  8391. case '<i': font.italic = y.val ? parsexmlbool(y.val) : 1; break;
  8392. case '<i/>': font.italic = 1; break;
  8393. /* 18.4.13 u CT_UnderlineProperty */
  8394. case '<u':
  8395. switch(y.val) {
  8396. case "none": font.underline = 0x00; break;
  8397. case "single": font.underline = 0x01; break;
  8398. case "double": font.underline = 0x02; break;
  8399. case "singleAccounting": font.underline = 0x21; break;
  8400. case "doubleAccounting": font.underline = 0x22; break;
  8401. } break;
  8402. case '<u/>': font.underline = 1; break;
  8403. /* 18.4.10 strike CT_BooleanProperty */
  8404. case '<strike': font.strike = y.val ? parsexmlbool(y.val) : 1; break;
  8405. case '<strike/>': font.strike = 1; break;
  8406. /* 18.4.2 outline CT_BooleanProperty */
  8407. case '<outline': font.outline = y.val ? parsexmlbool(y.val) : 1; break;
  8408. case '<outline/>': font.outline = 1; break;
  8409. /* 18.8.36 shadow CT_BooleanProperty */
  8410. case '<shadow': font.shadow = y.val ? parsexmlbool(y.val) : 1; break;
  8411. case '<shadow/>': font.shadow = 1; break;
  8412. /* 18.8.12 condense CT_BooleanProperty */
  8413. case '<condense': font.condense = y.val ? parsexmlbool(y.val) : 1; break;
  8414. case '<condense/>': font.condense = 1; break;
  8415. /* 18.8.17 extend CT_BooleanProperty */
  8416. case '<extend': font.extend = y.val ? parsexmlbool(y.val) : 1; break;
  8417. case '<extend/>': font.extend = 1; break;
  8418. /* 18.4.11 sz CT_FontSize */
  8419. case '<sz': if(y.val) font.sz = +y.val; break;
  8420. case '<sz/>': case '</sz>': break;
  8421. /* 18.4.14 vertAlign CT_VerticalAlignFontProperty */
  8422. case '<vertAlign': if(y.val) font.vertAlign = y.val; break;
  8423. case '<vertAlign/>': case '</vertAlign>': break;
  8424. /* 18.8.18 family CT_FontFamily */
  8425. case '<family': if(y.val) font.family = parseInt(y.val,10); break;
  8426. case '<family/>': case '</family>': break;
  8427. /* 18.8.35 scheme CT_FontScheme */
  8428. case '<scheme': if(y.val) font.scheme = y.val; break;
  8429. case '<scheme/>': case '</scheme>': break;
  8430. /* 18.4.1 charset CT_IntProperty */
  8431. case '<charset':
  8432. if(y.val == '1') break;
  8433. y.codepage = CS2CP[parseInt(y.val, 10)];
  8434. break;
  8435. /* 18.?.? color CT_Color */
  8436. case '<color':
  8437. if(!font.color) font.color = {};
  8438. if(y.auto) font.color.auto = parsexmlbool(y.auto);
  8439. if(y.rgb) font.color.rgb = y.rgb.slice(-6);
  8440. else if(y.indexed) {
  8441. font.color.index = parseInt(y.indexed, 10);
  8442. var icv = XLSIcv[font.color.index];
  8443. if(font.color.index == 81) icv = XLSIcv[1];
  8444. if(!icv) throw new Error(x);
  8445. font.color.rgb = icv[0].toString(16) + icv[1].toString(16) + icv[2].toString(16);
  8446. } else if(y.theme) {
  8447. font.color.theme = parseInt(y.theme, 10);
  8448. if(y.tint) font.color.tint = parseFloat(y.tint);
  8449. if(y.theme && themes.themeElements && themes.themeElements.clrScheme) {
  8450. font.color.rgb = rgb_tint(themes.themeElements.clrScheme[font.color.theme].rgb, font.color.tint || 0);
  8451. }
  8452. }
  8453. break;
  8454. case '<color/>': case '</color>': break;
  8455. /* note: sometimes mc:AlternateContent appears bare */
  8456. case '<AlternateContent': pass = true; break;
  8457. case '</AlternateContent>': pass = false; break;
  8458. /* 18.2.10 extLst CT_ExtensionList ? */
  8459. case '<extLst': case '<extLst>': case '</extLst>': break;
  8460. case '<ext': pass = true; break;
  8461. case '</ext>': pass = false; break;
  8462. default: if(opts && opts.WTF) {
  8463. if(!pass) throw new Error('unrecognized ' + y[0] + ' in fonts');
  8464. }
  8465. }
  8466. });
  8467. }
  8468. /* 18.8.31 numFmts CT_NumFmts */
  8469. function parse_numFmts(t, styles, opts) {
  8470. styles.NumberFmt = [];
  8471. var k/*Array<number>*/ = (keys(SSF._table));
  8472. for(var i=0; i < k.length; ++i) styles.NumberFmt[k[i]] = SSF._table[k[i]];
  8473. var m = t[0].match(tagregex);
  8474. if(!m) return;
  8475. for(i=0; i < m.length; ++i) {
  8476. var y = parsexmltag(m[i]);
  8477. switch(strip_ns(y[0])) {
  8478. case '<numFmts': case '</numFmts>': case '<numFmts/>': case '<numFmts>': break;
  8479. case '<numFmt': {
  8480. var f=unescapexml(utf8read(y.formatCode)), j=parseInt(y.numFmtId,10);
  8481. styles.NumberFmt[j] = f;
  8482. if(j>0) {
  8483. if(j > 0x188) {
  8484. for(j = 0x188; j > 0x3c; --j) if(styles.NumberFmt[j] == null) break;
  8485. styles.NumberFmt[j] = f;
  8486. }
  8487. SSF.load(f,j);
  8488. }
  8489. } break;
  8490. case '</numFmt>': break;
  8491. default: if(opts.WTF) throw new Error('unrecognized ' + y[0] + ' in numFmts');
  8492. }
  8493. }
  8494. }
  8495. function write_numFmts(NF) {
  8496. var o = ["<numFmts>"];
  8497. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  8498. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) o[o.length] = (writextag('numFmt',null,{numFmtId:i,formatCode:escapexml(NF[i])}));
  8499. });
  8500. if(o.length === 1) return "";
  8501. o[o.length] = ("</numFmts>");
  8502. o[0] = writextag('numFmts', null, { count:o.length-2 }).replace("/>", ">");
  8503. return o.join("");
  8504. }
  8505. /* 18.8.10 cellXfs CT_CellXfs */
  8506. var cellXF_uint = [ "numFmtId", "fillId", "fontId", "borderId", "xfId" ];
  8507. var cellXF_bool = [ "applyAlignment", "applyBorder", "applyFill", "applyFont", "applyNumberFormat", "applyProtection", "pivotButton", "quotePrefix" ];
  8508. function parse_cellXfs(t, styles, opts) {
  8509. styles.CellXf = [];
  8510. var xf;
  8511. var pass = false;
  8512. (t[0].match(tagregex)||[]).forEach(function(x) {
  8513. var y = parsexmltag(x), i = 0;
  8514. switch(strip_ns(y[0])) {
  8515. case '<cellXfs': case '<cellXfs>': case '<cellXfs/>': case '</cellXfs>': break;
  8516. /* 18.8.45 xf CT_Xf */
  8517. case '<xf': case '<xf/>':
  8518. xf = y;
  8519. delete xf[0];
  8520. for(i = 0; i < cellXF_uint.length; ++i) if(xf[cellXF_uint[i]])
  8521. xf[cellXF_uint[i]] = parseInt(xf[cellXF_uint[i]], 10);
  8522. for(i = 0; i < cellXF_bool.length; ++i) if(xf[cellXF_bool[i]])
  8523. xf[cellXF_bool[i]] = parsexmlbool(xf[cellXF_bool[i]]);
  8524. if(xf.numFmtId > 0x188) {
  8525. for(i = 0x188; i > 0x3c; --i) if(styles.NumberFmt[xf.numFmtId] == styles.NumberFmt[i]) { xf.numFmtId = i; break; }
  8526. }
  8527. styles.CellXf.push(xf); break;
  8528. case '</xf>': break;
  8529. /* 18.8.1 alignment CT_CellAlignment */
  8530. case '<alignment': case '<alignment/>':
  8531. var alignment = {};
  8532. if(y.vertical) alignment.vertical = y.vertical;
  8533. if(y.horizontal) alignment.horizontal = y.horizontal;
  8534. if(y.textRotation != null) alignment.textRotation = y.textRotation;
  8535. if(y.indent) alignment.indent = y.indent;
  8536. if(y.wrapText) alignment.wrapText = parsexmlbool(y.wrapText);
  8537. xf.alignment = alignment;
  8538. break;
  8539. case '</alignment>': break;
  8540. /* 18.8.33 protection CT_CellProtection */
  8541. case '<protection':
  8542. break;
  8543. case '</protection>': case '<protection/>': break;
  8544. /* note: sometimes mc:AlternateContent appears bare */
  8545. case '<AlternateContent': pass = true; break;
  8546. case '</AlternateContent>': pass = false; break;
  8547. /* 18.2.10 extLst CT_ExtensionList ? */
  8548. case '<extLst': case '<extLst>': case '</extLst>': break;
  8549. case '<ext': pass = true; break;
  8550. case '</ext>': pass = false; break;
  8551. default: if(opts && opts.WTF) {
  8552. if(!pass) throw new Error('unrecognized ' + y[0] + ' in cellXfs');
  8553. }
  8554. }
  8555. });
  8556. }
  8557. function write_cellXfs(cellXfs) {
  8558. var o = [];
  8559. o[o.length] = (writextag('cellXfs',null));
  8560. cellXfs.forEach(function(c) {
  8561. o[o.length] = (writextag('xf', null, c));
  8562. });
  8563. o[o.length] = ("</cellXfs>");
  8564. if(o.length === 2) return "";
  8565. o[0] = writextag('cellXfs',null, {count:o.length-2}).replace("/>",">");
  8566. return o.join("");
  8567. }
  8568. /* 18.8 Styles CT_Stylesheet*/
  8569. var parse_sty_xml= (function make_pstyx() {
  8570. var numFmtRegex = /<(?:\w+:)?numFmts([^>]*)>[\S\s]*?<\/(?:\w+:)?numFmts>/;
  8571. var cellXfRegex = /<(?:\w+:)?cellXfs([^>]*)>[\S\s]*?<\/(?:\w+:)?cellXfs>/;
  8572. var fillsRegex = /<(?:\w+:)?fills([^>]*)>[\S\s]*?<\/(?:\w+:)?fills>/;
  8573. var fontsRegex = /<(?:\w+:)?fonts([^>]*)>[\S\s]*?<\/(?:\w+:)?fonts>/;
  8574. var bordersRegex = /<(?:\w+:)?borders([^>]*)>[\S\s]*?<\/(?:\w+:)?borders>/;
  8575. return function parse_sty_xml(data, themes, opts) {
  8576. var styles = {};
  8577. if(!data) return styles;
  8578. data = data.replace(/<!--([\s\S]*?)-->/mg,"").replace(/<!DOCTYPE[^\[]*\[[^\]]*\]>/gm,"");
  8579. /* 18.8.39 styleSheet CT_Stylesheet */
  8580. var t;
  8581. /* 18.8.31 numFmts CT_NumFmts ? */
  8582. if((t=data.match(numFmtRegex))) parse_numFmts(t, styles, opts);
  8583. /* 18.8.23 fonts CT_Fonts ? */
  8584. if((t=data.match(fontsRegex))) parse_fonts(t, styles, themes, opts);
  8585. /* 18.8.21 fills CT_Fills ? */
  8586. if((t=data.match(fillsRegex))) parse_fills(t, styles, themes, opts);
  8587. /* 18.8.5 borders CT_Borders ? */
  8588. if((t=data.match(bordersRegex))) parse_borders(t, styles, themes, opts);
  8589. /* 18.8.9 cellStyleXfs CT_CellStyleXfs ? */
  8590. /* 18.8.8 cellStyles CT_CellStyles ? */
  8591. /* 18.8.10 cellXfs CT_CellXfs ? */
  8592. if((t=data.match(cellXfRegex))) parse_cellXfs(t, styles, opts);
  8593. /* 18.8.15 dxfs CT_Dxfs ? */
  8594. /* 18.8.42 tableStyles CT_TableStyles ? */
  8595. /* 18.8.11 colors CT_Colors ? */
  8596. /* 18.2.10 extLst CT_ExtensionList ? */
  8597. return styles;
  8598. };
  8599. })();
  8600. var STYLES_XML_ROOT = writextag('styleSheet', null, {
  8601. 'xmlns': XMLNS.main[0],
  8602. 'xmlns:vt': XMLNS.vt
  8603. });
  8604. RELS.STY = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
  8605. function write_sty_xml(wb, opts) {
  8606. var o = [XML_HEADER, STYLES_XML_ROOT], w;
  8607. if(wb.SSF && (w = write_numFmts(wb.SSF)) != null) o[o.length] = w;
  8608. o[o.length] = ('<fonts count="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
  8609. o[o.length] = ('<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>');
  8610. o[o.length] = ('<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>');
  8611. o[o.length] = ('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>');
  8612. if((w = write_cellXfs(opts.cellXfs))) o[o.length] = (w);
  8613. o[o.length] = ('<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>');
  8614. o[o.length] = ('<dxfs count="0"/>');
  8615. o[o.length] = ('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4"/>');
  8616. if(o.length>2){ o[o.length] = ('</styleSheet>'); o[1]=o[1].replace("/>",">"); }
  8617. return o.join("");
  8618. }
  8619. /* [MS-XLSB] 2.4.657 BrtFmt */
  8620. function parse_BrtFmt(data, length) {
  8621. var numFmtId = data.read_shift(2);
  8622. var stFmtCode = parse_XLWideString(data,length-2);
  8623. return [numFmtId, stFmtCode];
  8624. }
  8625. function write_BrtFmt(i, f, o) {
  8626. if(!o) o = new_buf(6 + 4 * f.length);
  8627. o.write_shift(2, i);
  8628. write_XLWideString(f, o);
  8629. var out = (o.length > o.l) ? o.slice(0, o.l) : o;
  8630. if(o.l == null) o.l = o.length;
  8631. return out;
  8632. }
  8633. /* [MS-XLSB] 2.4.659 BrtFont TODO */
  8634. function parse_BrtFont(data, length, opts) {
  8635. var out = ({});
  8636. out.sz = data.read_shift(2) / 20;
  8637. var grbit = parse_FontFlags(data, 2, opts);
  8638. if(grbit.fItalic) out.italic = 1;
  8639. if(grbit.fCondense) out.condense = 1;
  8640. if(grbit.fExtend) out.extend = 1;
  8641. if(grbit.fShadow) out.shadow = 1;
  8642. if(grbit.fOutline) out.outline = 1;
  8643. if(grbit.fStrikeout) out.strike = 1;
  8644. var bls = data.read_shift(2);
  8645. if(bls === 0x02BC) out.bold = 1;
  8646. switch(data.read_shift(2)) {
  8647. /* case 0: out.vertAlign = "baseline"; break; */
  8648. case 1: out.vertAlign = "superscript"; break;
  8649. case 2: out.vertAlign = "subscript"; break;
  8650. }
  8651. var underline = data.read_shift(1);
  8652. if(underline != 0) out.underline = underline;
  8653. var family = data.read_shift(1);
  8654. if(family > 0) out.family = family;
  8655. var bCharSet = data.read_shift(1);
  8656. if(bCharSet > 0) out.charset = bCharSet;
  8657. data.l++;
  8658. out.color = parse_BrtColor(data, 8);
  8659. switch(data.read_shift(1)) {
  8660. /* case 0: out.scheme = "none": break; */
  8661. case 1: out.scheme = "major"; break;
  8662. case 2: out.scheme = "minor"; break;
  8663. }
  8664. out.name = parse_XLWideString(data, length - 21);
  8665. return out;
  8666. }
  8667. function write_BrtFont(font, o) {
  8668. if(!o) o = new_buf(25+4*32);
  8669. o.write_shift(2, font.sz * 20);
  8670. write_FontFlags(font, o);
  8671. o.write_shift(2, font.bold ? 0x02BC : 0x0190);
  8672. var sss = 0;
  8673. if(font.vertAlign == "superscript") sss = 1;
  8674. else if(font.vertAlign == "subscript") sss = 2;
  8675. o.write_shift(2, sss);
  8676. o.write_shift(1, font.underline || 0);
  8677. o.write_shift(1, font.family || 0);
  8678. o.write_shift(1, font.charset || 0);
  8679. o.write_shift(1, 0);
  8680. write_BrtColor(font.color, o);
  8681. var scheme = 0;
  8682. if(font.scheme == "major") scheme = 1;
  8683. if(font.scheme == "minor") scheme = 2;
  8684. o.write_shift(1, scheme);
  8685. write_XLWideString(font.name, o);
  8686. return o.length > o.l ? o.slice(0, o.l) : o;
  8687. }
  8688. /* [MS-XLSB] 2.4.650 BrtFill */
  8689. var XLSBFillPTNames = [
  8690. "none",
  8691. "solid",
  8692. "mediumGray",
  8693. "darkGray",
  8694. "lightGray",
  8695. "darkHorizontal",
  8696. "darkVertical",
  8697. "darkDown",
  8698. "darkUp",
  8699. "darkGrid",
  8700. "darkTrellis",
  8701. "lightHorizontal",
  8702. "lightVertical",
  8703. "lightDown",
  8704. "lightUp",
  8705. "lightGrid",
  8706. "lightTrellis",
  8707. "gray125",
  8708. "gray0625"
  8709. ];
  8710. var rev_XLSBFillPTNames = (evert(XLSBFillPTNames));
  8711. /* TODO: gradient fill representation */
  8712. var parse_BrtFill = parsenoop;
  8713. function write_BrtFill(fill, o) {
  8714. if(!o) o = new_buf(4*3 + 8*7 + 16*1);
  8715. var fls = rev_XLSBFillPTNames[fill.patternType];
  8716. if(fls == null) fls = 0x28;
  8717. o.write_shift(4, fls);
  8718. var j = 0;
  8719. if(fls != 0x28) {
  8720. /* TODO: custom FG Color */
  8721. write_BrtColor({auto:1}, o);
  8722. /* TODO: custom BG Color */
  8723. write_BrtColor({auto:1}, o);
  8724. for(; j < 12; ++j) o.write_shift(4, 0);
  8725. } else {
  8726. for(; j < 4; ++j) o.write_shift(4, 0);
  8727. for(; j < 12; ++j) o.write_shift(4, 0); /* TODO */
  8728. /* iGradientType */
  8729. /* xnumDegree */
  8730. /* xnumFillToLeft */
  8731. /* xnumFillToRight */
  8732. /* xnumFillToTop */
  8733. /* xnumFillToBottom */
  8734. /* cNumStop */
  8735. /* xfillGradientStop */
  8736. }
  8737. return o.length > o.l ? o.slice(0, o.l) : o;
  8738. }
  8739. /* [MS-XLSB] 2.4.824 BrtXF */
  8740. function parse_BrtXF(data, length) {
  8741. var tgt = data.l + length;
  8742. var ixfeParent = data.read_shift(2);
  8743. var ifmt = data.read_shift(2);
  8744. data.l = tgt;
  8745. return {ixfe:ixfeParent, numFmtId:ifmt };
  8746. }
  8747. function write_BrtXF(data, ixfeP, o) {
  8748. if(!o) o = new_buf(16);
  8749. o.write_shift(2, ixfeP||0);
  8750. o.write_shift(2, data.numFmtId||0);
  8751. o.write_shift(2, 0); /* iFont */
  8752. o.write_shift(2, 0); /* iFill */
  8753. o.write_shift(2, 0); /* ixBorder */
  8754. o.write_shift(1, 0); /* trot */
  8755. o.write_shift(1, 0); /* indent */
  8756. var flow = 0;
  8757. o.write_shift(1, flow); /* flags */
  8758. o.write_shift(1, 0); /* flags */
  8759. o.write_shift(1, 0); /* xfGrbitAtr */
  8760. o.write_shift(1, 0);
  8761. return o;
  8762. }
  8763. /* [MS-XLSB] 2.5.4 Blxf TODO */
  8764. function write_Blxf(data, o) {
  8765. if(!o) o = new_buf(10);
  8766. o.write_shift(1, 0); /* dg */
  8767. o.write_shift(1, 0);
  8768. o.write_shift(4, 0); /* color */
  8769. o.write_shift(4, 0); /* color */
  8770. return o;
  8771. }
  8772. /* [MS-XLSB] 2.4.302 BrtBorder TODO */
  8773. var parse_BrtBorder = parsenoop;
  8774. function write_BrtBorder(border, o) {
  8775. if(!o) o = new_buf(51);
  8776. o.write_shift(1, 0); /* diagonal */
  8777. write_Blxf(null, o); /* top */
  8778. write_Blxf(null, o); /* bottom */
  8779. write_Blxf(null, o); /* left */
  8780. write_Blxf(null, o); /* right */
  8781. write_Blxf(null, o); /* diag */
  8782. return o.length > o.l ? o.slice(0, o.l) : o;
  8783. }
  8784. /* [MS-XLSB] 2.4.763 BrtStyle TODO */
  8785. function write_BrtStyle(style, o) {
  8786. if(!o) o = new_buf(12+4*10);
  8787. o.write_shift(4, style.xfId);
  8788. o.write_shift(2, 1);
  8789. o.write_shift(1, +style.builtinId);
  8790. o.write_shift(1, 0); /* iLevel */
  8791. write_XLNullableWideString(style.name || "", o);
  8792. return o.length > o.l ? o.slice(0, o.l) : o;
  8793. }
  8794. /* [MS-XLSB] 2.4.272 BrtBeginTableStyles */
  8795. function write_BrtBeginTableStyles(cnt, defTableStyle, defPivotStyle) {
  8796. var o = new_buf(4+256*2*4);
  8797. o.write_shift(4, cnt);
  8798. write_XLNullableWideString(defTableStyle, o);
  8799. write_XLNullableWideString(defPivotStyle, o);
  8800. return o.length > o.l ? o.slice(0, o.l) : o;
  8801. }
  8802. /* [MS-XLSB] 2.1.7.50 Styles */
  8803. function parse_sty_bin(data, themes, opts) {
  8804. var styles = {};
  8805. styles.NumberFmt = ([]);
  8806. for(var y in SSF._table) styles.NumberFmt[y] = SSF._table[y];
  8807. styles.CellXf = [];
  8808. styles.Fonts = [];
  8809. var state = [];
  8810. var pass = false;
  8811. recordhopper(data, function hopper_sty(val, R_n, RT) {
  8812. switch(RT) {
  8813. case 0x002C: /* 'BrtFmt' */
  8814. styles.NumberFmt[val[0]] = val[1]; SSF.load(val[1], val[0]);
  8815. break;
  8816. case 0x002B: /* 'BrtFont' */
  8817. styles.Fonts.push(val);
  8818. if(val.color.theme != null && themes && themes.themeElements && themes.themeElements.clrScheme) {
  8819. val.color.rgb = rgb_tint(themes.themeElements.clrScheme[val.color.theme].rgb, val.color.tint || 0);
  8820. }
  8821. break;
  8822. case 0x0401: /* 'BrtKnownFonts' */ break;
  8823. case 0x002D: /* 'BrtFill' */
  8824. break;
  8825. case 0x002E: /* 'BrtBorder' */
  8826. break;
  8827. case 0x002F: /* 'BrtXF' */
  8828. if(state[state.length - 1] == "BrtBeginCellXFs") {
  8829. styles.CellXf.push(val);
  8830. }
  8831. break;
  8832. case 0x0030: /* 'BrtStyle' */
  8833. case 0x01FB: /* 'BrtDXF' */
  8834. case 0x023C: /* 'BrtMRUColor' */
  8835. case 0x01DB: /* 'BrtIndexedColor': */
  8836. break;
  8837. case 0x0493: /* 'BrtDXF14' */
  8838. case 0x0836: /* 'BrtDXF15' */
  8839. case 0x046A: /* 'BrtSlicerStyleElement' */
  8840. case 0x0200: /* 'BrtTableStyleElement' */
  8841. case 0x082F: /* 'BrtTimelineStyleElement' */
  8842. case 0x0C00: /* 'BrtUid' */
  8843. break;
  8844. case 0x0023: /* 'BrtFRTBegin' */
  8845. pass = true; break;
  8846. case 0x0024: /* 'BrtFRTEnd' */
  8847. pass = false; break;
  8848. case 0x0025: /* 'BrtACBegin' */
  8849. state.push(R_n); pass = true; break;
  8850. case 0x0026: /* 'BrtACEnd' */
  8851. state.pop(); pass = false; break;
  8852. default:
  8853. if((R_n||"").indexOf("Begin") > 0) state.push(R_n);
  8854. else if((R_n||"").indexOf("End") > 0) state.pop();
  8855. else if(!pass || (opts.WTF && state[state.length-1] != "BrtACBegin")) throw new Error("Unexpected record " + RT + " " + R_n);
  8856. }
  8857. });
  8858. return styles;
  8859. }
  8860. function write_FMTS_bin(ba, NF) {
  8861. if(!NF) return;
  8862. var cnt = 0;
  8863. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  8864. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) ++cnt;
  8865. });
  8866. if(cnt == 0) return;
  8867. write_record(ba, "BrtBeginFmts", write_UInt32LE(cnt));
  8868. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  8869. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) write_record(ba, "BrtFmt", write_BrtFmt(i, NF[i]));
  8870. });
  8871. write_record(ba, "BrtEndFmts");
  8872. }
  8873. function write_FONTS_bin(ba) {
  8874. var cnt = 1;
  8875. if(cnt == 0) return;
  8876. write_record(ba, "BrtBeginFonts", write_UInt32LE(cnt));
  8877. write_record(ba, "BrtFont", write_BrtFont({
  8878. sz:12,
  8879. color: {theme:1},
  8880. name: "Calibri",
  8881. family: 2,
  8882. scheme: "minor"
  8883. }));
  8884. /* 1*65491BrtFont [ACFONTS] */
  8885. write_record(ba, "BrtEndFonts");
  8886. }
  8887. function write_FILLS_bin(ba) {
  8888. var cnt = 2;
  8889. if(cnt == 0) return;
  8890. write_record(ba, "BrtBeginFills", write_UInt32LE(cnt));
  8891. write_record(ba, "BrtFill", write_BrtFill({patternType:"none"}));
  8892. write_record(ba, "BrtFill", write_BrtFill({patternType:"gray125"}));
  8893. /* 1*65431BrtFill */
  8894. write_record(ba, "BrtEndFills");
  8895. }
  8896. function write_BORDERS_bin(ba) {
  8897. var cnt = 1;
  8898. if(cnt == 0) return;
  8899. write_record(ba, "BrtBeginBorders", write_UInt32LE(cnt));
  8900. write_record(ba, "BrtBorder", write_BrtBorder({}));
  8901. /* 1*65430BrtBorder */
  8902. write_record(ba, "BrtEndBorders");
  8903. }
  8904. function write_CELLSTYLEXFS_bin(ba) {
  8905. var cnt = 1;
  8906. write_record(ba, "BrtBeginCellStyleXFs", write_UInt32LE(cnt));
  8907. write_record(ba, "BrtXF", write_BrtXF({
  8908. numFmtId: 0,
  8909. fontId: 0,
  8910. fillId: 0,
  8911. borderId: 0
  8912. }, 0xFFFF));
  8913. /* 1*65430(BrtXF *FRT) */
  8914. write_record(ba, "BrtEndCellStyleXFs");
  8915. }
  8916. function write_CELLXFS_bin(ba, data) {
  8917. write_record(ba, "BrtBeginCellXFs", write_UInt32LE(data.length));
  8918. data.forEach(function(c) { write_record(ba, "BrtXF", write_BrtXF(c,0)); });
  8919. /* 1*65430(BrtXF *FRT) */
  8920. write_record(ba, "BrtEndCellXFs");
  8921. }
  8922. function write_STYLES_bin(ba) {
  8923. var cnt = 1;
  8924. write_record(ba, "BrtBeginStyles", write_UInt32LE(cnt));
  8925. write_record(ba, "BrtStyle", write_BrtStyle({
  8926. xfId:0,
  8927. builtinId:0,
  8928. name:"Normal"
  8929. }));
  8930. /* 1*65430(BrtStyle *FRT) */
  8931. write_record(ba, "BrtEndStyles");
  8932. }
  8933. function write_DXFS_bin(ba) {
  8934. var cnt = 0;
  8935. write_record(ba, "BrtBeginDXFs", write_UInt32LE(cnt));
  8936. /* *2147483647(BrtDXF *FRT) */
  8937. write_record(ba, "BrtEndDXFs");
  8938. }
  8939. function write_TABLESTYLES_bin(ba) {
  8940. var cnt = 0;
  8941. write_record(ba, "BrtBeginTableStyles", write_BrtBeginTableStyles(cnt, "TableStyleMedium9", "PivotStyleMedium4"));
  8942. /* *TABLESTYLE */
  8943. write_record(ba, "BrtEndTableStyles");
  8944. }
  8945. function write_COLORPALETTE_bin() {
  8946. return;
  8947. /* BrtBeginColorPalette [INDEXEDCOLORS] [MRUCOLORS] BrtEndColorPalette */
  8948. }
  8949. /* [MS-XLSB] 2.1.7.50 Styles */
  8950. function write_sty_bin(wb, opts) {
  8951. var ba = buf_array();
  8952. write_record(ba, "BrtBeginStyleSheet");
  8953. write_FMTS_bin(ba, wb.SSF);
  8954. write_FONTS_bin(ba, wb);
  8955. write_FILLS_bin(ba, wb);
  8956. write_BORDERS_bin(ba, wb);
  8957. write_CELLSTYLEXFS_bin(ba, wb);
  8958. write_CELLXFS_bin(ba, opts.cellXfs);
  8959. write_STYLES_bin(ba, wb);
  8960. write_DXFS_bin(ba, wb);
  8961. write_TABLESTYLES_bin(ba, wb);
  8962. write_COLORPALETTE_bin(ba, wb);
  8963. /* FRTSTYLESHEET*/
  8964. write_record(ba, "BrtEndStyleSheet");
  8965. return ba.end();
  8966. }
  8967. RELS.THEME = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
  8968. /* Even though theme layout is dk1 lt1 dk2 lt2, true order is lt1 dk1 lt2 dk2 */
  8969. var XLSXThemeClrScheme = [
  8970. '</a:lt1>', '</a:dk1>', '</a:lt2>', '</a:dk2>',
  8971. '</a:accent1>', '</a:accent2>', '</a:accent3>',
  8972. '</a:accent4>', '</a:accent5>', '</a:accent6>',
  8973. '</a:hlink>', '</a:folHlink>'
  8974. ];
  8975. /* 20.1.6.2 clrScheme CT_ColorScheme */
  8976. function parse_clrScheme(t, themes, opts) {
  8977. themes.themeElements.clrScheme = [];
  8978. var color = {};
  8979. (t[0].match(tagregex)||[]).forEach(function(x) {
  8980. var y = parsexmltag(x);
  8981. switch(y[0]) {
  8982. /* 20.1.6.2 clrScheme (Color Scheme) CT_ColorScheme */
  8983. case '<a:clrScheme': case '</a:clrScheme>': break;
  8984. /* 20.1.2.3.32 srgbClr CT_SRgbColor */
  8985. case '<a:srgbClr':
  8986. color.rgb = y.val; break;
  8987. /* 20.1.2.3.33 sysClr CT_SystemColor */
  8988. case '<a:sysClr':
  8989. color.rgb = y.lastClr; break;
  8990. /* 20.1.4.1.1 accent1 (Accent 1) */
  8991. /* 20.1.4.1.2 accent2 (Accent 2) */
  8992. /* 20.1.4.1.3 accent3 (Accent 3) */
  8993. /* 20.1.4.1.4 accent4 (Accent 4) */
  8994. /* 20.1.4.1.5 accent5 (Accent 5) */
  8995. /* 20.1.4.1.6 accent6 (Accent 6) */
  8996. /* 20.1.4.1.9 dk1 (Dark 1) */
  8997. /* 20.1.4.1.10 dk2 (Dark 2) */
  8998. /* 20.1.4.1.15 folHlink (Followed Hyperlink) */
  8999. /* 20.1.4.1.19 hlink (Hyperlink) */
  9000. /* 20.1.4.1.22 lt1 (Light 1) */
  9001. /* 20.1.4.1.23 lt2 (Light 2) */
  9002. case '<a:dk1>': case '</a:dk1>':
  9003. case '<a:lt1>': case '</a:lt1>':
  9004. case '<a:dk2>': case '</a:dk2>':
  9005. case '<a:lt2>': case '</a:lt2>':
  9006. case '<a:accent1>': case '</a:accent1>':
  9007. case '<a:accent2>': case '</a:accent2>':
  9008. case '<a:accent3>': case '</a:accent3>':
  9009. case '<a:accent4>': case '</a:accent4>':
  9010. case '<a:accent5>': case '</a:accent5>':
  9011. case '<a:accent6>': case '</a:accent6>':
  9012. case '<a:hlink>': case '</a:hlink>':
  9013. case '<a:folHlink>': case '</a:folHlink>':
  9014. if (y[0].charAt(1) === '/') {
  9015. themes.themeElements.clrScheme[XLSXThemeClrScheme.indexOf(y[0])] = color;
  9016. color = {};
  9017. } else {
  9018. color.name = y[0].slice(3, y[0].length - 1);
  9019. }
  9020. break;
  9021. default: if(opts && opts.WTF) throw new Error('Unrecognized ' + y[0] + ' in clrScheme');
  9022. }
  9023. });
  9024. }
  9025. /* 20.1.4.1.18 fontScheme CT_FontScheme */
  9026. function parse_fontScheme() { }
  9027. /* 20.1.4.1.15 fmtScheme CT_StyleMatrix */
  9028. function parse_fmtScheme() { }
  9029. var clrsregex = /<a:clrScheme([^>]*)>[\s\S]*<\/a:clrScheme>/;
  9030. var fntsregex = /<a:fontScheme([^>]*)>[\s\S]*<\/a:fontScheme>/;
  9031. var fmtsregex = /<a:fmtScheme([^>]*)>[\s\S]*<\/a:fmtScheme>/;
  9032. /* 20.1.6.10 themeElements CT_BaseStyles */
  9033. function parse_themeElements(data, themes, opts) {
  9034. themes.themeElements = {};
  9035. var t;
  9036. [
  9037. /* clrScheme CT_ColorScheme */
  9038. ['clrScheme', clrsregex, parse_clrScheme],
  9039. /* fontScheme CT_FontScheme */
  9040. ['fontScheme', fntsregex, parse_fontScheme],
  9041. /* fmtScheme CT_StyleMatrix */
  9042. ['fmtScheme', fmtsregex, parse_fmtScheme]
  9043. ].forEach(function(m) {
  9044. if(!(t=data.match(m[1]))) throw new Error(m[0] + ' not found in themeElements');
  9045. m[2](t, themes, opts);
  9046. });
  9047. }
  9048. var themeltregex = /<a:themeElements([^>]*)>[\s\S]*<\/a:themeElements>/;
  9049. /* 14.2.7 Theme Part */
  9050. function parse_theme_xml(data, opts) {
  9051. /* 20.1.6.9 theme CT_OfficeStyleSheet */
  9052. if(!data || data.length === 0) return parse_theme_xml(write_theme());
  9053. var t;
  9054. var themes = {};
  9055. /* themeElements CT_BaseStyles */
  9056. if(!(t=data.match(themeltregex))) throw new Error('themeElements not found in theme');
  9057. parse_themeElements(t[0], themes, opts);
  9058. themes.raw = data;
  9059. return themes;
  9060. }
  9061. function write_theme(Themes, opts) {
  9062. if(opts && opts.themeXLSX) return opts.themeXLSX;
  9063. if(Themes && typeof Themes.raw == "string") return Themes.raw;
  9064. var o = [XML_HEADER];
  9065. o[o.length] = '<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme">';
  9066. o[o.length] = '<a:themeElements>';
  9067. o[o.length] = '<a:clrScheme name="Office">';
  9068. o[o.length] = '<a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1>';
  9069. o[o.length] = '<a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1>';
  9070. o[o.length] = '<a:dk2><a:srgbClr val="1F497D"/></a:dk2>';
  9071. o[o.length] = '<a:lt2><a:srgbClr val="EEECE1"/></a:lt2>';
  9072. o[o.length] = '<a:accent1><a:srgbClr val="4F81BD"/></a:accent1>';
  9073. o[o.length] = '<a:accent2><a:srgbClr val="C0504D"/></a:accent2>';
  9074. o[o.length] = '<a:accent3><a:srgbClr val="9BBB59"/></a:accent3>';
  9075. o[o.length] = '<a:accent4><a:srgbClr val="8064A2"/></a:accent4>';
  9076. o[o.length] = '<a:accent5><a:srgbClr val="4BACC6"/></a:accent5>';
  9077. o[o.length] = '<a:accent6><a:srgbClr val="F79646"/></a:accent6>';
  9078. o[o.length] = '<a:hlink><a:srgbClr val="0000FF"/></a:hlink>';
  9079. o[o.length] = '<a:folHlink><a:srgbClr val="800080"/></a:folHlink>';
  9080. o[o.length] = '</a:clrScheme>';
  9081. o[o.length] = '<a:fontScheme name="Office">';
  9082. o[o.length] = '<a:majorFont>';
  9083. o[o.length] = '<a:latin typeface="Cambria"/>';
  9084. o[o.length] = '<a:ea typeface=""/>';
  9085. o[o.length] = '<a:cs typeface=""/>';
  9086. o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
  9087. o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
  9088. o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
  9089. o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
  9090. o[o.length] = '<a:font script="Arab" typeface="Times New Roman"/>';
  9091. o[o.length] = '<a:font script="Hebr" typeface="Times New Roman"/>';
  9092. o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
  9093. o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
  9094. o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
  9095. o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
  9096. o[o.length] = '<a:font script="Khmr" typeface="MoolBoran"/>';
  9097. o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
  9098. o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
  9099. o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
  9100. o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
  9101. o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
  9102. o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
  9103. o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
  9104. o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
  9105. o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
  9106. o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
  9107. o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
  9108. o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
  9109. o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
  9110. o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
  9111. o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
  9112. o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
  9113. o[o.length] = '<a:font script="Viet" typeface="Times New Roman"/>';
  9114. o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
  9115. o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
  9116. o[o.length] = '</a:majorFont>';
  9117. o[o.length] = '<a:minorFont>';
  9118. o[o.length] = '<a:latin typeface="Calibri"/>';
  9119. o[o.length] = '<a:ea typeface=""/>';
  9120. o[o.length] = '<a:cs typeface=""/>';
  9121. o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
  9122. o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
  9123. o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
  9124. o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
  9125. o[o.length] = '<a:font script="Arab" typeface="Arial"/>';
  9126. o[o.length] = '<a:font script="Hebr" typeface="Arial"/>';
  9127. o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
  9128. o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
  9129. o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
  9130. o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
  9131. o[o.length] = '<a:font script="Khmr" typeface="DaunPenh"/>';
  9132. o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
  9133. o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
  9134. o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
  9135. o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
  9136. o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
  9137. o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
  9138. o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
  9139. o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
  9140. o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
  9141. o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
  9142. o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
  9143. o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
  9144. o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
  9145. o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
  9146. o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
  9147. o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
  9148. o[o.length] = '<a:font script="Viet" typeface="Arial"/>';
  9149. o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
  9150. o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
  9151. o[o.length] = '</a:minorFont>';
  9152. o[o.length] = '</a:fontScheme>';
  9153. o[o.length] = '<a:fmtScheme name="Office">';
  9154. o[o.length] = '<a:fillStyleLst>';
  9155. o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
  9156. o[o.length] = '<a:gradFill rotWithShape="1">';
  9157. o[o.length] = '<a:gsLst>';
  9158. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="50000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  9159. o[o.length] = '<a:gs pos="35000"><a:schemeClr val="phClr"><a:tint val="37000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  9160. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="15000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  9161. o[o.length] = '</a:gsLst>';
  9162. o[o.length] = '<a:lin ang="16200000" scaled="1"/>';
  9163. o[o.length] = '</a:gradFill>';
  9164. o[o.length] = '<a:gradFill rotWithShape="1">';
  9165. o[o.length] = '<a:gsLst>';
  9166. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="100000"/><a:shade val="100000"/><a:satMod val="130000"/></a:schemeClr></a:gs>';
  9167. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="50000"/><a:shade val="100000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  9168. o[o.length] = '</a:gsLst>';
  9169. o[o.length] = '<a:lin ang="16200000" scaled="0"/>';
  9170. o[o.length] = '</a:gradFill>';
  9171. o[o.length] = '</a:fillStyleLst>';
  9172. o[o.length] = '<a:lnStyleLst>';
  9173. o[o.length] = '<a:ln w="9525" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"><a:shade val="95000"/><a:satMod val="105000"/></a:schemeClr></a:solidFill><a:prstDash val="solid"/></a:ln>';
  9174. o[o.length] = '<a:ln w="25400" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln>';
  9175. o[o.length] = '<a:ln w="38100" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln>';
  9176. o[o.length] = '</a:lnStyleLst>';
  9177. o[o.length] = '<a:effectStyleLst>';
  9178. o[o.length] = '<a:effectStyle>';
  9179. o[o.length] = '<a:effectLst>';
  9180. o[o.length] = '<a:outerShdw blurRad="40000" dist="20000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="38000"/></a:srgbClr></a:outerShdw>';
  9181. o[o.length] = '</a:effectLst>';
  9182. o[o.length] = '</a:effectStyle>';
  9183. o[o.length] = '<a:effectStyle>';
  9184. o[o.length] = '<a:effectLst>';
  9185. o[o.length] = '<a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw>';
  9186. o[o.length] = '</a:effectLst>';
  9187. o[o.length] = '</a:effectStyle>';
  9188. o[o.length] = '<a:effectStyle>';
  9189. o[o.length] = '<a:effectLst>';
  9190. o[o.length] = '<a:outerShdw blurRad="40000" dist="23000" dir="5400000" rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw>';
  9191. o[o.length] = '</a:effectLst>';
  9192. o[o.length] = '<a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0" lon="0" rev="0"/></a:camera><a:lightRig rig="threePt" dir="t"><a:rot lat="0" lon="0" rev="1200000"/></a:lightRig></a:scene3d>';
  9193. o[o.length] = '<a:sp3d><a:bevelT w="63500" h="25400"/></a:sp3d>';
  9194. o[o.length] = '</a:effectStyle>';
  9195. o[o.length] = '</a:effectStyleLst>';
  9196. o[o.length] = '<a:bgFillStyleLst>';
  9197. o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
  9198. o[o.length] = '<a:gradFill rotWithShape="1">';
  9199. o[o.length] = '<a:gsLst>';
  9200. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="40000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  9201. o[o.length] = '<a:gs pos="40000"><a:schemeClr val="phClr"><a:tint val="45000"/><a:shade val="99000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
  9202. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="20000"/><a:satMod val="255000"/></a:schemeClr></a:gs>';
  9203. o[o.length] = '</a:gsLst>';
  9204. o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="-80000" r="50000" b="180000"/></a:path>';
  9205. o[o.length] = '</a:gradFill>';
  9206. o[o.length] = '<a:gradFill rotWithShape="1">';
  9207. o[o.length] = '<a:gsLst>';
  9208. o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="80000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
  9209. o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="30000"/><a:satMod val="200000"/></a:schemeClr></a:gs>';
  9210. o[o.length] = '</a:gsLst>';
  9211. o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="50000" r="50000" b="50000"/></a:path>';
  9212. o[o.length] = '</a:gradFill>';
  9213. o[o.length] = '</a:bgFillStyleLst>';
  9214. o[o.length] = '</a:fmtScheme>';
  9215. o[o.length] = '</a:themeElements>';
  9216. o[o.length] = '<a:objectDefaults>';
  9217. o[o.length] = '<a:spDef>';
  9218. o[o.length] = '<a:spPr/><a:bodyPr/><a:lstStyle/><a:style><a:lnRef idx="1"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="3"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="2"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="lt1"/></a:fontRef></a:style>';
  9219. o[o.length] = '</a:spDef>';
  9220. o[o.length] = '<a:lnDef>';
  9221. o[o.length] = '<a:spPr/><a:bodyPr/><a:lstStyle/><a:style><a:lnRef idx="2"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="0"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="1"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef></a:style>';
  9222. o[o.length] = '</a:lnDef>';
  9223. o[o.length] = '</a:objectDefaults>';
  9224. o[o.length] = '<a:extraClrSchemeLst/>';
  9225. o[o.length] = '</a:theme>';
  9226. return o.join("");
  9227. }
  9228. /* [MS-XLS] 2.4.326 TODO: payload is a zip file */
  9229. function parse_Theme(blob, length, opts) {
  9230. var end = blob.l + length;
  9231. var dwThemeVersion = blob.read_shift(4);
  9232. if(dwThemeVersion === 124226) return;
  9233. if(!opts.cellStyles || !jszip) { blob.l = end; return; }
  9234. var data = blob.slice(blob.l);
  9235. blob.l = end;
  9236. var zip; try { zip = new jszip(data); } catch(e) { return; }
  9237. var themeXML = getzipstr(zip, "theme/theme/theme1.xml", true);
  9238. if(!themeXML) return;
  9239. return parse_theme_xml(themeXML, opts);
  9240. }
  9241. /* 2.5.49 */
  9242. function parse_ColorTheme(blob) { return blob.read_shift(4); }
  9243. /* 2.5.155 */
  9244. function parse_FullColorExt(blob) {
  9245. var o = {};
  9246. o.xclrType = blob.read_shift(2);
  9247. o.nTintShade = blob.read_shift(2);
  9248. switch(o.xclrType) {
  9249. case 0: blob.l += 4; break;
  9250. case 1: o.xclrValue = parse_IcvXF(blob, 4); break;
  9251. case 2: o.xclrValue = parse_LongRGBA(blob, 4); break;
  9252. case 3: o.xclrValue = parse_ColorTheme(blob, 4); break;
  9253. case 4: blob.l += 4; break;
  9254. }
  9255. blob.l += 8;
  9256. return o;
  9257. }
  9258. /* 2.5.164 TODO: read 7 bits*/
  9259. function parse_IcvXF(blob, length) {
  9260. return parsenoop(blob, length);
  9261. }
  9262. /* 2.5.280 */
  9263. function parse_XFExtGradient(blob, length) {
  9264. return parsenoop(blob, length);
  9265. }
  9266. /* [MS-XLS] 2.5.108 */
  9267. function parse_ExtProp(blob) {
  9268. var extType = blob.read_shift(2);
  9269. var cb = blob.read_shift(2) - 4;
  9270. var o = [extType];
  9271. switch(extType) {
  9272. case 0x04: case 0x05: case 0x07: case 0x08:
  9273. case 0x09: case 0x0A: case 0x0B: case 0x0D:
  9274. o[1] = parse_FullColorExt(blob, cb); break;
  9275. case 0x06: o[1] = parse_XFExtGradient(blob, cb); break;
  9276. case 0x0E: case 0x0F: o[1] = blob.read_shift(cb === 1 ? 1 : 2); break;
  9277. default: throw new Error("Unrecognized ExtProp type: " + extType + " " + cb);
  9278. }
  9279. return o;
  9280. }
  9281. /* 2.4.355 */
  9282. function parse_XFExt(blob, length) {
  9283. var end = blob.l + length;
  9284. blob.l += 2;
  9285. var ixfe = blob.read_shift(2);
  9286. blob.l += 2;
  9287. var cexts = blob.read_shift(2);
  9288. var ext = [];
  9289. while(cexts-- > 0) ext.push(parse_ExtProp(blob, end-blob.l));
  9290. return {ixfe:ixfe, ext:ext};
  9291. }
  9292. /* xf is an XF, see parse_XFExt for xfext */
  9293. function update_xfext(xf, xfext) {
  9294. xfext.forEach(function(xfe) {
  9295. switch(xfe[0]) { /* 2.5.108 extPropData */
  9296. case 0x04: break; /* foreground color */
  9297. case 0x05: break; /* background color */
  9298. case 0x06: break; /* gradient fill */
  9299. case 0x07: break; /* top cell border color */
  9300. case 0x08: break; /* bottom cell border color */
  9301. case 0x09: break; /* left cell border color */
  9302. case 0x0a: break; /* right cell border color */
  9303. case 0x0b: break; /* diagonal cell border color */
  9304. case 0x0d: /* text color */
  9305. break;
  9306. case 0x0e: break; /* font scheme */
  9307. case 0x0f: break; /* indentation level */
  9308. }
  9309. });
  9310. }
  9311. /* 18.6 Calculation Chain */
  9312. function parse_cc_xml(data) {
  9313. var d = [];
  9314. if(!data) return d;
  9315. var i = 1;
  9316. (data.match(tagregex)||[]).forEach(function(x) {
  9317. var y = parsexmltag(x);
  9318. switch(y[0]) {
  9319. case '<?xml': break;
  9320. /* 18.6.2 calcChain CT_CalcChain 1 */
  9321. case '<calcChain': case '<calcChain>': case '</calcChain>': break;
  9322. /* 18.6.1 c CT_CalcCell 1 */
  9323. case '<c': delete y[0]; if(y.i) i = y.i; else y.i = i; d.push(y); break;
  9324. }
  9325. });
  9326. return d;
  9327. }
  9328. //function write_cc_xml(data, opts) { }
  9329. /* [MS-XLSB] 2.6.4.1 */
  9330. function parse_BrtCalcChainItem$(data) {
  9331. var out = {};
  9332. out.i = data.read_shift(4);
  9333. var cell = {};
  9334. cell.r = data.read_shift(4);
  9335. cell.c = data.read_shift(4);
  9336. out.r = encode_cell(cell);
  9337. var flags = data.read_shift(1);
  9338. if(flags & 0x2) out.l = '1';
  9339. if(flags & 0x8) out.a = '1';
  9340. return out;
  9341. }
  9342. /* 18.6 Calculation Chain */
  9343. function parse_cc_bin(data, name, opts) {
  9344. var out = [];
  9345. var pass = false;
  9346. recordhopper(data, function hopper_cc(val, R_n, RT) {
  9347. switch(RT) {
  9348. case 0x003F: /* 'BrtCalcChainItem$' */
  9349. out.push(val); break;
  9350. default:
  9351. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  9352. else if((R_n||"").indexOf("End") > 0){/* empty */}
  9353. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  9354. }
  9355. });
  9356. return out;
  9357. }
  9358. //function write_cc_bin(data, opts) { }
  9359. /* 18.14 Supplementary Workbook Data */
  9360. function parse_xlink_xml() {
  9361. //var opts = _opts || {};
  9362. //if(opts.WTF) throw "XLSX External Link";
  9363. }
  9364. /* [MS-XLSB] 2.1.7.25 External Link */
  9365. function parse_xlink_bin(data, rel, name, _opts) {
  9366. if(!data) return data;
  9367. var opts = _opts || {};
  9368. var pass = false, end = false;
  9369. recordhopper(data, function xlink_parse(val, R_n, RT) {
  9370. if(end) return;
  9371. switch(RT) {
  9372. case 0x0167: /* 'BrtSupTabs' */
  9373. case 0x016B: /* 'BrtExternTableStart' */
  9374. case 0x016C: /* 'BrtExternTableEnd' */
  9375. case 0x016E: /* 'BrtExternRowHdr' */
  9376. case 0x016F: /* 'BrtExternCellBlank' */
  9377. case 0x0170: /* 'BrtExternCellReal' */
  9378. case 0x0171: /* 'BrtExternCellBool' */
  9379. case 0x0172: /* 'BrtExternCellError' */
  9380. case 0x0173: /* 'BrtExternCellString' */
  9381. case 0x01D8: /* 'BrtExternValueMeta' */
  9382. case 0x0241: /* 'BrtSupNameStart' */
  9383. case 0x0242: /* 'BrtSupNameValueStart' */
  9384. case 0x0243: /* 'BrtSupNameValueEnd' */
  9385. case 0x0244: /* 'BrtSupNameNum' */
  9386. case 0x0245: /* 'BrtSupNameErr' */
  9387. case 0x0246: /* 'BrtSupNameSt' */
  9388. case 0x0247: /* 'BrtSupNameNil' */
  9389. case 0x0248: /* 'BrtSupNameBool' */
  9390. case 0x0249: /* 'BrtSupNameFmla' */
  9391. case 0x024A: /* 'BrtSupNameBits' */
  9392. case 0x024B: /* 'BrtSupNameEnd' */
  9393. break;
  9394. case 0x0023: /* 'BrtFRTBegin' */
  9395. pass = true; break;
  9396. case 0x0024: /* 'BrtFRTEnd' */
  9397. pass = false; break;
  9398. default:
  9399. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  9400. else if((R_n||"").indexOf("End") > 0){/* empty */}
  9401. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT.toString(16) + " " + R_n);
  9402. }
  9403. }, opts);
  9404. }
  9405. /* 20.5 DrawingML - SpreadsheetML Drawing */
  9406. RELS.IMG = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
  9407. RELS.DRAW = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
  9408. /* 20.5.2.35 wsDr CT_Drawing */
  9409. function parse_drawing(data, rels) {
  9410. if(!data) return "??";
  9411. /*
  9412. Chartsheet Drawing:
  9413. - 20.5.2.35 wsDr CT_Drawing
  9414. - 20.5.2.1 absoluteAnchor CT_AbsoluteAnchor
  9415. - 20.5.2.16 graphicFrame CT_GraphicalObjectFrame
  9416. - 20.1.2.2.16 graphic CT_GraphicalObject
  9417. - 20.1.2.2.17 graphicData CT_GraphicalObjectData
  9418. - chart reference
  9419. the actual type is based on the URI of the graphicData
  9420. TODO: handle embedded charts and other types of graphics
  9421. */
  9422. var id = (data.match(/<c:chart [^>]*r:id="([^"]*)"/)||["",""])[1];
  9423. return rels['!id'][id].Target;
  9424. }
  9425. /* L.5.5.2 SpreadsheetML Comments + VML Schema */
  9426. var _shapeid = 1024;
  9427. function write_comments_vml(rId, comments) {
  9428. var csize = [21600, 21600];
  9429. /* L.5.2.1.2 Path Attribute */
  9430. var bbox = ["m0,0l0",csize[1],csize[0],csize[1],csize[0],"0xe"].join(",");
  9431. var o = [
  9432. writextag("xml", null, { 'xmlns:v': XLMLNS.v, 'xmlns:o': XLMLNS.o, 'xmlns:x': XLMLNS.x, 'xmlns:mv': XLMLNS.mv }).replace(/\/>/,">"),
  9433. writextag("o:shapelayout", writextag("o:idmap", null, {'v:ext':"edit", 'data':rId}), {'v:ext':"edit"}),
  9434. writextag("v:shapetype", [
  9435. writextag("v:stroke", null, {joinstyle:"miter"}),
  9436. writextag("v:path", null, {gradientshapeok:"t", 'o:connecttype':"rect"})
  9437. ].join(""), {id:"_x0000_t202", 'o:spt':202, coordsize:csize.join(","),path:bbox})
  9438. ];
  9439. while(_shapeid < rId * 1000) _shapeid += 1000;
  9440. comments.forEach(function(x) {
  9441. var c = decode_cell(x[0]);
  9442. var fillopts = {'color2':"#BEFF82", 'type':"gradient"};
  9443. if(fillopts.type == "gradient") fillopts.angle = "-180";
  9444. var fillparm = fillopts.type == "gradient" ? writextag("o:fill", null, {type:"gradientUnscaled", 'v:ext':"view"}) : null;
  9445. var fillxml = writextag('v:fill', fillparm, fillopts);
  9446. var shadata = ({on:"t", 'obscured':"t"});
  9447. ++_shapeid;
  9448. o = o.concat([
  9449. '<v:shape' + wxt_helper({
  9450. id:'_x0000_s' + _shapeid,
  9451. type:"#_x0000_t202",
  9452. style:"position:absolute; margin-left:80pt;margin-top:5pt;width:104pt;height:64pt;z-index:10" + (x[1].hidden ? ";visibility:hidden" : "") ,
  9453. fillcolor:"#ECFAD4",
  9454. strokecolor:"#edeaa1"
  9455. }) + '>',
  9456. fillxml,
  9457. writextag("v:shadow", null, shadata),
  9458. writextag("v:path", null, {'o:connecttype':"none"}),
  9459. '<v:textbox><div style="text-align:left"></div></v:textbox>',
  9460. '<x:ClientData ObjectType="Note">',
  9461. '<x:MoveWithCells/>',
  9462. '<x:SizeWithCells/>',
  9463. /* Part 4 19.4.2.3 Anchor (Anchor) */
  9464. writetag('x:Anchor', [c.c+1, 0, c.r+1, 0, c.c+3, 20, c.r+5, 20].join(",")),
  9465. writetag('x:AutoFill', "False"),
  9466. writetag('x:Row', String(c.r)),
  9467. writetag('x:Column', String(c.c)),
  9468. x[1].hidden ? '' : '<x:Visible/>',
  9469. '</x:ClientData>',
  9470. '</v:shape>'
  9471. ]); });
  9472. o.push('</xml>');
  9473. return o.join("");
  9474. }
  9475. RELS.CMNT = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
  9476. function sheet_insert_comments(sheet, comments) {
  9477. var dense = Array.isArray(sheet);
  9478. var cell;
  9479. comments.forEach(function(comment) {
  9480. var r = decode_cell(comment.ref);
  9481. if(dense) {
  9482. if(!sheet[r.r]) sheet[r.r] = [];
  9483. cell = sheet[r.r][r.c];
  9484. } else cell = sheet[comment.ref];
  9485. if (!cell) {
  9486. cell = ({t:"z"});
  9487. if(dense) sheet[r.r][r.c] = cell;
  9488. else sheet[comment.ref] = cell;
  9489. var range = safe_decode_range(sheet["!ref"]||"BDWGO1000001:A1");
  9490. if(range.s.r > r.r) range.s.r = r.r;
  9491. if(range.e.r < r.r) range.e.r = r.r;
  9492. if(range.s.c > r.c) range.s.c = r.c;
  9493. if(range.e.c < r.c) range.e.c = r.c;
  9494. var encoded = encode_range(range);
  9495. if (encoded !== sheet["!ref"]) sheet["!ref"] = encoded;
  9496. }
  9497. if (!cell.c) cell.c = [];
  9498. var o = ({a: comment.author, t: comment.t, r: comment.r});
  9499. if(comment.h) o.h = comment.h;
  9500. cell.c.push(o);
  9501. });
  9502. }
  9503. /* 18.7 Comments */
  9504. function parse_comments_xml(data, opts) {
  9505. /* 18.7.6 CT_Comments */
  9506. if(data.match(/<(?:\w+:)?comments *\/>/)) return [];
  9507. var authors = [];
  9508. var commentList = [];
  9509. var authtag = data.match(/<(?:\w+:)?authors>([\s\S]*)<\/(?:\w+:)?authors>/);
  9510. if(authtag && authtag[1]) authtag[1].split(/<\/\w*:?author>/).forEach(function(x) {
  9511. if(x === "" || x.trim() === "") return;
  9512. var a = x.match(/<(?:\w+:)?author[^>]*>(.*)/);
  9513. if(a) authors.push(a[1]);
  9514. });
  9515. var cmnttag = data.match(/<(?:\w+:)?commentList>([\s\S]*)<\/(?:\w+:)?commentList>/);
  9516. if(cmnttag && cmnttag[1]) cmnttag[1].split(/<\/\w*:?comment>/).forEach(function(x) {
  9517. if(x === "" || x.trim() === "") return;
  9518. var cm = x.match(/<(?:\w+:)?comment[^>]*>/);
  9519. if(!cm) return;
  9520. var y = parsexmltag(cm[0]);
  9521. var comment = ({ author: y.authorId && authors[y.authorId] || "sheetjsghost", ref: y.ref, guid: y.guid });
  9522. var cell = decode_cell(y.ref);
  9523. if(opts.sheetRows && opts.sheetRows <= cell.r) return;
  9524. var textMatch = x.match(/<(?:\w+:)?text>([\s\S]*)<\/(?:\w+:)?text>/);
  9525. var rt = !!textMatch && !!textMatch[1] && parse_si(textMatch[1]) || {r:"",t:"",h:""};
  9526. comment.r = rt.r;
  9527. if(rt.r == "<t></t>") rt.t = rt.h = "";
  9528. comment.t = rt.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
  9529. if(opts.cellHTML) comment.h = rt.h;
  9530. commentList.push(comment);
  9531. });
  9532. return commentList;
  9533. }
  9534. var CMNT_XML_ROOT = writextag('comments', null, { 'xmlns': XMLNS.main[0] });
  9535. function write_comments_xml(data) {
  9536. var o = [XML_HEADER, CMNT_XML_ROOT];
  9537. var iauthor = [];
  9538. o.push("<authors>");
  9539. data.forEach(function(x) { x[1].forEach(function(w) { var a = escapexml(w.a);
  9540. if(iauthor.indexOf(a) > -1) return;
  9541. iauthor.push(a);
  9542. o.push("<author>" + a + "</author>");
  9543. }); });
  9544. o.push("</authors>");
  9545. o.push("<commentList>");
  9546. data.forEach(function(d) {
  9547. d[1].forEach(function(c) {
  9548. /* 18.7.3 CT_Comment */
  9549. o.push('<comment ref="' + d[0] + '" authorId="' + iauthor.indexOf(escapexml(c.a)) + '"><text>');
  9550. o.push(writetag("t", c.t == null ? "" : escapexml(c.t)));
  9551. o.push('</text></comment>');
  9552. });
  9553. });
  9554. o.push("</commentList>");
  9555. if(o.length>2) { o[o.length] = ('</comments>'); o[1]=o[1].replace("/>",">"); }
  9556. return o.join("");
  9557. }
  9558. /* [MS-XLSB] 2.4.28 BrtBeginComment */
  9559. function parse_BrtBeginComment(data) {
  9560. var out = {};
  9561. out.iauthor = data.read_shift(4);
  9562. var rfx = parse_UncheckedRfX(data, 16);
  9563. out.rfx = rfx.s;
  9564. out.ref = encode_cell(rfx.s);
  9565. data.l += 16; /*var guid = parse_GUID(data); */
  9566. return out;
  9567. }
  9568. function write_BrtBeginComment(data, o) {
  9569. if(o == null) o = new_buf(36);
  9570. o.write_shift(4, data[1].iauthor);
  9571. write_UncheckedRfX((data[0]), o);
  9572. o.write_shift(4, 0);
  9573. o.write_shift(4, 0);
  9574. o.write_shift(4, 0);
  9575. o.write_shift(4, 0);
  9576. return o;
  9577. }
  9578. /* [MS-XLSB] 2.4.327 BrtCommentAuthor */
  9579. var parse_BrtCommentAuthor = parse_XLWideString;
  9580. function write_BrtCommentAuthor(data) { return write_XLWideString(data.slice(0, 54)); }
  9581. /* [MS-XLSB] 2.1.7.8 Comments */
  9582. function parse_comments_bin(data, opts) {
  9583. var out = [];
  9584. var authors = [];
  9585. var c = {};
  9586. var pass = false;
  9587. recordhopper(data, function hopper_cmnt(val, R_n, RT) {
  9588. switch(RT) {
  9589. case 0x0278: /* 'BrtCommentAuthor' */
  9590. authors.push(val); break;
  9591. case 0x027B: /* 'BrtBeginComment' */
  9592. c = val; break;
  9593. case 0x027D: /* 'BrtCommentText' */
  9594. c.t = val.t; c.h = val.h; c.r = val.r; break;
  9595. case 0x027C: /* 'BrtEndComment' */
  9596. c.author = authors[c.iauthor];
  9597. delete (c).iauthor;
  9598. if(opts.sheetRows && c.rfx && opts.sheetRows <= c.rfx.r) break;
  9599. if(!c.t) c.t = "";
  9600. delete c.rfx; out.push(c); break;
  9601. case 0x0C00: /* 'BrtUid' */
  9602. break;
  9603. case 0x0023: /* 'BrtFRTBegin' */
  9604. pass = true; break;
  9605. case 0x0024: /* 'BrtFRTEnd' */
  9606. pass = false; break;
  9607. case 0x0025: /* 'BrtACBegin' */ break;
  9608. case 0x0026: /* 'BrtACEnd' */ break;
  9609. default:
  9610. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  9611. else if((R_n||"").indexOf("End") > 0){/* empty */}
  9612. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  9613. }
  9614. });
  9615. return out;
  9616. }
  9617. function write_comments_bin(data) {
  9618. var ba = buf_array();
  9619. var iauthor = [];
  9620. write_record(ba, "BrtBeginComments");
  9621. write_record(ba, "BrtBeginCommentAuthors");
  9622. data.forEach(function(comment) {
  9623. comment[1].forEach(function(c) {
  9624. if(iauthor.indexOf(c.a) > -1) return;
  9625. iauthor.push(c.a.slice(0,54));
  9626. write_record(ba, "BrtCommentAuthor", write_BrtCommentAuthor(c.a));
  9627. });
  9628. });
  9629. write_record(ba, "BrtEndCommentAuthors");
  9630. write_record(ba, "BrtBeginCommentList");
  9631. data.forEach(function(comment) {
  9632. comment[1].forEach(function(c) {
  9633. c.iauthor = iauthor.indexOf(c.a);
  9634. var range = {s:decode_cell(comment[0]),e:decode_cell(comment[0])};
  9635. write_record(ba, "BrtBeginComment", write_BrtBeginComment([range, c]));
  9636. if(c.t && c.t.length > 0) write_record(ba, "BrtCommentText", write_BrtCommentText(c));
  9637. write_record(ba, "BrtEndComment");
  9638. delete c.iauthor;
  9639. });
  9640. });
  9641. write_record(ba, "BrtEndCommentList");
  9642. write_record(ba, "BrtEndComments");
  9643. return ba.end();
  9644. }
  9645. var CT_VBA = "application/vnd.ms-office.vbaProject";
  9646. function make_vba_xls(cfb) {
  9647. var newcfb = CFB.utils.cfb_new({root:"R"});
  9648. cfb.FullPaths.forEach(function(p, i) {
  9649. if(p.slice(-1) === "/" || !p.match(/_VBA_PROJECT_CUR/)) return;
  9650. var newpath = p.replace(/^[^\/]*/,"R").replace(/\/_VBA_PROJECT_CUR\u0000*/, "");
  9651. CFB.utils.cfb_add(newcfb, newpath, cfb.FileIndex[i].content);
  9652. });
  9653. return CFB.write(newcfb);
  9654. }
  9655. function fill_vba_xls(cfb, vba) {
  9656. vba.FullPaths.forEach(function(p, i) {
  9657. if(i == 0) return;
  9658. var newpath = p.replace(/[^\/]*[\/]/, "/_VBA_PROJECT_CUR/");
  9659. if(newpath.slice(-1) !== "/") CFB.utils.cfb_add(cfb, newpath, vba.FileIndex[i].content);
  9660. });
  9661. }
  9662. var VBAFMTS = [ "xlsb", "xlsm", "xlam", "biff8", "xla" ];
  9663. RELS.DS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet";
  9664. RELS.MS = "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet";
  9665. /* macro and dialog sheet stubs */
  9666. function parse_ds_bin() { return {'!type':'dialog'}; }
  9667. function parse_ds_xml() { return {'!type':'dialog'}; }
  9668. function parse_ms_bin() { return {'!type':'macro'}; }
  9669. function parse_ms_xml() { return {'!type':'macro'}; }
  9670. /* TODO: it will be useful to parse the function str */
  9671. var rc_to_a1 = (function(){
  9672. var rcregex = /(^|[^A-Za-z_])R(\[?-?\d+\]|[1-9]\d*|)C(\[?-?\d+\]|[1-9]\d*|)(?![A-Za-z0-9_])/g;
  9673. var rcbase = ({r:0,c:0});
  9674. function rcfunc($$,$1,$2,$3) {
  9675. var cRel = false, rRel = false;
  9676. if($2.length == 0) rRel = true;
  9677. else if($2.charAt(0) == "[") { rRel = true; $2 = $2.slice(1, -1); }
  9678. if($3.length == 0) cRel = true;
  9679. else if($3.charAt(0) == "[") { cRel = true; $3 = $3.slice(1, -1); }
  9680. var R = $2.length>0?parseInt($2,10)|0:0, C = $3.length>0?parseInt($3,10)|0:0;
  9681. if(cRel) C += rcbase.c; else --C;
  9682. if(rRel) R += rcbase.r; else --R;
  9683. return $1 + (cRel ? "" : "$") + encode_col(C) + (rRel ? "" : "$") + encode_row(R);
  9684. }
  9685. return function rc_to_a1(fstr, base) {
  9686. rcbase = base;
  9687. return fstr.replace(rcregex, rcfunc);
  9688. };
  9689. })();
  9690. var crefregex = /(^|[^._A-Z0-9])([$]?)([A-Z]{1,2}|[A-W][A-Z]{2}|X[A-E][A-Z]|XF[A-D])([$]?)(10[0-3]\d{4}|104[0-7]\d{3}|1048[0-4]\d{2}|10485[0-6]\d|104857[0-6]|[1-9]\d{0,5})(?![_.\(A-Za-z0-9])/g;
  9691. var a1_to_rc =(function(){
  9692. return function a1_to_rc(fstr, base) {
  9693. return fstr.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
  9694. var c = decode_col($3) - ($2 ? 0 : base.c);
  9695. var r = decode_row($5) - ($4 ? 0 : base.r);
  9696. var R = (r == 0 ? "" : !$4 ? "[" + r + "]" : (r+1));
  9697. var C = (c == 0 ? "" : !$2 ? "[" + c + "]" : (c+1));
  9698. return $1 + "R" + R + "C" + C;
  9699. });
  9700. };
  9701. })();
  9702. /* no defined name can collide with a valid cell address A1:XFD1048576 ... except LOG10! */
  9703. function shift_formula_str(f, delta) {
  9704. return f.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
  9705. return $1+($2=="$" ? $2+$3 : encode_col(decode_col($3)+delta.c))+($4=="$" ? $4+$5 : encode_row(decode_row($5) + delta.r));
  9706. });
  9707. }
  9708. function shift_formula_xlsx(f, range, cell) {
  9709. var r = decode_range(range), s = r.s, c = decode_cell(cell);
  9710. var delta = {r:c.r - s.r, c:c.c - s.c};
  9711. return shift_formula_str(f, delta);
  9712. }
  9713. /* TODO: parse formula */
  9714. function fuzzyfmla(f) {
  9715. if(f.length == 1) return false;
  9716. return true;
  9717. }
  9718. function _xlfn(f) {
  9719. return f.replace(/_xlfn\./g,"");
  9720. }
  9721. function parseread1(blob) { blob.l+=1; return; }
  9722. /* [MS-XLS] 2.5.51 */
  9723. function parse_ColRelU(blob, length) {
  9724. var c = blob.read_shift(length == 1 ? 1 : 2);
  9725. return [c & 0x3FFF, (c >> 14) & 1, (c >> 15) & 1];
  9726. }
  9727. /* [MS-XLS] 2.5.198.105 ; [MS-XLSB] 2.5.97.89 */
  9728. function parse_RgceArea(blob, length, opts) {
  9729. var w = 2;
  9730. if(opts) {
  9731. if(opts.biff >= 2 && opts.biff <= 5) return parse_RgceArea_BIFF2(blob, length, opts);
  9732. else if(opts.biff == 12) w = 4;
  9733. }
  9734. var r=blob.read_shift(w), R=blob.read_shift(w);
  9735. var c=parse_ColRelU(blob, 2);
  9736. var C=parse_ColRelU(blob, 2);
  9737. return { s:{r:r, c:c[0], cRel:c[1], rRel:c[2]}, e:{r:R, c:C[0], cRel:C[1], rRel:C[2]} };
  9738. }
  9739. /* BIFF 2-5 encodes flags in the row field */
  9740. function parse_RgceArea_BIFF2(blob) {
  9741. var r=parse_ColRelU(blob, 2), R=parse_ColRelU(blob, 2);
  9742. var c=blob.read_shift(1);
  9743. var C=blob.read_shift(1);
  9744. return { s:{r:r[0], c:c, cRel:r[1], rRel:r[2]}, e:{r:R[0], c:C, cRel:R[1], rRel:R[2]} };
  9745. }
  9746. /* [MS-XLS] 2.5.198.105 ; [MS-XLSB] 2.5.97.90 */
  9747. function parse_RgceAreaRel(blob, length, opts) {
  9748. if(opts.biff < 8) return parse_RgceArea_BIFF2(blob, length, opts);
  9749. var r=blob.read_shift(opts.biff == 12 ? 4 : 2), R=blob.read_shift(opts.biff == 12 ? 4 : 2);
  9750. var c=parse_ColRelU(blob, 2);
  9751. var C=parse_ColRelU(blob, 2);
  9752. return { s:{r:r, c:c[0], cRel:c[1], rRel:c[2]}, e:{r:R, c:C[0], cRel:C[1], rRel:C[2]} };
  9753. }
  9754. /* [MS-XLS] 2.5.198.109 ; [MS-XLSB] 2.5.97.91 */
  9755. function parse_RgceLoc(blob, length, opts) {
  9756. if(opts && opts.biff >= 2 && opts.biff <= 5) return parse_RgceLoc_BIFF2(blob, length, opts);
  9757. var r = blob.read_shift(opts && opts.biff == 12 ? 4 : 2);
  9758. var c = parse_ColRelU(blob, 2);
  9759. return {r:r, c:c[0], cRel:c[1], rRel:c[2]};
  9760. }
  9761. function parse_RgceLoc_BIFF2(blob) {
  9762. var r = parse_ColRelU(blob, 2);
  9763. var c = blob.read_shift(1);
  9764. return {r:r[0], c:c, cRel:r[1], rRel:r[2]};
  9765. }
  9766. /* [MS-XLS] 2.5.198.107, 2.5.47 */
  9767. function parse_RgceElfLoc(blob) {
  9768. var r = blob.read_shift(2);
  9769. var c = blob.read_shift(2);
  9770. return {r:r, c:c & 0xFF, fQuoted:!!(c & 0x4000), cRel:c>>15, rRel:c>>15 };
  9771. }
  9772. /* [MS-XLS] 2.5.198.111 ; [MS-XLSB] 2.5.97.92 TODO */
  9773. function parse_RgceLocRel(blob, length, opts) {
  9774. var biff = opts && opts.biff ? opts.biff : 8;
  9775. if(biff >= 2 && biff <= 5) return parse_RgceLocRel_BIFF2(blob, length, opts);
  9776. var r = blob.read_shift(biff >= 12 ? 4 : 2);
  9777. var cl = blob.read_shift(2);
  9778. var cRel = (cl & 0x4000) >> 14, rRel = (cl & 0x8000) >> 15;
  9779. cl &= 0x3FFF;
  9780. if(rRel == 1) while(r > 0x7FFFF) r -= 0x100000;
  9781. if(cRel == 1) while(cl > 0x1FFF) cl = cl - 0x4000;
  9782. return {r:r,c:cl,cRel:cRel,rRel:rRel};
  9783. }
  9784. function parse_RgceLocRel_BIFF2(blob) {
  9785. var rl = blob.read_shift(2);
  9786. var c = blob.read_shift(1);
  9787. var rRel = (rl & 0x8000) >> 15, cRel = (rl & 0x4000) >> 14;
  9788. rl &= 0x3FFF;
  9789. if(rRel == 1 && rl >= 0x2000) rl = rl - 0x4000;
  9790. if(cRel == 1 && c >= 0x80) c = c - 0x100;
  9791. return {r:rl,c:c,cRel:cRel,rRel:rRel};
  9792. }
  9793. /* [MS-XLS] 2.5.198.27 ; [MS-XLSB] 2.5.97.18 */
  9794. function parse_PtgArea(blob, length, opts) {
  9795. var type = (blob[blob.l++] & 0x60) >> 5;
  9796. var area = parse_RgceArea(blob, opts.biff >= 2 && opts.biff <= 5 ? 6 : 8, opts);
  9797. return [type, area];
  9798. }
  9799. /* [MS-XLS] 2.5.198.28 ; [MS-XLSB] 2.5.97.19 */
  9800. function parse_PtgArea3d(blob, length, opts) {
  9801. var type = (blob[blob.l++] & 0x60) >> 5;
  9802. var ixti = blob.read_shift(2, 'i');
  9803. var w = 8;
  9804. if(opts) switch(opts.biff) {
  9805. case 5: blob.l += 12; w = 6; break;
  9806. case 12: w = 12; break;
  9807. }
  9808. var area = parse_RgceArea(blob, w, opts);
  9809. return [type, ixti, area];
  9810. }
  9811. /* [MS-XLS] 2.5.198.29 ; [MS-XLSB] 2.5.97.20 */
  9812. function parse_PtgAreaErr(blob, length, opts) {
  9813. var type = (blob[blob.l++] & 0x60) >> 5;
  9814. blob.l += opts && (opts.biff > 8) ? 12 : (opts.biff < 8 ? 6 : 8);
  9815. return [type];
  9816. }
  9817. /* [MS-XLS] 2.5.198.30 ; [MS-XLSB] 2.5.97.21 */
  9818. function parse_PtgAreaErr3d(blob, length, opts) {
  9819. var type = (blob[blob.l++] & 0x60) >> 5;
  9820. var ixti = blob.read_shift(2);
  9821. var w = 8;
  9822. if(opts) switch(opts.biff) {
  9823. case 5: blob.l += 12; w = 6; break;
  9824. case 12: w = 12; break;
  9825. }
  9826. blob.l += w;
  9827. return [type, ixti];
  9828. }
  9829. /* [MS-XLS] 2.5.198.31 ; [MS-XLSB] 2.5.97.22 */
  9830. function parse_PtgAreaN(blob, length, opts) {
  9831. var type = (blob[blob.l++] & 0x60) >> 5;
  9832. var area = parse_RgceAreaRel(blob, length - 1, opts);
  9833. return [type, area];
  9834. }
  9835. /* [MS-XLS] 2.5.198.32 ; [MS-XLSB] 2.5.97.23 */
  9836. function parse_PtgArray(blob, length, opts) {
  9837. var type = (blob[blob.l++] & 0x60) >> 5;
  9838. blob.l += opts.biff == 2 ? 6 : opts.biff == 12 ? 14 : 7;
  9839. return [type];
  9840. }
  9841. /* [MS-XLS] 2.5.198.33 ; [MS-XLSB] 2.5.97.24 */
  9842. function parse_PtgAttrBaxcel(blob) {
  9843. var bitSemi = blob[blob.l+1] & 0x01; /* 1 = volatile */
  9844. var bitBaxcel = 1;
  9845. blob.l += 4;
  9846. return [bitSemi, bitBaxcel];
  9847. }
  9848. /* [MS-XLS] 2.5.198.34 ; [MS-XLSB] 2.5.97.25 */
  9849. function parse_PtgAttrChoose(blob, length, opts) {
  9850. blob.l +=2;
  9851. var offset = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  9852. var o = [];
  9853. /* offset is 1 less than the number of elements */
  9854. for(var i = 0; i <= offset; ++i) o.push(blob.read_shift(opts && opts.biff == 2 ? 1 : 2));
  9855. return o;
  9856. }
  9857. /* [MS-XLS] 2.5.198.35 ; [MS-XLSB] 2.5.97.26 */
  9858. function parse_PtgAttrGoto(blob, length, opts) {
  9859. var bitGoto = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  9860. blob.l += 2;
  9861. return [bitGoto, blob.read_shift(opts && opts.biff == 2 ? 1 : 2)];
  9862. }
  9863. /* [MS-XLS] 2.5.198.36 ; [MS-XLSB] 2.5.97.27 */
  9864. function parse_PtgAttrIf(blob, length, opts) {
  9865. var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  9866. blob.l += 2;
  9867. return [bitIf, blob.read_shift(opts && opts.biff == 2 ? 1 : 2)];
  9868. }
  9869. /* [MS-XLSB] 2.5.97.28 */
  9870. function parse_PtgAttrIfError(blob) {
  9871. var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  9872. blob.l += 2;
  9873. return [bitIf, blob.read_shift(2)];
  9874. }
  9875. /* [MS-XLS] 2.5.198.37 ; [MS-XLSB] 2.5.97.29 */
  9876. function parse_PtgAttrSemi(blob, length, opts) {
  9877. var bitSemi = (blob[blob.l+1] & 0xFF) ? 1 : 0;
  9878. blob.l += opts && opts.biff == 2 ? 3 : 4;
  9879. return [bitSemi];
  9880. }
  9881. /* [MS-XLS] 2.5.198.40 ; [MS-XLSB] 2.5.97.32 */
  9882. function parse_PtgAttrSpaceType(blob) {
  9883. var type = blob.read_shift(1), cch = blob.read_shift(1);
  9884. return [type, cch];
  9885. }
  9886. /* [MS-XLS] 2.5.198.38 ; [MS-XLSB] 2.5.97.30 */
  9887. function parse_PtgAttrSpace(blob) {
  9888. blob.read_shift(2);
  9889. return parse_PtgAttrSpaceType(blob, 2);
  9890. }
  9891. /* [MS-XLS] 2.5.198.39 ; [MS-XLSB] 2.5.97.31 */
  9892. function parse_PtgAttrSpaceSemi(blob) {
  9893. blob.read_shift(2);
  9894. return parse_PtgAttrSpaceType(blob, 2);
  9895. }
  9896. /* [MS-XLS] 2.5.198.84 ; [MS-XLSB] 2.5.97.68 TODO */
  9897. function parse_PtgRef(blob, length, opts) {
  9898. //var ptg = blob[blob.l] & 0x1F;
  9899. var type = (blob[blob.l] & 0x60)>>5;
  9900. blob.l += 1;
  9901. var loc = parse_RgceLoc(blob, 0, opts);
  9902. return [type, loc];
  9903. }
  9904. /* [MS-XLS] 2.5.198.88 ; [MS-XLSB] 2.5.97.72 TODO */
  9905. function parse_PtgRefN(blob, length, opts) {
  9906. var type = (blob[blob.l] & 0x60)>>5;
  9907. blob.l += 1;
  9908. var loc = parse_RgceLocRel(blob, 0, opts);
  9909. return [type, loc];
  9910. }
  9911. /* [MS-XLS] 2.5.198.85 ; [MS-XLSB] 2.5.97.69 TODO */
  9912. function parse_PtgRef3d(blob, length, opts) {
  9913. var type = (blob[blob.l] & 0x60)>>5;
  9914. blob.l += 1;
  9915. var ixti = blob.read_shift(2); // XtiIndex
  9916. if(opts && opts.biff == 5) blob.l += 12;
  9917. var loc = parse_RgceLoc(blob, 0, opts); // TODO: or RgceLocRel
  9918. return [type, ixti, loc];
  9919. }
  9920. /* [MS-XLS] 2.5.198.62 ; [MS-XLSB] 2.5.97.45 TODO */
  9921. function parse_PtgFunc(blob, length, opts) {
  9922. //var ptg = blob[blob.l] & 0x1F;
  9923. var type = (blob[blob.l] & 0x60)>>5;
  9924. blob.l += 1;
  9925. var iftab = blob.read_shift(opts && opts.biff <= 3 ? 1 : 2);
  9926. return [FtabArgc[iftab], Ftab[iftab], type];
  9927. }
  9928. /* [MS-XLS] 2.5.198.63 ; [MS-XLSB] 2.5.97.46 TODO */
  9929. function parse_PtgFuncVar(blob, length, opts) {
  9930. var type = blob[blob.l++];
  9931. var cparams = blob.read_shift(1), tab = opts && opts.biff <= 3 ? [(type == 0x58 ? -1 : 0), blob.read_shift(1)]: parsetab(blob);
  9932. return [cparams, (tab[0] === 0 ? Ftab : Cetab)[tab[1]]];
  9933. }
  9934. function parsetab(blob) {
  9935. return [blob[blob.l+1]>>7, blob.read_shift(2) & 0x7FFF];
  9936. }
  9937. /* [MS-XLS] 2.5.198.41 ; [MS-XLSB] 2.5.97.33 */
  9938. function parse_PtgAttrSum(blob, length, opts) {
  9939. blob.l += opts && opts.biff == 2 ? 3 : 4; return;
  9940. }
  9941. /* [MS-XLS] 2.5.198.58 ; [MS-XLSB] 2.5.97.40 */
  9942. function parse_PtgExp(blob, length, opts) {
  9943. blob.l++;
  9944. if(opts && opts.biff == 12) return [blob.read_shift(4, 'i'), 0];
  9945. var row = blob.read_shift(2);
  9946. var col = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  9947. return [row, col];
  9948. }
  9949. /* [MS-XLS] 2.5.198.57 ; [MS-XLSB] 2.5.97.39 */
  9950. function parse_PtgErr(blob) { blob.l++; return BErr[blob.read_shift(1)]; }
  9951. /* [MS-XLS] 2.5.198.66 ; [MS-XLSB] 2.5.97.49 */
  9952. function parse_PtgInt(blob) { blob.l++; return blob.read_shift(2); }
  9953. /* [MS-XLS] 2.5.198.42 ; [MS-XLSB] 2.5.97.34 */
  9954. function parse_PtgBool(blob) { blob.l++; return blob.read_shift(1)!==0;}
  9955. /* [MS-XLS] 2.5.198.79 ; [MS-XLSB] 2.5.97.63 */
  9956. function parse_PtgNum(blob) { blob.l++; return parse_Xnum(blob, 8); }
  9957. /* [MS-XLS] 2.5.198.89 ; [MS-XLSB] 2.5.97.74 */
  9958. function parse_PtgStr(blob, length, opts) { blob.l++; return parse_ShortXLUnicodeString(blob, length-1, opts); }
  9959. /* [MS-XLS] 2.5.192.112 + 2.5.192.11{3,4,5,6,7} */
  9960. /* [MS-XLSB] 2.5.97.93 + 2.5.97.9{4,5,6,7} */
  9961. function parse_SerAr(blob, biff) {
  9962. var val = [blob.read_shift(1)];
  9963. if(biff == 12) switch(val[0]) {
  9964. case 0x02: val[0] = 0x04; break; /* SerBool */
  9965. case 0x04: val[0] = 0x10; break; /* SerErr */
  9966. case 0x00: val[0] = 0x01; break; /* SerNum */
  9967. case 0x01: val[0] = 0x02; break; /* SerStr */
  9968. }
  9969. switch(val[0]) {
  9970. case 0x04: /* SerBool -- boolean */
  9971. val[1] = parsebool(blob, 1) ? 'TRUE' : 'FALSE';
  9972. if(biff != 12) blob.l += 7; break;
  9973. case 0x25: /* appears to be an alias */
  9974. case 0x10: /* SerErr -- error */
  9975. val[1] = BErr[blob[blob.l]];
  9976. blob.l += ((biff == 12) ? 4 : 8); break;
  9977. case 0x00: /* SerNil -- honestly, I'm not sure how to reproduce this */
  9978. blob.l += 8; break;
  9979. case 0x01: /* SerNum -- Xnum */
  9980. val[1] = parse_Xnum(blob, 8); break;
  9981. case 0x02: /* SerStr -- XLUnicodeString (<256 chars) */
  9982. val[1] = parse_XLUnicodeString2(blob, 0, {biff:biff > 0 && biff < 8 ? 2 : biff}); break;
  9983. default: throw new Error("Bad SerAr: " + val[0]); /* Unreachable */
  9984. }
  9985. return val;
  9986. }
  9987. /* [MS-XLS] 2.5.198.61 ; [MS-XLSB] 2.5.97.44 */
  9988. function parse_PtgExtraMem(blob, cce, opts) {
  9989. var count = blob.read_shift((opts.biff == 12) ? 4 : 2);
  9990. var out = [];
  9991. for(var i = 0; i != count; ++i) out.push(((opts.biff == 12) ? parse_UncheckedRfX : parse_Ref8U)(blob, 8));
  9992. return out;
  9993. }
  9994. /* [MS-XLS] 2.5.198.59 ; [MS-XLSB] 2.5.97.41 */
  9995. function parse_PtgExtraArray(blob, length, opts) {
  9996. var rows = 0, cols = 0;
  9997. if(opts.biff == 12) {
  9998. rows = blob.read_shift(4); // DRw
  9999. cols = blob.read_shift(4); // DCol
  10000. } else {
  10001. cols = 1 + blob.read_shift(1); //DColByteU
  10002. rows = 1 + blob.read_shift(2); //DRw
  10003. }
  10004. if(opts.biff >= 2 && opts.biff < 8) { --rows; if(--cols == 0) cols = 0x100; }
  10005. // $FlowIgnore
  10006. for(var i = 0, o = []; i != rows && (o[i] = []); ++i)
  10007. for(var j = 0; j != cols; ++j) o[i][j] = parse_SerAr(blob, opts.biff);
  10008. return o;
  10009. }
  10010. /* [MS-XLS] 2.5.198.76 ; [MS-XLSB] 2.5.97.60 */
  10011. function parse_PtgName(blob, length, opts) {
  10012. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10013. var w = (!opts || (opts.biff >= 8)) ? 4 : 2;
  10014. var nameindex = blob.read_shift(w);
  10015. switch(opts.biff) {
  10016. case 2: blob.l += 5; break;
  10017. case 3: case 4: blob.l += 8; break;
  10018. case 5: blob.l += 12; break;
  10019. }
  10020. return [type, 0, nameindex];
  10021. }
  10022. /* [MS-XLS] 2.5.198.77 ; [MS-XLSB] 2.5.97.61 */
  10023. function parse_PtgNameX(blob, length, opts) {
  10024. if(opts.biff == 5) return parse_PtgNameX_BIFF5(blob, length, opts);
  10025. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10026. var ixti = blob.read_shift(2); // XtiIndex
  10027. var nameindex = blob.read_shift(4);
  10028. return [type, ixti, nameindex];
  10029. }
  10030. function parse_PtgNameX_BIFF5(blob) {
  10031. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10032. var ixti = blob.read_shift(2, 'i'); // XtiIndex
  10033. blob.l += 8;
  10034. var nameindex = blob.read_shift(2);
  10035. blob.l += 12;
  10036. return [type, ixti, nameindex];
  10037. }
  10038. /* [MS-XLS] 2.5.198.70 ; [MS-XLSB] 2.5.97.54 */
  10039. function parse_PtgMemArea(blob, length, opts) {
  10040. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10041. blob.l += (opts && opts.biff == 2 ? 3 : 4);
  10042. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  10043. return [type, cce];
  10044. }
  10045. /* [MS-XLS] 2.5.198.72 ; [MS-XLSB] 2.5.97.56 */
  10046. function parse_PtgMemFunc(blob, length, opts) {
  10047. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10048. var cce = blob.read_shift(opts && opts.biff == 2 ? 1 : 2);
  10049. return [type, cce];
  10050. }
  10051. /* [MS-XLS] 2.5.198.86 ; [MS-XLSB] 2.5.97.69 */
  10052. function parse_PtgRefErr(blob, length, opts) {
  10053. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10054. blob.l += 4;
  10055. if(opts.biff < 8) blob.l--;
  10056. if(opts.biff == 12) blob.l += 2;
  10057. return [type];
  10058. }
  10059. /* [MS-XLS] 2.5.198.87 ; [MS-XLSB] 2.5.97.71 */
  10060. function parse_PtgRefErr3d(blob, length, opts) {
  10061. var type = (blob[blob.l++] & 0x60) >> 5;
  10062. var ixti = blob.read_shift(2);
  10063. var w = 4;
  10064. if(opts) switch(opts.biff) {
  10065. case 5: w = 15; break;
  10066. case 12: w = 6; break;
  10067. }
  10068. blob.l += w;
  10069. return [type, ixti];
  10070. }
  10071. /* [MS-XLS] 2.5.198.71 ; [MS-XLSB] 2.5.97.55 */
  10072. var parse_PtgMemErr = parsenoop;
  10073. /* [MS-XLS] 2.5.198.73 ; [MS-XLSB] 2.5.97.57 */
  10074. var parse_PtgMemNoMem = parsenoop;
  10075. /* [MS-XLS] 2.5.198.92 */
  10076. var parse_PtgTbl = parsenoop;
  10077. function parse_PtgElfLoc(blob, length, opts) {
  10078. blob.l += 2;
  10079. return [parse_RgceElfLoc(blob, 4, opts)];
  10080. }
  10081. function parse_PtgElfNoop(blob) {
  10082. blob.l += 6;
  10083. return [];
  10084. }
  10085. /* [MS-XLS] 2.5.198.46 */
  10086. var parse_PtgElfCol = parse_PtgElfLoc;
  10087. /* [MS-XLS] 2.5.198.47 */
  10088. var parse_PtgElfColS = parse_PtgElfNoop;
  10089. /* [MS-XLS] 2.5.198.48 */
  10090. var parse_PtgElfColSV = parse_PtgElfNoop;
  10091. /* [MS-XLS] 2.5.198.49 */
  10092. var parse_PtgElfColV = parse_PtgElfLoc;
  10093. /* [MS-XLS] 2.5.198.50 */
  10094. function parse_PtgElfLel(blob) {
  10095. blob.l += 2;
  10096. return [parseuint16(blob), blob.read_shift(2) & 0x01];
  10097. }
  10098. /* [MS-XLS] 2.5.198.51 */
  10099. var parse_PtgElfRadical = parse_PtgElfLoc;
  10100. /* [MS-XLS] 2.5.198.52 */
  10101. var parse_PtgElfRadicalLel = parse_PtgElfLel;
  10102. /* [MS-XLS] 2.5.198.53 */
  10103. var parse_PtgElfRadicalS = parse_PtgElfNoop;
  10104. /* [MS-XLS] 2.5.198.54 */
  10105. var parse_PtgElfRw = parse_PtgElfLoc;
  10106. /* [MS-XLS] 2.5.198.55 */
  10107. var parse_PtgElfRwV = parse_PtgElfLoc;
  10108. /* [MS-XLSB] 2.5.97.52 TODO */
  10109. var PtgListRT = [
  10110. "Data",
  10111. "All",
  10112. "Headers",
  10113. "??",
  10114. "?Data2",
  10115. "??",
  10116. "?DataHeaders",
  10117. "??",
  10118. "Totals",
  10119. "??",
  10120. "??",
  10121. "??",
  10122. "?DataTotals",
  10123. "??",
  10124. "??",
  10125. "??",
  10126. "?Current"
  10127. ];
  10128. function parse_PtgList(blob) {
  10129. blob.l += 2;
  10130. var ixti = blob.read_shift(2);
  10131. var flags = blob.read_shift(2);
  10132. var idx = blob.read_shift(4);
  10133. var c = blob.read_shift(2);
  10134. var C = blob.read_shift(2);
  10135. var rt = PtgListRT[(flags >> 2) & 0x1F];
  10136. return {ixti: ixti, coltype:(flags&0x3), rt:rt, idx:idx, c:c, C:C};
  10137. }
  10138. /* [MS-XLS] 2.5.198.91 ; [MS-XLSB] 2.5.97.76 */
  10139. function parse_PtgSxName(blob) {
  10140. blob.l += 2;
  10141. return [blob.read_shift(4)];
  10142. }
  10143. /* [XLS] old spec */
  10144. function parse_PtgSheet(blob, length, opts) {
  10145. blob.l += 5;
  10146. blob.l += 2;
  10147. blob.l += (opts.biff == 2 ? 1 : 4);
  10148. return ["PTGSHEET"];
  10149. }
  10150. function parse_PtgEndSheet(blob, length, opts) {
  10151. blob.l += (opts.biff == 2 ? 4 : 5);
  10152. return ["PTGENDSHEET"];
  10153. }
  10154. function parse_PtgMemAreaN(blob) {
  10155. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10156. var cce = blob.read_shift(2);
  10157. return [type, cce];
  10158. }
  10159. function parse_PtgMemNoMemN(blob) {
  10160. var type = (blob.read_shift(1) >>> 5) & 0x03;
  10161. var cce = blob.read_shift(2);
  10162. return [type, cce];
  10163. }
  10164. function parse_PtgAttrNoop(blob) {
  10165. blob.l += 4;
  10166. return [0, 0];
  10167. }
  10168. /* [MS-XLS] 2.5.198.25 ; [MS-XLSB] 2.5.97.16 */
  10169. var PtgTypes = {
  10170. 0x01: { n:'PtgExp', f:parse_PtgExp },
  10171. 0x02: { n:'PtgTbl', f:parse_PtgTbl },
  10172. 0x03: { n:'PtgAdd', f:parseread1 },
  10173. 0x04: { n:'PtgSub', f:parseread1 },
  10174. 0x05: { n:'PtgMul', f:parseread1 },
  10175. 0x06: { n:'PtgDiv', f:parseread1 },
  10176. 0x07: { n:'PtgPower', f:parseread1 },
  10177. 0x08: { n:'PtgConcat', f:parseread1 },
  10178. 0x09: { n:'PtgLt', f:parseread1 },
  10179. 0x0A: { n:'PtgLe', f:parseread1 },
  10180. 0x0B: { n:'PtgEq', f:parseread1 },
  10181. 0x0C: { n:'PtgGe', f:parseread1 },
  10182. 0x0D: { n:'PtgGt', f:parseread1 },
  10183. 0x0E: { n:'PtgNe', f:parseread1 },
  10184. 0x0F: { n:'PtgIsect', f:parseread1 },
  10185. 0x10: { n:'PtgUnion', f:parseread1 },
  10186. 0x11: { n:'PtgRange', f:parseread1 },
  10187. 0x12: { n:'PtgUplus', f:parseread1 },
  10188. 0x13: { n:'PtgUminus', f:parseread1 },
  10189. 0x14: { n:'PtgPercent', f:parseread1 },
  10190. 0x15: { n:'PtgParen', f:parseread1 },
  10191. 0x16: { n:'PtgMissArg', f:parseread1 },
  10192. 0x17: { n:'PtgStr', f:parse_PtgStr },
  10193. 0x1A: { n:'PtgSheet', f:parse_PtgSheet },
  10194. 0x1B: { n:'PtgEndSheet', f:parse_PtgEndSheet },
  10195. 0x1C: { n:'PtgErr', f:parse_PtgErr },
  10196. 0x1D: { n:'PtgBool', f:parse_PtgBool },
  10197. 0x1E: { n:'PtgInt', f:parse_PtgInt },
  10198. 0x1F: { n:'PtgNum', f:parse_PtgNum },
  10199. 0x20: { n:'PtgArray', f:parse_PtgArray },
  10200. 0x21: { n:'PtgFunc', f:parse_PtgFunc },
  10201. 0x22: { n:'PtgFuncVar', f:parse_PtgFuncVar },
  10202. 0x23: { n:'PtgName', f:parse_PtgName },
  10203. 0x24: { n:'PtgRef', f:parse_PtgRef },
  10204. 0x25: { n:'PtgArea', f:parse_PtgArea },
  10205. 0x26: { n:'PtgMemArea', f:parse_PtgMemArea },
  10206. 0x27: { n:'PtgMemErr', f:parse_PtgMemErr },
  10207. 0x28: { n:'PtgMemNoMem', f:parse_PtgMemNoMem },
  10208. 0x29: { n:'PtgMemFunc', f:parse_PtgMemFunc },
  10209. 0x2A: { n:'PtgRefErr', f:parse_PtgRefErr },
  10210. 0x2B: { n:'PtgAreaErr', f:parse_PtgAreaErr },
  10211. 0x2C: { n:'PtgRefN', f:parse_PtgRefN },
  10212. 0x2D: { n:'PtgAreaN', f:parse_PtgAreaN },
  10213. 0x2E: { n:'PtgMemAreaN', f:parse_PtgMemAreaN },
  10214. 0x2F: { n:'PtgMemNoMemN', f:parse_PtgMemNoMemN },
  10215. 0x39: { n:'PtgNameX', f:parse_PtgNameX },
  10216. 0x3A: { n:'PtgRef3d', f:parse_PtgRef3d },
  10217. 0x3B: { n:'PtgArea3d', f:parse_PtgArea3d },
  10218. 0x3C: { n:'PtgRefErr3d', f:parse_PtgRefErr3d },
  10219. 0x3D: { n:'PtgAreaErr3d', f:parse_PtgAreaErr3d },
  10220. 0xFF: {}
  10221. };
  10222. /* These are duplicated in the PtgTypes table */
  10223. var PtgDupes = {
  10224. 0x40: 0x20, 0x60: 0x20,
  10225. 0x41: 0x21, 0x61: 0x21,
  10226. 0x42: 0x22, 0x62: 0x22,
  10227. 0x43: 0x23, 0x63: 0x23,
  10228. 0x44: 0x24, 0x64: 0x24,
  10229. 0x45: 0x25, 0x65: 0x25,
  10230. 0x46: 0x26, 0x66: 0x26,
  10231. 0x47: 0x27, 0x67: 0x27,
  10232. 0x48: 0x28, 0x68: 0x28,
  10233. 0x49: 0x29, 0x69: 0x29,
  10234. 0x4A: 0x2A, 0x6A: 0x2A,
  10235. 0x4B: 0x2B, 0x6B: 0x2B,
  10236. 0x4C: 0x2C, 0x6C: 0x2C,
  10237. 0x4D: 0x2D, 0x6D: 0x2D,
  10238. 0x4E: 0x2E, 0x6E: 0x2E,
  10239. 0x4F: 0x2F, 0x6F: 0x2F,
  10240. 0x58: 0x22, 0x78: 0x22,
  10241. 0x59: 0x39, 0x79: 0x39,
  10242. 0x5A: 0x3A, 0x7A: 0x3A,
  10243. 0x5B: 0x3B, 0x7B: 0x3B,
  10244. 0x5C: 0x3C, 0x7C: 0x3C,
  10245. 0x5D: 0x3D, 0x7D: 0x3D
  10246. };
  10247. (function(){for(var y in PtgDupes) PtgTypes[y] = PtgTypes[PtgDupes[y]];})();
  10248. var Ptg18 = {
  10249. 0x01: { n:'PtgElfLel', f:parse_PtgElfLel },
  10250. 0x02: { n:'PtgElfRw', f:parse_PtgElfRw },
  10251. 0x03: { n:'PtgElfCol', f:parse_PtgElfCol },
  10252. 0x06: { n:'PtgElfRwV', f:parse_PtgElfRwV },
  10253. 0x07: { n:'PtgElfColV', f:parse_PtgElfColV },
  10254. 0x0A: { n:'PtgElfRadical', f:parse_PtgElfRadical },
  10255. 0x0B: { n:'PtgElfRadicalS', f:parse_PtgElfRadicalS },
  10256. 0x0D: { n:'PtgElfColS', f:parse_PtgElfColS },
  10257. 0x0F: { n:'PtgElfColSV', f:parse_PtgElfColSV },
  10258. 0x10: { n:'PtgElfRadicalLel', f:parse_PtgElfRadicalLel },
  10259. 0x19: { n:'PtgList', f:parse_PtgList },
  10260. 0x1D: { n:'PtgSxName', f:parse_PtgSxName },
  10261. 0xFF: {}
  10262. };
  10263. var Ptg19 = {
  10264. 0x00: { n:'PtgAttrNoop', f:parse_PtgAttrNoop },
  10265. 0x01: { n:'PtgAttrSemi', f:parse_PtgAttrSemi },
  10266. 0x02: { n:'PtgAttrIf', f:parse_PtgAttrIf },
  10267. 0x04: { n:'PtgAttrChoose', f:parse_PtgAttrChoose },
  10268. 0x08: { n:'PtgAttrGoto', f:parse_PtgAttrGoto },
  10269. 0x10: { n:'PtgAttrSum', f:parse_PtgAttrSum },
  10270. 0x20: { n:'PtgAttrBaxcel', f:parse_PtgAttrBaxcel },
  10271. 0x40: { n:'PtgAttrSpace', f:parse_PtgAttrSpace },
  10272. 0x41: { n:'PtgAttrSpaceSemi', f:parse_PtgAttrSpaceSemi },
  10273. 0x80: { n:'PtgAttrIfError', f:parse_PtgAttrIfError },
  10274. 0xFF: {}
  10275. };
  10276. Ptg19[0x21] = Ptg19[0x20];
  10277. /* [MS-XLS] 2.5.198.103 ; [MS-XLSB] 2.5.97.87 */
  10278. function parse_RgbExtra(blob, length, rgce, opts) {
  10279. if(opts.biff < 8) return parsenoop(blob, length);
  10280. var target = blob.l + length;
  10281. var o = [];
  10282. for(var i = 0; i !== rgce.length; ++i) {
  10283. switch(rgce[i][0]) {
  10284. case 'PtgArray': /* PtgArray -> PtgExtraArray */
  10285. rgce[i][1] = parse_PtgExtraArray(blob, 0, opts);
  10286. o.push(rgce[i][1]);
  10287. break;
  10288. case 'PtgMemArea': /* PtgMemArea -> PtgExtraMem */
  10289. rgce[i][2] = parse_PtgExtraMem(blob, rgce[i][1], opts);
  10290. o.push(rgce[i][2]);
  10291. break;
  10292. case 'PtgExp': /* PtgExp -> PtgExtraCol */
  10293. if(opts && opts.biff == 12) {
  10294. rgce[i][1][1] = blob.read_shift(4);
  10295. o.push(rgce[i][1]);
  10296. } break;
  10297. case 'PtgList': /* TODO: PtgList -> PtgExtraList */
  10298. case 'PtgElfRadicalS': /* TODO: PtgElfRadicalS -> PtgExtraElf */
  10299. case 'PtgElfColS': /* TODO: PtgElfColS -> PtgExtraElf */
  10300. case 'PtgElfColSV': /* TODO: PtgElfColSV -> PtgExtraElf */
  10301. throw "Unsupported " + rgce[i][0];
  10302. default: break;
  10303. }
  10304. }
  10305. length = target - blob.l;
  10306. /* note: this is technically an error but Excel disregards */
  10307. //if(target !== blob.l && blob.l !== target - length) throw new Error(target + " != " + blob.l);
  10308. if(length !== 0) o.push(parsenoop(blob, length));
  10309. return o;
  10310. }
  10311. /* [MS-XLS] 2.5.198.104 ; [MS-XLSB] 2.5.97.88 */
  10312. function parse_Rgce(blob, length, opts) {
  10313. var target = blob.l + length;
  10314. var R, id, ptgs = [];
  10315. while(target != blob.l) {
  10316. length = target - blob.l;
  10317. id = blob[blob.l];
  10318. R = PtgTypes[id];
  10319. if(id === 0x18 || id === 0x19) R = (id === 0x18 ? Ptg18 : Ptg19)[blob[blob.l + 1]];
  10320. if(!R || !R.f) { /*ptgs.push*/(parsenoop(blob, length)); }
  10321. else { ptgs.push([R.n, R.f(blob, length, opts)]); }
  10322. }
  10323. return ptgs;
  10324. }
  10325. function stringify_array(f) {
  10326. var o = [];
  10327. for(var i = 0; i < f.length; ++i) {
  10328. var x = f[i], r = [];
  10329. for(var j = 0; j < x.length; ++j) {
  10330. var y = x[j];
  10331. if(y) switch(y[0]) {
  10332. // TODO: handle embedded quotes
  10333. case 0x02:
  10334. r.push('"' + y[1].replace(/"/g,'""') + '"'); break;
  10335. default: r.push(y[1]);
  10336. } else r.push("");
  10337. }
  10338. o.push(r.join(","));
  10339. }
  10340. return o.join(";");
  10341. }
  10342. /* [MS-XLS] 2.2.2 ; [MS-XLSB] 2.2.2 TODO */
  10343. var PtgBinOp = {
  10344. PtgAdd: "+",
  10345. PtgConcat: "&",
  10346. PtgDiv: "/",
  10347. PtgEq: "=",
  10348. PtgGe: ">=",
  10349. PtgGt: ">",
  10350. PtgLe: "<=",
  10351. PtgLt: "<",
  10352. PtgMul: "*",
  10353. PtgNe: "<>",
  10354. PtgPower: "^",
  10355. PtgSub: "-"
  10356. };
  10357. // List of invalid characters needs to be tested further
  10358. var quoteCharacters = new RegExp(/[^\w\u4E00-\u9FFF\u3040-\u30FF]/);
  10359. function formula_quote_sheet_name(sname, opts) {
  10360. if(!sname && !(opts && opts.biff <= 5 && opts.biff >= 2)) throw new Error("empty sheet name");
  10361. if (quoteCharacters.test(sname)) return "'" + sname + "'";
  10362. return sname;
  10363. }
  10364. function get_ixti_raw(supbooks, ixti, opts) {
  10365. if(!supbooks) return "SH33TJSERR0";
  10366. if(opts.biff > 8 && (!supbooks.XTI || !supbooks.XTI[ixti])) return supbooks.SheetNames[ixti];
  10367. if(!supbooks.XTI) return "SH33TJSERR6";
  10368. var XTI = supbooks.XTI[ixti];
  10369. if(opts.biff < 8) {
  10370. if(ixti > 10000) ixti-= 65536;
  10371. if(ixti < 0) ixti = -ixti;
  10372. return ixti == 0 ? "" : supbooks.XTI[ixti - 1];
  10373. }
  10374. if(!XTI) return "SH33TJSERR1";
  10375. var o = "";
  10376. if(opts.biff > 8) switch(supbooks[XTI[0]][0]) {
  10377. case 0x0165: /* 'BrtSupSelf' */
  10378. o = XTI[1] == -1 ? "#REF" : supbooks.SheetNames[XTI[1]];
  10379. return XTI[1] == XTI[2] ? o : o + ":" + supbooks.SheetNames[XTI[2]];
  10380. case 0x0166: /* 'BrtSupSame' */
  10381. if(opts.SID != null) return supbooks.SheetNames[opts.SID];
  10382. return "SH33TJSSAME" + supbooks[XTI[0]][0];
  10383. case 0x0163: /* 'BrtSupBookSrc' */
  10384. /* falls through */
  10385. default: return "SH33TJSSRC" + supbooks[XTI[0]][0];
  10386. }
  10387. switch(supbooks[XTI[0]][0][0]) {
  10388. case 0x0401:
  10389. o = XTI[1] == -1 ? "#REF" : (supbooks.SheetNames[XTI[1]] || "SH33TJSERR3");
  10390. return XTI[1] == XTI[2] ? o : o + ":" + supbooks.SheetNames[XTI[2]];
  10391. case 0x3A01: return supbooks[XTI[0]].slice(1).map(function(name) { return name.Name; }).join(";;"); //return "SH33TJSERR8";
  10392. default:
  10393. if(!supbooks[XTI[0]][0][3]) return "SH33TJSERR2";
  10394. o = XTI[1] == -1 ? "#REF" : (supbooks[XTI[0]][0][3][XTI[1]] || "SH33TJSERR4");
  10395. return XTI[1] == XTI[2] ? o : o + ":" + supbooks[XTI[0]][0][3][XTI[2]];
  10396. }
  10397. }
  10398. function get_ixti(supbooks, ixti, opts) {
  10399. return formula_quote_sheet_name(get_ixti_raw(supbooks, ixti, opts), opts);
  10400. }
  10401. function stringify_formula(formula/*Array<any>*/, range, cell, supbooks, opts) {
  10402. var biff = (opts && opts.biff) || 8;
  10403. var _range = /*range != null ? range :*/ {s:{c:0, r:0},e:{c:0, r:0}};
  10404. var stack = [], e1, e2, c, ixti=0, nameidx=0, r, sname="";
  10405. if(!formula[0] || !formula[0][0]) return "";
  10406. var last_sp = -1, sp = "";
  10407. for(var ff = 0, fflen = formula[0].length; ff < fflen; ++ff) {
  10408. var f = formula[0][ff];
  10409. switch(f[0]) {
  10410. case 'PtgUminus': /* [MS-XLS] 2.5.198.93 */
  10411. stack.push("-" + stack.pop()); break;
  10412. case 'PtgUplus': /* [MS-XLS] 2.5.198.95 */
  10413. stack.push("+" + stack.pop()); break;
  10414. case 'PtgPercent': /* [MS-XLS] 2.5.198.81 */
  10415. stack.push(stack.pop() + "%"); break;
  10416. case 'PtgAdd': /* [MS-XLS] 2.5.198.26 */
  10417. case 'PtgConcat': /* [MS-XLS] 2.5.198.43 */
  10418. case 'PtgDiv': /* [MS-XLS] 2.5.198.45 */
  10419. case 'PtgEq': /* [MS-XLS] 2.5.198.56 */
  10420. case 'PtgGe': /* [MS-XLS] 2.5.198.64 */
  10421. case 'PtgGt': /* [MS-XLS] 2.5.198.65 */
  10422. case 'PtgLe': /* [MS-XLS] 2.5.198.68 */
  10423. case 'PtgLt': /* [MS-XLS] 2.5.198.69 */
  10424. case 'PtgMul': /* [MS-XLS] 2.5.198.75 */
  10425. case 'PtgNe': /* [MS-XLS] 2.5.198.78 */
  10426. case 'PtgPower': /* [MS-XLS] 2.5.198.82 */
  10427. case 'PtgSub': /* [MS-XLS] 2.5.198.90 */
  10428. e1 = stack.pop(); e2 = stack.pop();
  10429. if(last_sp >= 0) {
  10430. switch(formula[0][last_sp][1][0]) {
  10431. case 0:
  10432. // $FlowIgnore
  10433. sp = fill(" ", formula[0][last_sp][1][1]); break;
  10434. case 1:
  10435. // $FlowIgnore
  10436. sp = fill("\r", formula[0][last_sp][1][1]); break;
  10437. default:
  10438. sp = "";
  10439. // $FlowIgnore
  10440. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + formula[0][last_sp][1][0]);
  10441. }
  10442. e2 = e2 + sp;
  10443. last_sp = -1;
  10444. }
  10445. stack.push(e2+PtgBinOp[f[0]]+e1);
  10446. break;
  10447. case 'PtgIsect': /* [MS-XLS] 2.5.198.67 */
  10448. e1 = stack.pop(); e2 = stack.pop();
  10449. stack.push(e2+" "+e1);
  10450. break;
  10451. case 'PtgUnion': /* [MS-XLS] 2.5.198.94 */
  10452. e1 = stack.pop(); e2 = stack.pop();
  10453. stack.push(e2+","+e1);
  10454. break;
  10455. case 'PtgRange': /* [MS-XLS] 2.5.198.83 */
  10456. e1 = stack.pop(); e2 = stack.pop();
  10457. stack.push(e2+":"+e1);
  10458. break;
  10459. case 'PtgAttrChoose': /* [MS-XLS] 2.5.198.34 */
  10460. break;
  10461. case 'PtgAttrGoto': /* [MS-XLS] 2.5.198.35 */
  10462. break;
  10463. case 'PtgAttrIf': /* [MS-XLS] 2.5.198.36 */
  10464. break;
  10465. case 'PtgAttrIfError': /* [MS-XLSB] 2.5.97.28 */
  10466. break;
  10467. case 'PtgRef': /* [MS-XLS] 2.5.198.84 */
  10468. c = shift_cell_xls((f[1][1]), _range, opts);
  10469. stack.push(encode_cell_xls(c, biff));
  10470. break;
  10471. case 'PtgRefN': /* [MS-XLS] 2.5.198.88 */
  10472. c = cell ? shift_cell_xls((f[1][1]), cell, opts) : (f[1][1]);
  10473. stack.push(encode_cell_xls(c, biff));
  10474. break;
  10475. case 'PtgRef3d': /* [MS-XLS] 2.5.198.85 */
  10476. ixti = f[1][1]; c = shift_cell_xls((f[1][2]), _range, opts);
  10477. sname = get_ixti(supbooks, ixti, opts);
  10478. var w = sname; /* IE9 fails on defined names */ // eslint-disable-line no-unused-vars
  10479. stack.push(sname + "!" + encode_cell_xls(c, biff));
  10480. break;
  10481. case 'PtgFunc': /* [MS-XLS] 2.5.198.62 */
  10482. case 'PtgFuncVar': /* [MS-XLS] 2.5.198.63 */
  10483. /* f[1] = [argc, func, type] */
  10484. var argc = (f[1][0]), func = (f[1][1]);
  10485. if(!argc) argc = 0;
  10486. argc &= 0x7F;
  10487. var args = argc == 0 ? [] : stack.slice(-argc);
  10488. stack.length -= argc;
  10489. if(func === 'User') func = args.shift();
  10490. stack.push(func + "(" + args.join(",") + ")");
  10491. break;
  10492. case 'PtgBool': /* [MS-XLS] 2.5.198.42 */
  10493. stack.push(f[1] ? "TRUE" : "FALSE"); break;
  10494. case 'PtgInt': /* [MS-XLS] 2.5.198.66 */
  10495. stack.push(f[1]); break;
  10496. case 'PtgNum': /* [MS-XLS] 2.5.198.79 TODO: precision? */
  10497. stack.push(String(f[1])); break;
  10498. case 'PtgStr': /* [MS-XLS] 2.5.198.89 */
  10499. // $FlowIgnore
  10500. stack.push('"' + f[1].replace(/"/g, '""') + '"'); break;
  10501. case 'PtgErr': /* [MS-XLS] 2.5.198.57 */
  10502. stack.push(f[1]); break;
  10503. case 'PtgAreaN': /* [MS-XLS] 2.5.198.31 TODO */
  10504. r = shift_range_xls(f[1][1], cell ? {s:cell} : _range, opts);
  10505. stack.push(encode_range_xls((r), opts));
  10506. break;
  10507. case 'PtgArea': /* [MS-XLS] 2.5.198.27 TODO: fixed points */
  10508. r = shift_range_xls(f[1][1], _range, opts);
  10509. stack.push(encode_range_xls((r), opts));
  10510. break;
  10511. case 'PtgArea3d': /* [MS-XLS] 2.5.198.28 TODO */
  10512. ixti = f[1][1]; r = f[1][2];
  10513. sname = get_ixti(supbooks, ixti, opts);
  10514. stack.push(sname + "!" + encode_range_xls((r), opts));
  10515. break;
  10516. case 'PtgAttrSum': /* [MS-XLS] 2.5.198.41 */
  10517. stack.push("SUM(" + stack.pop() + ")");
  10518. break;
  10519. case 'PtgAttrBaxcel': /* [MS-XLS] 2.5.198.33 */
  10520. case 'PtgAttrSemi': /* [MS-XLS] 2.5.198.37 */
  10521. break;
  10522. case 'PtgName': /* [MS-XLS] 2.5.198.76 ; [MS-XLSB] 2.5.97.60 TODO: revisions */
  10523. /* f[1] = type, 0, nameindex */
  10524. nameidx = (f[1][2]);
  10525. var lbl = (supbooks.names||[])[nameidx-1] || (supbooks[0]||[])[nameidx];
  10526. var name = lbl ? lbl.Name : "SH33TJSNAME" + String(nameidx);
  10527. if(name in XLSXFutureFunctions) name = XLSXFutureFunctions[name];
  10528. stack.push(name);
  10529. break;
  10530. case 'PtgNameX': /* [MS-XLS] 2.5.198.77 ; [MS-XLSB] 2.5.97.61 TODO: revisions */
  10531. /* f[1] = type, ixti, nameindex */
  10532. var bookidx = (f[1][1]); nameidx = (f[1][2]); var externbook;
  10533. /* TODO: Properly handle missing values -- this should be using get_ixti_raw primarily */
  10534. if(opts.biff <= 5) {
  10535. if(bookidx < 0) bookidx = -bookidx;
  10536. if(supbooks[bookidx]) externbook = supbooks[bookidx][nameidx];
  10537. } else {
  10538. var o = "";
  10539. if(((supbooks[bookidx]||[])[0]||[])[0] == 0x3A01){/* empty */}
  10540. else if(((supbooks[bookidx]||[])[0]||[])[0] == 0x0401){
  10541. if(supbooks[bookidx][nameidx] && supbooks[bookidx][nameidx].itab > 0) {
  10542. o = supbooks.SheetNames[supbooks[bookidx][nameidx].itab-1] + "!";
  10543. }
  10544. }
  10545. else o = supbooks.SheetNames[nameidx-1]+ "!";
  10546. if(supbooks[bookidx] && supbooks[bookidx][nameidx]) o += supbooks[bookidx][nameidx].Name;
  10547. else if(supbooks[0] && supbooks[0][nameidx]) o += supbooks[0][nameidx].Name;
  10548. else {
  10549. var ixtidata = get_ixti_raw(supbooks, bookidx, opts).split(";;");
  10550. if(ixtidata[nameidx - 1]) o = ixtidata[nameidx - 1]; // TODO: confirm this is correct
  10551. else o += "SH33TJSERRX";
  10552. }
  10553. stack.push(o);
  10554. break;
  10555. }
  10556. if(!externbook) externbook = {Name: "SH33TJSERRY"};
  10557. stack.push(externbook.Name);
  10558. break;
  10559. case 'PtgParen': /* [MS-XLS] 2.5.198.80 */
  10560. var lp = '(', rp = ')';
  10561. if(last_sp >= 0) {
  10562. sp = "";
  10563. switch(formula[0][last_sp][1][0]) {
  10564. // $FlowIgnore
  10565. case 2: lp = fill(" ", formula[0][last_sp][1][1]) + lp; break;
  10566. // $FlowIgnore
  10567. case 3: lp = fill("\r", formula[0][last_sp][1][1]) + lp; break;
  10568. // $FlowIgnore
  10569. case 4: rp = fill(" ", formula[0][last_sp][1][1]) + rp; break;
  10570. // $FlowIgnore
  10571. case 5: rp = fill("\r", formula[0][last_sp][1][1]) + rp; break;
  10572. default:
  10573. // $FlowIgnore
  10574. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + formula[0][last_sp][1][0]);
  10575. }
  10576. last_sp = -1;
  10577. }
  10578. stack.push(lp + stack.pop() + rp); break;
  10579. case 'PtgRefErr': /* [MS-XLS] 2.5.198.86 */
  10580. stack.push('#REF!'); break;
  10581. case 'PtgRefErr3d': /* [MS-XLS] 2.5.198.87 */
  10582. stack.push('#REF!'); break;
  10583. case 'PtgExp': /* [MS-XLS] 2.5.198.58 TODO */
  10584. c = {c:(f[1][1]),r:(f[1][0])};
  10585. var q = ({c: cell.c, r:cell.r});
  10586. if(supbooks.sharedf[encode_cell(c)]) {
  10587. var parsedf = (supbooks.sharedf[encode_cell(c)]);
  10588. stack.push(stringify_formula(parsedf, _range, q, supbooks, opts));
  10589. }
  10590. else {
  10591. var fnd = false;
  10592. for(e1=0;e1!=supbooks.arrayf.length; ++e1) {
  10593. /* TODO: should be something like range_has */
  10594. e2 = supbooks.arrayf[e1];
  10595. if(c.c < e2[0].s.c || c.c > e2[0].e.c) continue;
  10596. if(c.r < e2[0].s.r || c.r > e2[0].e.r) continue;
  10597. stack.push(stringify_formula(e2[1], _range, q, supbooks, opts));
  10598. fnd = true;
  10599. break;
  10600. }
  10601. if(!fnd) stack.push(f[1]);
  10602. }
  10603. break;
  10604. case 'PtgArray': /* [MS-XLS] 2.5.198.32 TODO */
  10605. stack.push("{" + stringify_array(f[1]) + "}");
  10606. break;
  10607. case 'PtgMemArea': /* [MS-XLS] 2.5.198.70 TODO: confirm this is a non-display */
  10608. //stack.push("(" + f[2].map(encode_range).join(",") + ")");
  10609. break;
  10610. case 'PtgAttrSpace': /* [MS-XLS] 2.5.198.38 */
  10611. case 'PtgAttrSpaceSemi': /* [MS-XLS] 2.5.198.39 */
  10612. last_sp = ff;
  10613. break;
  10614. case 'PtgTbl': /* [MS-XLS] 2.5.198.92 TODO */
  10615. break;
  10616. case 'PtgMemErr': /* [MS-XLS] 2.5.198.71 */
  10617. break;
  10618. case 'PtgMissArg': /* [MS-XLS] 2.5.198.74 */
  10619. stack.push("");
  10620. break;
  10621. case 'PtgAreaErr': /* [MS-XLS] 2.5.198.29 */
  10622. stack.push("#REF!"); break;
  10623. case 'PtgAreaErr3d': /* [MS-XLS] 2.5.198.30 */
  10624. stack.push("#REF!"); break;
  10625. case 'PtgList': /* [MS-XLSB] 2.5.97.52 */
  10626. // $FlowIgnore
  10627. stack.push("Table" + f[1].idx + "[#" + f[1].rt + "]");
  10628. break;
  10629. case 'PtgMemAreaN':
  10630. case 'PtgMemNoMemN':
  10631. case 'PtgAttrNoop':
  10632. case 'PtgSheet':
  10633. case 'PtgEndSheet':
  10634. break;
  10635. case 'PtgMemFunc': /* [MS-XLS] 2.5.198.72 TODO */
  10636. break;
  10637. case 'PtgMemNoMem': /* [MS-XLS] 2.5.198.73 TODO */
  10638. break;
  10639. case 'PtgElfCol': /* [MS-XLS] 2.5.198.46 */
  10640. case 'PtgElfColS': /* [MS-XLS] 2.5.198.47 */
  10641. case 'PtgElfColSV': /* [MS-XLS] 2.5.198.48 */
  10642. case 'PtgElfColV': /* [MS-XLS] 2.5.198.49 */
  10643. case 'PtgElfLel': /* [MS-XLS] 2.5.198.50 */
  10644. case 'PtgElfRadical': /* [MS-XLS] 2.5.198.51 */
  10645. case 'PtgElfRadicalLel': /* [MS-XLS] 2.5.198.52 */
  10646. case 'PtgElfRadicalS': /* [MS-XLS] 2.5.198.53 */
  10647. case 'PtgElfRw': /* [MS-XLS] 2.5.198.54 */
  10648. case 'PtgElfRwV': /* [MS-XLS] 2.5.198.55 */
  10649. throw new Error("Unsupported ELFs");
  10650. case 'PtgSxName': /* [MS-XLS] 2.5.198.91 TODO -- find a test case */
  10651. throw new Error('Unrecognized Formula Token: ' + String(f));
  10652. default: throw new Error('Unrecognized Formula Token: ' + String(f));
  10653. }
  10654. var PtgNonDisp = ['PtgAttrSpace', 'PtgAttrSpaceSemi', 'PtgAttrGoto'];
  10655. if(opts.biff != 3) if(last_sp >= 0 && PtgNonDisp.indexOf(formula[0][ff][0]) == -1) {
  10656. f = formula[0][last_sp];
  10657. var _left = true;
  10658. switch(f[1][0]) {
  10659. /* note: some bad XLSB files omit the PtgParen */
  10660. case 4: _left = false;
  10661. /* falls through */
  10662. case 0:
  10663. // $FlowIgnore
  10664. sp = fill(" ", f[1][1]); break;
  10665. case 5: _left = false;
  10666. /* falls through */
  10667. case 1:
  10668. // $FlowIgnore
  10669. sp = fill("\r", f[1][1]); break;
  10670. default:
  10671. sp = "";
  10672. // $FlowIgnore
  10673. if(opts.WTF) throw new Error("Unexpected PtgAttrSpaceType " + f[1][0]);
  10674. }
  10675. stack.push((_left ? sp : "") + stack.pop() + (_left ? "" : sp));
  10676. last_sp = -1;
  10677. }
  10678. }
  10679. if(stack.length > 1 && opts.WTF) throw new Error("bad formula stack");
  10680. return stack[0];
  10681. }
  10682. /* [MS-XLS] 2.5.198.1 TODO */
  10683. function parse_ArrayParsedFormula(blob, length, opts) {
  10684. var target = blob.l + length, len = opts.biff == 2 ? 1 : 2;
  10685. var rgcb, cce = blob.read_shift(len); // length of rgce
  10686. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  10687. var rgce = parse_Rgce(blob, cce, opts);
  10688. if(length !== cce + len) rgcb = parse_RgbExtra(blob, length - cce - len, rgce, opts);
  10689. blob.l = target;
  10690. return [rgce, rgcb];
  10691. }
  10692. /* [MS-XLS] 2.5.198.3 TODO */
  10693. function parse_XLSCellParsedFormula(blob, length, opts) {
  10694. var target = blob.l + length, len = opts.biff == 2 ? 1 : 2;
  10695. var rgcb, cce = blob.read_shift(len); // length of rgce
  10696. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  10697. var rgce = parse_Rgce(blob, cce, opts);
  10698. if(length !== cce + len) rgcb = parse_RgbExtra(blob, length - cce - len, rgce, opts);
  10699. blob.l = target;
  10700. return [rgce, rgcb];
  10701. }
  10702. /* [MS-XLS] 2.5.198.21 */
  10703. function parse_NameParsedFormula(blob, length, opts, cce) {
  10704. var target = blob.l + length;
  10705. var rgce = parse_Rgce(blob, cce, opts);
  10706. var rgcb;
  10707. if(target !== blob.l) rgcb = parse_RgbExtra(blob, target - blob.l, rgce, opts);
  10708. return [rgce, rgcb];
  10709. }
  10710. /* [MS-XLS] 2.5.198.118 TODO */
  10711. function parse_SharedParsedFormula(blob, length, opts) {
  10712. var target = blob.l + length;
  10713. var rgcb, cce = blob.read_shift(2); // length of rgce
  10714. var rgce = parse_Rgce(blob, cce, opts);
  10715. if(cce == 0xFFFF) return [[],parsenoop(blob, length-2)];
  10716. if(length !== cce + 2) rgcb = parse_RgbExtra(blob, target - cce - 2, rgce, opts);
  10717. return [rgce, rgcb];
  10718. }
  10719. /* [MS-XLS] 2.5.133 TODO: how to emit empty strings? */
  10720. function parse_FormulaValue(blob) {
  10721. var b;
  10722. if(__readUInt16LE(blob,blob.l + 6) !== 0xFFFF) return [parse_Xnum(blob),'n'];
  10723. switch(blob[blob.l]) {
  10724. case 0x00: blob.l += 8; return ["String", 's'];
  10725. case 0x01: b = blob[blob.l+2] === 0x1; blob.l += 8; return [b,'b'];
  10726. case 0x02: b = blob[blob.l+2]; blob.l += 8; return [b,'e'];
  10727. case 0x03: blob.l += 8; return ["",'s'];
  10728. }
  10729. return [];
  10730. }
  10731. function write_FormulaValue(value) {
  10732. if(value == null) {
  10733. // Blank String Value
  10734. var o = new_buf(8);
  10735. o.write_shift(1, 0x03);
  10736. o.write_shift(1, 0);
  10737. o.write_shift(2, 0);
  10738. o.write_shift(2, 0);
  10739. o.write_shift(2, 0xFFFF);
  10740. return o;
  10741. } else if(typeof value == "number") return write_Xnum(value);
  10742. return write_Xnum(0);
  10743. }
  10744. /* [MS-XLS] 2.4.127 TODO */
  10745. function parse_Formula(blob, length, opts) {
  10746. var end = blob.l + length;
  10747. var cell = parse_XLSCell(blob, 6);
  10748. if(opts.biff == 2) ++blob.l;
  10749. var val = parse_FormulaValue(blob,8);
  10750. var flags = blob.read_shift(1);
  10751. if(opts.biff != 2) {
  10752. blob.read_shift(1);
  10753. if(opts.biff >= 5) {
  10754. /*var chn = */blob.read_shift(4);
  10755. }
  10756. }
  10757. var cbf = parse_XLSCellParsedFormula(blob, end - blob.l, opts);
  10758. return {cell:cell, val:val[0], formula:cbf, shared: (flags >> 3) & 1, tt:val[1]};
  10759. }
  10760. function write_Formula(cell, R, C, opts, os) {
  10761. // Cell
  10762. var o1 = write_XLSCell(R, C, os);
  10763. // FormulaValue
  10764. var o2 = write_FormulaValue(cell.v);
  10765. // flags + cache
  10766. var o3 = new_buf(6);
  10767. var flags = 0x01 | 0x20;
  10768. o3.write_shift(2, flags);
  10769. o3.write_shift(4, 0);
  10770. // CellParsedFormula
  10771. var bf = new_buf(cell.bf.length);
  10772. for(var i = 0; i < cell.bf.length; ++i) bf[i] = cell.bf[i];
  10773. var out = bconcat([o1, o2, o3, bf]);
  10774. return out;
  10775. }
  10776. /* XLSB Parsed Formula records have the same shape */
  10777. function parse_XLSBParsedFormula(data, length, opts) {
  10778. var cce = data.read_shift(4);
  10779. var rgce = parse_Rgce(data, cce, opts);
  10780. var cb = data.read_shift(4);
  10781. var rgcb = cb > 0 ? parse_RgbExtra(data, cb, rgce, opts) : null;
  10782. return [rgce, rgcb];
  10783. }
  10784. /* [MS-XLSB] 2.5.97.1 ArrayParsedFormula */
  10785. var parse_XLSBArrayParsedFormula = parse_XLSBParsedFormula;
  10786. /* [MS-XLSB] 2.5.97.4 CellParsedFormula */
  10787. var parse_XLSBCellParsedFormula = parse_XLSBParsedFormula;
  10788. /* [MS-XLSB] 2.5.97.8 DVParsedFormula */
  10789. //var parse_XLSBDVParsedFormula = parse_XLSBParsedFormula;
  10790. /* [MS-XLSB] 2.5.97.9 FRTParsedFormula */
  10791. //var parse_XLSBFRTParsedFormula = parse_XLSBParsedFormula2;
  10792. /* [MS-XLSB] 2.5.97.12 NameParsedFormula */
  10793. var parse_XLSBNameParsedFormula = parse_XLSBParsedFormula;
  10794. /* [MS-XLSB] 2.5.97.98 SharedParsedFormula */
  10795. var parse_XLSBSharedParsedFormula = parse_XLSBParsedFormula;
  10796. /* [MS-XLS] 2.5.198.4 */
  10797. var Cetab = {
  10798. 0x0000: 'BEEP',
  10799. 0x0001: 'OPEN',
  10800. 0x0002: 'OPEN.LINKS',
  10801. 0x0003: 'CLOSE.ALL',
  10802. 0x0004: 'SAVE',
  10803. 0x0005: 'SAVE.AS',
  10804. 0x0006: 'FILE.DELETE',
  10805. 0x0007: 'PAGE.SETUP',
  10806. 0x0008: 'PRINT',
  10807. 0x0009: 'PRINTER.SETUP',
  10808. 0x000A: 'QUIT',
  10809. 0x000B: 'NEW.WINDOW',
  10810. 0x000C: 'ARRANGE.ALL',
  10811. 0x000D: 'WINDOW.SIZE',
  10812. 0x000E: 'WINDOW.MOVE',
  10813. 0x000F: 'FULL',
  10814. 0x0010: 'CLOSE',
  10815. 0x0011: 'RUN',
  10816. 0x0016: 'SET.PRINT.AREA',
  10817. 0x0017: 'SET.PRINT.TITLES',
  10818. 0x0018: 'SET.PAGE.BREAK',
  10819. 0x0019: 'REMOVE.PAGE.BREAK',
  10820. 0x001A: 'FONT',
  10821. 0x001B: 'DISPLAY',
  10822. 0x001C: 'PROTECT.DOCUMENT',
  10823. 0x001D: 'PRECISION',
  10824. 0x001E: 'A1.R1C1',
  10825. 0x001F: 'CALCULATE.NOW',
  10826. 0x0020: 'CALCULATION',
  10827. 0x0022: 'DATA.FIND',
  10828. 0x0023: 'EXTRACT',
  10829. 0x0024: 'DATA.DELETE',
  10830. 0x0025: 'SET.DATABASE',
  10831. 0x0026: 'SET.CRITERIA',
  10832. 0x0027: 'SORT',
  10833. 0x0028: 'DATA.SERIES',
  10834. 0x0029: 'TABLE',
  10835. 0x002A: 'FORMAT.NUMBER',
  10836. 0x002B: 'ALIGNMENT',
  10837. 0x002C: 'STYLE',
  10838. 0x002D: 'BORDER',
  10839. 0x002E: 'CELL.PROTECTION',
  10840. 0x002F: 'COLUMN.WIDTH',
  10841. 0x0030: 'UNDO',
  10842. 0x0031: 'CUT',
  10843. 0x0032: 'COPY',
  10844. 0x0033: 'PASTE',
  10845. 0x0034: 'CLEAR',
  10846. 0x0035: 'PASTE.SPECIAL',
  10847. 0x0036: 'EDIT.DELETE',
  10848. 0x0037: 'INSERT',
  10849. 0x0038: 'FILL.RIGHT',
  10850. 0x0039: 'FILL.DOWN',
  10851. 0x003D: 'DEFINE.NAME',
  10852. 0x003E: 'CREATE.NAMES',
  10853. 0x003F: 'FORMULA.GOTO',
  10854. 0x0040: 'FORMULA.FIND',
  10855. 0x0041: 'SELECT.LAST.CELL',
  10856. 0x0042: 'SHOW.ACTIVE.CELL',
  10857. 0x0043: 'GALLERY.AREA',
  10858. 0x0044: 'GALLERY.BAR',
  10859. 0x0045: 'GALLERY.COLUMN',
  10860. 0x0046: 'GALLERY.LINE',
  10861. 0x0047: 'GALLERY.PIE',
  10862. 0x0048: 'GALLERY.SCATTER',
  10863. 0x0049: 'COMBINATION',
  10864. 0x004A: 'PREFERRED',
  10865. 0x004B: 'ADD.OVERLAY',
  10866. 0x004C: 'GRIDLINES',
  10867. 0x004D: 'SET.PREFERRED',
  10868. 0x004E: 'AXES',
  10869. 0x004F: 'LEGEND',
  10870. 0x0050: 'ATTACH.TEXT',
  10871. 0x0051: 'ADD.ARROW',
  10872. 0x0052: 'SELECT.CHART',
  10873. 0x0053: 'SELECT.PLOT.AREA',
  10874. 0x0054: 'PATTERNS',
  10875. 0x0055: 'MAIN.CHART',
  10876. 0x0056: 'OVERLAY',
  10877. 0x0057: 'SCALE',
  10878. 0x0058: 'FORMAT.LEGEND',
  10879. 0x0059: 'FORMAT.TEXT',
  10880. 0x005A: 'EDIT.REPEAT',
  10881. 0x005B: 'PARSE',
  10882. 0x005C: 'JUSTIFY',
  10883. 0x005D: 'HIDE',
  10884. 0x005E: 'UNHIDE',
  10885. 0x005F: 'WORKSPACE',
  10886. 0x0060: 'FORMULA',
  10887. 0x0061: 'FORMULA.FILL',
  10888. 0x0062: 'FORMULA.ARRAY',
  10889. 0x0063: 'DATA.FIND.NEXT',
  10890. 0x0064: 'DATA.FIND.PREV',
  10891. 0x0065: 'FORMULA.FIND.NEXT',
  10892. 0x0066: 'FORMULA.FIND.PREV',
  10893. 0x0067: 'ACTIVATE',
  10894. 0x0068: 'ACTIVATE.NEXT',
  10895. 0x0069: 'ACTIVATE.PREV',
  10896. 0x006A: 'UNLOCKED.NEXT',
  10897. 0x006B: 'UNLOCKED.PREV',
  10898. 0x006C: 'COPY.PICTURE',
  10899. 0x006D: 'SELECT',
  10900. 0x006E: 'DELETE.NAME',
  10901. 0x006F: 'DELETE.FORMAT',
  10902. 0x0070: 'VLINE',
  10903. 0x0071: 'HLINE',
  10904. 0x0072: 'VPAGE',
  10905. 0x0073: 'HPAGE',
  10906. 0x0074: 'VSCROLL',
  10907. 0x0075: 'HSCROLL',
  10908. 0x0076: 'ALERT',
  10909. 0x0077: 'NEW',
  10910. 0x0078: 'CANCEL.COPY',
  10911. 0x0079: 'SHOW.CLIPBOARD',
  10912. 0x007A: 'MESSAGE',
  10913. 0x007C: 'PASTE.LINK',
  10914. 0x007D: 'APP.ACTIVATE',
  10915. 0x007E: 'DELETE.ARROW',
  10916. 0x007F: 'ROW.HEIGHT',
  10917. 0x0080: 'FORMAT.MOVE',
  10918. 0x0081: 'FORMAT.SIZE',
  10919. 0x0082: 'FORMULA.REPLACE',
  10920. 0x0083: 'SEND.KEYS',
  10921. 0x0084: 'SELECT.SPECIAL',
  10922. 0x0085: 'APPLY.NAMES',
  10923. 0x0086: 'REPLACE.FONT',
  10924. 0x0087: 'FREEZE.PANES',
  10925. 0x0088: 'SHOW.INFO',
  10926. 0x0089: 'SPLIT',
  10927. 0x008A: 'ON.WINDOW',
  10928. 0x008B: 'ON.DATA',
  10929. 0x008C: 'DISABLE.INPUT',
  10930. 0x008E: 'OUTLINE',
  10931. 0x008F: 'LIST.NAMES',
  10932. 0x0090: 'FILE.CLOSE',
  10933. 0x0091: 'SAVE.WORKBOOK',
  10934. 0x0092: 'DATA.FORM',
  10935. 0x0093: 'COPY.CHART',
  10936. 0x0094: 'ON.TIME',
  10937. 0x0095: 'WAIT',
  10938. 0x0096: 'FORMAT.FONT',
  10939. 0x0097: 'FILL.UP',
  10940. 0x0098: 'FILL.LEFT',
  10941. 0x0099: 'DELETE.OVERLAY',
  10942. 0x009B: 'SHORT.MENUS',
  10943. 0x009F: 'SET.UPDATE.STATUS',
  10944. 0x00A1: 'COLOR.PALETTE',
  10945. 0x00A2: 'DELETE.STYLE',
  10946. 0x00A3: 'WINDOW.RESTORE',
  10947. 0x00A4: 'WINDOW.MAXIMIZE',
  10948. 0x00A6: 'CHANGE.LINK',
  10949. 0x00A7: 'CALCULATE.DOCUMENT',
  10950. 0x00A8: 'ON.KEY',
  10951. 0x00A9: 'APP.RESTORE',
  10952. 0x00AA: 'APP.MOVE',
  10953. 0x00AB: 'APP.SIZE',
  10954. 0x00AC: 'APP.MINIMIZE',
  10955. 0x00AD: 'APP.MAXIMIZE',
  10956. 0x00AE: 'BRING.TO.FRONT',
  10957. 0x00AF: 'SEND.TO.BACK',
  10958. 0x00B9: 'MAIN.CHART.TYPE',
  10959. 0x00BA: 'OVERLAY.CHART.TYPE',
  10960. 0x00BB: 'SELECT.END',
  10961. 0x00BC: 'OPEN.MAIL',
  10962. 0x00BD: 'SEND.MAIL',
  10963. 0x00BE: 'STANDARD.FONT',
  10964. 0x00BF: 'CONSOLIDATE',
  10965. 0x00C0: 'SORT.SPECIAL',
  10966. 0x00C1: 'GALLERY.3D.AREA',
  10967. 0x00C2: 'GALLERY.3D.COLUMN',
  10968. 0x00C3: 'GALLERY.3D.LINE',
  10969. 0x00C4: 'GALLERY.3D.PIE',
  10970. 0x00C5: 'VIEW.3D',
  10971. 0x00C6: 'GOAL.SEEK',
  10972. 0x00C7: 'WORKGROUP',
  10973. 0x00C8: 'FILL.GROUP',
  10974. 0x00C9: 'UPDATE.LINK',
  10975. 0x00CA: 'PROMOTE',
  10976. 0x00CB: 'DEMOTE',
  10977. 0x00CC: 'SHOW.DETAIL',
  10978. 0x00CE: 'UNGROUP',
  10979. 0x00CF: 'OBJECT.PROPERTIES',
  10980. 0x00D0: 'SAVE.NEW.OBJECT',
  10981. 0x00D1: 'SHARE',
  10982. 0x00D2: 'SHARE.NAME',
  10983. 0x00D3: 'DUPLICATE',
  10984. 0x00D4: 'APPLY.STYLE',
  10985. 0x00D5: 'ASSIGN.TO.OBJECT',
  10986. 0x00D6: 'OBJECT.PROTECTION',
  10987. 0x00D7: 'HIDE.OBJECT',
  10988. 0x00D8: 'SET.EXTRACT',
  10989. 0x00D9: 'CREATE.PUBLISHER',
  10990. 0x00DA: 'SUBSCRIBE.TO',
  10991. 0x00DB: 'ATTRIBUTES',
  10992. 0x00DC: 'SHOW.TOOLBAR',
  10993. 0x00DE: 'PRINT.PREVIEW',
  10994. 0x00DF: 'EDIT.COLOR',
  10995. 0x00E0: 'SHOW.LEVELS',
  10996. 0x00E1: 'FORMAT.MAIN',
  10997. 0x00E2: 'FORMAT.OVERLAY',
  10998. 0x00E3: 'ON.RECALC',
  10999. 0x00E4: 'EDIT.SERIES',
  11000. 0x00E5: 'DEFINE.STYLE',
  11001. 0x00F0: 'LINE.PRINT',
  11002. 0x00F3: 'ENTER.DATA',
  11003. 0x00F9: 'GALLERY.RADAR',
  11004. 0x00FA: 'MERGE.STYLES',
  11005. 0x00FB: 'EDITION.OPTIONS',
  11006. 0x00FC: 'PASTE.PICTURE',
  11007. 0x00FD: 'PASTE.PICTURE.LINK',
  11008. 0x00FE: 'SPELLING',
  11009. 0x0100: 'ZOOM',
  11010. 0x0103: 'INSERT.OBJECT',
  11011. 0x0104: 'WINDOW.MINIMIZE',
  11012. 0x0109: 'SOUND.NOTE',
  11013. 0x010A: 'SOUND.PLAY',
  11014. 0x010B: 'FORMAT.SHAPE',
  11015. 0x010C: 'EXTEND.POLYGON',
  11016. 0x010D: 'FORMAT.AUTO',
  11017. 0x0110: 'GALLERY.3D.BAR',
  11018. 0x0111: 'GALLERY.3D.SURFACE',
  11019. 0x0112: 'FILL.AUTO',
  11020. 0x0114: 'CUSTOMIZE.TOOLBAR',
  11021. 0x0115: 'ADD.TOOL',
  11022. 0x0116: 'EDIT.OBJECT',
  11023. 0x0117: 'ON.DOUBLECLICK',
  11024. 0x0118: 'ON.ENTRY',
  11025. 0x0119: 'WORKBOOK.ADD',
  11026. 0x011A: 'WORKBOOK.MOVE',
  11027. 0x011B: 'WORKBOOK.COPY',
  11028. 0x011C: 'WORKBOOK.OPTIONS',
  11029. 0x011D: 'SAVE.WORKSPACE',
  11030. 0x0120: 'CHART.WIZARD',
  11031. 0x0121: 'DELETE.TOOL',
  11032. 0x0122: 'MOVE.TOOL',
  11033. 0x0123: 'WORKBOOK.SELECT',
  11034. 0x0124: 'WORKBOOK.ACTIVATE',
  11035. 0x0125: 'ASSIGN.TO.TOOL',
  11036. 0x0127: 'COPY.TOOL',
  11037. 0x0128: 'RESET.TOOL',
  11038. 0x0129: 'CONSTRAIN.NUMERIC',
  11039. 0x012A: 'PASTE.TOOL',
  11040. 0x012E: 'WORKBOOK.NEW',
  11041. 0x0131: 'SCENARIO.CELLS',
  11042. 0x0132: 'SCENARIO.DELETE',
  11043. 0x0133: 'SCENARIO.ADD',
  11044. 0x0134: 'SCENARIO.EDIT',
  11045. 0x0135: 'SCENARIO.SHOW',
  11046. 0x0136: 'SCENARIO.SHOW.NEXT',
  11047. 0x0137: 'SCENARIO.SUMMARY',
  11048. 0x0138: 'PIVOT.TABLE.WIZARD',
  11049. 0x0139: 'PIVOT.FIELD.PROPERTIES',
  11050. 0x013A: 'PIVOT.FIELD',
  11051. 0x013B: 'PIVOT.ITEM',
  11052. 0x013C: 'PIVOT.ADD.FIELDS',
  11053. 0x013E: 'OPTIONS.CALCULATION',
  11054. 0x013F: 'OPTIONS.EDIT',
  11055. 0x0140: 'OPTIONS.VIEW',
  11056. 0x0141: 'ADDIN.MANAGER',
  11057. 0x0142: 'MENU.EDITOR',
  11058. 0x0143: 'ATTACH.TOOLBARS',
  11059. 0x0144: 'VBAActivate',
  11060. 0x0145: 'OPTIONS.CHART',
  11061. 0x0148: 'VBA.INSERT.FILE',
  11062. 0x014A: 'VBA.PROCEDURE.DEFINITION',
  11063. 0x0150: 'ROUTING.SLIP',
  11064. 0x0152: 'ROUTE.DOCUMENT',
  11065. 0x0153: 'MAIL.LOGON',
  11066. 0x0156: 'INSERT.PICTURE',
  11067. 0x0157: 'EDIT.TOOL',
  11068. 0x0158: 'GALLERY.DOUGHNUT',
  11069. 0x015E: 'CHART.TREND',
  11070. 0x0160: 'PIVOT.ITEM.PROPERTIES',
  11071. 0x0162: 'WORKBOOK.INSERT',
  11072. 0x0163: 'OPTIONS.TRANSITION',
  11073. 0x0164: 'OPTIONS.GENERAL',
  11074. 0x0172: 'FILTER.ADVANCED',
  11075. 0x0175: 'MAIL.ADD.MAILER',
  11076. 0x0176: 'MAIL.DELETE.MAILER',
  11077. 0x0177: 'MAIL.REPLY',
  11078. 0x0178: 'MAIL.REPLY.ALL',
  11079. 0x0179: 'MAIL.FORWARD',
  11080. 0x017A: 'MAIL.NEXT.LETTER',
  11081. 0x017B: 'DATA.LABEL',
  11082. 0x017C: 'INSERT.TITLE',
  11083. 0x017D: 'FONT.PROPERTIES',
  11084. 0x017E: 'MACRO.OPTIONS',
  11085. 0x017F: 'WORKBOOK.HIDE',
  11086. 0x0180: 'WORKBOOK.UNHIDE',
  11087. 0x0181: 'WORKBOOK.DELETE',
  11088. 0x0182: 'WORKBOOK.NAME',
  11089. 0x0184: 'GALLERY.CUSTOM',
  11090. 0x0186: 'ADD.CHART.AUTOFORMAT',
  11091. 0x0187: 'DELETE.CHART.AUTOFORMAT',
  11092. 0x0188: 'CHART.ADD.DATA',
  11093. 0x0189: 'AUTO.OUTLINE',
  11094. 0x018A: 'TAB.ORDER',
  11095. 0x018B: 'SHOW.DIALOG',
  11096. 0x018C: 'SELECT.ALL',
  11097. 0x018D: 'UNGROUP.SHEETS',
  11098. 0x018E: 'SUBTOTAL.CREATE',
  11099. 0x018F: 'SUBTOTAL.REMOVE',
  11100. 0x0190: 'RENAME.OBJECT',
  11101. 0x019C: 'WORKBOOK.SCROLL',
  11102. 0x019D: 'WORKBOOK.NEXT',
  11103. 0x019E: 'WORKBOOK.PREV',
  11104. 0x019F: 'WORKBOOK.TAB.SPLIT',
  11105. 0x01A0: 'FULL.SCREEN',
  11106. 0x01A1: 'WORKBOOK.PROTECT',
  11107. 0x01A4: 'SCROLLBAR.PROPERTIES',
  11108. 0x01A5: 'PIVOT.SHOW.PAGES',
  11109. 0x01A6: 'TEXT.TO.COLUMNS',
  11110. 0x01A7: 'FORMAT.CHARTTYPE',
  11111. 0x01A8: 'LINK.FORMAT',
  11112. 0x01A9: 'TRACER.DISPLAY',
  11113. 0x01AE: 'TRACER.NAVIGATE',
  11114. 0x01AF: 'TRACER.CLEAR',
  11115. 0x01B0: 'TRACER.ERROR',
  11116. 0x01B1: 'PIVOT.FIELD.GROUP',
  11117. 0x01B2: 'PIVOT.FIELD.UNGROUP',
  11118. 0x01B3: 'CHECKBOX.PROPERTIES',
  11119. 0x01B4: 'LABEL.PROPERTIES',
  11120. 0x01B5: 'LISTBOX.PROPERTIES',
  11121. 0x01B6: 'EDITBOX.PROPERTIES',
  11122. 0x01B7: 'PIVOT.REFRESH',
  11123. 0x01B8: 'LINK.COMBO',
  11124. 0x01B9: 'OPEN.TEXT',
  11125. 0x01BA: 'HIDE.DIALOG',
  11126. 0x01BB: 'SET.DIALOG.FOCUS',
  11127. 0x01BC: 'ENABLE.OBJECT',
  11128. 0x01BD: 'PUSHBUTTON.PROPERTIES',
  11129. 0x01BE: 'SET.DIALOG.DEFAULT',
  11130. 0x01BF: 'FILTER',
  11131. 0x01C0: 'FILTER.SHOW.ALL',
  11132. 0x01C1: 'CLEAR.OUTLINE',
  11133. 0x01C2: 'FUNCTION.WIZARD',
  11134. 0x01C3: 'ADD.LIST.ITEM',
  11135. 0x01C4: 'SET.LIST.ITEM',
  11136. 0x01C5: 'REMOVE.LIST.ITEM',
  11137. 0x01C6: 'SELECT.LIST.ITEM',
  11138. 0x01C7: 'SET.CONTROL.VALUE',
  11139. 0x01C8: 'SAVE.COPY.AS',
  11140. 0x01CA: 'OPTIONS.LISTS.ADD',
  11141. 0x01CB: 'OPTIONS.LISTS.DELETE',
  11142. 0x01CC: 'SERIES.AXES',
  11143. 0x01CD: 'SERIES.X',
  11144. 0x01CE: 'SERIES.Y',
  11145. 0x01CF: 'ERRORBAR.X',
  11146. 0x01D0: 'ERRORBAR.Y',
  11147. 0x01D1: 'FORMAT.CHART',
  11148. 0x01D2: 'SERIES.ORDER',
  11149. 0x01D3: 'MAIL.LOGOFF',
  11150. 0x01D4: 'CLEAR.ROUTING.SLIP',
  11151. 0x01D5: 'APP.ACTIVATE.MICROSOFT',
  11152. 0x01D6: 'MAIL.EDIT.MAILER',
  11153. 0x01D7: 'ON.SHEET',
  11154. 0x01D8: 'STANDARD.WIDTH',
  11155. 0x01D9: 'SCENARIO.MERGE',
  11156. 0x01DA: 'SUMMARY.INFO',
  11157. 0x01DB: 'FIND.FILE',
  11158. 0x01DC: 'ACTIVE.CELL.FONT',
  11159. 0x01DD: 'ENABLE.TIPWIZARD',
  11160. 0x01DE: 'VBA.MAKE.ADDIN',
  11161. 0x01E0: 'INSERTDATATABLE',
  11162. 0x01E1: 'WORKGROUP.OPTIONS',
  11163. 0x01E2: 'MAIL.SEND.MAILER',
  11164. 0x01E5: 'AUTOCORRECT',
  11165. 0x01E9: 'POST.DOCUMENT',
  11166. 0x01EB: 'PICKLIST',
  11167. 0x01ED: 'VIEW.SHOW',
  11168. 0x01EE: 'VIEW.DEFINE',
  11169. 0x01EF: 'VIEW.DELETE',
  11170. 0x01FD: 'SHEET.BACKGROUND',
  11171. 0x01FE: 'INSERT.MAP.OBJECT',
  11172. 0x01FF: 'OPTIONS.MENONO',
  11173. 0x0205: 'MSOCHECKS',
  11174. 0x0206: 'NORMAL',
  11175. 0x0207: 'LAYOUT',
  11176. 0x0208: 'RM.PRINT.AREA',
  11177. 0x0209: 'CLEAR.PRINT.AREA',
  11178. 0x020A: 'ADD.PRINT.AREA',
  11179. 0x020B: 'MOVE.BRK',
  11180. 0x0221: 'HIDECURR.NOTE',
  11181. 0x0222: 'HIDEALL.NOTES',
  11182. 0x0223: 'DELETE.NOTE',
  11183. 0x0224: 'TRAVERSE.NOTES',
  11184. 0x0225: 'ACTIVATE.NOTES',
  11185. 0x026C: 'PROTECT.REVISIONS',
  11186. 0x026D: 'UNPROTECT.REVISIONS',
  11187. 0x0287: 'OPTIONS.ME',
  11188. 0x028D: 'WEB.PUBLISH',
  11189. 0x029B: 'NEWWEBQUERY',
  11190. 0x02A1: 'PIVOT.TABLE.CHART',
  11191. 0x02F1: 'OPTIONS.SAVE',
  11192. 0x02F3: 'OPTIONS.SPELL',
  11193. 0x0328: 'HIDEALL.INKANNOTS'
  11194. };
  11195. /* [MS-XLS] 2.5.198.17 */
  11196. /* [MS-XLSB] 2.5.97.10 */
  11197. var Ftab = {
  11198. 0x0000: 'COUNT',
  11199. 0x0001: 'IF',
  11200. 0x0002: 'ISNA',
  11201. 0x0003: 'ISERROR',
  11202. 0x0004: 'SUM',
  11203. 0x0005: 'AVERAGE',
  11204. 0x0006: 'MIN',
  11205. 0x0007: 'MAX',
  11206. 0x0008: 'ROW',
  11207. 0x0009: 'COLUMN',
  11208. 0x000A: 'NA',
  11209. 0x000B: 'NPV',
  11210. 0x000C: 'STDEV',
  11211. 0x000D: 'DOLLAR',
  11212. 0x000E: 'FIXED',
  11213. 0x000F: 'SIN',
  11214. 0x0010: 'COS',
  11215. 0x0011: 'TAN',
  11216. 0x0012: 'ATAN',
  11217. 0x0013: 'PI',
  11218. 0x0014: 'SQRT',
  11219. 0x0015: 'EXP',
  11220. 0x0016: 'LN',
  11221. 0x0017: 'LOG10',
  11222. 0x0018: 'ABS',
  11223. 0x0019: 'INT',
  11224. 0x001A: 'SIGN',
  11225. 0x001B: 'ROUND',
  11226. 0x001C: 'LOOKUP',
  11227. 0x001D: 'INDEX',
  11228. 0x001E: 'REPT',
  11229. 0x001F: 'MID',
  11230. 0x0020: 'LEN',
  11231. 0x0021: 'VALUE',
  11232. 0x0022: 'TRUE',
  11233. 0x0023: 'FALSE',
  11234. 0x0024: 'AND',
  11235. 0x0025: 'OR',
  11236. 0x0026: 'NOT',
  11237. 0x0027: 'MOD',
  11238. 0x0028: 'DCOUNT',
  11239. 0x0029: 'DSUM',
  11240. 0x002A: 'DAVERAGE',
  11241. 0x002B: 'DMIN',
  11242. 0x002C: 'DMAX',
  11243. 0x002D: 'DSTDEV',
  11244. 0x002E: 'VAR',
  11245. 0x002F: 'DVAR',
  11246. 0x0030: 'TEXT',
  11247. 0x0031: 'LINEST',
  11248. 0x0032: 'TREND',
  11249. 0x0033: 'LOGEST',
  11250. 0x0034: 'GROWTH',
  11251. 0x0035: 'GOTO',
  11252. 0x0036: 'HALT',
  11253. 0x0037: 'RETURN',
  11254. 0x0038: 'PV',
  11255. 0x0039: 'FV',
  11256. 0x003A: 'NPER',
  11257. 0x003B: 'PMT',
  11258. 0x003C: 'RATE',
  11259. 0x003D: 'MIRR',
  11260. 0x003E: 'IRR',
  11261. 0x003F: 'RAND',
  11262. 0x0040: 'MATCH',
  11263. 0x0041: 'DATE',
  11264. 0x0042: 'TIME',
  11265. 0x0043: 'DAY',
  11266. 0x0044: 'MONTH',
  11267. 0x0045: 'YEAR',
  11268. 0x0046: 'WEEKDAY',
  11269. 0x0047: 'HOUR',
  11270. 0x0048: 'MINUTE',
  11271. 0x0049: 'SECOND',
  11272. 0x004A: 'NOW',
  11273. 0x004B: 'AREAS',
  11274. 0x004C: 'ROWS',
  11275. 0x004D: 'COLUMNS',
  11276. 0x004E: 'OFFSET',
  11277. 0x004F: 'ABSREF',
  11278. 0x0050: 'RELREF',
  11279. 0x0051: 'ARGUMENT',
  11280. 0x0052: 'SEARCH',
  11281. 0x0053: 'TRANSPOSE',
  11282. 0x0054: 'ERROR',
  11283. 0x0055: 'STEP',
  11284. 0x0056: 'TYPE',
  11285. 0x0057: 'ECHO',
  11286. 0x0058: 'SET.NAME',
  11287. 0x0059: 'CALLER',
  11288. 0x005A: 'DEREF',
  11289. 0x005B: 'WINDOWS',
  11290. 0x005C: 'SERIES',
  11291. 0x005D: 'DOCUMENTS',
  11292. 0x005E: 'ACTIVE.CELL',
  11293. 0x005F: 'SELECTION',
  11294. 0x0060: 'RESULT',
  11295. 0x0061: 'ATAN2',
  11296. 0x0062: 'ASIN',
  11297. 0x0063: 'ACOS',
  11298. 0x0064: 'CHOOSE',
  11299. 0x0065: 'HLOOKUP',
  11300. 0x0066: 'VLOOKUP',
  11301. 0x0067: 'LINKS',
  11302. 0x0068: 'INPUT',
  11303. 0x0069: 'ISREF',
  11304. 0x006A: 'GET.FORMULA',
  11305. 0x006B: 'GET.NAME',
  11306. 0x006C: 'SET.VALUE',
  11307. 0x006D: 'LOG',
  11308. 0x006E: 'EXEC',
  11309. 0x006F: 'CHAR',
  11310. 0x0070: 'LOWER',
  11311. 0x0071: 'UPPER',
  11312. 0x0072: 'PROPER',
  11313. 0x0073: 'LEFT',
  11314. 0x0074: 'RIGHT',
  11315. 0x0075: 'EXACT',
  11316. 0x0076: 'TRIM',
  11317. 0x0077: 'REPLACE',
  11318. 0x0078: 'SUBSTITUTE',
  11319. 0x0079: 'CODE',
  11320. 0x007A: 'NAMES',
  11321. 0x007B: 'DIRECTORY',
  11322. 0x007C: 'FIND',
  11323. 0x007D: 'CELL',
  11324. 0x007E: 'ISERR',
  11325. 0x007F: 'ISTEXT',
  11326. 0x0080: 'ISNUMBER',
  11327. 0x0081: 'ISBLANK',
  11328. 0x0082: 'T',
  11329. 0x0083: 'N',
  11330. 0x0084: 'FOPEN',
  11331. 0x0085: 'FCLOSE',
  11332. 0x0086: 'FSIZE',
  11333. 0x0087: 'FREADLN',
  11334. 0x0088: 'FREAD',
  11335. 0x0089: 'FWRITELN',
  11336. 0x008A: 'FWRITE',
  11337. 0x008B: 'FPOS',
  11338. 0x008C: 'DATEVALUE',
  11339. 0x008D: 'TIMEVALUE',
  11340. 0x008E: 'SLN',
  11341. 0x008F: 'SYD',
  11342. 0x0090: 'DDB',
  11343. 0x0091: 'GET.DEF',
  11344. 0x0092: 'REFTEXT',
  11345. 0x0093: 'TEXTREF',
  11346. 0x0094: 'INDIRECT',
  11347. 0x0095: 'REGISTER',
  11348. 0x0096: 'CALL',
  11349. 0x0097: 'ADD.BAR',
  11350. 0x0098: 'ADD.MENU',
  11351. 0x0099: 'ADD.COMMAND',
  11352. 0x009A: 'ENABLE.COMMAND',
  11353. 0x009B: 'CHECK.COMMAND',
  11354. 0x009C: 'RENAME.COMMAND',
  11355. 0x009D: 'SHOW.BAR',
  11356. 0x009E: 'DELETE.MENU',
  11357. 0x009F: 'DELETE.COMMAND',
  11358. 0x00A0: 'GET.CHART.ITEM',
  11359. 0x00A1: 'DIALOG.BOX',
  11360. 0x00A2: 'CLEAN',
  11361. 0x00A3: 'MDETERM',
  11362. 0x00A4: 'MINVERSE',
  11363. 0x00A5: 'MMULT',
  11364. 0x00A6: 'FILES',
  11365. 0x00A7: 'IPMT',
  11366. 0x00A8: 'PPMT',
  11367. 0x00A9: 'COUNTA',
  11368. 0x00AA: 'CANCEL.KEY',
  11369. 0x00AB: 'FOR',
  11370. 0x00AC: 'WHILE',
  11371. 0x00AD: 'BREAK',
  11372. 0x00AE: 'NEXT',
  11373. 0x00AF: 'INITIATE',
  11374. 0x00B0: 'REQUEST',
  11375. 0x00B1: 'POKE',
  11376. 0x00B2: 'EXECUTE',
  11377. 0x00B3: 'TERMINATE',
  11378. 0x00B4: 'RESTART',
  11379. 0x00B5: 'HELP',
  11380. 0x00B6: 'GET.BAR',
  11381. 0x00B7: 'PRODUCT',
  11382. 0x00B8: 'FACT',
  11383. 0x00B9: 'GET.CELL',
  11384. 0x00BA: 'GET.WORKSPACE',
  11385. 0x00BB: 'GET.WINDOW',
  11386. 0x00BC: 'GET.DOCUMENT',
  11387. 0x00BD: 'DPRODUCT',
  11388. 0x00BE: 'ISNONTEXT',
  11389. 0x00BF: 'GET.NOTE',
  11390. 0x00C0: 'NOTE',
  11391. 0x00C1: 'STDEVP',
  11392. 0x00C2: 'VARP',
  11393. 0x00C3: 'DSTDEVP',
  11394. 0x00C4: 'DVARP',
  11395. 0x00C5: 'TRUNC',
  11396. 0x00C6: 'ISLOGICAL',
  11397. 0x00C7: 'DCOUNTA',
  11398. 0x00C8: 'DELETE.BAR',
  11399. 0x00C9: 'UNREGISTER',
  11400. 0x00CC: 'USDOLLAR',
  11401. 0x00CD: 'FINDB',
  11402. 0x00CE: 'SEARCHB',
  11403. 0x00CF: 'REPLACEB',
  11404. 0x00D0: 'LEFTB',
  11405. 0x00D1: 'RIGHTB',
  11406. 0x00D2: 'MIDB',
  11407. 0x00D3: 'LENB',
  11408. 0x00D4: 'ROUNDUP',
  11409. 0x00D5: 'ROUNDDOWN',
  11410. 0x00D6: 'ASC',
  11411. 0x00D7: 'DBCS',
  11412. 0x00D8: 'RANK',
  11413. 0x00DB: 'ADDRESS',
  11414. 0x00DC: 'DAYS360',
  11415. 0x00DD: 'TODAY',
  11416. 0x00DE: 'VDB',
  11417. 0x00DF: 'ELSE',
  11418. 0x00E0: 'ELSE.IF',
  11419. 0x00E1: 'END.IF',
  11420. 0x00E2: 'FOR.CELL',
  11421. 0x00E3: 'MEDIAN',
  11422. 0x00E4: 'SUMPRODUCT',
  11423. 0x00E5: 'SINH',
  11424. 0x00E6: 'COSH',
  11425. 0x00E7: 'TANH',
  11426. 0x00E8: 'ASINH',
  11427. 0x00E9: 'ACOSH',
  11428. 0x00EA: 'ATANH',
  11429. 0x00EB: 'DGET',
  11430. 0x00EC: 'CREATE.OBJECT',
  11431. 0x00ED: 'VOLATILE',
  11432. 0x00EE: 'LAST.ERROR',
  11433. 0x00EF: 'CUSTOM.UNDO',
  11434. 0x00F0: 'CUSTOM.REPEAT',
  11435. 0x00F1: 'FORMULA.CONVERT',
  11436. 0x00F2: 'GET.LINK.INFO',
  11437. 0x00F3: 'TEXT.BOX',
  11438. 0x00F4: 'INFO',
  11439. 0x00F5: 'GROUP',
  11440. 0x00F6: 'GET.OBJECT',
  11441. 0x00F7: 'DB',
  11442. 0x00F8: 'PAUSE',
  11443. 0x00FB: 'RESUME',
  11444. 0x00FC: 'FREQUENCY',
  11445. 0x00FD: 'ADD.TOOLBAR',
  11446. 0x00FE: 'DELETE.TOOLBAR',
  11447. 0x00FF: 'User',
  11448. 0x0100: 'RESET.TOOLBAR',
  11449. 0x0101: 'EVALUATE',
  11450. 0x0102: 'GET.TOOLBAR',
  11451. 0x0103: 'GET.TOOL',
  11452. 0x0104: 'SPELLING.CHECK',
  11453. 0x0105: 'ERROR.TYPE',
  11454. 0x0106: 'APP.TITLE',
  11455. 0x0107: 'WINDOW.TITLE',
  11456. 0x0108: 'SAVE.TOOLBAR',
  11457. 0x0109: 'ENABLE.TOOL',
  11458. 0x010A: 'PRESS.TOOL',
  11459. 0x010B: 'REGISTER.ID',
  11460. 0x010C: 'GET.WORKBOOK',
  11461. 0x010D: 'AVEDEV',
  11462. 0x010E: 'BETADIST',
  11463. 0x010F: 'GAMMALN',
  11464. 0x0110: 'BETAINV',
  11465. 0x0111: 'BINOMDIST',
  11466. 0x0112: 'CHIDIST',
  11467. 0x0113: 'CHIINV',
  11468. 0x0114: 'COMBIN',
  11469. 0x0115: 'CONFIDENCE',
  11470. 0x0116: 'CRITBINOM',
  11471. 0x0117: 'EVEN',
  11472. 0x0118: 'EXPONDIST',
  11473. 0x0119: 'FDIST',
  11474. 0x011A: 'FINV',
  11475. 0x011B: 'FISHER',
  11476. 0x011C: 'FISHERINV',
  11477. 0x011D: 'FLOOR',
  11478. 0x011E: 'GAMMADIST',
  11479. 0x011F: 'GAMMAINV',
  11480. 0x0120: 'CEILING',
  11481. 0x0121: 'HYPGEOMDIST',
  11482. 0x0122: 'LOGNORMDIST',
  11483. 0x0123: 'LOGINV',
  11484. 0x0124: 'NEGBINOMDIST',
  11485. 0x0125: 'NORMDIST',
  11486. 0x0126: 'NORMSDIST',
  11487. 0x0127: 'NORMINV',
  11488. 0x0128: 'NORMSINV',
  11489. 0x0129: 'STANDARDIZE',
  11490. 0x012A: 'ODD',
  11491. 0x012B: 'PERMUT',
  11492. 0x012C: 'POISSON',
  11493. 0x012D: 'TDIST',
  11494. 0x012E: 'WEIBULL',
  11495. 0x012F: 'SUMXMY2',
  11496. 0x0130: 'SUMX2MY2',
  11497. 0x0131: 'SUMX2PY2',
  11498. 0x0132: 'CHITEST',
  11499. 0x0133: 'CORREL',
  11500. 0x0134: 'COVAR',
  11501. 0x0135: 'FORECAST',
  11502. 0x0136: 'FTEST',
  11503. 0x0137: 'INTERCEPT',
  11504. 0x0138: 'PEARSON',
  11505. 0x0139: 'RSQ',
  11506. 0x013A: 'STEYX',
  11507. 0x013B: 'SLOPE',
  11508. 0x013C: 'TTEST',
  11509. 0x013D: 'PROB',
  11510. 0x013E: 'DEVSQ',
  11511. 0x013F: 'GEOMEAN',
  11512. 0x0140: 'HARMEAN',
  11513. 0x0141: 'SUMSQ',
  11514. 0x0142: 'KURT',
  11515. 0x0143: 'SKEW',
  11516. 0x0144: 'ZTEST',
  11517. 0x0145: 'LARGE',
  11518. 0x0146: 'SMALL',
  11519. 0x0147: 'QUARTILE',
  11520. 0x0148: 'PERCENTILE',
  11521. 0x0149: 'PERCENTRANK',
  11522. 0x014A: 'MODE',
  11523. 0x014B: 'TRIMMEAN',
  11524. 0x014C: 'TINV',
  11525. 0x014E: 'MOVIE.COMMAND',
  11526. 0x014F: 'GET.MOVIE',
  11527. 0x0150: 'CONCATENATE',
  11528. 0x0151: 'POWER',
  11529. 0x0152: 'PIVOT.ADD.DATA',
  11530. 0x0153: 'GET.PIVOT.TABLE',
  11531. 0x0154: 'GET.PIVOT.FIELD',
  11532. 0x0155: 'GET.PIVOT.ITEM',
  11533. 0x0156: 'RADIANS',
  11534. 0x0157: 'DEGREES',
  11535. 0x0158: 'SUBTOTAL',
  11536. 0x0159: 'SUMIF',
  11537. 0x015A: 'COUNTIF',
  11538. 0x015B: 'COUNTBLANK',
  11539. 0x015C: 'SCENARIO.GET',
  11540. 0x015D: 'OPTIONS.LISTS.GET',
  11541. 0x015E: 'ISPMT',
  11542. 0x015F: 'DATEDIF',
  11543. 0x0160: 'DATESTRING',
  11544. 0x0161: 'NUMBERSTRING',
  11545. 0x0162: 'ROMAN',
  11546. 0x0163: 'OPEN.DIALOG',
  11547. 0x0164: 'SAVE.DIALOG',
  11548. 0x0165: 'VIEW.GET',
  11549. 0x0166: 'GETPIVOTDATA',
  11550. 0x0167: 'HYPERLINK',
  11551. 0x0168: 'PHONETIC',
  11552. 0x0169: 'AVERAGEA',
  11553. 0x016A: 'MAXA',
  11554. 0x016B: 'MINA',
  11555. 0x016C: 'STDEVPA',
  11556. 0x016D: 'VARPA',
  11557. 0x016E: 'STDEVA',
  11558. 0x016F: 'VARA',
  11559. 0x0170: 'BAHTTEXT',
  11560. 0x0171: 'THAIDAYOFWEEK',
  11561. 0x0172: 'THAIDIGIT',
  11562. 0x0173: 'THAIMONTHOFYEAR',
  11563. 0x0174: 'THAINUMSOUND',
  11564. 0x0175: 'THAINUMSTRING',
  11565. 0x0176: 'THAISTRINGLENGTH',
  11566. 0x0177: 'ISTHAIDIGIT',
  11567. 0x0178: 'ROUNDBAHTDOWN',
  11568. 0x0179: 'ROUNDBAHTUP',
  11569. 0x017A: 'THAIYEAR',
  11570. 0x017B: 'RTD',
  11571. 0x017C: 'CUBEVALUE',
  11572. 0x017D: 'CUBEMEMBER',
  11573. 0x017E: 'CUBEMEMBERPROPERTY',
  11574. 0x017F: 'CUBERANKEDMEMBER',
  11575. 0x0180: 'HEX2BIN',
  11576. 0x0181: 'HEX2DEC',
  11577. 0x0182: 'HEX2OCT',
  11578. 0x0183: 'DEC2BIN',
  11579. 0x0184: 'DEC2HEX',
  11580. 0x0185: 'DEC2OCT',
  11581. 0x0186: 'OCT2BIN',
  11582. 0x0187: 'OCT2HEX',
  11583. 0x0188: 'OCT2DEC',
  11584. 0x0189: 'BIN2DEC',
  11585. 0x018A: 'BIN2OCT',
  11586. 0x018B: 'BIN2HEX',
  11587. 0x018C: 'IMSUB',
  11588. 0x018D: 'IMDIV',
  11589. 0x018E: 'IMPOWER',
  11590. 0x018F: 'IMABS',
  11591. 0x0190: 'IMSQRT',
  11592. 0x0191: 'IMLN',
  11593. 0x0192: 'IMLOG2',
  11594. 0x0193: 'IMLOG10',
  11595. 0x0194: 'IMSIN',
  11596. 0x0195: 'IMCOS',
  11597. 0x0196: 'IMEXP',
  11598. 0x0197: 'IMARGUMENT',
  11599. 0x0198: 'IMCONJUGATE',
  11600. 0x0199: 'IMAGINARY',
  11601. 0x019A: 'IMREAL',
  11602. 0x019B: 'COMPLEX',
  11603. 0x019C: 'IMSUM',
  11604. 0x019D: 'IMPRODUCT',
  11605. 0x019E: 'SERIESSUM',
  11606. 0x019F: 'FACTDOUBLE',
  11607. 0x01A0: 'SQRTPI',
  11608. 0x01A1: 'QUOTIENT',
  11609. 0x01A2: 'DELTA',
  11610. 0x01A3: 'GESTEP',
  11611. 0x01A4: 'ISEVEN',
  11612. 0x01A5: 'ISODD',
  11613. 0x01A6: 'MROUND',
  11614. 0x01A7: 'ERF',
  11615. 0x01A8: 'ERFC',
  11616. 0x01A9: 'BESSELJ',
  11617. 0x01AA: 'BESSELK',
  11618. 0x01AB: 'BESSELY',
  11619. 0x01AC: 'BESSELI',
  11620. 0x01AD: 'XIRR',
  11621. 0x01AE: 'XNPV',
  11622. 0x01AF: 'PRICEMAT',
  11623. 0x01B0: 'YIELDMAT',
  11624. 0x01B1: 'INTRATE',
  11625. 0x01B2: 'RECEIVED',
  11626. 0x01B3: 'DISC',
  11627. 0x01B4: 'PRICEDISC',
  11628. 0x01B5: 'YIELDDISC',
  11629. 0x01B6: 'TBILLEQ',
  11630. 0x01B7: 'TBILLPRICE',
  11631. 0x01B8: 'TBILLYIELD',
  11632. 0x01B9: 'PRICE',
  11633. 0x01BA: 'YIELD',
  11634. 0x01BB: 'DOLLARDE',
  11635. 0x01BC: 'DOLLARFR',
  11636. 0x01BD: 'NOMINAL',
  11637. 0x01BE: 'EFFECT',
  11638. 0x01BF: 'CUMPRINC',
  11639. 0x01C0: 'CUMIPMT',
  11640. 0x01C1: 'EDATE',
  11641. 0x01C2: 'EOMONTH',
  11642. 0x01C3: 'YEARFRAC',
  11643. 0x01C4: 'COUPDAYBS',
  11644. 0x01C5: 'COUPDAYS',
  11645. 0x01C6: 'COUPDAYSNC',
  11646. 0x01C7: 'COUPNCD',
  11647. 0x01C8: 'COUPNUM',
  11648. 0x01C9: 'COUPPCD',
  11649. 0x01CA: 'DURATION',
  11650. 0x01CB: 'MDURATION',
  11651. 0x01CC: 'ODDLPRICE',
  11652. 0x01CD: 'ODDLYIELD',
  11653. 0x01CE: 'ODDFPRICE',
  11654. 0x01CF: 'ODDFYIELD',
  11655. 0x01D0: 'RANDBETWEEN',
  11656. 0x01D1: 'WEEKNUM',
  11657. 0x01D2: 'AMORDEGRC',
  11658. 0x01D3: 'AMORLINC',
  11659. 0x01D4: 'CONVERT',
  11660. 0x02D4: 'SHEETJS',
  11661. 0x01D5: 'ACCRINT',
  11662. 0x01D6: 'ACCRINTM',
  11663. 0x01D7: 'WORKDAY',
  11664. 0x01D8: 'NETWORKDAYS',
  11665. 0x01D9: 'GCD',
  11666. 0x01DA: 'MULTINOMIAL',
  11667. 0x01DB: 'LCM',
  11668. 0x01DC: 'FVSCHEDULE',
  11669. 0x01DD: 'CUBEKPIMEMBER',
  11670. 0x01DE: 'CUBESET',
  11671. 0x01DF: 'CUBESETCOUNT',
  11672. 0x01E0: 'IFERROR',
  11673. 0x01E1: 'COUNTIFS',
  11674. 0x01E2: 'SUMIFS',
  11675. 0x01E3: 'AVERAGEIF',
  11676. 0x01E4: 'AVERAGEIFS'
  11677. };
  11678. var FtabArgc = {
  11679. 0x0002: 1, /* ISNA */
  11680. 0x0003: 1, /* ISERROR */
  11681. 0x000A: 0, /* NA */
  11682. 0x000F: 1, /* SIN */
  11683. 0x0010: 1, /* COS */
  11684. 0x0011: 1, /* TAN */
  11685. 0x0012: 1, /* ATAN */
  11686. 0x0013: 0, /* PI */
  11687. 0x0014: 1, /* SQRT */
  11688. 0x0015: 1, /* EXP */
  11689. 0x0016: 1, /* LN */
  11690. 0x0017: 1, /* LOG10 */
  11691. 0x0018: 1, /* ABS */
  11692. 0x0019: 1, /* INT */
  11693. 0x001A: 1, /* SIGN */
  11694. 0x001B: 2, /* ROUND */
  11695. 0x001E: 2, /* REPT */
  11696. 0x001F: 3, /* MID */
  11697. 0x0020: 1, /* LEN */
  11698. 0x0021: 1, /* VALUE */
  11699. 0x0022: 0, /* TRUE */
  11700. 0x0023: 0, /* FALSE */
  11701. 0x0026: 1, /* NOT */
  11702. 0x0027: 2, /* MOD */
  11703. 0x0028: 3, /* DCOUNT */
  11704. 0x0029: 3, /* DSUM */
  11705. 0x002A: 3, /* DAVERAGE */
  11706. 0x002B: 3, /* DMIN */
  11707. 0x002C: 3, /* DMAX */
  11708. 0x002D: 3, /* DSTDEV */
  11709. 0x002F: 3, /* DVAR */
  11710. 0x0030: 2, /* TEXT */
  11711. 0x0035: 1, /* GOTO */
  11712. 0x003D: 3, /* MIRR */
  11713. 0x003F: 0, /* RAND */
  11714. 0x0041: 3, /* DATE */
  11715. 0x0042: 3, /* TIME */
  11716. 0x0043: 1, /* DAY */
  11717. 0x0044: 1, /* MONTH */
  11718. 0x0045: 1, /* YEAR */
  11719. 0x0046: 1, /* WEEKDAY */
  11720. 0x0047: 1, /* HOUR */
  11721. 0x0048: 1, /* MINUTE */
  11722. 0x0049: 1, /* SECOND */
  11723. 0x004A: 0, /* NOW */
  11724. 0x004B: 1, /* AREAS */
  11725. 0x004C: 1, /* ROWS */
  11726. 0x004D: 1, /* COLUMNS */
  11727. 0x004F: 2, /* ABSREF */
  11728. 0x0050: 2, /* RELREF */
  11729. 0x0053: 1, /* TRANSPOSE */
  11730. 0x0055: 0, /* STEP */
  11731. 0x0056: 1, /* TYPE */
  11732. 0x0059: 0, /* CALLER */
  11733. 0x005A: 1, /* DEREF */
  11734. 0x005E: 0, /* ACTIVE.CELL */
  11735. 0x005F: 0, /* SELECTION */
  11736. 0x0061: 2, /* ATAN2 */
  11737. 0x0062: 1, /* ASIN */
  11738. 0x0063: 1, /* ACOS */
  11739. 0x0065: 3, /* HLOOKUP */
  11740. 0x0066: 3, /* VLOOKUP */
  11741. 0x0069: 1, /* ISREF */
  11742. 0x006A: 1, /* GET.FORMULA */
  11743. 0x006C: 2, /* SET.VALUE */
  11744. 0x006F: 1, /* CHAR */
  11745. 0x0070: 1, /* LOWER */
  11746. 0x0071: 1, /* UPPER */
  11747. 0x0072: 1, /* PROPER */
  11748. 0x0075: 2, /* EXACT */
  11749. 0x0076: 1, /* TRIM */
  11750. 0x0077: 4, /* REPLACE */
  11751. 0x0079: 1, /* CODE */
  11752. 0x007E: 1, /* ISERR */
  11753. 0x007F: 1, /* ISTEXT */
  11754. 0x0080: 1, /* ISNUMBER */
  11755. 0x0081: 1, /* ISBLANK */
  11756. 0x0082: 1, /* T */
  11757. 0x0083: 1, /* N */
  11758. 0x0085: 1, /* FCLOSE */
  11759. 0x0086: 1, /* FSIZE */
  11760. 0x0087: 1, /* FREADLN */
  11761. 0x0088: 2, /* FREAD */
  11762. 0x0089: 2, /* FWRITELN */
  11763. 0x008A: 2, /* FWRITE */
  11764. 0x008C: 1, /* DATEVALUE */
  11765. 0x008D: 1, /* TIMEVALUE */
  11766. 0x008E: 3, /* SLN */
  11767. 0x008F: 4, /* SYD */
  11768. 0x0090: 4, /* DDB */
  11769. 0x00A1: 1, /* DIALOG.BOX */
  11770. 0x00A2: 1, /* CLEAN */
  11771. 0x00A3: 1, /* MDETERM */
  11772. 0x00A4: 1, /* MINVERSE */
  11773. 0x00A5: 2, /* MMULT */
  11774. 0x00AC: 1, /* WHILE */
  11775. 0x00AF: 2, /* INITIATE */
  11776. 0x00B0: 2, /* REQUEST */
  11777. 0x00B1: 3, /* POKE */
  11778. 0x00B2: 2, /* EXECUTE */
  11779. 0x00B3: 1, /* TERMINATE */
  11780. 0x00B8: 1, /* FACT */
  11781. 0x00BA: 1, /* GET.WORKSPACE */
  11782. 0x00BD: 3, /* DPRODUCT */
  11783. 0x00BE: 1, /* ISNONTEXT */
  11784. 0x00C3: 3, /* DSTDEVP */
  11785. 0x00C4: 3, /* DVARP */
  11786. 0x00C5: 1, /* TRUNC */
  11787. 0x00C6: 1, /* ISLOGICAL */
  11788. 0x00C7: 3, /* DCOUNTA */
  11789. 0x00C9: 1, /* UNREGISTER */
  11790. 0x00CF: 4, /* REPLACEB */
  11791. 0x00D2: 3, /* MIDB */
  11792. 0x00D3: 1, /* LENB */
  11793. 0x00D4: 2, /* ROUNDUP */
  11794. 0x00D5: 2, /* ROUNDDOWN */
  11795. 0x00D6: 1, /* ASC */
  11796. 0x00D7: 1, /* DBCS */
  11797. 0x00E1: 0, /* END.IF */
  11798. 0x00E5: 1, /* SINH */
  11799. 0x00E6: 1, /* COSH */
  11800. 0x00E7: 1, /* TANH */
  11801. 0x00E8: 1, /* ASINH */
  11802. 0x00E9: 1, /* ACOSH */
  11803. 0x00EA: 1, /* ATANH */
  11804. 0x00EB: 3, /* DGET */
  11805. 0x00F4: 1, /* INFO */
  11806. 0x00F7: 4, /* DB */
  11807. 0x00FC: 2, /* FREQUENCY */
  11808. 0x0101: 1, /* EVALUATE */
  11809. 0x0105: 1, /* ERROR.TYPE */
  11810. 0x010F: 1, /* GAMMALN */
  11811. 0x0111: 4, /* BINOMDIST */
  11812. 0x0112: 2, /* CHIDIST */
  11813. 0x0113: 2, /* CHIINV */
  11814. 0x0114: 2, /* COMBIN */
  11815. 0x0115: 3, /* CONFIDENCE */
  11816. 0x0116: 3, /* CRITBINOM */
  11817. 0x0117: 1, /* EVEN */
  11818. 0x0118: 3, /* EXPONDIST */
  11819. 0x0119: 3, /* FDIST */
  11820. 0x011A: 3, /* FINV */
  11821. 0x011B: 1, /* FISHER */
  11822. 0x011C: 1, /* FISHERINV */
  11823. 0x011D: 2, /* FLOOR */
  11824. 0x011E: 4, /* GAMMADIST */
  11825. 0x011F: 3, /* GAMMAINV */
  11826. 0x0120: 2, /* CEILING */
  11827. 0x0121: 4, /* HYPGEOMDIST */
  11828. 0x0122: 3, /* LOGNORMDIST */
  11829. 0x0123: 3, /* LOGINV */
  11830. 0x0124: 3, /* NEGBINOMDIST */
  11831. 0x0125: 4, /* NORMDIST */
  11832. 0x0126: 1, /* NORMSDIST */
  11833. 0x0127: 3, /* NORMINV */
  11834. 0x0128: 1, /* NORMSINV */
  11835. 0x0129: 3, /* STANDARDIZE */
  11836. 0x012A: 1, /* ODD */
  11837. 0x012B: 2, /* PERMUT */
  11838. 0x012C: 3, /* POISSON */
  11839. 0x012D: 3, /* TDIST */
  11840. 0x012E: 4, /* WEIBULL */
  11841. 0x012F: 2, /* SUMXMY2 */
  11842. 0x0130: 2, /* SUMX2MY2 */
  11843. 0x0131: 2, /* SUMX2PY2 */
  11844. 0x0132: 2, /* CHITEST */
  11845. 0x0133: 2, /* CORREL */
  11846. 0x0134: 2, /* COVAR */
  11847. 0x0135: 3, /* FORECAST */
  11848. 0x0136: 2, /* FTEST */
  11849. 0x0137: 2, /* INTERCEPT */
  11850. 0x0138: 2, /* PEARSON */
  11851. 0x0139: 2, /* RSQ */
  11852. 0x013A: 2, /* STEYX */
  11853. 0x013B: 2, /* SLOPE */
  11854. 0x013C: 4, /* TTEST */
  11855. 0x0145: 2, /* LARGE */
  11856. 0x0146: 2, /* SMALL */
  11857. 0x0147: 2, /* QUARTILE */
  11858. 0x0148: 2, /* PERCENTILE */
  11859. 0x014B: 2, /* TRIMMEAN */
  11860. 0x014C: 2, /* TINV */
  11861. 0x0151: 2, /* POWER */
  11862. 0x0156: 1, /* RADIANS */
  11863. 0x0157: 1, /* DEGREES */
  11864. 0x015A: 2, /* COUNTIF */
  11865. 0x015B: 1, /* COUNTBLANK */
  11866. 0x015E: 4, /* ISPMT */
  11867. 0x015F: 3, /* DATEDIF */
  11868. 0x0160: 1, /* DATESTRING */
  11869. 0x0161: 2, /* NUMBERSTRING */
  11870. 0x0168: 1, /* PHONETIC */
  11871. 0x0170: 1, /* BAHTTEXT */
  11872. 0x0171: 1, /* THAIDAYOFWEEK */
  11873. 0x0172: 1, /* THAIDIGIT */
  11874. 0x0173: 1, /* THAIMONTHOFYEAR */
  11875. 0x0174: 1, /* THAINUMSOUND */
  11876. 0x0175: 1, /* THAINUMSTRING */
  11877. 0x0176: 1, /* THAISTRINGLENGTH */
  11878. 0x0177: 1, /* ISTHAIDIGIT */
  11879. 0x0178: 1, /* ROUNDBAHTDOWN */
  11880. 0x0179: 1, /* ROUNDBAHTUP */
  11881. 0x017A: 1, /* THAIYEAR */
  11882. 0x017E: 3, /* CUBEMEMBERPROPERTY */
  11883. 0x0181: 1, /* HEX2DEC */
  11884. 0x0188: 1, /* OCT2DEC */
  11885. 0x0189: 1, /* BIN2DEC */
  11886. 0x018C: 2, /* IMSUB */
  11887. 0x018D: 2, /* IMDIV */
  11888. 0x018E: 2, /* IMPOWER */
  11889. 0x018F: 1, /* IMABS */
  11890. 0x0190: 1, /* IMSQRT */
  11891. 0x0191: 1, /* IMLN */
  11892. 0x0192: 1, /* IMLOG2 */
  11893. 0x0193: 1, /* IMLOG10 */
  11894. 0x0194: 1, /* IMSIN */
  11895. 0x0195: 1, /* IMCOS */
  11896. 0x0196: 1, /* IMEXP */
  11897. 0x0197: 1, /* IMARGUMENT */
  11898. 0x0198: 1, /* IMCONJUGATE */
  11899. 0x0199: 1, /* IMAGINARY */
  11900. 0x019A: 1, /* IMREAL */
  11901. 0x019E: 4, /* SERIESSUM */
  11902. 0x019F: 1, /* FACTDOUBLE */
  11903. 0x01A0: 1, /* SQRTPI */
  11904. 0x01A1: 2, /* QUOTIENT */
  11905. 0x01A4: 1, /* ISEVEN */
  11906. 0x01A5: 1, /* ISODD */
  11907. 0x01A6: 2, /* MROUND */
  11908. 0x01A8: 1, /* ERFC */
  11909. 0x01A9: 2, /* BESSELJ */
  11910. 0x01AA: 2, /* BESSELK */
  11911. 0x01AB: 2, /* BESSELY */
  11912. 0x01AC: 2, /* BESSELI */
  11913. 0x01AE: 3, /* XNPV */
  11914. 0x01B6: 3, /* TBILLEQ */
  11915. 0x01B7: 3, /* TBILLPRICE */
  11916. 0x01B8: 3, /* TBILLYIELD */
  11917. 0x01BB: 2, /* DOLLARDE */
  11918. 0x01BC: 2, /* DOLLARFR */
  11919. 0x01BD: 2, /* NOMINAL */
  11920. 0x01BE: 2, /* EFFECT */
  11921. 0x01BF: 6, /* CUMPRINC */
  11922. 0x01C0: 6, /* CUMIPMT */
  11923. 0x01C1: 2, /* EDATE */
  11924. 0x01C2: 2, /* EOMONTH */
  11925. 0x01D0: 2, /* RANDBETWEEN */
  11926. 0x01D4: 3, /* CONVERT */
  11927. 0x01DC: 2, /* FVSCHEDULE */
  11928. 0x01DF: 1, /* CUBESETCOUNT */
  11929. 0x01E0: 2, /* IFERROR */
  11930. 0xFFFF: 0
  11931. };
  11932. /* [MS-XLSX] 2.2.3 Functions */
  11933. /* [MS-XLSB] 2.5.97.10 Ftab */
  11934. var XLSXFutureFunctions = {
  11935. "_xlfn.ACOT": "ACOT",
  11936. "_xlfn.ACOTH": "ACOTH",
  11937. "_xlfn.AGGREGATE": "AGGREGATE",
  11938. "_xlfn.ARABIC": "ARABIC",
  11939. "_xlfn.AVERAGEIF": "AVERAGEIF",
  11940. "_xlfn.AVERAGEIFS": "AVERAGEIFS",
  11941. "_xlfn.BASE": "BASE",
  11942. "_xlfn.BETA.DIST": "BETA.DIST",
  11943. "_xlfn.BETA.INV": "BETA.INV",
  11944. "_xlfn.BINOM.DIST": "BINOM.DIST",
  11945. "_xlfn.BINOM.DIST.RANGE": "BINOM.DIST.RANGE",
  11946. "_xlfn.BINOM.INV": "BINOM.INV",
  11947. "_xlfn.BITAND": "BITAND",
  11948. "_xlfn.BITLSHIFT": "BITLSHIFT",
  11949. "_xlfn.BITOR": "BITOR",
  11950. "_xlfn.BITRSHIFT": "BITRSHIFT",
  11951. "_xlfn.BITXOR": "BITXOR",
  11952. "_xlfn.CEILING.MATH": "CEILING.MATH",
  11953. "_xlfn.CEILING.PRECISE": "CEILING.PRECISE",
  11954. "_xlfn.CHISQ.DIST": "CHISQ.DIST",
  11955. "_xlfn.CHISQ.DIST.RT": "CHISQ.DIST.RT",
  11956. "_xlfn.CHISQ.INV": "CHISQ.INV",
  11957. "_xlfn.CHISQ.INV.RT": "CHISQ.INV.RT",
  11958. "_xlfn.CHISQ.TEST": "CHISQ.TEST",
  11959. "_xlfn.COMBINA": "COMBINA",
  11960. "_xlfn.CONCAT": "CONCAT",
  11961. "_xlfn.CONFIDENCE.NORM": "CONFIDENCE.NORM",
  11962. "_xlfn.CONFIDENCE.T": "CONFIDENCE.T",
  11963. "_xlfn.COT": "COT",
  11964. "_xlfn.COTH": "COTH",
  11965. "_xlfn.COUNTIFS": "COUNTIFS",
  11966. "_xlfn.COVARIANCE.P": "COVARIANCE.P",
  11967. "_xlfn.COVARIANCE.S": "COVARIANCE.S",
  11968. "_xlfn.CSC": "CSC",
  11969. "_xlfn.CSCH": "CSCH",
  11970. "_xlfn.DAYS": "DAYS",
  11971. "_xlfn.DECIMAL": "DECIMAL",
  11972. "_xlfn.ECMA.CEILING": "ECMA.CEILING",
  11973. "_xlfn.ERF.PRECISE": "ERF.PRECISE",
  11974. "_xlfn.ERFC.PRECISE": "ERFC.PRECISE",
  11975. "_xlfn.EXPON.DIST": "EXPON.DIST",
  11976. "_xlfn.F.DIST": "F.DIST",
  11977. "_xlfn.F.DIST.RT": "F.DIST.RT",
  11978. "_xlfn.F.INV": "F.INV",
  11979. "_xlfn.F.INV.RT": "F.INV.RT",
  11980. "_xlfn.F.TEST": "F.TEST",
  11981. "_xlfn.FILTERXML": "FILTERXML",
  11982. "_xlfn.FLOOR.MATH": "FLOOR.MATH",
  11983. "_xlfn.FLOOR.PRECISE": "FLOOR.PRECISE",
  11984. "_xlfn.FORECAST.ETS": "FORECAST.ETS",
  11985. "_xlfn.FORECAST.ETS.CONFINT": "FORECAST.ETS.CONFINT",
  11986. "_xlfn.FORECAST.ETS.SEASONALITY": "FORECAST.ETS.SEASONALITY",
  11987. "_xlfn.FORECAST.ETS.STAT": "FORECAST.ETS.STAT",
  11988. "_xlfn.FORECAST.LINEAR": "FORECAST.LINEAR",
  11989. "_xlfn.FORMULATEXT": "FORMULATEXT",
  11990. "_xlfn.GAMMA": "GAMMA",
  11991. "_xlfn.GAMMA.DIST": "GAMMA.DIST",
  11992. "_xlfn.GAMMA.INV": "GAMMA.INV",
  11993. "_xlfn.GAMMALN.PRECISE": "GAMMALN.PRECISE",
  11994. "_xlfn.GAUSS": "GAUSS",
  11995. "_xlfn.HYPGEOM.DIST": "HYPGEOM.DIST",
  11996. "_xlfn.IFERROR": "IFERROR",
  11997. "_xlfn.IFNA": "IFNA",
  11998. "_xlfn.IFS": "IFS",
  11999. "_xlfn.IMCOSH": "IMCOSH",
  12000. "_xlfn.IMCOT": "IMCOT",
  12001. "_xlfn.IMCSC": "IMCSC",
  12002. "_xlfn.IMCSCH": "IMCSCH",
  12003. "_xlfn.IMSEC": "IMSEC",
  12004. "_xlfn.IMSECH": "IMSECH",
  12005. "_xlfn.IMSINH": "IMSINH",
  12006. "_xlfn.IMTAN": "IMTAN",
  12007. "_xlfn.ISFORMULA": "ISFORMULA",
  12008. "_xlfn.ISO.CEILING": "ISO.CEILING",
  12009. "_xlfn.ISOWEEKNUM": "ISOWEEKNUM",
  12010. "_xlfn.LOGNORM.DIST": "LOGNORM.DIST",
  12011. "_xlfn.LOGNORM.INV": "LOGNORM.INV",
  12012. "_xlfn.MAXIFS": "MAXIFS",
  12013. "_xlfn.MINIFS": "MINIFS",
  12014. "_xlfn.MODE.MULT": "MODE.MULT",
  12015. "_xlfn.MODE.SNGL": "MODE.SNGL",
  12016. "_xlfn.MUNIT": "MUNIT",
  12017. "_xlfn.NEGBINOM.DIST": "NEGBINOM.DIST",
  12018. "_xlfn.NETWORKDAYS.INTL": "NETWORKDAYS.INTL",
  12019. "_xlfn.NIGBINOM": "NIGBINOM",
  12020. "_xlfn.NORM.DIST": "NORM.DIST",
  12021. "_xlfn.NORM.INV": "NORM.INV",
  12022. "_xlfn.NORM.S.DIST": "NORM.S.DIST",
  12023. "_xlfn.NORM.S.INV": "NORM.S.INV",
  12024. "_xlfn.NUMBERVALUE": "NUMBERVALUE",
  12025. "_xlfn.PDURATION": "PDURATION",
  12026. "_xlfn.PERCENTILE.EXC": "PERCENTILE.EXC",
  12027. "_xlfn.PERCENTILE.INC": "PERCENTILE.INC",
  12028. "_xlfn.PERCENTRANK.EXC": "PERCENTRANK.EXC",
  12029. "_xlfn.PERCENTRANK.INC": "PERCENTRANK.INC",
  12030. "_xlfn.PERMUTATIONA": "PERMUTATIONA",
  12031. "_xlfn.PHI": "PHI",
  12032. "_xlfn.POISSON.DIST": "POISSON.DIST",
  12033. "_xlfn.QUARTILE.EXC": "QUARTILE.EXC",
  12034. "_xlfn.QUARTILE.INC": "QUARTILE.INC",
  12035. "_xlfn.QUERYSTRING": "QUERYSTRING",
  12036. "_xlfn.RANK.AVG": "RANK.AVG",
  12037. "_xlfn.RANK.EQ": "RANK.EQ",
  12038. "_xlfn.RRI": "RRI",
  12039. "_xlfn.SEC": "SEC",
  12040. "_xlfn.SECH": "SECH",
  12041. "_xlfn.SHEET": "SHEET",
  12042. "_xlfn.SHEETS": "SHEETS",
  12043. "_xlfn.SKEW.P": "SKEW.P",
  12044. "_xlfn.STDEV.P": "STDEV.P",
  12045. "_xlfn.STDEV.S": "STDEV.S",
  12046. "_xlfn.SUMIFS": "SUMIFS",
  12047. "_xlfn.SWITCH": "SWITCH",
  12048. "_xlfn.T.DIST": "T.DIST",
  12049. "_xlfn.T.DIST.2T": "T.DIST.2T",
  12050. "_xlfn.T.DIST.RT": "T.DIST.RT",
  12051. "_xlfn.T.INV": "T.INV",
  12052. "_xlfn.T.INV.2T": "T.INV.2T",
  12053. "_xlfn.T.TEST": "T.TEST",
  12054. "_xlfn.TEXTJOIN": "TEXTJOIN",
  12055. "_xlfn.UNICHAR": "UNICHAR",
  12056. "_xlfn.UNICODE": "UNICODE",
  12057. "_xlfn.VAR.P": "VAR.P",
  12058. "_xlfn.VAR.S": "VAR.S",
  12059. "_xlfn.WEBSERVICE": "WEBSERVICE",
  12060. "_xlfn.WEIBULL.DIST": "WEIBULL.DIST",
  12061. "_xlfn.WORKDAY.INTL": "WORKDAY.INTL",
  12062. "_xlfn.XOR": "XOR",
  12063. "_xlfn.Z.TEST": "Z.TEST"
  12064. };
  12065. /* Part 3 TODO: actually parse formulae */
  12066. function ods_to_csf_formula(f) {
  12067. if(f.slice(0,3) == "of:") f = f.slice(3);
  12068. /* 5.2 Basic Expressions */
  12069. if(f.charCodeAt(0) == 61) {
  12070. f = f.slice(1);
  12071. if(f.charCodeAt(0) == 61) f = f.slice(1);
  12072. }
  12073. f = f.replace(/COM\.MICROSOFT\./g, "");
  12074. /* Part 3 Section 5.8 References */
  12075. f = f.replace(/\[((?:\.[A-Z]+[0-9]+)(?::\.[A-Z]+[0-9]+)?)\]/g, function($$, $1) { return $1.replace(/\./g,""); });
  12076. /* TODO: something other than this */
  12077. f = f.replace(/\[.(#[A-Z]*[?!])\]/g, "$1");
  12078. return f.replace(/[;~]/g,",").replace(/\|/g,";");
  12079. }
  12080. function csf_to_ods_formula(f) {
  12081. var o = "of:=" + f.replace(crefregex, "$1[.$2$3$4$5]").replace(/\]:\[/g,":");
  12082. /* TODO: something other than this */
  12083. return o.replace(/;/g, "|").replace(/,/g,";");
  12084. }
  12085. function ods_to_csf_3D(r) {
  12086. var a = r.split(":");
  12087. var s = a[0].split(".")[0];
  12088. return [s, a[0].split(".")[1] + (a.length > 1 ? (":" + (a[1].split(".")[1] || a[1].split(".")[0])) : "")];
  12089. }
  12090. function csf_to_ods_3D(r) {
  12091. return r.replace(/\./,"!");
  12092. }
  12093. var strs = {}; // shared strings
  12094. var _ssfopts = {}; // spreadsheet formatting options
  12095. RELS.WS = [
  12096. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
  12097. "http://purl.oclc.org/ooxml/officeDocument/relationships/worksheet"
  12098. ];
  12099. /*global Map */
  12100. var browser_has_Map = typeof Map !== 'undefined';
  12101. function get_sst_id(sst, str, rev) {
  12102. var i = 0, len = sst.length;
  12103. if(rev) {
  12104. if(browser_has_Map ? rev.has(str) : Object.prototype.hasOwnProperty.call(rev, str)) {
  12105. var revarr = browser_has_Map ? rev.get(str) : rev[str];
  12106. for(; i < revarr.length; ++i) {
  12107. if(sst[revarr[i]].t === str) { sst.Count ++; return revarr[i]; }
  12108. }
  12109. }
  12110. } else for(; i < len; ++i) {
  12111. if(sst[i].t === str) { sst.Count ++; return i; }
  12112. }
  12113. sst[len] = ({t:str}); sst.Count ++; sst.Unique ++;
  12114. if(rev) {
  12115. if(browser_has_Map) {
  12116. if(!rev.has(str)) rev.set(str, []);
  12117. rev.get(str).push(len);
  12118. } else {
  12119. if(!Object.prototype.hasOwnProperty.call(rev, str)) rev[str] = [];
  12120. rev[str].push(len);
  12121. }
  12122. }
  12123. return len;
  12124. }
  12125. function col_obj_w(C, col) {
  12126. var p = ({min:C+1,max:C+1});
  12127. /* wch (chars), wpx (pixels) */
  12128. var wch = -1;
  12129. if(col.MDW) MDW = col.MDW;
  12130. if(col.width != null) p.customWidth = 1;
  12131. else if(col.wpx != null) wch = px2char(col.wpx);
  12132. else if(col.wch != null) wch = col.wch;
  12133. if(wch > -1) { p.width = char2width(wch); p.customWidth = 1; }
  12134. else if(col.width != null) p.width = col.width;
  12135. if(col.hidden) p.hidden = true;
  12136. return p;
  12137. }
  12138. function default_margins(margins, mode) {
  12139. if(!margins) return;
  12140. var defs = [0.7, 0.7, 0.75, 0.75, 0.3, 0.3];
  12141. if(mode == 'xlml') defs = [1, 1, 1, 1, 0.5, 0.5];
  12142. if(margins.left == null) margins.left = defs[0];
  12143. if(margins.right == null) margins.right = defs[1];
  12144. if(margins.top == null) margins.top = defs[2];
  12145. if(margins.bottom == null) margins.bottom = defs[3];
  12146. if(margins.header == null) margins.header = defs[4];
  12147. if(margins.footer == null) margins.footer = defs[5];
  12148. }
  12149. function get_cell_style(styles, cell, opts) {
  12150. var z = opts.revssf[cell.z != null ? cell.z : "General"];
  12151. var i = 0x3c, len = styles.length;
  12152. if(z == null && opts.ssf) {
  12153. for(; i < 0x188; ++i) if(opts.ssf[i] == null) {
  12154. SSF.load(cell.z, i);
  12155. // $FlowIgnore
  12156. opts.ssf[i] = cell.z;
  12157. opts.revssf[cell.z] = z = i;
  12158. break;
  12159. }
  12160. }
  12161. for(i = 0; i != len; ++i) if(styles[i].numFmtId === z) return i;
  12162. styles[len] = {
  12163. numFmtId:z,
  12164. fontId:0,
  12165. fillId:0,
  12166. borderId:0,
  12167. xfId:0,
  12168. applyNumberFormat:1
  12169. };
  12170. return len;
  12171. }
  12172. function safe_format(p, fmtid, fillid, opts, themes, styles) {
  12173. try {
  12174. if(opts.cellNF) p.z = SSF._table[fmtid];
  12175. } catch(e) { if(opts.WTF) throw e; }
  12176. if(p.t === 'z') return;
  12177. if(p.t === 'd' && typeof p.v === 'string') p.v = parseDate(p.v);
  12178. if(!opts || opts.cellText !== false) try {
  12179. if(SSF._table[fmtid] == null) SSF.load(SSFImplicit[fmtid] || "General", fmtid);
  12180. if(p.t === 'e') p.w = p.w || BErr[p.v];
  12181. else if(fmtid === 0) {
  12182. if(p.t === 'n') {
  12183. if((p.v|0) === p.v) p.w = SSF._general_int(p.v);
  12184. else p.w = SSF._general_num(p.v);
  12185. }
  12186. else if(p.t === 'd') {
  12187. var dd = datenum(p.v);
  12188. if((dd|0) === dd) p.w = SSF._general_int(dd);
  12189. else p.w = SSF._general_num(dd);
  12190. }
  12191. else if(p.v === undefined) return "";
  12192. else p.w = SSF._general(p.v,_ssfopts);
  12193. }
  12194. else if(p.t === 'd') p.w = SSF.format(fmtid,datenum(p.v),_ssfopts);
  12195. else p.w = SSF.format(fmtid,p.v,_ssfopts);
  12196. } catch(e) { if(opts.WTF) throw e; }
  12197. if(!opts.cellStyles) return;
  12198. if(fillid != null) try {
  12199. p.s = styles.Fills[fillid];
  12200. if (p.s.fgColor && p.s.fgColor.theme && !p.s.fgColor.rgb) {
  12201. p.s.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.fgColor.theme].rgb, p.s.fgColor.tint || 0);
  12202. if(opts.WTF) p.s.fgColor.raw_rgb = themes.themeElements.clrScheme[p.s.fgColor.theme].rgb;
  12203. }
  12204. if (p.s.bgColor && p.s.bgColor.theme) {
  12205. p.s.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.bgColor.theme].rgb, p.s.bgColor.tint || 0);
  12206. if(opts.WTF) p.s.bgColor.raw_rgb = themes.themeElements.clrScheme[p.s.bgColor.theme].rgb;
  12207. }
  12208. } catch(e) { if(opts.WTF && styles.Fills) throw e; }
  12209. }
  12210. function check_ws(ws, sname, i) {
  12211. if(ws && ws['!ref']) {
  12212. var range = safe_decode_range(ws['!ref']);
  12213. if(range.e.c < range.s.c || range.e.r < range.s.r) throw new Error("Bad range (" + i + "): " + ws['!ref']);
  12214. }
  12215. }
  12216. function parse_ws_xml_dim(ws, s) {
  12217. var d = safe_decode_range(s);
  12218. 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);
  12219. }
  12220. var mergecregex = /<(?:\w:)?mergeCell ref="[A-Z0-9:]+"\s*[\/]?>/g;
  12221. var sheetdataregex = /<(?:\w+:)?sheetData[^>]*>([\s\S]*)<\/(?:\w+:)?sheetData>/;
  12222. var hlinkregex = /<(?:\w:)?hyperlink [^>]*>/mg;
  12223. var dimregex = /"(\w*:\w*)"/;
  12224. var colregex = /<(?:\w:)?col\b[^>]*[\/]?>/g;
  12225. var afregex = /<(?:\w:)?autoFilter[^>]*([\/]|>([\s\S]*)<\/(?:\w:)?autoFilter)>/g;
  12226. var marginregex= /<(?:\w:)?pageMargins[^>]*\/>/g;
  12227. var sheetprregex = /<(?:\w:)?sheetPr\b(?:[^>a-z][^>]*)?\/>/;
  12228. var svsregex = /<(?:\w:)?sheetViews[^>]*(?:[\/]|>([\s\S]*)<\/(?:\w:)?sheetViews)>/;
  12229. /* 18.3 Worksheets */
  12230. function parse_ws_xml(data, opts, idx, rels, wb, themes, styles) {
  12231. if(!data) return data;
  12232. if(!rels) rels = {'!id':{}};
  12233. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  12234. /* 18.3.1.99 worksheet CT_Worksheet */
  12235. var s = opts.dense ? ([]) : ({});
  12236. var refguess = ({s: {r:2000000, c:2000000}, e: {r:0, c:0} });
  12237. var data1 = "", data2 = "";
  12238. var mtch = data.match(sheetdataregex);
  12239. if(mtch) {
  12240. data1 = data.slice(0, mtch.index);
  12241. data2 = data.slice(mtch.index + mtch[0].length);
  12242. } else data1 = data2 = data;
  12243. /* 18.3.1.82 sheetPr CT_SheetPr */
  12244. var sheetPr = data1.match(sheetprregex);
  12245. if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
  12246. /* 18.3.1.35 dimension CT_SheetDimension */
  12247. var ridx = (data1.match(/<(?:\w*:)?dimension/)||{index:-1}).index;
  12248. if(ridx > 0) {
  12249. var ref = data1.slice(ridx,ridx+50).match(dimregex);
  12250. if(ref) parse_ws_xml_dim(s, ref[1]);
  12251. }
  12252. /* 18.3.1.88 sheetViews CT_SheetViews */
  12253. var svs = data1.match(svsregex);
  12254. if(svs && svs[1]) parse_ws_xml_sheetviews(svs[1], wb);
  12255. /* 18.3.1.17 cols CT_Cols */
  12256. var columns = [];
  12257. if(opts.cellStyles) {
  12258. /* 18.3.1.13 col CT_Col */
  12259. var cols = data1.match(colregex);
  12260. if(cols) parse_ws_xml_cols(columns, cols);
  12261. }
  12262. /* 18.3.1.80 sheetData CT_SheetData ? */
  12263. if(mtch) parse_ws_xml_data(mtch[1], s, opts, refguess, themes, styles);
  12264. /* 18.3.1.2 autoFilter CT_AutoFilter */
  12265. var afilter = data2.match(afregex);
  12266. if(afilter) s['!autofilter'] = parse_ws_xml_autofilter(afilter[0]);
  12267. /* 18.3.1.55 mergeCells CT_MergeCells */
  12268. var merges = [];
  12269. var _merge = data2.match(mergecregex);
  12270. if(_merge) for(ridx = 0; ridx != _merge.length; ++ridx)
  12271. merges[ridx] = safe_decode_range(_merge[ridx].slice(_merge[ridx].indexOf("\"")+1));
  12272. /* 18.3.1.48 hyperlinks CT_Hyperlinks */
  12273. var hlink = data2.match(hlinkregex);
  12274. if(hlink) parse_ws_xml_hlinks(s, hlink, rels);
  12275. /* 18.3.1.62 pageMargins CT_PageMargins */
  12276. var margins = data2.match(marginregex);
  12277. if(margins) s['!margins'] = parse_ws_xml_margins(parsexmltag(margins[0]));
  12278. if(!s["!ref"] && refguess.e.c >= refguess.s.c && refguess.e.r >= refguess.s.r) s["!ref"] = encode_range(refguess);
  12279. if(opts.sheetRows > 0 && s["!ref"]) {
  12280. var tmpref = safe_decode_range(s["!ref"]);
  12281. if(opts.sheetRows <= +tmpref.e.r) {
  12282. tmpref.e.r = opts.sheetRows - 1;
  12283. if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r;
  12284. if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r;
  12285. if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c;
  12286. if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c;
  12287. s["!fullref"] = s["!ref"];
  12288. s["!ref"] = encode_range(tmpref);
  12289. }
  12290. }
  12291. if(columns.length > 0) s["!cols"] = columns;
  12292. if(merges.length > 0) s["!merges"] = merges;
  12293. return s;
  12294. }
  12295. function write_ws_xml_merges(merges) {
  12296. if(merges.length === 0) return "";
  12297. var o = '<mergeCells count="' + merges.length + '">';
  12298. for(var i = 0; i != merges.length; ++i) o += '<mergeCell ref="' + encode_range(merges[i]) + '"/>';
  12299. return o + '</mergeCells>';
  12300. }
  12301. /* 18.3.1.82-3 sheetPr CT_ChartsheetPr / CT_SheetPr */
  12302. function parse_ws_xml_sheetpr(sheetPr, s, wb, idx) {
  12303. var data = parsexmltag(sheetPr);
  12304. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  12305. if(data.codeName) wb.Sheets[idx].CodeName = unescapexml(utf8read(data.codeName));
  12306. }
  12307. function write_ws_xml_sheetpr(ws, wb, idx, opts, o) {
  12308. var needed = false;
  12309. var props = {}, payload = null;
  12310. if(opts.bookType !== 'xlsx' && wb.vbaraw) {
  12311. var cname = wb.SheetNames[idx];
  12312. try { if(wb.Workbook) cname = wb.Workbook.Sheets[idx].CodeName || cname; } catch(e) {}
  12313. needed = true;
  12314. props.codeName = utf8write(escapexml(cname));
  12315. }
  12316. if(ws && ws["!outline"]) {
  12317. var outlineprops = {summaryBelow:1, summaryRight:1};
  12318. if(ws["!outline"].above) outlineprops.summaryBelow = 0;
  12319. if(ws["!outline"].left) outlineprops.summaryRight = 0;
  12320. payload = (payload||"") + writextag('outlinePr', null, outlineprops);
  12321. }
  12322. if(!needed && !payload) return;
  12323. o[o.length] = (writextag('sheetPr', payload, props));
  12324. }
  12325. /* 18.3.1.85 sheetProtection CT_SheetProtection */
  12326. var sheetprot_deffalse = ["objects", "scenarios", "selectLockedCells", "selectUnlockedCells"];
  12327. var sheetprot_deftrue = [
  12328. "formatColumns", "formatRows", "formatCells",
  12329. "insertColumns", "insertRows", "insertHyperlinks",
  12330. "deleteColumns", "deleteRows",
  12331. "sort", "autoFilter", "pivotTables"
  12332. ];
  12333. function write_ws_xml_protection(sp) {
  12334. // algorithmName, hashValue, saltValue, spinCount
  12335. var o = ({sheet:1});
  12336. sheetprot_deffalse.forEach(function(n) { if(sp[n] != null && sp[n]) o[n] = "1"; });
  12337. sheetprot_deftrue.forEach(function(n) { if(sp[n] != null && !sp[n]) o[n] = "0"; });
  12338. /* TODO: algorithm */
  12339. if(sp.password) o.password = crypto_CreatePasswordVerifier_Method1(sp.password).toString(16).toUpperCase();
  12340. return writextag('sheetProtection', null, o);
  12341. }
  12342. function parse_ws_xml_hlinks(s, data, rels) {
  12343. var dense = Array.isArray(s);
  12344. for(var i = 0; i != data.length; ++i) {
  12345. var val = parsexmltag(utf8read(data[i]), true);
  12346. if(!val.ref) return;
  12347. var rel = ((rels || {})['!id']||[])[val.id];
  12348. if(rel) {
  12349. val.Target = rel.Target;
  12350. if(val.location) val.Target += "#"+val.location;
  12351. } else {
  12352. val.Target = "#" + val.location;
  12353. rel = {Target: val.Target, TargetMode: 'Internal'};
  12354. }
  12355. val.Rel = rel;
  12356. if(val.tooltip) { val.Tooltip = val.tooltip; delete val.tooltip; }
  12357. var rng = safe_decode_range(val.ref);
  12358. for(var R=rng.s.r;R<=rng.e.r;++R) for(var C=rng.s.c;C<=rng.e.c;++C) {
  12359. var addr = encode_cell({c:C,r:R});
  12360. if(dense) {
  12361. if(!s[R]) s[R] = [];
  12362. if(!s[R][C]) s[R][C] = {t:"z",v:undefined};
  12363. s[R][C].l = val;
  12364. } else {
  12365. if(!s[addr]) s[addr] = {t:"z",v:undefined};
  12366. s[addr].l = val;
  12367. }
  12368. }
  12369. }
  12370. }
  12371. function parse_ws_xml_margins(margin) {
  12372. var o = {};
  12373. ["left", "right", "top", "bottom", "header", "footer"].forEach(function(k) {
  12374. if(margin[k]) o[k] = parseFloat(margin[k]);
  12375. });
  12376. return o;
  12377. }
  12378. function write_ws_xml_margins(margin) {
  12379. default_margins(margin);
  12380. return writextag('pageMargins', null, margin);
  12381. }
  12382. function parse_ws_xml_cols(columns, cols) {
  12383. var seencol = false;
  12384. for(var coli = 0; coli != cols.length; ++coli) {
  12385. var coll = parsexmltag(cols[coli], true);
  12386. if(coll.hidden) coll.hidden = parsexmlbool(coll.hidden);
  12387. var colm=parseInt(coll.min, 10)-1, colM=parseInt(coll.max,10)-1;
  12388. delete coll.min; delete coll.max; coll.width = +coll.width;
  12389. if(!seencol && coll.width) { seencol = true; find_mdw_colw(coll.width); }
  12390. process_col(coll);
  12391. while(colm <= colM) columns[colm++] = dup(coll);
  12392. }
  12393. }
  12394. function write_ws_xml_cols(ws, cols) {
  12395. var o = ["<cols>"], col;
  12396. for(var i = 0; i != cols.length; ++i) {
  12397. if(!(col = cols[i])) continue;
  12398. o[o.length] = (writextag('col', null, col_obj_w(i, col)));
  12399. }
  12400. o[o.length] = "</cols>";
  12401. return o.join("");
  12402. }
  12403. function parse_ws_xml_autofilter(data) {
  12404. var o = { ref: (data.match(/ref="([^"]*)"/)||[])[1]};
  12405. return o;
  12406. }
  12407. function write_ws_xml_autofilter(data, ws, wb, idx) {
  12408. var ref = typeof data.ref == "string" ? data.ref : encode_range(data.ref);
  12409. if(!wb.Workbook) wb.Workbook = ({Sheets:[]});
  12410. if(!wb.Workbook.Names) wb.Workbook.Names = [];
  12411. var names = wb.Workbook.Names;
  12412. var range = decode_range(ref);
  12413. if(range.s.r == range.e.r) { range.e.r = decode_range(ws["!ref"]).e.r; ref = encode_range(range); }
  12414. for(var i = 0; i < names.length; ++i) {
  12415. var name = names[i];
  12416. if(name.Name != '_xlnm._FilterDatabase') continue;
  12417. if(name.Sheet != idx) continue;
  12418. name.Ref = "'" + wb.SheetNames[idx] + "'!" + ref; break;
  12419. }
  12420. if(i == names.length) names.push({ Name: '_xlnm._FilterDatabase', Sheet: idx, Ref: "'" + wb.SheetNames[idx] + "'!" + ref });
  12421. return writextag("autoFilter", null, {ref:ref});
  12422. }
  12423. /* 18.3.1.88 sheetViews CT_SheetViews */
  12424. /* 18.3.1.87 sheetView CT_SheetView */
  12425. var sviewregex = /<(?:\w:)?sheetView(?:[^>a-z][^>]*)?\/?>/;
  12426. function parse_ws_xml_sheetviews(data, wb) {
  12427. if(!wb.Views) wb.Views = [{}];
  12428. (data.match(sviewregex)||[]).forEach(function(r, i) {
  12429. var tag = parsexmltag(r);
  12430. // $FlowIgnore
  12431. if(!wb.Views[i]) wb.Views[i] = {};
  12432. // $FlowIgnore
  12433. if(parsexmlbool(tag.rightToLeft)) wb.Views[i].RTL = true;
  12434. });
  12435. }
  12436. function write_ws_xml_sheetviews(ws, opts, idx, wb) {
  12437. var sview = ({workbookViewId:"0"});
  12438. // $FlowIgnore
  12439. if((((wb||{}).Workbook||{}).Views||[])[0]) sview.rightToLeft = wb.Workbook.Views[0].RTL ? "1" : "0";
  12440. return writextag("sheetViews", writextag("sheetView", null, sview), {});
  12441. }
  12442. function write_ws_xml_cell(cell, ref, ws, opts) {
  12443. if(cell.v === undefined && typeof cell.f !== "string" || cell.t === 'z') return "";
  12444. var vv = "";
  12445. var oldt = cell.t, oldv = cell.v;
  12446. if(cell.t !== "z") switch(cell.t) {
  12447. case 'b': vv = cell.v ? "1" : "0"; break;
  12448. case 'n': vv = ''+cell.v; break;
  12449. case 'e': vv = BErr[cell.v]; break;
  12450. case 'd':
  12451. if(opts && opts.cellDates) vv = parseDate(cell.v, -1).toISOString();
  12452. else {
  12453. cell = dup(cell);
  12454. cell.t = 'n';
  12455. vv = ''+(cell.v = datenum(parseDate(cell.v)));
  12456. }
  12457. if(typeof cell.z === 'undefined') cell.z = SSF._table[14];
  12458. break;
  12459. default: vv = cell.v; break;
  12460. }
  12461. var v = writetag('v', escapexml(vv)), o = ({r:ref});
  12462. /* TODO: cell style */
  12463. var os = get_cell_style(opts.cellXfs, cell, opts);
  12464. if(os !== 0) o.s = os;
  12465. switch(cell.t) {
  12466. case 'n': break;
  12467. case 'd': o.t = "d"; break;
  12468. case 'b': o.t = "b"; break;
  12469. case 'e': o.t = "e"; break;
  12470. case 'z': break;
  12471. default: if(cell.v == null) { delete cell.t; break; }
  12472. if(opts && opts.bookSST) {
  12473. v = writetag('v', ''+get_sst_id(opts.Strings, cell.v, opts.revStrings));
  12474. o.t = "s"; break;
  12475. }
  12476. o.t = "str"; break;
  12477. }
  12478. if(cell.t != oldt) { cell.t = oldt; cell.v = oldv; }
  12479. if(typeof cell.f == "string" && cell.f) {
  12480. var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null;
  12481. v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : "");
  12482. }
  12483. if(cell.l) ws['!links'].push([ref, cell.l]);
  12484. if(cell.c) ws['!comments'].push([ref, cell.c]);
  12485. return writextag('c', v, o);
  12486. }
  12487. var parse_ws_xml_data = (function() {
  12488. var cellregex = /<(?:\w+:)?c[ \/>]/, rowregex = /<\/(?:\w+:)?row>/;
  12489. var rregex = /r=["']([^"']*)["']/, isregex = /<(?:\w+:)?is>([\S\s]*?)<\/(?:\w+:)?is>/;
  12490. var refregex = /ref=["']([^"']*)["']/;
  12491. var match_v = matchtag("v"), match_f = matchtag("f");
  12492. return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
  12493. var ri = 0, x = "", cells = [], cref = [], idx=0, i=0, cc=0, d="", p;
  12494. var tag, tagr = 0, tagc = 0;
  12495. var sstr, ftag;
  12496. var fmtid = 0, fillid = 0;
  12497. var do_format = Array.isArray(styles.CellXf), cf;
  12498. var arrayf = [];
  12499. var sharedf = [];
  12500. var dense = Array.isArray(s);
  12501. var rows = [], rowobj = {}, rowrite = false;
  12502. var sheetStubs = !!opts.sheetStubs;
  12503. for(var marr = sdata.split(rowregex), mt = 0, marrlen = marr.length; mt != marrlen; ++mt) {
  12504. x = marr[mt].trim();
  12505. var xlen = x.length;
  12506. if(xlen === 0) continue;
  12507. /* 18.3.1.73 row CT_Row */
  12508. for(ri = 0; ri < xlen; ++ri) if(x.charCodeAt(ri) === 62) break; ++ri;
  12509. tag = parsexmltag(x.slice(0,ri), true);
  12510. tagr = tag.r != null ? parseInt(tag.r, 10) : tagr+1; tagc = -1;
  12511. if(opts.sheetRows && opts.sheetRows < tagr) continue;
  12512. if(guess.s.r > tagr - 1) guess.s.r = tagr - 1;
  12513. if(guess.e.r < tagr - 1) guess.e.r = tagr - 1;
  12514. if(opts && opts.cellStyles) {
  12515. rowobj = {}; rowrite = false;
  12516. if(tag.ht) { rowrite = true; rowobj.hpt = parseFloat(tag.ht); rowobj.hpx = pt2px(rowobj.hpt); }
  12517. if(tag.hidden == "1") { rowrite = true; rowobj.hidden = true; }
  12518. if(tag.outlineLevel != null) { rowrite = true; rowobj.level = +tag.outlineLevel; }
  12519. if(rowrite) rows[tagr-1] = rowobj;
  12520. }
  12521. /* 18.3.1.4 c CT_Cell */
  12522. cells = x.slice(ri).split(cellregex);
  12523. for(var rslice = 0; rslice != cells.length; ++rslice) if(cells[rslice].trim().charAt(0) != "<") break;
  12524. cells = cells.slice(rslice);
  12525. for(ri = 0; ri != cells.length; ++ri) {
  12526. x = cells[ri].trim();
  12527. if(x.length === 0) continue;
  12528. cref = x.match(rregex); idx = ri; i=0; cc=0;
  12529. x = "<c " + (x.slice(0,1)=="<"?">":"") + x;
  12530. if(cref != null && cref.length === 2) {
  12531. idx = 0; d=cref[1];
  12532. for(i=0; i != d.length; ++i) {
  12533. if((cc=d.charCodeAt(i)-64) < 1 || cc > 26) break;
  12534. idx = 26*idx + cc;
  12535. }
  12536. --idx;
  12537. tagc = idx;
  12538. } else ++tagc;
  12539. for(i = 0; i != x.length; ++i) if(x.charCodeAt(i) === 62) break; ++i;
  12540. tag = parsexmltag(x.slice(0,i), true);
  12541. if(!tag.r) tag.r = encode_cell({r:tagr-1, c:tagc});
  12542. d = x.slice(i);
  12543. p = ({t:""});
  12544. if((cref=d.match(match_v))!= null && cref[1] !== '') p.v=unescapexml(cref[1]);
  12545. if(opts.cellFormula) {
  12546. if((cref=d.match(match_f))!= null && cref[1] !== '') {
  12547. /* TODO: match against XLSXFutureFunctions */
  12548. p.f=unescapexml(utf8read(cref[1])).replace(/\r\n/g, "\n");
  12549. if(!opts.xlfn) p.f = _xlfn(p.f);
  12550. if(cref[0].indexOf('t="array"') > -1) {
  12551. p.F = (d.match(refregex)||[])[1];
  12552. if(p.F.indexOf(":") > -1) arrayf.push([safe_decode_range(p.F), p.F]);
  12553. } else if(cref[0].indexOf('t="shared"') > -1) {
  12554. // TODO: parse formula
  12555. ftag = parsexmltag(cref[0]);
  12556. var ___f = unescapexml(utf8read(cref[1]));
  12557. if(!opts.xlfn) ___f = _xlfn(___f);
  12558. sharedf[parseInt(ftag.si, 10)] = [ftag, ___f, tag.r];
  12559. }
  12560. } else if((cref=d.match(/<f[^>]*\/>/))) {
  12561. ftag = parsexmltag(cref[0]);
  12562. if(sharedf[ftag.si]) p.f = shift_formula_xlsx(sharedf[ftag.si][1], sharedf[ftag.si][2]/*[0].ref*/, tag.r);
  12563. }
  12564. /* TODO: factor out contains logic */
  12565. var _tag = decode_cell(tag.r);
  12566. for(i = 0; i < arrayf.length; ++i)
  12567. if(_tag.r >= arrayf[i][0].s.r && _tag.r <= arrayf[i][0].e.r)
  12568. if(_tag.c >= arrayf[i][0].s.c && _tag.c <= arrayf[i][0].e.c)
  12569. p.F = arrayf[i][1];
  12570. }
  12571. if(tag.t == null && p.v === undefined) {
  12572. if(p.f || p.F) {
  12573. p.v = 0; p.t = "n";
  12574. } else if(!sheetStubs) continue;
  12575. else p.t = "z";
  12576. }
  12577. else p.t = tag.t || "n";
  12578. if(guess.s.c > tagc) guess.s.c = tagc;
  12579. if(guess.e.c < tagc) guess.e.c = tagc;
  12580. /* 18.18.11 t ST_CellType */
  12581. switch(p.t) {
  12582. case 'n':
  12583. if(p.v == "" || p.v == null) {
  12584. if(!sheetStubs) continue;
  12585. p.t = 'z';
  12586. } else p.v = parseFloat(p.v);
  12587. break;
  12588. case 's':
  12589. if(typeof p.v == 'undefined') {
  12590. if(!sheetStubs) continue;
  12591. p.t = 'z';
  12592. } else {
  12593. sstr = strs[parseInt(p.v, 10)];
  12594. p.v = sstr.t;
  12595. p.r = sstr.r;
  12596. if(opts.cellHTML) p.h = sstr.h;
  12597. }
  12598. break;
  12599. case 'str':
  12600. p.t = "s";
  12601. p.v = (p.v!=null) ? utf8read(p.v) : '';
  12602. if(opts.cellHTML) p.h = escapehtml(p.v);
  12603. break;
  12604. case 'inlineStr':
  12605. cref = d.match(isregex);
  12606. p.t = 's';
  12607. if(cref != null && (sstr = parse_si(cref[1]))) {
  12608. p.v = sstr.t;
  12609. if(opts.cellHTML) p.h = sstr.h;
  12610. } else p.v = "";
  12611. break;
  12612. case 'b': p.v = parsexmlbool(p.v); break;
  12613. case 'd':
  12614. if(opts.cellDates) p.v = parseDate(p.v, 1);
  12615. else { p.v = datenum(parseDate(p.v, 1)); p.t = 'n'; }
  12616. break;
  12617. /* error string in .w, number in .v */
  12618. case 'e':
  12619. if(!opts || opts.cellText !== false) p.w = p.v;
  12620. p.v = RBErr[p.v]; break;
  12621. }
  12622. /* formatting */
  12623. fmtid = fillid = 0;
  12624. cf = null;
  12625. if(do_format && tag.s !== undefined) {
  12626. cf = styles.CellXf[tag.s];
  12627. if(cf != null) {
  12628. if(cf.numFmtId != null) fmtid = cf.numFmtId;
  12629. if(opts.cellStyles) {
  12630. if(cf.fillId != null) fillid = cf.fillId;
  12631. }
  12632. }
  12633. }
  12634. safe_format(p, fmtid, fillid, opts, themes, styles);
  12635. if(opts.cellDates && do_format && p.t == 'n' && SSF.is_date(SSF._table[fmtid])) { p.t = 'd'; p.v = numdate(p.v); }
  12636. if(dense) {
  12637. var _r = decode_cell(tag.r);
  12638. if(!s[_r.r]) s[_r.r] = [];
  12639. s[_r.r][_r.c] = p;
  12640. } else s[tag.r] = p;
  12641. }
  12642. }
  12643. if(rows.length > 0) s['!rows'] = rows;
  12644. }; })();
  12645. function write_ws_xml_data(ws, opts, idx, wb) {
  12646. var o = [], r = [], range = safe_decode_range(ws['!ref']), cell="", ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
  12647. var dense = Array.isArray(ws);
  12648. var params = ({r:rr}), row, height = -1;
  12649. for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
  12650. for(R = range.s.r; R <= range.e.r; ++R) {
  12651. r = [];
  12652. rr = encode_row(R);
  12653. for(C = range.s.c; C <= range.e.c; ++C) {
  12654. ref = cols[C] + rr;
  12655. var _cell = dense ? (ws[R]||[])[C]: ws[ref];
  12656. if(_cell === undefined) continue;
  12657. if((cell = write_ws_xml_cell(_cell, ref, ws, opts, idx, wb)) != null) r.push(cell);
  12658. }
  12659. if(r.length > 0 || (rows && rows[R])) {
  12660. params = ({r:rr});
  12661. if(rows && rows[R]) {
  12662. row = rows[R];
  12663. if(row.hidden) params.hidden = 1;
  12664. height = -1;
  12665. if(row.hpx) height = px2pt(row.hpx);
  12666. else if(row.hpt) height = row.hpt;
  12667. if(height > -1) { params.ht = height; params.customHeight = 1; }
  12668. if(row.level) { params.outlineLevel = row.level; }
  12669. }
  12670. o[o.length] = (writextag('row', r.join(""), params));
  12671. }
  12672. }
  12673. if(rows) for(; R < rows.length; ++R) {
  12674. if(rows && rows[R]) {
  12675. params = ({r:R+1});
  12676. row = rows[R];
  12677. if(row.hidden) params.hidden = 1;
  12678. height = -1;
  12679. if (row.hpx) height = px2pt(row.hpx);
  12680. else if (row.hpt) height = row.hpt;
  12681. if (height > -1) { params.ht = height; params.customHeight = 1; }
  12682. if (row.level) { params.outlineLevel = row.level; }
  12683. o[o.length] = (writextag('row', "", params));
  12684. }
  12685. }
  12686. return o.join("");
  12687. }
  12688. var WS_XML_ROOT = writextag('worksheet', null, {
  12689. 'xmlns': XMLNS.main[0],
  12690. 'xmlns:r': XMLNS.r
  12691. });
  12692. function write_ws_xml(idx, opts, wb, rels) {
  12693. var o = [XML_HEADER, WS_XML_ROOT];
  12694. var s = wb.SheetNames[idx], sidx = 0, rdata = "";
  12695. var ws = wb.Sheets[s];
  12696. if(ws == null) ws = {};
  12697. var ref = ws['!ref'] || 'A1';
  12698. var range = safe_decode_range(ref);
  12699. if(range.e.c > 0x3FFF || range.e.r > 0xFFFFF) {
  12700. if(opts.WTF) throw new Error("Range " + ref + " exceeds format limit A1:XFD1048576");
  12701. range.e.c = Math.min(range.e.c, 0x3FFF);
  12702. range.e.r = Math.min(range.e.c, 0xFFFFF);
  12703. ref = encode_range(range);
  12704. }
  12705. if(!rels) rels = {};
  12706. ws['!comments'] = [];
  12707. var _drawing = [];
  12708. write_ws_xml_sheetpr(ws, wb, idx, opts, o);
  12709. o[o.length] = (writextag('dimension', null, {'ref': ref}));
  12710. o[o.length] = write_ws_xml_sheetviews(ws, opts, idx, wb);
  12711. /* TODO: store in WB, process styles */
  12712. if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {
  12713. defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16',
  12714. baseColWidth:opts.sheetFormat.baseColWidth||'10',
  12715. outlineLevelRow:opts.sheetFormat.outlineLevelRow||'7'
  12716. }));
  12717. if(ws['!cols'] != null && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
  12718. o[sidx = o.length] = '<sheetData/>';
  12719. ws['!links'] = [];
  12720. if(ws['!ref'] != null) {
  12721. rdata = write_ws_xml_data(ws, opts, idx, wb, rels);
  12722. if(rdata.length > 0) o[o.length] = (rdata);
  12723. }
  12724. if(o.length>sidx+1) { o[o.length] = ('</sheetData>'); o[sidx]=o[sidx].replace("/>",">"); }
  12725. /* sheetCalcPr */
  12726. if(ws['!protect'] != null) o[o.length] = write_ws_xml_protection(ws['!protect']);
  12727. /* protectedRanges */
  12728. /* scenarios */
  12729. if(ws['!autofilter'] != null) o[o.length] = write_ws_xml_autofilter(ws['!autofilter'], ws, wb, idx);
  12730. /* sortState */
  12731. /* dataConsolidate */
  12732. /* customSheetViews */
  12733. if(ws['!merges'] != null && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));
  12734. /* phoneticPr */
  12735. /* conditionalFormatting */
  12736. /* dataValidations */
  12737. var relc = -1, rel, rId = -1;
  12738. if(ws['!links'].length > 0) {
  12739. o[o.length] = "<hyperlinks>";
  12740. ws['!links'].forEach(function(l) {
  12741. if(!l[1].Target) return;
  12742. rel = ({"ref":l[0]});
  12743. if(l[1].Target.charAt(0) != "#") {
  12744. rId = add_rels(rels, -1, escapexml(l[1].Target).replace(/#.*$/, ""), RELS.HLINK);
  12745. rel["r:id"] = "rId"+rId;
  12746. }
  12747. if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1));
  12748. if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip);
  12749. o[o.length] = writextag("hyperlink",null,rel);
  12750. });
  12751. o[o.length] = "</hyperlinks>";
  12752. }
  12753. delete ws['!links'];
  12754. /* printOptions */
  12755. if(ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']);
  12756. /* pageSetup */
  12757. /* headerFooter */
  12758. /* rowBreaks */
  12759. /* colBreaks */
  12760. /* customProperties */
  12761. /* cellWatches */
  12762. if(!opts || opts.ignoreEC || (opts.ignoreEC == (void 0))) o[o.length] = writetag("ignoredErrors", writextag("ignoredError", null, {numberStoredAsText:1, sqref:ref}));
  12763. /* smartTags */
  12764. if(_drawing.length > 0) {
  12765. rId = add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
  12766. o[o.length] = writextag("drawing", null, {"r:id":"rId" + rId});
  12767. ws['!drawing'] = _drawing;
  12768. }
  12769. if(ws['!comments'].length > 0) {
  12770. rId = add_rels(rels, -1, "../drawings/vmlDrawing" + (idx+1) + ".vml", RELS.VML);
  12771. o[o.length] = writextag("legacyDrawing", null, {"r:id":"rId" + rId});
  12772. ws['!legacy'] = rId;
  12773. }
  12774. /* legacyDrawingHF */
  12775. /* picture */
  12776. /* oleObjects */
  12777. /* controls */
  12778. /* webPublishItems */
  12779. /* tableParts */
  12780. /* extLst */
  12781. if(o.length>1) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); }
  12782. return o.join("");
  12783. }
  12784. /* [MS-XLSB] 2.4.726 BrtRowHdr */
  12785. function parse_BrtRowHdr(data, length) {
  12786. var z = ({});
  12787. var tgt = data.l + length;
  12788. z.r = data.read_shift(4);
  12789. data.l += 4; // TODO: ixfe
  12790. var miyRw = data.read_shift(2);
  12791. data.l += 1; // TODO: top/bot padding
  12792. var flags = data.read_shift(1);
  12793. data.l = tgt;
  12794. if(flags & 0x07) z.level = flags & 0x07;
  12795. if(flags & 0x10) z.hidden = true;
  12796. if(flags & 0x20) z.hpt = miyRw / 20;
  12797. return z;
  12798. }
  12799. function write_BrtRowHdr(R, range, ws) {
  12800. var o = new_buf(17+8*16);
  12801. var row = (ws['!rows']||[])[R]||{};
  12802. o.write_shift(4, R);
  12803. o.write_shift(4, 0); /* TODO: ixfe */
  12804. var miyRw = 0x0140;
  12805. if(row.hpx) miyRw = px2pt(row.hpx) * 20;
  12806. else if(row.hpt) miyRw = row.hpt * 20;
  12807. o.write_shift(2, miyRw);
  12808. o.write_shift(1, 0); /* top/bot padding */
  12809. var flags = 0x0;
  12810. if(row.level) flags |= row.level;
  12811. if(row.hidden) flags |= 0x10;
  12812. if(row.hpx || row.hpt) flags |= 0x20;
  12813. o.write_shift(1, flags);
  12814. o.write_shift(1, 0); /* phonetic guide */
  12815. /* [MS-XLSB] 2.5.8 BrtColSpan explains the mechanism */
  12816. var ncolspan = 0, lcs = o.l;
  12817. o.l += 4;
  12818. var caddr = {r:R, c:0};
  12819. for(var i = 0; i < 16; ++i) {
  12820. if((range.s.c > ((i+1) << 10)) || (range.e.c < (i << 10))) continue;
  12821. var first = -1, last = -1;
  12822. for(var j = (i<<10); j < ((i+1)<<10); ++j) {
  12823. caddr.c = j;
  12824. var cell = Array.isArray(ws) ? (ws[caddr.r]||[])[caddr.c] : ws[encode_cell(caddr)];
  12825. if(cell) { if(first < 0) first = j; last = j; }
  12826. }
  12827. if(first < 0) continue;
  12828. ++ncolspan;
  12829. o.write_shift(4, first);
  12830. o.write_shift(4, last);
  12831. }
  12832. var l = o.l;
  12833. o.l = lcs;
  12834. o.write_shift(4, ncolspan);
  12835. o.l = l;
  12836. return o.length > o.l ? o.slice(0, o.l) : o;
  12837. }
  12838. function write_row_header(ba, ws, range, R) {
  12839. var o = write_BrtRowHdr(R, range, ws);
  12840. if((o.length > 17) || (ws['!rows']||[])[R]) write_record(ba, 'BrtRowHdr', o);
  12841. }
  12842. /* [MS-XLSB] 2.4.820 BrtWsDim */
  12843. var parse_BrtWsDim = parse_UncheckedRfX;
  12844. var write_BrtWsDim = write_UncheckedRfX;
  12845. /* [MS-XLSB] 2.4.821 BrtWsFmtInfo */
  12846. function parse_BrtWsFmtInfo() {
  12847. }
  12848. //function write_BrtWsFmtInfo(ws, o) { }
  12849. /* [MS-XLSB] 2.4.823 BrtWsProp */
  12850. function parse_BrtWsProp(data, length) {
  12851. var z = {};
  12852. /* TODO: pull flags */
  12853. data.l += 19;
  12854. z.name = parse_XLSBCodeName(data, length - 19);
  12855. return z;
  12856. }
  12857. function write_BrtWsProp(str, o) {
  12858. if(o == null) o = new_buf(84+4*str.length);
  12859. for(var i = 0; i < 3; ++i) o.write_shift(1,0);
  12860. write_BrtColor({auto:1}, o);
  12861. o.write_shift(-4,-1);
  12862. o.write_shift(-4,-1);
  12863. write_XLSBCodeName(str, o);
  12864. return o.slice(0, o.l);
  12865. }
  12866. /* [MS-XLSB] 2.4.306 BrtCellBlank */
  12867. function parse_BrtCellBlank(data) {
  12868. var cell = parse_XLSBCell(data);
  12869. return [cell];
  12870. }
  12871. function write_BrtCellBlank(cell, ncell, o) {
  12872. if(o == null) o = new_buf(8);
  12873. return write_XLSBCell(ncell, o);
  12874. }
  12875. /* [MS-XLSB] 2.4.307 BrtCellBool */
  12876. function parse_BrtCellBool(data) {
  12877. var cell = parse_XLSBCell(data);
  12878. var fBool = data.read_shift(1);
  12879. return [cell, fBool, 'b'];
  12880. }
  12881. function write_BrtCellBool(cell, ncell, o) {
  12882. if(o == null) o = new_buf(9);
  12883. write_XLSBCell(ncell, o);
  12884. o.write_shift(1, cell.v ? 1 : 0);
  12885. return o;
  12886. }
  12887. /* [MS-XLSB] 2.4.308 BrtCellError */
  12888. function parse_BrtCellError(data) {
  12889. var cell = parse_XLSBCell(data);
  12890. var bError = data.read_shift(1);
  12891. return [cell, bError, 'e'];
  12892. }
  12893. /* [MS-XLSB] 2.4.311 BrtCellIsst */
  12894. function parse_BrtCellIsst(data) {
  12895. var cell = parse_XLSBCell(data);
  12896. var isst = data.read_shift(4);
  12897. return [cell, isst, 's'];
  12898. }
  12899. function write_BrtCellIsst(cell, ncell, o) {
  12900. if(o == null) o = new_buf(12);
  12901. write_XLSBCell(ncell, o);
  12902. o.write_shift(4, ncell.v);
  12903. return o;
  12904. }
  12905. /* [MS-XLSB] 2.4.313 BrtCellReal */
  12906. function parse_BrtCellReal(data) {
  12907. var cell = parse_XLSBCell(data);
  12908. var value = parse_Xnum(data);
  12909. return [cell, value, 'n'];
  12910. }
  12911. function write_BrtCellReal(cell, ncell, o) {
  12912. if(o == null) o = new_buf(16);
  12913. write_XLSBCell(ncell, o);
  12914. write_Xnum(cell.v, o);
  12915. return o;
  12916. }
  12917. /* [MS-XLSB] 2.4.314 BrtCellRk */
  12918. function parse_BrtCellRk(data) {
  12919. var cell = parse_XLSBCell(data);
  12920. var value = parse_RkNumber(data);
  12921. return [cell, value, 'n'];
  12922. }
  12923. function write_BrtCellRk(cell, ncell, o) {
  12924. if(o == null) o = new_buf(12);
  12925. write_XLSBCell(ncell, o);
  12926. write_RkNumber(cell.v, o);
  12927. return o;
  12928. }
  12929. /* [MS-XLSB] 2.4.317 BrtCellSt */
  12930. function parse_BrtCellSt(data) {
  12931. var cell = parse_XLSBCell(data);
  12932. var value = parse_XLWideString(data);
  12933. return [cell, value, 'str'];
  12934. }
  12935. function write_BrtCellSt(cell, ncell, o) {
  12936. if(o == null) o = new_buf(12 + 4 * cell.v.length);
  12937. write_XLSBCell(ncell, o);
  12938. write_XLWideString(cell.v, o);
  12939. return o.length > o.l ? o.slice(0, o.l) : o;
  12940. }
  12941. /* [MS-XLSB] 2.4.653 BrtFmlaBool */
  12942. function parse_BrtFmlaBool(data, length, opts) {
  12943. var end = data.l + length;
  12944. var cell = parse_XLSBCell(data);
  12945. cell.r = opts['!row'];
  12946. var value = data.read_shift(1);
  12947. var o = [cell, value, 'b'];
  12948. if(opts.cellFormula) {
  12949. data.l += 2;
  12950. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  12951. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  12952. }
  12953. else data.l = end;
  12954. return o;
  12955. }
  12956. /* [MS-XLSB] 2.4.654 BrtFmlaError */
  12957. function parse_BrtFmlaError(data, length, opts) {
  12958. var end = data.l + length;
  12959. var cell = parse_XLSBCell(data);
  12960. cell.r = opts['!row'];
  12961. var value = data.read_shift(1);
  12962. var o = [cell, value, 'e'];
  12963. if(opts.cellFormula) {
  12964. data.l += 2;
  12965. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  12966. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  12967. }
  12968. else data.l = end;
  12969. return o;
  12970. }
  12971. /* [MS-XLSB] 2.4.655 BrtFmlaNum */
  12972. function parse_BrtFmlaNum(data, length, opts) {
  12973. var end = data.l + length;
  12974. var cell = parse_XLSBCell(data);
  12975. cell.r = opts['!row'];
  12976. var value = parse_Xnum(data);
  12977. var o = [cell, value, 'n'];
  12978. if(opts.cellFormula) {
  12979. data.l += 2;
  12980. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  12981. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  12982. }
  12983. else data.l = end;
  12984. return o;
  12985. }
  12986. /* [MS-XLSB] 2.4.656 BrtFmlaString */
  12987. function parse_BrtFmlaString(data, length, opts) {
  12988. var end = data.l + length;
  12989. var cell = parse_XLSBCell(data);
  12990. cell.r = opts['!row'];
  12991. var value = parse_XLWideString(data);
  12992. var o = [cell, value, 'str'];
  12993. if(opts.cellFormula) {
  12994. data.l += 2;
  12995. var formula = parse_XLSBCellParsedFormula(data, end - data.l, opts);
  12996. o[3] = stringify_formula(formula, null/*range*/, cell, opts.supbooks, opts);/* TODO */
  12997. }
  12998. else data.l = end;
  12999. return o;
  13000. }
  13001. /* [MS-XLSB] 2.4.682 BrtMergeCell */
  13002. var parse_BrtMergeCell = parse_UncheckedRfX;
  13003. var write_BrtMergeCell = write_UncheckedRfX;
  13004. /* [MS-XLSB] 2.4.107 BrtBeginMergeCells */
  13005. function write_BrtBeginMergeCells(cnt, o) {
  13006. if(o == null) o = new_buf(4);
  13007. o.write_shift(4, cnt);
  13008. return o;
  13009. }
  13010. /* [MS-XLSB] 2.4.662 BrtHLink */
  13011. function parse_BrtHLink(data, length) {
  13012. var end = data.l + length;
  13013. var rfx = parse_UncheckedRfX(data, 16);
  13014. var relId = parse_XLNullableWideString(data);
  13015. var loc = parse_XLWideString(data);
  13016. var tooltip = parse_XLWideString(data);
  13017. var display = parse_XLWideString(data);
  13018. data.l = end;
  13019. var o = ({rfx:rfx, relId:relId, loc:loc, display:display});
  13020. if(tooltip) o.Tooltip = tooltip;
  13021. return o;
  13022. }
  13023. function write_BrtHLink(l, rId) {
  13024. var o = new_buf(50+4*(l[1].Target.length + (l[1].Tooltip || "").length));
  13025. write_UncheckedRfX({s:decode_cell(l[0]), e:decode_cell(l[0])}, o);
  13026. write_RelID("rId" + rId, o);
  13027. var locidx = l[1].Target.indexOf("#");
  13028. var loc = locidx == -1 ? "" : l[1].Target.slice(locidx+1);
  13029. write_XLWideString(loc || "", o);
  13030. write_XLWideString(l[1].Tooltip || "", o);
  13031. write_XLWideString("", o);
  13032. return o.slice(0, o.l);
  13033. }
  13034. /* [MS-XLSB] 2.4.692 BrtPane */
  13035. function parse_BrtPane(/*data, length, opts*/) {
  13036. }
  13037. /* [MS-XLSB] 2.4.6 BrtArrFmla */
  13038. function parse_BrtArrFmla(data, length, opts) {
  13039. var end = data.l + length;
  13040. var rfx = parse_RfX(data, 16);
  13041. var fAlwaysCalc = data.read_shift(1);
  13042. var o = [rfx]; o[2] = fAlwaysCalc;
  13043. if(opts.cellFormula) {
  13044. var formula = parse_XLSBArrayParsedFormula(data, end - data.l, opts);
  13045. o[1] = formula;
  13046. } else data.l = end;
  13047. return o;
  13048. }
  13049. /* [MS-XLSB] 2.4.750 BrtShrFmla */
  13050. function parse_BrtShrFmla(data, length, opts) {
  13051. var end = data.l + length;
  13052. var rfx = parse_UncheckedRfX(data, 16);
  13053. var o = [rfx];
  13054. if(opts.cellFormula) {
  13055. var formula = parse_XLSBSharedParsedFormula(data, end - data.l, opts);
  13056. o[1] = formula;
  13057. data.l = end;
  13058. } else data.l = end;
  13059. return o;
  13060. }
  13061. /* [MS-XLSB] 2.4.323 BrtColInfo */
  13062. /* TODO: once XLS ColInfo is set, combine the functions */
  13063. function write_BrtColInfo(C, col, o) {
  13064. if(o == null) o = new_buf(18);
  13065. var p = col_obj_w(C, col);
  13066. o.write_shift(-4, C);
  13067. o.write_shift(-4, C);
  13068. o.write_shift(4, (p.width || 10) * 256);
  13069. o.write_shift(4, 0/*ixfe*/); // style
  13070. var flags = 0;
  13071. if(col.hidden) flags |= 0x01;
  13072. if(typeof p.width == 'number') flags |= 0x02;
  13073. if(col.level) flags |= (col.level << 8);
  13074. o.write_shift(2, flags); // bit flag
  13075. return o;
  13076. }
  13077. /* [MS-XLSB] 2.4.678 BrtMargins */
  13078. var BrtMarginKeys = ["left","right","top","bottom","header","footer"];
  13079. function parse_BrtMargins(data) {
  13080. var margins = ({});
  13081. BrtMarginKeys.forEach(function(k) { margins[k] = parse_Xnum(data, 8); });
  13082. return margins;
  13083. }
  13084. function write_BrtMargins(margins, o) {
  13085. if(o == null) o = new_buf(6*8);
  13086. default_margins(margins);
  13087. BrtMarginKeys.forEach(function(k) { write_Xnum((margins)[k], o); });
  13088. return o;
  13089. }
  13090. /* [MS-XLSB] 2.4.299 BrtBeginWsView */
  13091. function parse_BrtBeginWsView(data) {
  13092. var f = data.read_shift(2);
  13093. data.l += 28;
  13094. return { RTL: f & 0x20 };
  13095. }
  13096. function write_BrtBeginWsView(ws, Workbook, o) {
  13097. if(o == null) o = new_buf(30);
  13098. var f = 0x39c;
  13099. if((((Workbook||{}).Views||[])[0]||{}).RTL) f |= 0x20;
  13100. o.write_shift(2, f); // bit flag
  13101. o.write_shift(4, 0);
  13102. o.write_shift(4, 0); // view first row
  13103. o.write_shift(4, 0); // view first col
  13104. o.write_shift(1, 0); // gridline color ICV
  13105. o.write_shift(1, 0);
  13106. o.write_shift(2, 0);
  13107. o.write_shift(2, 100); // zoom scale
  13108. o.write_shift(2, 0);
  13109. o.write_shift(2, 0);
  13110. o.write_shift(2, 0);
  13111. o.write_shift(4, 0); // workbook view id
  13112. return o;
  13113. }
  13114. /* [MS-XLSB] 2.4.309 BrtCellIgnoreEC */
  13115. function write_BrtCellIgnoreEC(ref) {
  13116. var o = new_buf(24);
  13117. o.write_shift(4, 4);
  13118. o.write_shift(4, 1);
  13119. write_UncheckedRfX(ref, o);
  13120. return o;
  13121. }
  13122. /* [MS-XLSB] 2.4.748 BrtSheetProtection */
  13123. function write_BrtSheetProtection(sp, o) {
  13124. if(o == null) o = new_buf(16*4+2);
  13125. o.write_shift(2, sp.password ? crypto_CreatePasswordVerifier_Method1(sp.password) : 0);
  13126. o.write_shift(4, 1); // this record should not be written if no protection
  13127. [
  13128. ["objects", false], // fObjects
  13129. ["scenarios", false], // fScenarios
  13130. ["formatCells", true], // fFormatCells
  13131. ["formatColumns", true], // fFormatColumns
  13132. ["formatRows", true], // fFormatRows
  13133. ["insertColumns", true], // fInsertColumns
  13134. ["insertRows", true], // fInsertRows
  13135. ["insertHyperlinks", true], // fInsertHyperlinks
  13136. ["deleteColumns", true], // fDeleteColumns
  13137. ["deleteRows", true], // fDeleteRows
  13138. ["selectLockedCells", false], // fSelLockedCells
  13139. ["sort", true], // fSort
  13140. ["autoFilter", true], // fAutoFilter
  13141. ["pivotTables", true], // fPivotTables
  13142. ["selectUnlockedCells", false] // fSelUnlockedCells
  13143. ].forEach(function(n) {
  13144. if(n[1]) o.write_shift(4, sp[n[0]] != null && !sp[n[0]] ? 1 : 0);
  13145. else o.write_shift(4, sp[n[0]] != null && sp[n[0]] ? 0 : 1);
  13146. });
  13147. return o;
  13148. }
  13149. function parse_BrtDVal(/*data, length, opts*/) {
  13150. }
  13151. function parse_BrtDVal14(/*data, length, opts*/) {
  13152. }
  13153. /* [MS-XLSB] 2.1.7.61 Worksheet */
  13154. function parse_ws_bin(data, _opts, idx, rels, wb, themes, styles) {
  13155. if(!data) return data;
  13156. var opts = _opts || {};
  13157. if(!rels) rels = {'!id':{}};
  13158. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  13159. var s = (opts.dense ? [] : {});
  13160. var ref;
  13161. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  13162. var state = [];
  13163. var pass = false, end = false;
  13164. var row, p, cf, R, C, addr, sstr, rr, cell;
  13165. var merges = [];
  13166. opts.biff = 12;
  13167. opts['!row'] = 0;
  13168. var ai = 0, af = false;
  13169. var arrayf = [];
  13170. var sharedf = {};
  13171. var supbooks = opts.supbooks || wb.supbooks || ([[]]);
  13172. supbooks.sharedf = sharedf;
  13173. supbooks.arrayf = arrayf;
  13174. supbooks.SheetNames = wb.SheetNames || wb.Sheets.map(function(x) { return x.name; });
  13175. if(!opts.supbooks) {
  13176. opts.supbooks = supbooks;
  13177. if(wb.Names) for(var i = 0; i < wb.Names.length; ++i) supbooks[0][i+1] = wb.Names[i];
  13178. }
  13179. var colinfo = [], rowinfo = [];
  13180. var seencol = false;
  13181. recordhopper(data, function ws_parse(val, R_n, RT) {
  13182. if(end) return;
  13183. switch(RT) {
  13184. case 0x0094: /* 'BrtWsDim' */
  13185. ref = val; break;
  13186. case 0x0000: /* 'BrtRowHdr' */
  13187. row = val;
  13188. if(opts.sheetRows && opts.sheetRows <= row.r) end=true;
  13189. rr = encode_row(R = row.r);
  13190. opts['!row'] = row.r;
  13191. if(val.hidden || val.hpt || val.level != null) {
  13192. if(val.hpt) val.hpx = pt2px(val.hpt);
  13193. rowinfo[val.r] = val;
  13194. }
  13195. break;
  13196. case 0x0002: /* 'BrtCellRk' */
  13197. case 0x0003: /* 'BrtCellError' */
  13198. case 0x0004: /* 'BrtCellBool' */
  13199. case 0x0005: /* 'BrtCellReal' */
  13200. case 0x0006: /* 'BrtCellSt' */
  13201. case 0x0007: /* 'BrtCellIsst' */
  13202. case 0x0008: /* 'BrtFmlaString' */
  13203. case 0x0009: /* 'BrtFmlaNum' */
  13204. case 0x000A: /* 'BrtFmlaBool' */
  13205. case 0x000B: /* 'BrtFmlaError' */
  13206. p = ({t:val[2]});
  13207. switch(val[2]) {
  13208. case 'n': p.v = val[1]; break;
  13209. case 's': sstr = strs[val[1]]; p.v = sstr.t; p.r = sstr.r; break;
  13210. case 'b': p.v = val[1] ? true : false; break;
  13211. case 'e': p.v = val[1]; if(opts.cellText !== false) p.w = BErr[p.v]; break;
  13212. case 'str': p.t = 's'; p.v = val[1]; break;
  13213. }
  13214. if((cf = styles.CellXf[val[0].iStyleRef])) safe_format(p,cf.numFmtId,null,opts, themes, styles);
  13215. C = val[0].c;
  13216. if(opts.dense) { if(!s[R]) s[R] = []; s[R][C] = p; }
  13217. else s[encode_col(C) + rr] = p;
  13218. if(opts.cellFormula) {
  13219. af = false;
  13220. for(ai = 0; ai < arrayf.length; ++ai) {
  13221. var aii = arrayf[ai];
  13222. if(row.r >= aii[0].s.r && row.r <= aii[0].e.r)
  13223. if(C >= aii[0].s.c && C <= aii[0].e.c) {
  13224. p.F = encode_range(aii[0]); af = true;
  13225. }
  13226. }
  13227. if(!af && val.length > 3) p.f = val[3];
  13228. }
  13229. if(refguess.s.r > row.r) refguess.s.r = row.r;
  13230. if(refguess.s.c > C) refguess.s.c = C;
  13231. if(refguess.e.r < row.r) refguess.e.r = row.r;
  13232. if(refguess.e.c < C) refguess.e.c = C;
  13233. if(opts.cellDates && cf && p.t == 'n' && SSF.is_date(SSF._table[cf.numFmtId])) {
  13234. var _d = SSF.parse_date_code(p.v); if(_d) { p.t = 'd'; p.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  13235. }
  13236. break;
  13237. case 0x0001: /* 'BrtCellBlank' */
  13238. if(!opts.sheetStubs || pass) break;
  13239. p = ({t:'z',v:undefined});
  13240. C = val[0].c;
  13241. if(opts.dense) { if(!s[R]) s[R] = []; s[R][C] = p; }
  13242. else s[encode_col(C) + rr] = p;
  13243. if(refguess.s.r > row.r) refguess.s.r = row.r;
  13244. if(refguess.s.c > C) refguess.s.c = C;
  13245. if(refguess.e.r < row.r) refguess.e.r = row.r;
  13246. if(refguess.e.c < C) refguess.e.c = C;
  13247. break;
  13248. case 0x00B0: /* 'BrtMergeCell' */
  13249. merges.push(val); break;
  13250. case 0x01EE: /* 'BrtHLink' */
  13251. var rel = rels['!id'][val.relId];
  13252. if(rel) {
  13253. val.Target = rel.Target;
  13254. if(val.loc) val.Target += "#"+val.loc;
  13255. val.Rel = rel;
  13256. } else if(val.relId == '') {
  13257. val.Target = "#" + val.loc;
  13258. }
  13259. for(R=val.rfx.s.r;R<=val.rfx.e.r;++R) for(C=val.rfx.s.c;C<=val.rfx.e.c;++C) {
  13260. if(opts.dense) {
  13261. if(!s[R]) s[R] = [];
  13262. if(!s[R][C]) s[R][C] = {t:'z',v:undefined};
  13263. s[R][C].l = val;
  13264. } else {
  13265. addr = encode_cell({c:C,r:R});
  13266. if(!s[addr]) s[addr] = {t:'z',v:undefined};
  13267. s[addr].l = val;
  13268. }
  13269. }
  13270. break;
  13271. case 0x01AA: /* 'BrtArrFmla' */
  13272. if(!opts.cellFormula) break;
  13273. arrayf.push(val);
  13274. cell = ((opts.dense ? s[R][C] : s[encode_col(C) + rr]));
  13275. cell.f = stringify_formula(val[1], refguess, {r:row.r, c:C}, supbooks, opts);
  13276. cell.F = encode_range(val[0]);
  13277. break;
  13278. case 0x01AB: /* 'BrtShrFmla' */
  13279. if(!opts.cellFormula) break;
  13280. sharedf[encode_cell(val[0].s)] = val[1];
  13281. cell = (opts.dense ? s[R][C] : s[encode_col(C) + rr]);
  13282. cell.f = stringify_formula(val[1], refguess, {r:row.r, c:C}, supbooks, opts);
  13283. break;
  13284. /* identical to 'ColInfo' in XLS */
  13285. case 0x003C: /* 'BrtColInfo' */
  13286. if(!opts.cellStyles) break;
  13287. while(val.e >= val.s) {
  13288. colinfo[val.e--] = { width: val.w/256, hidden: !!(val.flags & 0x01), level: val.level };
  13289. if(!seencol) { seencol = true; find_mdw_colw(val.w/256); }
  13290. process_col(colinfo[val.e+1]);
  13291. }
  13292. break;
  13293. case 0x00A1: /* 'BrtBeginAFilter' */
  13294. s['!autofilter'] = { ref:encode_range(val) };
  13295. break;
  13296. case 0x01DC: /* 'BrtMargins' */
  13297. s['!margins'] = val;
  13298. break;
  13299. case 0x0093: /* 'BrtWsProp' */
  13300. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  13301. if(val.name) wb.Sheets[idx].CodeName = val.name;
  13302. break;
  13303. case 0x0089: /* 'BrtBeginWsView' */
  13304. if(!wb.Views) wb.Views = [{}];
  13305. if(!wb.Views[0]) wb.Views[0] = {};
  13306. if(val.RTL) wb.Views[0].RTL = true;
  13307. break;
  13308. case 0x01E5: /* 'BrtWsFmtInfo' */
  13309. break;
  13310. case 0x0040: /* 'BrtDVal' */
  13311. case 0x041D: /* 'BrtDVal14' */
  13312. break;
  13313. case 0x0097: /* 'BrtPane' */
  13314. break;
  13315. case 0x00AF: /* 'BrtAFilterDateGroupItem' */
  13316. case 0x0284: /* 'BrtActiveX' */
  13317. case 0x0271: /* 'BrtBigName' */
  13318. case 0x0232: /* 'BrtBkHim' */
  13319. case 0x018C: /* 'BrtBrk' */
  13320. case 0x0458: /* 'BrtCFIcon' */
  13321. case 0x047A: /* 'BrtCFRuleExt' */
  13322. case 0x01D7: /* 'BrtCFVO' */
  13323. case 0x041A: /* 'BrtCFVO14' */
  13324. case 0x0289: /* 'BrtCellIgnoreEC' */
  13325. case 0x0451: /* 'BrtCellIgnoreEC14' */
  13326. case 0x0031: /* 'BrtCellMeta' */
  13327. case 0x024D: /* 'BrtCellSmartTagProperty' */
  13328. case 0x025F: /* 'BrtCellWatch' */
  13329. case 0x0234: /* 'BrtColor' */
  13330. case 0x041F: /* 'BrtColor14' */
  13331. case 0x00A8: /* 'BrtColorFilter' */
  13332. case 0x00AE: /* 'BrtCustomFilter' */
  13333. case 0x049C: /* 'BrtCustomFilter14' */
  13334. case 0x01F3: /* 'BrtDRef' */
  13335. case 0x01FB: /* 'BrtDXF' */
  13336. case 0x0226: /* 'BrtDrawing' */
  13337. case 0x00AB: /* 'BrtDynamicFilter' */
  13338. case 0x00A7: /* 'BrtFilter' */
  13339. case 0x0499: /* 'BrtFilter14' */
  13340. case 0x00A9: /* 'BrtIconFilter' */
  13341. case 0x049D: /* 'BrtIconFilter14' */
  13342. case 0x0227: /* 'BrtLegacyDrawing' */
  13343. case 0x0228: /* 'BrtLegacyDrawingHF' */
  13344. case 0x0295: /* 'BrtListPart' */
  13345. case 0x027F: /* 'BrtOleObject' */
  13346. case 0x01DE: /* 'BrtPageSetup' */
  13347. case 0x0219: /* 'BrtPhoneticInfo' */
  13348. case 0x01DD: /* 'BrtPrintOptions' */
  13349. case 0x0218: /* 'BrtRangeProtection' */
  13350. case 0x044F: /* 'BrtRangeProtection14' */
  13351. case 0x02A8: /* 'BrtRangeProtectionIso' */
  13352. case 0x0450: /* 'BrtRangeProtectionIso14' */
  13353. case 0x0400: /* 'BrtRwDescent' */
  13354. case 0x0098: /* 'BrtSel' */
  13355. case 0x0297: /* 'BrtSheetCalcProp' */
  13356. case 0x0217: /* 'BrtSheetProtection' */
  13357. case 0x02A6: /* 'BrtSheetProtectionIso' */
  13358. case 0x01F8: /* 'BrtSlc' */
  13359. case 0x0413: /* 'BrtSparkline' */
  13360. case 0x01AC: /* 'BrtTable' */
  13361. case 0x00AA: /* 'BrtTop10Filter' */
  13362. case 0x0C00: /* 'BrtUid' */
  13363. case 0x0032: /* 'BrtValueMeta' */
  13364. case 0x0816: /* 'BrtWebExtension' */
  13365. case 0x0415: /* 'BrtWsFmtInfoEx14' */
  13366. break;
  13367. case 0x0023: /* 'BrtFRTBegin' */
  13368. pass = true; break;
  13369. case 0x0024: /* 'BrtFRTEnd' */
  13370. pass = false; break;
  13371. case 0x0025: /* 'BrtACBegin' */
  13372. state.push(R_n); pass = true; break;
  13373. case 0x0026: /* 'BrtACEnd' */
  13374. state.pop(); pass = false; break;
  13375. default:
  13376. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  13377. else if((R_n||"").indexOf("End") > 0){/* empty */}
  13378. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  13379. }
  13380. }, opts);
  13381. delete opts.supbooks;
  13382. delete opts['!row'];
  13383. if(!s["!ref"] && (refguess.s.r < 2000000 || ref && (ref.e.r > 0 || ref.e.c > 0 || ref.s.r > 0 || ref.s.c > 0))) s["!ref"] = encode_range(ref || refguess);
  13384. if(opts.sheetRows && s["!ref"]) {
  13385. var tmpref = safe_decode_range(s["!ref"]);
  13386. if(opts.sheetRows <= +tmpref.e.r) {
  13387. tmpref.e.r = opts.sheetRows - 1;
  13388. if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r;
  13389. if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r;
  13390. if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c;
  13391. if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c;
  13392. s["!fullref"] = s["!ref"];
  13393. s["!ref"] = encode_range(tmpref);
  13394. }
  13395. }
  13396. if(merges.length > 0) s["!merges"] = merges;
  13397. if(colinfo.length > 0) s["!cols"] = colinfo;
  13398. if(rowinfo.length > 0) s["!rows"] = rowinfo;
  13399. return s;
  13400. }
  13401. /* TODO: something useful -- this is a stub */
  13402. function write_ws_bin_cell(ba, cell, R, C, opts, ws) {
  13403. if(cell.v === undefined) return;
  13404. var vv = "";
  13405. switch(cell.t) {
  13406. case 'b': vv = cell.v ? "1" : "0"; break;
  13407. case 'd': // no BrtCellDate :(
  13408. cell = dup(cell);
  13409. cell.z = cell.z || SSF._table[14];
  13410. cell.v = datenum(parseDate(cell.v)); cell.t = 'n';
  13411. break;
  13412. /* falls through */
  13413. case 'n': case 'e': vv = ''+cell.v; break;
  13414. default: vv = cell.v; break;
  13415. }
  13416. var o = ({r:R, c:C});
  13417. /* TODO: cell style */
  13418. o.s = get_cell_style(opts.cellXfs, cell, opts);
  13419. if(cell.l) ws['!links'].push([encode_cell(o), cell.l]);
  13420. if(cell.c) ws['!comments'].push([encode_cell(o), cell.c]);
  13421. switch(cell.t) {
  13422. case 's': case 'str':
  13423. if(opts.bookSST) {
  13424. vv = get_sst_id(opts.Strings, (cell.v), opts.revStrings);
  13425. o.t = "s"; o.v = vv;
  13426. write_record(ba, "BrtCellIsst", write_BrtCellIsst(cell, o));
  13427. } else {
  13428. o.t = "str";
  13429. write_record(ba, "BrtCellSt", write_BrtCellSt(cell, o));
  13430. }
  13431. return;
  13432. case 'n':
  13433. /* TODO: determine threshold for Real vs RK */
  13434. if(cell.v == (cell.v | 0) && cell.v > -1000 && cell.v < 1000) write_record(ba, "BrtCellRk", write_BrtCellRk(cell, o));
  13435. else write_record(ba, "BrtCellReal", write_BrtCellReal(cell, o));
  13436. return;
  13437. case 'b':
  13438. o.t = "b";
  13439. write_record(ba, "BrtCellBool", write_BrtCellBool(cell, o));
  13440. return;
  13441. case 'e': /* TODO: error */ o.t = "e"; break;
  13442. }
  13443. write_record(ba, "BrtCellBlank", write_BrtCellBlank(cell, o));
  13444. }
  13445. function write_CELLTABLE(ba, ws, idx, opts) {
  13446. var range = safe_decode_range(ws['!ref'] || "A1"), ref, rr = "", cols = [];
  13447. write_record(ba, 'BrtBeginSheetData');
  13448. var dense = Array.isArray(ws);
  13449. var cap = range.e.r;
  13450. if(ws['!rows']) cap = Math.max(range.e.r, ws['!rows'].length - 1);
  13451. for(var R = range.s.r; R <= cap; ++R) {
  13452. rr = encode_row(R);
  13453. /* [ACCELLTABLE] */
  13454. /* BrtRowHdr */
  13455. write_row_header(ba, ws, range, R);
  13456. if(R <= range.e.r) for(var C = range.s.c; C <= range.e.c; ++C) {
  13457. /* *16384CELL */
  13458. if(R === range.s.r) cols[C] = encode_col(C);
  13459. ref = cols[C] + rr;
  13460. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  13461. if(!cell) continue;
  13462. /* write cell */
  13463. write_ws_bin_cell(ba, cell, R, C, opts, ws);
  13464. }
  13465. }
  13466. write_record(ba, 'BrtEndSheetData');
  13467. }
  13468. function write_MERGECELLS(ba, ws) {
  13469. if(!ws || !ws['!merges']) return;
  13470. write_record(ba, 'BrtBeginMergeCells', write_BrtBeginMergeCells(ws['!merges'].length));
  13471. ws['!merges'].forEach(function(m) { write_record(ba, 'BrtMergeCell', write_BrtMergeCell(m)); });
  13472. write_record(ba, 'BrtEndMergeCells');
  13473. }
  13474. function write_COLINFOS(ba, ws) {
  13475. if(!ws || !ws['!cols']) return;
  13476. write_record(ba, 'BrtBeginColInfos');
  13477. ws['!cols'].forEach(function(m, i) { if(m) write_record(ba, 'BrtColInfo', write_BrtColInfo(i, m)); });
  13478. write_record(ba, 'BrtEndColInfos');
  13479. }
  13480. function write_IGNOREECS(ba, ws) {
  13481. if(!ws || !ws['!ref']) return;
  13482. write_record(ba, 'BrtBeginCellIgnoreECs');
  13483. write_record(ba, 'BrtCellIgnoreEC', write_BrtCellIgnoreEC(safe_decode_range(ws['!ref'])));
  13484. write_record(ba, 'BrtEndCellIgnoreECs');
  13485. }
  13486. function write_HLINKS(ba, ws, rels) {
  13487. /* *BrtHLink */
  13488. ws['!links'].forEach(function(l) {
  13489. if(!l[1].Target) return;
  13490. var rId = add_rels(rels, -1, l[1].Target.replace(/#.*$/, ""), RELS.HLINK);
  13491. write_record(ba, "BrtHLink", write_BrtHLink(l, rId));
  13492. });
  13493. delete ws['!links'];
  13494. }
  13495. function write_LEGACYDRAWING(ba, ws, idx, rels) {
  13496. /* [BrtLegacyDrawing] */
  13497. if(ws['!comments'].length > 0) {
  13498. var rId = add_rels(rels, -1, "../drawings/vmlDrawing" + (idx+1) + ".vml", RELS.VML);
  13499. write_record(ba, "BrtLegacyDrawing", write_RelID("rId" + rId));
  13500. ws['!legacy'] = rId;
  13501. }
  13502. }
  13503. function write_AUTOFILTER(ba, ws, wb, idx) {
  13504. if(!ws['!autofilter']) return;
  13505. var data = ws['!autofilter'];
  13506. var ref = typeof data.ref === "string" ? data.ref : encode_range(data.ref);
  13507. /* Update FilterDatabase defined name for the worksheet */
  13508. if(!wb.Workbook) wb.Workbook = ({Sheets:[]});
  13509. if(!wb.Workbook.Names) wb.Workbook.Names = [];
  13510. var names = wb.Workbook.Names;
  13511. var range = decode_range(ref);
  13512. if(range.s.r == range.e.r) { range.e.r = decode_range(ws["!ref"]).e.r; ref = encode_range(range); }
  13513. for(var i = 0; i < names.length; ++i) {
  13514. var name = names[i];
  13515. if(name.Name != '_xlnm._FilterDatabase') continue;
  13516. if(name.Sheet != idx) continue;
  13517. name.Ref = "'" + wb.SheetNames[idx] + "'!" + ref; break;
  13518. }
  13519. if(i == names.length) names.push({ Name: '_xlnm._FilterDatabase', Sheet: idx, Ref: "'" + wb.SheetNames[idx] + "'!" + ref });
  13520. write_record(ba, "BrtBeginAFilter", write_UncheckedRfX(safe_decode_range(ref)));
  13521. /* *FILTERCOLUMN */
  13522. /* [SORTSTATE] */
  13523. /* BrtEndAFilter */
  13524. write_record(ba, "BrtEndAFilter");
  13525. }
  13526. function write_WSVIEWS2(ba, ws, Workbook) {
  13527. write_record(ba, "BrtBeginWsViews");
  13528. { /* 1*WSVIEW2 */
  13529. /* [ACUID] */
  13530. write_record(ba, "BrtBeginWsView", write_BrtBeginWsView(ws, Workbook));
  13531. /* [BrtPane] */
  13532. /* *4BrtSel */
  13533. /* *4SXSELECT */
  13534. /* *FRT */
  13535. write_record(ba, "BrtEndWsView");
  13536. }
  13537. /* *FRT */
  13538. write_record(ba, "BrtEndWsViews");
  13539. }
  13540. function write_WSFMTINFO() {
  13541. /* [ACWSFMTINFO] */
  13542. //write_record(ba, "BrtWsFmtInfo", write_BrtWsFmtInfo(ws));
  13543. }
  13544. function write_SHEETPROTECT(ba, ws) {
  13545. if(!ws['!protect']) return;
  13546. /* [BrtSheetProtectionIso] */
  13547. write_record(ba, "BrtSheetProtection", write_BrtSheetProtection(ws['!protect']));
  13548. }
  13549. function write_ws_bin(idx, opts, wb, rels) {
  13550. var ba = buf_array();
  13551. var s = wb.SheetNames[idx], ws = wb.Sheets[s] || {};
  13552. var c = s; try { if(wb && wb.Workbook) c = wb.Workbook.Sheets[idx].CodeName || c; } catch(e) {}
  13553. var r = safe_decode_range(ws['!ref'] || "A1");
  13554. if(r.e.c > 0x3FFF || r.e.r > 0xFFFFF) {
  13555. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:XFD1048576");
  13556. r.e.c = Math.min(r.e.c, 0x3FFF);
  13557. r.e.r = Math.min(r.e.c, 0xFFFFF);
  13558. }
  13559. ws['!links'] = [];
  13560. /* passed back to write_zip and removed there */
  13561. ws['!comments'] = [];
  13562. write_record(ba, "BrtBeginSheet");
  13563. if(wb.vbaraw) write_record(ba, "BrtWsProp", write_BrtWsProp(c));
  13564. write_record(ba, "BrtWsDim", write_BrtWsDim(r));
  13565. write_WSVIEWS2(ba, ws, wb.Workbook);
  13566. write_WSFMTINFO(ba, ws);
  13567. write_COLINFOS(ba, ws, idx, opts, wb);
  13568. write_CELLTABLE(ba, ws, idx, opts, wb);
  13569. /* [BrtSheetCalcProp] */
  13570. write_SHEETPROTECT(ba, ws);
  13571. /* *([BrtRangeProtectionIso] BrtRangeProtection) */
  13572. /* [SCENMAN] */
  13573. write_AUTOFILTER(ba, ws, wb, idx);
  13574. /* [SORTSTATE] */
  13575. /* [DCON] */
  13576. /* [USERSHVIEWS] */
  13577. write_MERGECELLS(ba, ws);
  13578. /* [BrtPhoneticInfo] */
  13579. /* *CONDITIONALFORMATTING */
  13580. /* [DVALS] */
  13581. write_HLINKS(ba, ws, rels);
  13582. /* [BrtPrintOptions] */
  13583. if(ws['!margins']) write_record(ba, "BrtMargins", write_BrtMargins(ws['!margins']));
  13584. /* [BrtPageSetup] */
  13585. /* [HEADERFOOTER] */
  13586. /* [RWBRK] */
  13587. /* [COLBRK] */
  13588. /* *BrtBigName */
  13589. /* [CELLWATCHES] */
  13590. if(!opts || opts.ignoreEC || (opts.ignoreEC == (void 0))) write_IGNOREECS(ba, ws);
  13591. /* [SMARTTAGS] */
  13592. /* [BrtDrawing] */
  13593. write_LEGACYDRAWING(ba, ws, idx, rels);
  13594. /* [BrtLegacyDrawingHF] */
  13595. /* [BrtBkHim] */
  13596. /* [OLEOBJECTS] */
  13597. /* [ACTIVEXCONTROLS] */
  13598. /* [WEBPUBITEMS] */
  13599. /* [LISTPARTS] */
  13600. /* FRTWORKSHEET */
  13601. write_record(ba, "BrtEndSheet");
  13602. return ba.end();
  13603. }
  13604. RELS.CHART = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
  13605. RELS.CHARTEX = "http://schemas.microsoft.com/office/2014/relationships/chartEx";
  13606. function parse_Cache(data) {
  13607. var col = [];
  13608. var num = data.match(/^<c:numCache>/);
  13609. var f;
  13610. /* 21.2.2.150 pt CT_NumVal */
  13611. (data.match(/<c:pt idx="(\d*)">(.*?)<\/c:pt>/mg)||[]).forEach(function(pt) {
  13612. var q = pt.match(/<c:pt idx="(\d*?)"><c:v>(.*)<\/c:v><\/c:pt>/);
  13613. if(!q) return;
  13614. col[+q[1]] = num ? +q[2] : q[2];
  13615. });
  13616. /* 21.2.2.71 formatCode CT_Xstring */
  13617. var nf = unescapexml((data.match(/<c:formatCode>([\s\S]*?)<\/c:formatCode>/) || ["","General"])[1]);
  13618. (data.match(/<c:f>(.*?)<\/c:f>/mg)||[]).forEach(function(F) { f = F.replace(/<.*?>/g,""); });
  13619. return [col, nf, f];
  13620. }
  13621. /* 21.2 DrawingML - Charts */
  13622. function parse_chart(data, name, opts, rels, wb, csheet) {
  13623. var cs = ((csheet || {"!type":"chart"}));
  13624. if(!data) return csheet;
  13625. /* 21.2.2.27 chart CT_Chart */
  13626. var C = 0, R = 0, col = "A";
  13627. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  13628. /* 21.2.2.120 numCache CT_NumData */
  13629. (data.match(/<c:numCache>[\s\S]*?<\/c:numCache>/gm)||[]).forEach(function(nc) {
  13630. var cache = parse_Cache(nc);
  13631. refguess.s.r = refguess.s.c = 0;
  13632. refguess.e.c = C;
  13633. col = encode_col(C);
  13634. cache[0].forEach(function(n,i) {
  13635. cs[col + encode_row(i)] = {t:'n', v:n, z:cache[1] };
  13636. R = i;
  13637. });
  13638. if(refguess.e.r < R) refguess.e.r = R;
  13639. ++C;
  13640. });
  13641. if(C > 0) cs["!ref"] = encode_range(refguess);
  13642. return cs;
  13643. }
  13644. RELS.CS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet";
  13645. var CS_XML_ROOT = writextag('chartsheet', null, {
  13646. 'xmlns': XMLNS.main[0],
  13647. 'xmlns:r': XMLNS.r
  13648. });
  13649. /* 18.3 Worksheets also covers Chartsheets */
  13650. function parse_cs_xml(data, opts, idx, rels, wb) {
  13651. if(!data) return data;
  13652. /* 18.3.1.12 chartsheet CT_ChartSheet */
  13653. if(!rels) rels = {'!id':{}};
  13654. var s = ({'!type':"chart", '!drawel':null, '!rel':""});
  13655. var m;
  13656. /* 18.3.1.83 sheetPr CT_ChartsheetPr */
  13657. var sheetPr = data.match(sheetprregex);
  13658. if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
  13659. /* 18.3.1.36 drawing CT_Drawing */
  13660. if((m = data.match(/drawing r:id="(.*?)"/))) s['!rel'] = m[1];
  13661. if(rels['!id'][s['!rel']]) s['!drawel'] = rels['!id'][s['!rel']];
  13662. return s;
  13663. }
  13664. function write_cs_xml(idx, opts, wb, rels) {
  13665. var o = [XML_HEADER, CS_XML_ROOT];
  13666. o[o.length] = writextag("drawing", null, {"r:id": "rId1"});
  13667. add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
  13668. if(o.length>2) { o[o.length] = ('</chartsheet>'); o[1]=o[1].replace("/>",">"); }
  13669. return o.join("");
  13670. }
  13671. /* [MS-XLSB] 2.4.331 BrtCsProp */
  13672. function parse_BrtCsProp(data, length) {
  13673. data.l += 10;
  13674. var name = parse_XLWideString(data, length - 10);
  13675. return { name: name };
  13676. }
  13677. /* [MS-XLSB] 2.1.7.7 Chart Sheet */
  13678. function parse_cs_bin(data, opts, idx, rels, wb) {
  13679. if(!data) return data;
  13680. if(!rels) rels = {'!id':{}};
  13681. var s = {'!type':"chart", '!drawel':null, '!rel':""};
  13682. var state = [];
  13683. var pass = false;
  13684. recordhopper(data, function cs_parse(val, R_n, RT) {
  13685. switch(RT) {
  13686. case 0x0226: /* 'BrtDrawing' */
  13687. s['!rel'] = val; break;
  13688. case 0x028B: /* 'BrtCsProp' */
  13689. if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
  13690. if(val.name) wb.Sheets[idx].CodeName = val.name;
  13691. break;
  13692. case 0x0232: /* 'BrtBkHim' */
  13693. case 0x028C: /* 'BrtCsPageSetup' */
  13694. case 0x029D: /* 'BrtCsProtection' */
  13695. case 0x02A7: /* 'BrtCsProtectionIso' */
  13696. case 0x0227: /* 'BrtLegacyDrawing' */
  13697. case 0x0228: /* 'BrtLegacyDrawingHF' */
  13698. case 0x01DC: /* 'BrtMargins' */
  13699. case 0x0C00: /* 'BrtUid' */
  13700. break;
  13701. case 0x0023: /* 'BrtFRTBegin' */
  13702. pass = true; break;
  13703. case 0x0024: /* 'BrtFRTEnd' */
  13704. pass = false; break;
  13705. case 0x0025: /* 'BrtACBegin' */
  13706. state.push(R_n); break;
  13707. case 0x0026: /* 'BrtACEnd' */
  13708. state.pop(); break;
  13709. default:
  13710. if((R_n||"").indexOf("Begin") > 0) state.push(R_n);
  13711. else if((R_n||"").indexOf("End") > 0) state.pop();
  13712. else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
  13713. }
  13714. }, opts);
  13715. if(rels['!id'][s['!rel']]) s['!drawel'] = rels['!id'][s['!rel']];
  13716. return s;
  13717. }
  13718. function write_cs_bin() {
  13719. var ba = buf_array();
  13720. write_record(ba, "BrtBeginSheet");
  13721. /* [BrtCsProp] */
  13722. /* CSVIEWS */
  13723. /* [[BrtCsProtectionIso] BrtCsProtection] */
  13724. /* [USERCSVIEWS] */
  13725. /* [BrtMargins] */
  13726. /* [BrtCsPageSetup] */
  13727. /* [HEADERFOOTER] */
  13728. /* BrtDrawing */
  13729. /* [BrtLegacyDrawing] */
  13730. /* [BrtLegacyDrawingHF] */
  13731. /* [BrtBkHim] */
  13732. /* [WEBPUBITEMS] */
  13733. /* FRTCHARTSHEET */
  13734. write_record(ba, "BrtEndSheet");
  13735. return ba.end();
  13736. }
  13737. /* 18.2.28 (CT_WorkbookProtection) Defaults */
  13738. var WBPropsDef = [
  13739. ['allowRefreshQuery', false, "bool"],
  13740. ['autoCompressPictures', true, "bool"],
  13741. ['backupFile', false, "bool"],
  13742. ['checkCompatibility', false, "bool"],
  13743. ['CodeName', ''],
  13744. ['date1904', false, "bool"],
  13745. ['defaultThemeVersion', 0, "int"],
  13746. ['filterPrivacy', false, "bool"],
  13747. ['hidePivotFieldList', false, "bool"],
  13748. ['promptedSolutions', false, "bool"],
  13749. ['publishItems', false, "bool"],
  13750. ['refreshAllConnections', false, "bool"],
  13751. ['saveExternalLinkValues', true, "bool"],
  13752. ['showBorderUnselectedTables', true, "bool"],
  13753. ['showInkAnnotation', true, "bool"],
  13754. ['showObjects', 'all'],
  13755. ['showPivotChartFilter', false, "bool"],
  13756. ['updateLinks', 'userSet']
  13757. ];
  13758. /* 18.2.30 (CT_BookView) Defaults */
  13759. var WBViewDef = [
  13760. ['activeTab', 0, "int"],
  13761. ['autoFilterDateGrouping', true, "bool"],
  13762. ['firstSheet', 0, "int"],
  13763. ['minimized', false, "bool"],
  13764. ['showHorizontalScroll', true, "bool"],
  13765. ['showSheetTabs', true, "bool"],
  13766. ['showVerticalScroll', true, "bool"],
  13767. ['tabRatio', 600, "int"],
  13768. ['visibility', 'visible']
  13769. //window{Height,Width}, {x,y}Window
  13770. ];
  13771. /* 18.2.19 (CT_Sheet) Defaults */
  13772. var SheetDef = [
  13773. //['state', 'visible']
  13774. ];
  13775. /* 18.2.2 (CT_CalcPr) Defaults */
  13776. var CalcPrDef = [
  13777. ['calcCompleted', 'true'],
  13778. ['calcMode', 'auto'],
  13779. ['calcOnSave', 'true'],
  13780. ['concurrentCalc', 'true'],
  13781. ['fullCalcOnLoad', 'false'],
  13782. ['fullPrecision', 'true'],
  13783. ['iterate', 'false'],
  13784. ['iterateCount', '100'],
  13785. ['iterateDelta', '0.001'],
  13786. ['refMode', 'A1']
  13787. ];
  13788. /* 18.2.3 (CT_CustomWorkbookView) Defaults */
  13789. /*var CustomWBViewDef = [
  13790. ['autoUpdate', 'false'],
  13791. ['changesSavedWin', 'false'],
  13792. ['includeHiddenRowCol', 'true'],
  13793. ['includePrintSettings', 'true'],
  13794. ['maximized', 'false'],
  13795. ['minimized', 'false'],
  13796. ['onlySync', 'false'],
  13797. ['personalView', 'false'],
  13798. ['showComments', 'commIndicator'],
  13799. ['showFormulaBar', 'true'],
  13800. ['showHorizontalScroll', 'true'],
  13801. ['showObjects', 'all'],
  13802. ['showSheetTabs', 'true'],
  13803. ['showStatusbar', 'true'],
  13804. ['showVerticalScroll', 'true'],
  13805. ['tabRatio', '600'],
  13806. ['xWindow', '0'],
  13807. ['yWindow', '0']
  13808. ];*/
  13809. function push_defaults_array(target, defaults) {
  13810. for(var j = 0; j != target.length; ++j) { var w = target[j];
  13811. for(var i=0; i != defaults.length; ++i) { var z = defaults[i];
  13812. if(w[z[0]] == null) w[z[0]] = z[1];
  13813. else switch(z[2]) {
  13814. case "bool": if(typeof w[z[0]] == "string") w[z[0]] = parsexmlbool(w[z[0]]); break;
  13815. case "int": if(typeof w[z[0]] == "string") w[z[0]] = parseInt(w[z[0]], 10); break;
  13816. }
  13817. }
  13818. }
  13819. }
  13820. function push_defaults(target, defaults) {
  13821. for(var i = 0; i != defaults.length; ++i) { var z = defaults[i];
  13822. if(target[z[0]] == null) target[z[0]] = z[1];
  13823. else switch(z[2]) {
  13824. case "bool": if(typeof target[z[0]] == "string") target[z[0]] = parsexmlbool(target[z[0]]); break;
  13825. case "int": if(typeof target[z[0]] == "string") target[z[0]] = parseInt(target[z[0]], 10); break;
  13826. }
  13827. }
  13828. }
  13829. function parse_wb_defaults(wb) {
  13830. push_defaults(wb.WBProps, WBPropsDef);
  13831. push_defaults(wb.CalcPr, CalcPrDef);
  13832. push_defaults_array(wb.WBView, WBViewDef);
  13833. push_defaults_array(wb.Sheets, SheetDef);
  13834. _ssfopts.date1904 = parsexmlbool(wb.WBProps.date1904);
  13835. }
  13836. function safe1904(wb) {
  13837. /* TODO: store date1904 somewhere else */
  13838. if(!wb.Workbook) return "false";
  13839. if(!wb.Workbook.WBProps) return "false";
  13840. return parsexmlbool(wb.Workbook.WBProps.date1904) ? "true" : "false";
  13841. }
  13842. var badchars = "][*?\/\\".split("");
  13843. function check_ws_name(n, safe) {
  13844. if(n.length > 31) { if(safe) return false; throw new Error("Sheet names cannot exceed 31 chars"); }
  13845. var _good = true;
  13846. badchars.forEach(function(c) {
  13847. if(n.indexOf(c) == -1) return;
  13848. if(!safe) throw new Error("Sheet name cannot contain : \\ / ? * [ ]");
  13849. _good = false;
  13850. });
  13851. return _good;
  13852. }
  13853. function check_wb_names(N, S, codes) {
  13854. N.forEach(function(n,i) {
  13855. check_ws_name(n);
  13856. for(var j = 0; j < i; ++j) if(n == N[j]) throw new Error("Duplicate Sheet Name: " + n);
  13857. if(codes) {
  13858. var cn = (S && S[i] && S[i].CodeName) || n;
  13859. if(cn.charCodeAt(0) == 95 && cn.length > 22) throw new Error("Bad Code Name: Worksheet" + cn);
  13860. }
  13861. });
  13862. }
  13863. function check_wb(wb) {
  13864. if(!wb || !wb.SheetNames || !wb.Sheets) throw new Error("Invalid Workbook");
  13865. if(!wb.SheetNames.length) throw new Error("Workbook is empty");
  13866. var Sheets = (wb.Workbook && wb.Workbook.Sheets) || [];
  13867. check_wb_names(wb.SheetNames, Sheets, !!wb.vbaraw);
  13868. for(var i = 0; i < wb.SheetNames.length; ++i) check_ws(wb.Sheets[wb.SheetNames[i]], wb.SheetNames[i], i);
  13869. /* TODO: validate workbook */
  13870. }
  13871. /* 18.2 Workbook */
  13872. var wbnsregex = /<\w+:workbook/;
  13873. function parse_wb_xml(data, opts) {
  13874. if(!data) throw new Error("Could not find file");
  13875. var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, Names:[], xmlns: "" };
  13876. var pass = false, xmlns = "xmlns";
  13877. var dname = {}, dnstart = 0;
  13878. data.replace(tagregex, function xml_wb(x, idx) {
  13879. var y = parsexmltag(x);
  13880. switch(strip_ns(y[0])) {
  13881. case '<?xml': break;
  13882. /* 18.2.27 workbook CT_Workbook 1 */
  13883. case '<workbook':
  13884. if(x.match(wbnsregex)) xmlns = "xmlns" + x.match(/<(\w+):/)[1];
  13885. wb.xmlns = y[xmlns];
  13886. break;
  13887. case '</workbook>': break;
  13888. /* 18.2.13 fileVersion CT_FileVersion ? */
  13889. case '<fileVersion': delete y[0]; wb.AppVersion = y; break;
  13890. case '<fileVersion/>': case '</fileVersion>': break;
  13891. /* 18.2.12 fileSharing CT_FileSharing ? */
  13892. case '<fileSharing':
  13893. break;
  13894. case '<fileSharing/>': break;
  13895. /* 18.2.28 workbookPr CT_WorkbookPr ? */
  13896. case '<workbookPr':
  13897. case '<workbookPr/>':
  13898. WBPropsDef.forEach(function(w) {
  13899. if(y[w[0]] == null) return;
  13900. switch(w[2]) {
  13901. case "bool": wb.WBProps[w[0]] = parsexmlbool(y[w[0]]); break;
  13902. case "int": wb.WBProps[w[0]] = parseInt(y[w[0]], 10); break;
  13903. default: wb.WBProps[w[0]] = y[w[0]];
  13904. }
  13905. });
  13906. if(y.codeName) wb.WBProps.CodeName = utf8read(y.codeName);
  13907. break;
  13908. case '</workbookPr>': break;
  13909. /* 18.2.29 workbookProtection CT_WorkbookProtection ? */
  13910. case '<workbookProtection':
  13911. break;
  13912. case '<workbookProtection/>': break;
  13913. /* 18.2.1 bookViews CT_BookViews ? */
  13914. case '<bookViews': case '<bookViews>': case '</bookViews>': break;
  13915. /* 18.2.30 workbookView CT_BookView + */
  13916. case '<workbookView': case '<workbookView/>': delete y[0]; wb.WBView.push(y); break;
  13917. case '</workbookView>': break;
  13918. /* 18.2.20 sheets CT_Sheets 1 */
  13919. case '<sheets': case '<sheets>': case '</sheets>': break; // aggregate sheet
  13920. /* 18.2.19 sheet CT_Sheet + */
  13921. case '<sheet':
  13922. switch(y.state) {
  13923. case "hidden": y.Hidden = 1; break;
  13924. case "veryHidden": y.Hidden = 2; break;
  13925. default: y.Hidden = 0;
  13926. }
  13927. delete y.state;
  13928. y.name = unescapexml(utf8read(y.name));
  13929. delete y[0]; wb.Sheets.push(y); break;
  13930. case '</sheet>': break;
  13931. /* 18.2.15 functionGroups CT_FunctionGroups ? */
  13932. case '<functionGroups': case '<functionGroups/>': break;
  13933. /* 18.2.14 functionGroup CT_FunctionGroup + */
  13934. case '<functionGroup': break;
  13935. /* 18.2.9 externalReferences CT_ExternalReferences ? */
  13936. case '<externalReferences': case '</externalReferences>': case '<externalReferences>': break;
  13937. /* 18.2.8 externalReference CT_ExternalReference + */
  13938. case '<externalReference': break;
  13939. /* 18.2.6 definedNames CT_DefinedNames ? */
  13940. case '<definedNames/>': break;
  13941. case '<definedNames>': case '<definedNames': pass=true; break;
  13942. case '</definedNames>': pass=false; break;
  13943. /* 18.2.5 definedName CT_DefinedName + */
  13944. case '<definedName': {
  13945. dname = {};
  13946. dname.Name = utf8read(y.name);
  13947. if(y.comment) dname.Comment = y.comment;
  13948. if(y.localSheetId) dname.Sheet = +y.localSheetId;
  13949. if(parsexmlbool(y.hidden||"0")) dname.Hidden = true;
  13950. dnstart = idx + x.length;
  13951. } break;
  13952. case '</definedName>': {
  13953. dname.Ref = unescapexml(utf8read(data.slice(dnstart, idx)));
  13954. wb.Names.push(dname);
  13955. } break;
  13956. case '<definedName/>': break;
  13957. /* 18.2.2 calcPr CT_CalcPr ? */
  13958. case '<calcPr': delete y[0]; wb.CalcPr = y; break;
  13959. case '<calcPr/>': delete y[0]; wb.CalcPr = y; break;
  13960. case '</calcPr>': break;
  13961. /* 18.2.16 oleSize CT_OleSize ? (ref required) */
  13962. case '<oleSize': break;
  13963. /* 18.2.4 customWorkbookViews CT_CustomWorkbookViews ? */
  13964. case '<customWorkbookViews>': case '</customWorkbookViews>': case '<customWorkbookViews': break;
  13965. /* 18.2.3 customWorkbookView CT_CustomWorkbookView + */
  13966. case '<customWorkbookView': case '</customWorkbookView>': break;
  13967. /* 18.2.18 pivotCaches CT_PivotCaches ? */
  13968. case '<pivotCaches>': case '</pivotCaches>': case '<pivotCaches': break;
  13969. /* 18.2.17 pivotCache CT_PivotCache ? */
  13970. case '<pivotCache': break;
  13971. /* 18.2.21 smartTagPr CT_SmartTagPr ? */
  13972. case '<smartTagPr': case '<smartTagPr/>': break;
  13973. /* 18.2.23 smartTagTypes CT_SmartTagTypes ? */
  13974. case '<smartTagTypes': case '<smartTagTypes>': case '</smartTagTypes>': break;
  13975. /* 18.2.22 smartTagType CT_SmartTagType ? */
  13976. case '<smartTagType': break;
  13977. /* 18.2.24 webPublishing CT_WebPublishing ? */
  13978. case '<webPublishing': case '<webPublishing/>': break;
  13979. /* 18.2.11 fileRecoveryPr CT_FileRecoveryPr ? */
  13980. case '<fileRecoveryPr': case '<fileRecoveryPr/>': break;
  13981. /* 18.2.26 webPublishObjects CT_WebPublishObjects ? */
  13982. case '<webPublishObjects>': case '<webPublishObjects': case '</webPublishObjects>': break;
  13983. /* 18.2.25 webPublishObject CT_WebPublishObject ? */
  13984. case '<webPublishObject': break;
  13985. /* 18.2.10 extLst CT_ExtensionList ? */
  13986. case '<extLst': case '<extLst>': case '</extLst>': case '<extLst/>': break;
  13987. /* 18.2.7 ext CT_Extension + */
  13988. case '<ext': pass=true; break; //TODO: check with versions of excel
  13989. case '</ext>': pass=false; break;
  13990. /* Others */
  13991. case '<ArchID': break;
  13992. case '<AlternateContent':
  13993. case '<AlternateContent>': pass=true; break;
  13994. case '</AlternateContent>': pass=false; break;
  13995. /* TODO */
  13996. case '<revisionPtr': break;
  13997. default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in workbook');
  13998. }
  13999. return x;
  14000. });
  14001. if(XMLNS.main.indexOf(wb.xmlns) === -1) throw new Error("Unknown Namespace: " + wb.xmlns);
  14002. parse_wb_defaults(wb);
  14003. return wb;
  14004. }
  14005. var WB_XML_ROOT = writextag('workbook', null, {
  14006. 'xmlns': XMLNS.main[0],
  14007. //'xmlns:mx': XMLNS.mx,
  14008. //'xmlns:s': XMLNS.main[0],
  14009. 'xmlns:r': XMLNS.r
  14010. });
  14011. function write_wb_xml(wb) {
  14012. var o = [XML_HEADER];
  14013. o[o.length] = WB_XML_ROOT;
  14014. var write_names = (wb.Workbook && (wb.Workbook.Names||[]).length > 0);
  14015. /* fileVersion */
  14016. /* fileSharing */
  14017. var workbookPr = ({codeName:"ThisWorkbook"});
  14018. if(wb.Workbook && wb.Workbook.WBProps) {
  14019. WBPropsDef.forEach(function(x) {
  14020. if((wb.Workbook.WBProps[x[0]]) == null) return;
  14021. if((wb.Workbook.WBProps[x[0]]) == x[1]) return;
  14022. workbookPr[x[0]] = (wb.Workbook.WBProps[x[0]]);
  14023. });
  14024. if(wb.Workbook.WBProps.CodeName) { workbookPr.codeName = wb.Workbook.WBProps.CodeName; delete workbookPr.CodeName; }
  14025. }
  14026. o[o.length] = (writextag('workbookPr', null, workbookPr));
  14027. /* workbookProtection */
  14028. var sheets = wb.Workbook && wb.Workbook.Sheets || [];
  14029. var i = 0;
  14030. /* bookViews only written if first worksheet is hidden */
  14031. if(sheets && sheets[0] && !!sheets[0].Hidden) {
  14032. o[o.length] = "<bookViews>";
  14033. for(i = 0; i != wb.SheetNames.length; ++i) {
  14034. if(!sheets[i]) break;
  14035. if(!sheets[i].Hidden) break;
  14036. }
  14037. if(i == wb.SheetNames.length) i = 0;
  14038. o[o.length] = '<workbookView firstSheet="' + i + '" activeTab="' + i + '"/>';
  14039. o[o.length] = "</bookViews>";
  14040. }
  14041. o[o.length] = "<sheets>";
  14042. for(i = 0; i != wb.SheetNames.length; ++i) {
  14043. var sht = ({name:escapexml(wb.SheetNames[i].slice(0,31))});
  14044. sht.sheetId = ""+(i+1);
  14045. sht["r:id"] = "rId"+(i+1);
  14046. if(sheets[i]) switch(sheets[i].Hidden) {
  14047. case 1: sht.state = "hidden"; break;
  14048. case 2: sht.state = "veryHidden"; break;
  14049. }
  14050. o[o.length] = (writextag('sheet',null,sht));
  14051. }
  14052. o[o.length] = "</sheets>";
  14053. /* functionGroups */
  14054. /* externalReferences */
  14055. if(write_names) {
  14056. o[o.length] = "<definedNames>";
  14057. if(wb.Workbook && wb.Workbook.Names) wb.Workbook.Names.forEach(function(n) {
  14058. var d = {name:n.Name};
  14059. if(n.Comment) d.comment = n.Comment;
  14060. if(n.Sheet != null) d.localSheetId = ""+n.Sheet;
  14061. if(n.Hidden) d.hidden = "1";
  14062. if(!n.Ref) return;
  14063. o[o.length] = writextag('definedName', escapexml(n.Ref), d);
  14064. });
  14065. o[o.length] = "</definedNames>";
  14066. }
  14067. /* calcPr */
  14068. /* oleSize */
  14069. /* customWorkbookViews */
  14070. /* pivotCaches */
  14071. /* smartTagPr */
  14072. /* smartTagTypes */
  14073. /* webPublishing */
  14074. /* fileRecoveryPr */
  14075. /* webPublishObjects */
  14076. /* extLst */
  14077. if(o.length>2){ o[o.length] = '</workbook>'; o[1]=o[1].replace("/>",">"); }
  14078. return o.join("");
  14079. }
  14080. /* [MS-XLSB] 2.4.304 BrtBundleSh */
  14081. function parse_BrtBundleSh(data, length) {
  14082. var z = {};
  14083. z.Hidden = data.read_shift(4); //hsState ST_SheetState
  14084. z.iTabID = data.read_shift(4);
  14085. z.strRelID = parse_RelID(data,length-8);
  14086. z.name = parse_XLWideString(data);
  14087. return z;
  14088. }
  14089. function write_BrtBundleSh(data, o) {
  14090. if(!o) o = new_buf(127);
  14091. o.write_shift(4, data.Hidden);
  14092. o.write_shift(4, data.iTabID);
  14093. write_RelID(data.strRelID, o);
  14094. write_XLWideString(data.name.slice(0,31), o);
  14095. return o.length > o.l ? o.slice(0, o.l) : o;
  14096. }
  14097. /* [MS-XLSB] 2.4.815 BrtWbProp */
  14098. function parse_BrtWbProp(data, length) {
  14099. var o = ({});
  14100. var flags = data.read_shift(4);
  14101. o.defaultThemeVersion = data.read_shift(4);
  14102. var strName = (length > 8) ? parse_XLWideString(data) : "";
  14103. if(strName.length > 0) o.CodeName = strName;
  14104. o.autoCompressPictures = !!(flags & 0x10000);
  14105. o.backupFile = !!(flags & 0x40);
  14106. o.checkCompatibility = !!(flags & 0x1000);
  14107. o.date1904 = !!(flags & 0x01);
  14108. o.filterPrivacy = !!(flags & 0x08);
  14109. o.hidePivotFieldList = !!(flags & 0x400);
  14110. o.promptedSolutions = !!(flags & 0x10);
  14111. o.publishItems = !!(flags & 0x800);
  14112. o.refreshAllConnections = !!(flags & 0x40000);
  14113. o.saveExternalLinkValues = !!(flags & 0x80);
  14114. o.showBorderUnselectedTables = !!(flags & 0x04);
  14115. o.showInkAnnotation = !!(flags & 0x20);
  14116. o.showObjects = ["all", "placeholders", "none"][(flags >> 13) & 0x03];
  14117. o.showPivotChartFilter = !!(flags & 0x8000);
  14118. o.updateLinks = ["userSet", "never", "always"][(flags >> 8) & 0x03];
  14119. return o;
  14120. }
  14121. function write_BrtWbProp(data, o) {
  14122. if(!o) o = new_buf(72);
  14123. var flags = 0;
  14124. if(data) {
  14125. /* TODO: mirror parse_BrtWbProp fields */
  14126. if(data.filterPrivacy) flags |= 0x08;
  14127. }
  14128. o.write_shift(4, flags);
  14129. o.write_shift(4, 0);
  14130. write_XLSBCodeName(data && data.CodeName || "ThisWorkbook", o);
  14131. return o.slice(0, o.l);
  14132. }
  14133. function parse_BrtFRTArchID$(data, length) {
  14134. var o = {};
  14135. data.read_shift(4);
  14136. o.ArchID = data.read_shift(4);
  14137. data.l += length - 8;
  14138. return o;
  14139. }
  14140. /* [MS-XLSB] 2.4.687 BrtName */
  14141. function parse_BrtName(data, length, opts) {
  14142. var end = data.l + length;
  14143. data.l += 4; //var flags = data.read_shift(4);
  14144. data.l += 1; //var chKey = data.read_shift(1);
  14145. var itab = data.read_shift(4);
  14146. var name = parse_XLNameWideString(data);
  14147. var formula = parse_XLSBNameParsedFormula(data, 0, opts);
  14148. var comment = parse_XLNullableWideString(data);
  14149. //if(0 /* fProc */) {
  14150. // unusedstring1: XLNullableWideString
  14151. // description: XLNullableWideString
  14152. // helpTopic: XLNullableWideString
  14153. // unusedstring2: XLNullableWideString
  14154. //}
  14155. data.l = end;
  14156. var out = ({Name:name, Ptg:formula});
  14157. if(itab < 0xFFFFFFF) out.Sheet = itab;
  14158. if(comment) out.Comment = comment;
  14159. return out;
  14160. }
  14161. /* [MS-XLSB] 2.1.7.61 Workbook */
  14162. function parse_wb_bin(data, opts) {
  14163. var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, xmlns: "" };
  14164. var state = [];
  14165. var pass = false;
  14166. if(!opts) opts = {};
  14167. opts.biff = 12;
  14168. var Names = [];
  14169. var supbooks = ([[]]);
  14170. supbooks.SheetNames = [];
  14171. supbooks.XTI = [];
  14172. recordhopper(data, function hopper_wb(val, R_n, RT) {
  14173. switch(RT) {
  14174. case 0x009C: /* 'BrtBundleSh' */
  14175. supbooks.SheetNames.push(val.name);
  14176. wb.Sheets.push(val); break;
  14177. case 0x0099: /* 'BrtWbProp' */
  14178. wb.WBProps = val; break;
  14179. case 0x0027: /* 'BrtName' */
  14180. if(val.Sheet != null) opts.SID = val.Sheet;
  14181. val.Ref = stringify_formula(val.Ptg, null, null, supbooks, opts);
  14182. delete opts.SID;
  14183. delete val.Ptg;
  14184. Names.push(val);
  14185. break;
  14186. case 0x040C: /* 'BrtNameExt' */ break;
  14187. case 0x0165: /* 'BrtSupSelf' */
  14188. case 0x0166: /* 'BrtSupSame' */
  14189. case 0x0163: /* 'BrtSupBookSrc' */
  14190. case 0x029B: /* 'BrtSupAddin' */
  14191. if(!supbooks[0].length) supbooks[0] = [RT, val];
  14192. else supbooks.push([RT, val]);
  14193. supbooks[supbooks.length - 1].XTI = [];
  14194. break;
  14195. case 0x016A: /* 'BrtExternSheet' */
  14196. if(supbooks.length === 0) { supbooks[0] = []; supbooks[0].XTI = []; }
  14197. supbooks[supbooks.length - 1].XTI = supbooks[supbooks.length - 1].XTI.concat(val);
  14198. supbooks.XTI = supbooks.XTI.concat(val);
  14199. break;
  14200. case 0x0169: /* 'BrtPlaceholderName' */
  14201. break;
  14202. /* case 'BrtModelTimeGroupingCalcCol' */
  14203. case 0x0C00: /* 'BrtUid' */
  14204. case 0x0C01: /* 'BrtRevisionPtr' */
  14205. case 0x0817: /* 'BrtAbsPath15' */
  14206. case 0x0216: /* 'BrtBookProtection' */
  14207. case 0x02A5: /* 'BrtBookProtectionIso' */
  14208. case 0x009E: /* 'BrtBookView' */
  14209. case 0x009D: /* 'BrtCalcProp' */
  14210. case 0x0262: /* 'BrtCrashRecErr' */
  14211. case 0x0802: /* 'BrtDecoupledPivotCacheID' */
  14212. case 0x009B: /* 'BrtFileRecover' */
  14213. case 0x0224: /* 'BrtFileSharing' */
  14214. case 0x02A4: /* 'BrtFileSharingIso' */
  14215. case 0x0080: /* 'BrtFileVersion' */
  14216. case 0x0299: /* 'BrtFnGroup' */
  14217. case 0x0850: /* 'BrtModelRelationship' */
  14218. case 0x084D: /* 'BrtModelTable' */
  14219. case 0x0225: /* 'BrtOleSize' */
  14220. case 0x0805: /* 'BrtPivotTableRef' */
  14221. case 0x0254: /* 'BrtSmartTagType' */
  14222. case 0x081C: /* 'BrtTableSlicerCacheID' */
  14223. case 0x081B: /* 'BrtTableSlicerCacheIDs' */
  14224. case 0x0822: /* 'BrtTimelineCachePivotCacheID' */
  14225. case 0x018D: /* 'BrtUserBookView' */
  14226. case 0x009A: /* 'BrtWbFactoid' */
  14227. case 0x045D: /* 'BrtWbProp14' */
  14228. case 0x0229: /* 'BrtWebOpt' */
  14229. case 0x082B: /* 'BrtWorkBookPr15' */
  14230. break;
  14231. case 0x0023: /* 'BrtFRTBegin' */
  14232. state.push(R_n); pass = true; break;
  14233. case 0x0024: /* 'BrtFRTEnd' */
  14234. state.pop(); pass = false; break;
  14235. case 0x0025: /* 'BrtACBegin' */
  14236. state.push(R_n); pass = true; break;
  14237. case 0x0026: /* 'BrtACEnd' */
  14238. state.pop(); pass = false; break;
  14239. case 0x0010: /* 'BrtFRTArchID$' */ break;
  14240. default:
  14241. if((R_n||"").indexOf("Begin") > 0){/* empty */}
  14242. else if((R_n||"").indexOf("End") > 0){/* empty */}
  14243. else if(!pass || (opts.WTF && state[state.length-1] != "BrtACBegin" && state[state.length-1] != "BrtFRTBegin")) throw new Error("Unexpected record " + RT + " " + R_n);
  14244. }
  14245. }, opts);
  14246. parse_wb_defaults(wb);
  14247. // $FlowIgnore
  14248. wb.Names = Names;
  14249. (wb).supbooks = supbooks;
  14250. return wb;
  14251. }
  14252. function write_BUNDLESHS(ba, wb) {
  14253. write_record(ba, "BrtBeginBundleShs");
  14254. for(var idx = 0; idx != wb.SheetNames.length; ++idx) {
  14255. var viz = wb.Workbook && wb.Workbook.Sheets && wb.Workbook.Sheets[idx] && wb.Workbook.Sheets[idx].Hidden || 0;
  14256. var d = { Hidden: viz, iTabID: idx+1, strRelID: 'rId' + (idx+1), name: wb.SheetNames[idx] };
  14257. write_record(ba, "BrtBundleSh", write_BrtBundleSh(d));
  14258. }
  14259. write_record(ba, "BrtEndBundleShs");
  14260. }
  14261. /* [MS-XLSB] 2.4.649 BrtFileVersion */
  14262. function write_BrtFileVersion(data, o) {
  14263. if(!o) o = new_buf(127);
  14264. for(var i = 0; i != 4; ++i) o.write_shift(4, 0);
  14265. write_XLWideString("SheetJS", o);
  14266. write_XLWideString(XLSX.version, o);
  14267. write_XLWideString(XLSX.version, o);
  14268. write_XLWideString("7262", o);
  14269. o.length = o.l;
  14270. return o.length > o.l ? o.slice(0, o.l) : o;
  14271. }
  14272. /* [MS-XLSB] 2.4.301 BrtBookView */
  14273. function write_BrtBookView(idx, o) {
  14274. if(!o) o = new_buf(29);
  14275. o.write_shift(-4, 0);
  14276. o.write_shift(-4, 460);
  14277. o.write_shift(4, 28800);
  14278. o.write_shift(4, 17600);
  14279. o.write_shift(4, 500);
  14280. o.write_shift(4, idx);
  14281. o.write_shift(4, idx);
  14282. var flags = 0x78;
  14283. o.write_shift(1, flags);
  14284. return o.length > o.l ? o.slice(0, o.l) : o;
  14285. }
  14286. function write_BOOKVIEWS(ba, wb) {
  14287. /* required if hidden tab appears before visible tab */
  14288. if(!wb.Workbook || !wb.Workbook.Sheets) return;
  14289. var sheets = wb.Workbook.Sheets;
  14290. var i = 0, vistab = -1, hidden = -1;
  14291. for(; i < sheets.length; ++i) {
  14292. if(!sheets[i] || !sheets[i].Hidden && vistab == -1) vistab = i;
  14293. else if(sheets[i].Hidden == 1 && hidden == -1) hidden = i;
  14294. }
  14295. if(hidden > vistab) return;
  14296. write_record(ba, "BrtBeginBookViews");
  14297. write_record(ba, "BrtBookView", write_BrtBookView(vistab));
  14298. /* 1*(BrtBookView *FRT) */
  14299. write_record(ba, "BrtEndBookViews");
  14300. }
  14301. /* [MS-XLSB] 2.4.305 BrtCalcProp */
  14302. /*function write_BrtCalcProp(data, o) {
  14303. if(!o) o = new_buf(26);
  14304. o.write_shift(4,0); // force recalc
  14305. o.write_shift(4,1);
  14306. o.write_shift(4,0);
  14307. write_Xnum(0, o);
  14308. o.write_shift(-4, 1023);
  14309. o.write_shift(1, 0x33);
  14310. o.write_shift(1, 0x00);
  14311. return o;
  14312. }*/
  14313. /* [MS-XLSB] 2.4.646 BrtFileRecover */
  14314. /*function write_BrtFileRecover(data, o) {
  14315. if(!o) o = new_buf(1);
  14316. o.write_shift(1,0);
  14317. return o;
  14318. }*/
  14319. /* [MS-XLSB] 2.1.7.61 Workbook */
  14320. function write_wb_bin(wb, opts) {
  14321. var ba = buf_array();
  14322. write_record(ba, "BrtBeginBook");
  14323. write_record(ba, "BrtFileVersion", write_BrtFileVersion());
  14324. /* [[BrtFileSharingIso] BrtFileSharing] */
  14325. write_record(ba, "BrtWbProp", write_BrtWbProp(wb.Workbook && wb.Workbook.WBProps || null));
  14326. /* [ACABSPATH] */
  14327. /* [[BrtBookProtectionIso] BrtBookProtection] */
  14328. write_BOOKVIEWS(ba, wb, opts);
  14329. write_BUNDLESHS(ba, wb, opts);
  14330. /* [FNGROUP] */
  14331. /* [EXTERNALS] */
  14332. /* *BrtName */
  14333. /* write_record(ba, "BrtCalcProp", write_BrtCalcProp()); */
  14334. /* [BrtOleSize] */
  14335. /* *(BrtUserBookView *FRT) */
  14336. /* [PIVOTCACHEIDS] */
  14337. /* [BrtWbFactoid] */
  14338. /* [SMARTTAGTYPES] */
  14339. /* [BrtWebOpt] */
  14340. /* write_record(ba, "BrtFileRecover", write_BrtFileRecover()); */
  14341. /* [WEBPUBITEMS] */
  14342. /* [CRERRS] */
  14343. /* FRTWORKBOOK */
  14344. write_record(ba, "BrtEndBook");
  14345. return ba.end();
  14346. }
  14347. function parse_wb(data, name, opts) {
  14348. if(name.slice(-4)===".bin") return parse_wb_bin((data), opts);
  14349. return parse_wb_xml((data), opts);
  14350. }
  14351. function parse_ws(data, name, idx, opts, rels, wb, themes, styles) {
  14352. if(name.slice(-4)===".bin") return parse_ws_bin((data), opts, idx, rels, wb, themes, styles);
  14353. return parse_ws_xml((data), opts, idx, rels, wb, themes, styles);
  14354. }
  14355. function parse_cs(data, name, idx, opts, rels, wb, themes, styles) {
  14356. if(name.slice(-4)===".bin") return parse_cs_bin((data), opts, idx, rels, wb, themes, styles);
  14357. return parse_cs_xml((data), opts, idx, rels, wb, themes, styles);
  14358. }
  14359. function parse_ms(data, name, idx, opts, rels, wb, themes, styles) {
  14360. if(name.slice(-4)===".bin") return parse_ms_bin((data), opts, idx, rels, wb, themes, styles);
  14361. return parse_ms_xml((data), opts, idx, rels, wb, themes, styles);
  14362. }
  14363. function parse_ds(data, name, idx, opts, rels, wb, themes, styles) {
  14364. if(name.slice(-4)===".bin") return parse_ds_bin((data), opts, idx, rels, wb, themes, styles);
  14365. return parse_ds_xml((data), opts, idx, rels, wb, themes, styles);
  14366. }
  14367. function parse_sty(data, name, themes, opts) {
  14368. if(name.slice(-4)===".bin") return parse_sty_bin((data), themes, opts);
  14369. return parse_sty_xml((data), themes, opts);
  14370. }
  14371. function parse_theme(data, name, opts) {
  14372. return parse_theme_xml(data, opts);
  14373. }
  14374. function parse_sst(data, name, opts) {
  14375. if(name.slice(-4)===".bin") return parse_sst_bin((data), opts);
  14376. return parse_sst_xml((data), opts);
  14377. }
  14378. function parse_cmnt(data, name, opts) {
  14379. if(name.slice(-4)===".bin") return parse_comments_bin((data), opts);
  14380. return parse_comments_xml((data), opts);
  14381. }
  14382. function parse_cc(data, name, opts) {
  14383. if(name.slice(-4)===".bin") return parse_cc_bin((data), name, opts);
  14384. return parse_cc_xml((data), name, opts);
  14385. }
  14386. function parse_xlink(data, rel, name, opts) {
  14387. if(name.slice(-4)===".bin") return parse_xlink_bin((data), rel, name, opts);
  14388. return parse_xlink_xml((data), rel, name, opts);
  14389. }
  14390. function write_wb(wb, name, opts) {
  14391. return (name.slice(-4)===".bin" ? write_wb_bin : write_wb_xml)(wb, opts);
  14392. }
  14393. function write_ws(data, name, opts, wb, rels) {
  14394. return (name.slice(-4)===".bin" ? write_ws_bin : write_ws_xml)(data, opts, wb, rels);
  14395. }
  14396. // eslint-disable-next-line no-unused-vars
  14397. function write_cs(data, name, opts, wb, rels) {
  14398. return (name.slice(-4)===".bin" ? write_cs_bin : write_cs_xml)(data, opts, wb, rels);
  14399. }
  14400. function write_sty(data, name, opts) {
  14401. return (name.slice(-4)===".bin" ? write_sty_bin : write_sty_xml)(data, opts);
  14402. }
  14403. function write_sst(data, name, opts) {
  14404. return (name.slice(-4)===".bin" ? write_sst_bin : write_sst_xml)(data, opts);
  14405. }
  14406. function write_cmnt(data, name, opts) {
  14407. return (name.slice(-4)===".bin" ? write_comments_bin : write_comments_xml)(data, opts);
  14408. }
  14409. /*
  14410. function write_cc(data, name:string, opts) {
  14411. return (name.slice(-4)===".bin" ? write_cc_bin : write_cc_xml)(data, opts);
  14412. }
  14413. */
  14414. var attregexg2=/([\w:]+)=((?:")([^"]*)(?:")|(?:')([^']*)(?:'))/g;
  14415. var attregex2=/([\w:]+)=((?:")(?:[^"]*)(?:")|(?:')(?:[^']*)(?:'))/;
  14416. function xlml_parsexmltag(tag, skip_root) {
  14417. var words = tag.split(/\s+/);
  14418. var z = ([]); if(!skip_root) z[0] = words[0];
  14419. if(words.length === 1) return z;
  14420. var m = tag.match(attregexg2), y, j, w, i;
  14421. if(m) for(i = 0; i != m.length; ++i) {
  14422. y = m[i].match(attregex2);
  14423. if((j=y[1].indexOf(":")) === -1) z[y[1]] = y[2].slice(1,y[2].length-1);
  14424. else {
  14425. if(y[1].slice(0,6) === "xmlns:") w = "xmlns"+y[1].slice(6);
  14426. else w = y[1].slice(j+1);
  14427. z[w] = y[2].slice(1,y[2].length-1);
  14428. }
  14429. }
  14430. return z;
  14431. }
  14432. function xlml_parsexmltagobj(tag) {
  14433. var words = tag.split(/\s+/);
  14434. var z = {};
  14435. if(words.length === 1) return z;
  14436. var m = tag.match(attregexg2), y, j, w, i;
  14437. if(m) for(i = 0; i != m.length; ++i) {
  14438. y = m[i].match(attregex2);
  14439. if((j=y[1].indexOf(":")) === -1) z[y[1]] = y[2].slice(1,y[2].length-1);
  14440. else {
  14441. if(y[1].slice(0,6) === "xmlns:") w = "xmlns"+y[1].slice(6);
  14442. else w = y[1].slice(j+1);
  14443. z[w] = y[2].slice(1,y[2].length-1);
  14444. }
  14445. }
  14446. return z;
  14447. }
  14448. // ----
  14449. function xlml_format(format, value) {
  14450. var fmt = XLMLFormatMap[format] || unescapexml(format);
  14451. if(fmt === "General") return SSF._general(value);
  14452. return SSF.format(fmt, value);
  14453. }
  14454. function xlml_set_custprop(Custprops, key, cp, val) {
  14455. var oval = val;
  14456. switch((cp[0].match(/dt:dt="([\w.]+)"/)||["",""])[1]) {
  14457. case "boolean": oval = parsexmlbool(val); break;
  14458. case "i2": case "int": oval = parseInt(val, 10); break;
  14459. case "r4": case "float": oval = parseFloat(val); break;
  14460. case "date": case "dateTime.tz": oval = parseDate(val); break;
  14461. case "i8": case "string": case "fixed": case "uuid": case "bin.base64": break;
  14462. default: throw new Error("bad custprop:" + cp[0]);
  14463. }
  14464. Custprops[unescapexml(key)] = oval;
  14465. }
  14466. function safe_format_xlml(cell, nf, o) {
  14467. if(cell.t === 'z') return;
  14468. if(!o || o.cellText !== false) try {
  14469. if(cell.t === 'e') { cell.w = cell.w || BErr[cell.v]; }
  14470. else if(nf === "General") {
  14471. if(cell.t === 'n') {
  14472. if((cell.v|0) === cell.v) cell.w = SSF._general_int(cell.v);
  14473. else cell.w = SSF._general_num(cell.v);
  14474. }
  14475. else cell.w = SSF._general(cell.v);
  14476. }
  14477. else cell.w = xlml_format(nf||"General", cell.v);
  14478. } catch(e) { if(o.WTF) throw e; }
  14479. try {
  14480. var z = XLMLFormatMap[nf]||nf||"General";
  14481. if(o.cellNF) cell.z = z;
  14482. if(o.cellDates && cell.t == 'n' && SSF.is_date(z)) {
  14483. var _d = SSF.parse_date_code(cell.v); if(_d) { cell.t = 'd'; cell.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  14484. }
  14485. } catch(e) { if(o.WTF) throw e; }
  14486. }
  14487. function process_style_xlml(styles, stag, opts) {
  14488. if(opts.cellStyles) {
  14489. if(stag.Interior) {
  14490. var I = stag.Interior;
  14491. if(I.Pattern) I.patternType = XLMLPatternTypeMap[I.Pattern] || I.Pattern;
  14492. }
  14493. }
  14494. styles[stag.ID] = stag;
  14495. }
  14496. /* TODO: there must exist some form of OSP-blessed spec */
  14497. function parse_xlml_data(xml, ss, data, cell, base, styles, csty, row, arrayf, o) {
  14498. var nf = "General", sid = cell.StyleID, S = {}; o = o || {};
  14499. var interiors = [];
  14500. var i = 0;
  14501. if(sid === undefined && row) sid = row.StyleID;
  14502. if(sid === undefined && csty) sid = csty.StyleID;
  14503. while(styles[sid] !== undefined) {
  14504. if(styles[sid].nf) nf = styles[sid].nf;
  14505. if(styles[sid].Interior) interiors.push(styles[sid].Interior);
  14506. if(!styles[sid].Parent) break;
  14507. sid = styles[sid].Parent;
  14508. }
  14509. switch(data.Type) {
  14510. case 'Boolean':
  14511. cell.t = 'b';
  14512. cell.v = parsexmlbool(xml);
  14513. break;
  14514. case 'String':
  14515. cell.t = 's'; cell.r = xlml_fixstr(unescapexml(xml));
  14516. cell.v = (xml.indexOf("<") > -1 ? unescapexml(ss||xml).replace(/<.*?>/g, "") : cell.r); // todo: BR etc
  14517. break;
  14518. case 'DateTime':
  14519. if(xml.slice(-1) != "Z") xml += "Z";
  14520. cell.v = (parseDate(xml) - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  14521. if(cell.v !== cell.v) cell.v = unescapexml(xml);
  14522. else if(cell.v<60) cell.v = cell.v -1;
  14523. if(!nf || nf == "General") nf = "yyyy-mm-dd";
  14524. /* falls through */
  14525. case 'Number':
  14526. if(cell.v === undefined) cell.v=+xml;
  14527. if(!cell.t) cell.t = 'n';
  14528. break;
  14529. case 'Error': cell.t = 'e'; cell.v = RBErr[xml]; if(o.cellText !== false) cell.w = xml; break;
  14530. default:
  14531. if(xml == "" && ss == "") { cell.t = 'z'; }
  14532. else { cell.t = 's'; cell.v = xlml_fixstr(ss||xml); }
  14533. break;
  14534. }
  14535. safe_format_xlml(cell, nf, o);
  14536. if(o.cellFormula !== false) {
  14537. if(cell.Formula) {
  14538. var fstr = unescapexml(cell.Formula);
  14539. /* strictly speaking, the leading = is required but some writers omit */
  14540. if(fstr.charCodeAt(0) == 61 /* = */) fstr = fstr.slice(1);
  14541. cell.f = rc_to_a1(fstr, base);
  14542. delete cell.Formula;
  14543. if(cell.ArrayRange == "RC") cell.F = rc_to_a1("RC:RC", base);
  14544. else if(cell.ArrayRange) {
  14545. cell.F = rc_to_a1(cell.ArrayRange, base);
  14546. arrayf.push([safe_decode_range(cell.F), cell.F]);
  14547. }
  14548. } else {
  14549. for(i = 0; i < arrayf.length; ++i)
  14550. if(base.r >= arrayf[i][0].s.r && base.r <= arrayf[i][0].e.r)
  14551. if(base.c >= arrayf[i][0].s.c && base.c <= arrayf[i][0].e.c)
  14552. cell.F = arrayf[i][1];
  14553. }
  14554. }
  14555. if(o.cellStyles) {
  14556. interiors.forEach(function(x) {
  14557. if(!S.patternType && x.patternType) S.patternType = x.patternType;
  14558. });
  14559. cell.s = S;
  14560. }
  14561. if(cell.StyleID !== undefined) cell.ixfe = cell.StyleID;
  14562. }
  14563. function xlml_clean_comment(comment) {
  14564. comment.t = comment.v || "";
  14565. comment.t = comment.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
  14566. comment.v = comment.w = comment.ixfe = undefined;
  14567. }
  14568. function xlml_normalize(d) {
  14569. if(has_buf && Buffer.isBuffer(d)) return d.toString('utf8');
  14570. if(typeof d === 'string') return d;
  14571. /* duktape */
  14572. if(typeof Uint8Array !== 'undefined' && d instanceof Uint8Array) return utf8read(a2s(ab2a(d)));
  14573. throw new Error("Bad input format: expected Buffer or string");
  14574. }
  14575. /* TODO: Everything */
  14576. /* UOS uses CJK in tags */
  14577. var xlmlregex = /<(\/?)([^\s?><!\/:]*:|)([^\s?<>:\/]+)(?:[\s?:\/][^>]*)?>/mg;
  14578. //var xlmlregex = /<(\/?)([a-z0-9]*:|)(\w+)[^>]*>/mg;
  14579. function parse_xlml_xml(d, _opts) {
  14580. var opts = _opts || {};
  14581. make_ssf(SSF);
  14582. var str = debom(xlml_normalize(d));
  14583. if(opts.type == 'binary' || opts.type == 'array' || opts.type == 'base64') {
  14584. if(typeof cptable !== 'undefined') str = cptable.utils.decode(65001, char_codes(str));
  14585. else str = utf8read(str);
  14586. }
  14587. var opening = str.slice(0, 1024).toLowerCase(), ishtml = false;
  14588. if(opening.indexOf("<?xml") == -1) ["html", "table", "head", "meta", "script", "style", "div"].forEach(function(tag) { if(opening.indexOf("<" + tag) >= 0) ishtml = true; });
  14589. if(ishtml) return HTML_.to_workbook(str, opts);
  14590. var Rn;
  14591. var state = [], tmp;
  14592. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  14593. var sheets = {}, sheetnames = [], cursheet = (opts.dense ? [] : {}), sheetname = "";
  14594. var table = {}, cell = ({}), row = {};// eslint-disable-line no-unused-vars
  14595. var dtag = xlml_parsexmltag('<Data ss:Type="String">'), didx = 0;
  14596. var c = 0, r = 0;
  14597. var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  14598. var styles = {}, stag = {};
  14599. var ss = "", fidx = 0;
  14600. var merges = [];
  14601. var Props = {}, Custprops = {}, pidx = 0, cp = [];
  14602. var comments = [], comment = ({});
  14603. var cstys = [], csty, seencol = false;
  14604. var arrayf = [];
  14605. var rowinfo = [], rowobj = {}, cc = 0, rr = 0;
  14606. var Workbook = ({ Sheets:[], WBProps:{date1904:false} }), wsprops = {};
  14607. xlmlregex.lastIndex = 0;
  14608. str = str.replace(/<!--([\s\S]*?)-->/mg,"");
  14609. var raw_Rn3 = "";
  14610. while((Rn = xlmlregex.exec(str))) switch((Rn[3] = (raw_Rn3 = Rn[3]).toLowerCase())) {
  14611. case 'data' /*case 'Data'*/:
  14612. if(raw_Rn3 == "data") {
  14613. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  14614. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  14615. break;
  14616. }
  14617. if(state[state.length-1][1]) break;
  14618. if(Rn[1]==='/') parse_xlml_data(str.slice(didx, Rn.index), ss, dtag, state[state.length-1][0]==/*"Comment"*/"comment"?comment:cell, {c:c,r:r}, styles, cstys[c], row, arrayf, opts);
  14619. else { ss = ""; dtag = xlml_parsexmltag(Rn[0]); didx = Rn.index + Rn[0].length; }
  14620. break;
  14621. case 'cell' /*case 'Cell'*/:
  14622. if(Rn[1]==='/'){
  14623. if(comments.length > 0) cell.c = comments;
  14624. if((!opts.sheetRows || opts.sheetRows > r) && cell.v !== undefined) {
  14625. if(opts.dense) {
  14626. if(!cursheet[r]) cursheet[r] = [];
  14627. cursheet[r][c] = cell;
  14628. } else cursheet[encode_col(c) + encode_row(r)] = cell;
  14629. }
  14630. if(cell.HRef) {
  14631. cell.l = ({Target:cell.HRef});
  14632. if(cell.HRefScreenTip) cell.l.Tooltip = cell.HRefScreenTip;
  14633. delete cell.HRef; delete cell.HRefScreenTip;
  14634. }
  14635. if(cell.MergeAcross || cell.MergeDown) {
  14636. cc = c + (parseInt(cell.MergeAcross,10)|0);
  14637. rr = r + (parseInt(cell.MergeDown,10)|0);
  14638. merges.push({s:{c:c,r:r},e:{c:cc,r:rr}});
  14639. }
  14640. if(!opts.sheetStubs) { if(cell.MergeAcross) c = cc + 1; else ++c; }
  14641. else if(cell.MergeAcross || cell.MergeDown) {
  14642. for(var cma = c; cma <= cc; ++cma) {
  14643. for(var cmd = r; cmd <= rr; ++cmd) {
  14644. if(cma > c || cmd > r) {
  14645. if(opts.dense) {
  14646. if(!cursheet[cmd]) cursheet[cmd] = [];
  14647. cursheet[cmd][cma] = {t:'z'};
  14648. } else cursheet[encode_col(cma) + encode_row(cmd)] = {t:'z'};
  14649. }
  14650. }
  14651. }
  14652. c = cc + 1;
  14653. }
  14654. else ++c;
  14655. } else {
  14656. cell = xlml_parsexmltagobj(Rn[0]);
  14657. if(cell.Index) c = +cell.Index - 1;
  14658. if(c < refguess.s.c) refguess.s.c = c;
  14659. if(c > refguess.e.c) refguess.e.c = c;
  14660. if(Rn[0].slice(-2) === "/>") ++c;
  14661. comments = [];
  14662. }
  14663. break;
  14664. case 'row' /*case 'Row'*/:
  14665. if(Rn[1]==='/' || Rn[0].slice(-2) === "/>") {
  14666. if(r < refguess.s.r) refguess.s.r = r;
  14667. if(r > refguess.e.r) refguess.e.r = r;
  14668. if(Rn[0].slice(-2) === "/>") {
  14669. row = xlml_parsexmltag(Rn[0]);
  14670. if(row.Index) r = +row.Index - 1;
  14671. }
  14672. c = 0; ++r;
  14673. } else {
  14674. row = xlml_parsexmltag(Rn[0]);
  14675. if(row.Index) r = +row.Index - 1;
  14676. rowobj = {};
  14677. if(row.AutoFitHeight == "0" || row.Height) {
  14678. rowobj.hpx = parseInt(row.Height, 10); rowobj.hpt = px2pt(rowobj.hpx);
  14679. rowinfo[r] = rowobj;
  14680. }
  14681. if(row.Hidden == "1") { rowobj.hidden = true; rowinfo[r] = rowobj; }
  14682. }
  14683. break;
  14684. case 'worksheet' /*case 'Worksheet'*/: /* TODO: read range from FullRows/FullColumns */
  14685. if(Rn[1]==='/'){
  14686. if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));
  14687. sheetnames.push(sheetname);
  14688. if(refguess.s.r <= refguess.e.r && refguess.s.c <= refguess.e.c) {
  14689. cursheet["!ref"] = encode_range(refguess);
  14690. if(opts.sheetRows && opts.sheetRows <= refguess.e.r) {
  14691. cursheet["!fullref"] = cursheet["!ref"];
  14692. refguess.e.r = opts.sheetRows - 1;
  14693. cursheet["!ref"] = encode_range(refguess);
  14694. }
  14695. }
  14696. if(merges.length) cursheet["!merges"] = merges;
  14697. if(cstys.length > 0) cursheet["!cols"] = cstys;
  14698. if(rowinfo.length > 0) cursheet["!rows"] = rowinfo;
  14699. sheets[sheetname] = cursheet;
  14700. } else {
  14701. refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
  14702. r = c = 0;
  14703. state.push([Rn[3], false]);
  14704. tmp = xlml_parsexmltag(Rn[0]);
  14705. sheetname = unescapexml(tmp.Name);
  14706. cursheet = (opts.dense ? [] : {});
  14707. merges = [];
  14708. arrayf = [];
  14709. rowinfo = [];
  14710. wsprops = {name:sheetname, Hidden:0};
  14711. Workbook.Sheets.push(wsprops);
  14712. }
  14713. break;
  14714. case 'table' /*case 'Table'*/:
  14715. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  14716. else if(Rn[0].slice(-2) == "/>") break;
  14717. else {
  14718. table = xlml_parsexmltag(Rn[0]);
  14719. state.push([Rn[3], false]);
  14720. cstys = []; seencol = false;
  14721. }
  14722. break;
  14723. case 'style' /*case 'Style'*/:
  14724. if(Rn[1]==='/') process_style_xlml(styles, stag, opts);
  14725. else stag = xlml_parsexmltag(Rn[0]);
  14726. break;
  14727. case 'numberformat' /*case 'NumberFormat'*/:
  14728. stag.nf = unescapexml(xlml_parsexmltag(Rn[0]).Format || "General");
  14729. if(XLMLFormatMap[stag.nf]) stag.nf = XLMLFormatMap[stag.nf];
  14730. for(var ssfidx = 0; ssfidx != 0x188; ++ssfidx) if(SSF._table[ssfidx] == stag.nf) break;
  14731. if(ssfidx == 0x188) for(ssfidx = 0x39; ssfidx != 0x188; ++ssfidx) if(SSF._table[ssfidx] == null) { SSF.load(stag.nf, ssfidx); break; }
  14732. break;
  14733. case 'column' /*case 'Column'*/:
  14734. if(state[state.length-1][0] !== /*'Table'*/'table') break;
  14735. csty = xlml_parsexmltag(Rn[0]);
  14736. if(csty.Hidden) { csty.hidden = true; delete csty.Hidden; }
  14737. if(csty.Width) csty.wpx = parseInt(csty.Width, 10);
  14738. if(!seencol && csty.wpx > 10) {
  14739. seencol = true; MDW = DEF_MDW; //find_mdw_wpx(csty.wpx);
  14740. for(var _col = 0; _col < cstys.length; ++_col) if(cstys[_col]) process_col(cstys[_col]);
  14741. }
  14742. if(seencol) process_col(csty);
  14743. cstys[(csty.Index-1||cstys.length)] = csty;
  14744. for(var i = 0; i < +csty.Span; ++i) cstys[cstys.length] = dup(csty);
  14745. break;
  14746. case 'namedrange' /*case 'NamedRange'*/:
  14747. if(Rn[1]==='/') break;
  14748. if(!Workbook.Names) Workbook.Names = [];
  14749. var _NamedRange = parsexmltag(Rn[0]);
  14750. var _DefinedName = ({
  14751. Name: _NamedRange.Name,
  14752. Ref: rc_to_a1(_NamedRange.RefersTo.slice(1), {r:0, c:0})
  14753. });
  14754. if(Workbook.Sheets.length>0) _DefinedName.Sheet=Workbook.Sheets.length-1;
  14755. Workbook.Names.push(_DefinedName);
  14756. break;
  14757. case 'namedcell' /*case 'NamedCell'*/: break;
  14758. case 'b' /*case 'B'*/: break;
  14759. case 'i' /*case 'I'*/: break;
  14760. case 'u' /*case 'U'*/: break;
  14761. case 's' /*case 'S'*/: break;
  14762. case 'em' /*case 'EM'*/: break;
  14763. case 'h2' /*case 'H2'*/: break;
  14764. case 'h3' /*case 'H3'*/: break;
  14765. case 'sub' /*case 'Sub'*/: break;
  14766. case 'sup' /*case 'Sup'*/: break;
  14767. case 'span' /*case 'Span'*/: break;
  14768. case 'alignment' /*case 'Alignment'*/:
  14769. break;
  14770. case 'borders' /*case 'Borders'*/: break;
  14771. case 'border' /*case 'Border'*/: break;
  14772. case 'font' /*case 'Font'*/:
  14773. if(Rn[0].slice(-2) === "/>") break;
  14774. else if(Rn[1]==="/") ss += str.slice(fidx, Rn.index);
  14775. else fidx = Rn.index + Rn[0].length;
  14776. break;
  14777. case 'interior' /*case 'Interior'*/:
  14778. if(!opts.cellStyles) break;
  14779. stag.Interior = xlml_parsexmltag(Rn[0]);
  14780. break;
  14781. case 'protection' /*case 'Protection'*/: break;
  14782. case 'author' /*case 'Author'*/:
  14783. case 'title' /*case 'Title'*/:
  14784. case 'description' /*case 'Description'*/:
  14785. case 'created' /*case 'Created'*/:
  14786. case 'keywords' /*case 'Keywords'*/:
  14787. case 'subject' /*case 'Subject'*/:
  14788. case 'category' /*case 'Category'*/:
  14789. case 'company' /*case 'Company'*/:
  14790. case 'lastauthor' /*case 'LastAuthor'*/:
  14791. case 'lastsaved' /*case 'LastSaved'*/:
  14792. case 'lastprinted' /*case 'LastPrinted'*/:
  14793. case 'version' /*case 'Version'*/:
  14794. case 'revision' /*case 'Revision'*/:
  14795. case 'totaltime' /*case 'TotalTime'*/:
  14796. case 'hyperlinkbase' /*case 'HyperlinkBase'*/:
  14797. case 'manager' /*case 'Manager'*/:
  14798. case 'contentstatus' /*case 'ContentStatus'*/:
  14799. case 'identifier' /*case 'Identifier'*/:
  14800. case 'language' /*case 'Language'*/:
  14801. case 'appname' /*case 'AppName'*/:
  14802. if(Rn[0].slice(-2) === "/>") break;
  14803. else if(Rn[1]==="/") xlml_set_prop(Props, raw_Rn3, str.slice(pidx, Rn.index));
  14804. else pidx = Rn.index + Rn[0].length;
  14805. break;
  14806. case 'paragraphs' /*case 'Paragraphs'*/: break;
  14807. case 'styles' /*case 'Styles'*/:
  14808. case 'workbook' /*case 'Workbook'*/:
  14809. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  14810. else state.push([Rn[3], false]);
  14811. break;
  14812. case 'comment' /*case 'Comment'*/:
  14813. if(Rn[1]==='/'){
  14814. if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));
  14815. xlml_clean_comment(comment);
  14816. comments.push(comment);
  14817. } else {
  14818. state.push([Rn[3], false]);
  14819. tmp = xlml_parsexmltag(Rn[0]);
  14820. comment = ({a:tmp.Author});
  14821. }
  14822. break;
  14823. case 'autofilter' /*case 'AutoFilter'*/:
  14824. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  14825. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  14826. var AutoFilter = xlml_parsexmltag(Rn[0]);
  14827. cursheet['!autofilter'] = { ref:rc_to_a1(AutoFilter.Range).replace(/\$/g,"") };
  14828. state.push([Rn[3], true]);
  14829. }
  14830. break;
  14831. case 'name' /*case 'Name'*/: break;
  14832. case 'datavalidation' /*case 'DataValidation'*/:
  14833. if(Rn[1]==='/'){
  14834. if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));
  14835. } else {
  14836. if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  14837. }
  14838. break;
  14839. case 'pixelsperinch' /*case 'PixelsPerInch'*/:
  14840. break;
  14841. case 'componentoptions' /*case 'ComponentOptions'*/:
  14842. case 'documentproperties' /*case 'DocumentProperties'*/:
  14843. case 'customdocumentproperties' /*case 'CustomDocumentProperties'*/:
  14844. case 'officedocumentsettings' /*case 'OfficeDocumentSettings'*/:
  14845. case 'pivottable' /*case 'PivotTable'*/:
  14846. case 'pivotcache' /*case 'PivotCache'*/:
  14847. case 'names' /*case 'Names'*/:
  14848. case 'mapinfo' /*case 'MapInfo'*/:
  14849. case 'pagebreaks' /*case 'PageBreaks'*/:
  14850. case 'querytable' /*case 'QueryTable'*/:
  14851. case 'sorting' /*case 'Sorting'*/:
  14852. case 'schema' /*case 'Schema'*/: //case 'data' /*case 'data'*/:
  14853. case 'conditionalformatting' /*case 'ConditionalFormatting'*/:
  14854. case 'smarttagtype' /*case 'SmartTagType'*/:
  14855. case 'smarttags' /*case 'SmartTags'*/:
  14856. case 'excelworkbook' /*case 'ExcelWorkbook'*/:
  14857. case 'workbookoptions' /*case 'WorkbookOptions'*/:
  14858. case 'worksheetoptions' /*case 'WorksheetOptions'*/:
  14859. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw new Error("Bad state: "+tmp.join("|"));}
  14860. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  14861. break;
  14862. default:
  14863. /* FODS file root is <office:document> */
  14864. if(state.length == 0 && Rn[3] == "document") return parse_fods(str, opts);
  14865. /* UOS file root is <uof:UOF> */
  14866. if(state.length == 0 && Rn[3] == "uof"/*"UOF"*/) return parse_fods(str, opts);
  14867. var seen = true;
  14868. switch(state[state.length-1][0]) {
  14869. /* OfficeDocumentSettings */
  14870. case 'officedocumentsettings' /*case 'OfficeDocumentSettings'*/: switch(Rn[3]) {
  14871. case 'allowpng' /*case 'AllowPNG'*/: break;
  14872. case 'removepersonalinformation' /*case 'RemovePersonalInformation'*/: break;
  14873. case 'downloadcomponents' /*case 'DownloadComponents'*/: break;
  14874. case 'locationofcomponents' /*case 'LocationOfComponents'*/: break;
  14875. case 'colors' /*case 'Colors'*/: break;
  14876. case 'color' /*case 'Color'*/: break;
  14877. case 'index' /*case 'Index'*/: break;
  14878. case 'rgb' /*case 'RGB'*/: break;
  14879. case 'targetscreensize' /*case 'TargetScreenSize'*/: break;
  14880. case 'readonlyrecommended' /*case 'ReadOnlyRecommended'*/: break;
  14881. default: seen = false;
  14882. } break;
  14883. /* ComponentOptions */
  14884. case 'componentoptions' /*case 'ComponentOptions'*/: switch(Rn[3]) {
  14885. case 'toolbar' /*case 'Toolbar'*/: break;
  14886. case 'hideofficelogo' /*case 'HideOfficeLogo'*/: break;
  14887. case 'spreadsheetautofit' /*case 'SpreadsheetAutoFit'*/: break;
  14888. case 'label' /*case 'Label'*/: break;
  14889. case 'caption' /*case 'Caption'*/: break;
  14890. case 'maxheight' /*case 'MaxHeight'*/: break;
  14891. case 'maxwidth' /*case 'MaxWidth'*/: break;
  14892. case 'nextsheetnumber' /*case 'NextSheetNumber'*/: break;
  14893. default: seen = false;
  14894. } break;
  14895. /* ExcelWorkbook */
  14896. case 'excelworkbook' /*case 'ExcelWorkbook'*/: switch(Rn[3]) {
  14897. case 'date1904' /*case 'Date1904'*/:
  14898. Workbook.WBProps.date1904 = true;
  14899. break;
  14900. case 'windowheight' /*case 'WindowHeight'*/: break;
  14901. case 'windowwidth' /*case 'WindowWidth'*/: break;
  14902. case 'windowtopx' /*case 'WindowTopX'*/: break;
  14903. case 'windowtopy' /*case 'WindowTopY'*/: break;
  14904. case 'tabratio' /*case 'TabRatio'*/: break;
  14905. case 'protectstructure' /*case 'ProtectStructure'*/: break;
  14906. case 'protectwindow' /*case 'ProtectWindow'*/: break;
  14907. case 'protectwindows' /*case 'ProtectWindows'*/: break;
  14908. case 'activesheet' /*case 'ActiveSheet'*/: break;
  14909. case 'displayinknotes' /*case 'DisplayInkNotes'*/: break;
  14910. case 'firstvisiblesheet' /*case 'FirstVisibleSheet'*/: break;
  14911. case 'supbook' /*case 'SupBook'*/: break;
  14912. case 'sheetname' /*case 'SheetName'*/: break;
  14913. case 'sheetindex' /*case 'SheetIndex'*/: break;
  14914. case 'sheetindexfirst' /*case 'SheetIndexFirst'*/: break;
  14915. case 'sheetindexlast' /*case 'SheetIndexLast'*/: break;
  14916. case 'dll' /*case 'Dll'*/: break;
  14917. case 'acceptlabelsinformulas' /*case 'AcceptLabelsInFormulas'*/: break;
  14918. case 'donotsavelinkvalues' /*case 'DoNotSaveLinkValues'*/: break;
  14919. case 'iteration' /*case 'Iteration'*/: break;
  14920. case 'maxiterations' /*case 'MaxIterations'*/: break;
  14921. case 'maxchange' /*case 'MaxChange'*/: break;
  14922. case 'path' /*case 'Path'*/: break;
  14923. case 'xct' /*case 'Xct'*/: break;
  14924. case 'count' /*case 'Count'*/: break;
  14925. case 'selectedsheets' /*case 'SelectedSheets'*/: break;
  14926. case 'calculation' /*case 'Calculation'*/: break;
  14927. case 'uncalced' /*case 'Uncalced'*/: break;
  14928. case 'startupprompt' /*case 'StartupPrompt'*/: break;
  14929. case 'crn' /*case 'Crn'*/: break;
  14930. case 'externname' /*case 'ExternName'*/: break;
  14931. case 'formula' /*case 'Formula'*/: break;
  14932. case 'colfirst' /*case 'ColFirst'*/: break;
  14933. case 'collast' /*case 'ColLast'*/: break;
  14934. case 'wantadvise' /*case 'WantAdvise'*/: break;
  14935. case 'boolean' /*case 'Boolean'*/: break;
  14936. case 'error' /*case 'Error'*/: break;
  14937. case 'text' /*case 'Text'*/: break;
  14938. case 'ole' /*case 'OLE'*/: break;
  14939. case 'noautorecover' /*case 'NoAutoRecover'*/: break;
  14940. case 'publishobjects' /*case 'PublishObjects'*/: break;
  14941. case 'donotcalculatebeforesave' /*case 'DoNotCalculateBeforeSave'*/: break;
  14942. case 'number' /*case 'Number'*/: break;
  14943. case 'refmoder1c1' /*case 'RefModeR1C1'*/: break;
  14944. case 'embedsavesmarttags' /*case 'EmbedSaveSmartTags'*/: break;
  14945. default: seen = false;
  14946. } break;
  14947. /* WorkbookOptions */
  14948. case 'workbookoptions' /*case 'WorkbookOptions'*/: switch(Rn[3]) {
  14949. case 'owcversion' /*case 'OWCVersion'*/: break;
  14950. case 'height' /*case 'Height'*/: break;
  14951. case 'width' /*case 'Width'*/: break;
  14952. default: seen = false;
  14953. } break;
  14954. /* WorksheetOptions */
  14955. case 'worksheetoptions' /*case 'WorksheetOptions'*/: switch(Rn[3]) {
  14956. case 'visible' /*case 'Visible'*/:
  14957. if(Rn[0].slice(-2) === "/>"){/* empty */}
  14958. else if(Rn[1]==="/") switch(str.slice(pidx, Rn.index)) {
  14959. case "SheetHidden": wsprops.Hidden = 1; break;
  14960. case "SheetVeryHidden": wsprops.Hidden = 2; break;
  14961. }
  14962. else pidx = Rn.index + Rn[0].length;
  14963. break;
  14964. case 'header' /*case 'Header'*/:
  14965. if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
  14966. cursheet['!margins'].header = parsexmltag(Rn[0]).Margin;
  14967. break;
  14968. case 'footer' /*case 'Footer'*/:
  14969. if(!cursheet['!margins']) default_margins(cursheet['!margins']={}, 'xlml');
  14970. cursheet['!margins'].footer = parsexmltag(Rn[0]).Margin;
  14971. break;
  14972. case 'pagemargins' /*case 'PageMargins'*/:
  14973. var pagemargins = parsexmltag(Rn[0]);
  14974. if(!cursheet['!margins']) default_margins(cursheet['!margins']={},'xlml');
  14975. if(pagemargins.Top) cursheet['!margins'].top = pagemargins.Top;
  14976. if(pagemargins.Left) cursheet['!margins'].left = pagemargins.Left;
  14977. if(pagemargins.Right) cursheet['!margins'].right = pagemargins.Right;
  14978. if(pagemargins.Bottom) cursheet['!margins'].bottom = pagemargins.Bottom;
  14979. break;
  14980. case 'displayrighttoleft' /*case 'DisplayRightToLeft'*/:
  14981. if(!Workbook.Views) Workbook.Views = [];
  14982. if(!Workbook.Views[0]) Workbook.Views[0] = {};
  14983. Workbook.Views[0].RTL = true;
  14984. break;
  14985. case 'freezepanes' /*case 'FreezePanes'*/: break;
  14986. case 'frozennosplit' /*case 'FrozenNoSplit'*/: break;
  14987. case 'splithorizontal' /*case 'SplitHorizontal'*/:
  14988. case 'splitvertical' /*case 'SplitVertical'*/:
  14989. break;
  14990. case 'donotdisplaygridlines' /*case 'DoNotDisplayGridlines'*/:
  14991. break;
  14992. case 'activerow' /*case 'ActiveRow'*/: break;
  14993. case 'activecol' /*case 'ActiveCol'*/: break;
  14994. case 'toprowbottompane' /*case 'TopRowBottomPane'*/: break;
  14995. case 'leftcolumnrightpane' /*case 'LeftColumnRightPane'*/: break;
  14996. case 'unsynced' /*case 'Unsynced'*/: break;
  14997. case 'print' /*case 'Print'*/: break;
  14998. case 'panes' /*case 'Panes'*/: break;
  14999. case 'scale' /*case 'Scale'*/: break;
  15000. case 'pane' /*case 'Pane'*/: break;
  15001. case 'number' /*case 'Number'*/: break;
  15002. case 'layout' /*case 'Layout'*/: break;
  15003. case 'pagesetup' /*case 'PageSetup'*/: break;
  15004. case 'selected' /*case 'Selected'*/: break;
  15005. case 'protectobjects' /*case 'ProtectObjects'*/: break;
  15006. case 'enableselection' /*case 'EnableSelection'*/: break;
  15007. case 'protectscenarios' /*case 'ProtectScenarios'*/: break;
  15008. case 'validprinterinfo' /*case 'ValidPrinterInfo'*/: break;
  15009. case 'horizontalresolution' /*case 'HorizontalResolution'*/: break;
  15010. case 'verticalresolution' /*case 'VerticalResolution'*/: break;
  15011. case 'numberofcopies' /*case 'NumberofCopies'*/: break;
  15012. case 'activepane' /*case 'ActivePane'*/: break;
  15013. case 'toprowvisible' /*case 'TopRowVisible'*/: break;
  15014. case 'leftcolumnvisible' /*case 'LeftColumnVisible'*/: break;
  15015. case 'fittopage' /*case 'FitToPage'*/: break;
  15016. case 'rangeselection' /*case 'RangeSelection'*/: break;
  15017. case 'papersizeindex' /*case 'PaperSizeIndex'*/: break;
  15018. case 'pagelayoutzoom' /*case 'PageLayoutZoom'*/: break;
  15019. case 'pagebreakzoom' /*case 'PageBreakZoom'*/: break;
  15020. case 'filteron' /*case 'FilterOn'*/: break;
  15021. case 'fitwidth' /*case 'FitWidth'*/: break;
  15022. case 'fitheight' /*case 'FitHeight'*/: break;
  15023. case 'commentslayout' /*case 'CommentsLayout'*/: break;
  15024. case 'zoom' /*case 'Zoom'*/: break;
  15025. case 'lefttoright' /*case 'LeftToRight'*/: break;
  15026. case 'gridlines' /*case 'Gridlines'*/: break;
  15027. case 'allowsort' /*case 'AllowSort'*/: break;
  15028. case 'allowfilter' /*case 'AllowFilter'*/: break;
  15029. case 'allowinsertrows' /*case 'AllowInsertRows'*/: break;
  15030. case 'allowdeleterows' /*case 'AllowDeleteRows'*/: break;
  15031. case 'allowinsertcols' /*case 'AllowInsertCols'*/: break;
  15032. case 'allowdeletecols' /*case 'AllowDeleteCols'*/: break;
  15033. case 'allowinserthyperlinks' /*case 'AllowInsertHyperlinks'*/: break;
  15034. case 'allowformatcells' /*case 'AllowFormatCells'*/: break;
  15035. case 'allowsizecols' /*case 'AllowSizeCols'*/: break;
  15036. case 'allowsizerows' /*case 'AllowSizeRows'*/: break;
  15037. case 'nosummaryrowsbelowdetail' /*case 'NoSummaryRowsBelowDetail'*/: break;
  15038. case 'tabcolorindex' /*case 'TabColorIndex'*/: break;
  15039. case 'donotdisplayheadings' /*case 'DoNotDisplayHeadings'*/: break;
  15040. case 'showpagelayoutzoom' /*case 'ShowPageLayoutZoom'*/: break;
  15041. case 'nosummarycolumnsrightdetail' /*case 'NoSummaryColumnsRightDetail'*/: break;
  15042. case 'blackandwhite' /*case 'BlackAndWhite'*/: break;
  15043. case 'donotdisplayzeros' /*case 'DoNotDisplayZeros'*/: break;
  15044. case 'displaypagebreak' /*case 'DisplayPageBreak'*/: break;
  15045. case 'rowcolheadings' /*case 'RowColHeadings'*/: break;
  15046. case 'donotdisplayoutline' /*case 'DoNotDisplayOutline'*/: break;
  15047. case 'noorientation' /*case 'NoOrientation'*/: break;
  15048. case 'allowusepivottables' /*case 'AllowUsePivotTables'*/: break;
  15049. case 'zeroheight' /*case 'ZeroHeight'*/: break;
  15050. case 'viewablerange' /*case 'ViewableRange'*/: break;
  15051. case 'selection' /*case 'Selection'*/: break;
  15052. case 'protectcontents' /*case 'ProtectContents'*/: break;
  15053. default: seen = false;
  15054. } break;
  15055. /* PivotTable */
  15056. case 'pivottable' /*case 'PivotTable'*/: case 'pivotcache' /*case 'PivotCache'*/: switch(Rn[3]) {
  15057. case 'immediateitemsondrop' /*case 'ImmediateItemsOnDrop'*/: break;
  15058. case 'showpagemultipleitemlabel' /*case 'ShowPageMultipleItemLabel'*/: break;
  15059. case 'compactrowindent' /*case 'CompactRowIndent'*/: break;
  15060. case 'location' /*case 'Location'*/: break;
  15061. case 'pivotfield' /*case 'PivotField'*/: break;
  15062. case 'orientation' /*case 'Orientation'*/: break;
  15063. case 'layoutform' /*case 'LayoutForm'*/: break;
  15064. case 'layoutsubtotallocation' /*case 'LayoutSubtotalLocation'*/: break;
  15065. case 'layoutcompactrow' /*case 'LayoutCompactRow'*/: break;
  15066. case 'position' /*case 'Position'*/: break;
  15067. case 'pivotitem' /*case 'PivotItem'*/: break;
  15068. case 'datatype' /*case 'DataType'*/: break;
  15069. case 'datafield' /*case 'DataField'*/: break;
  15070. case 'sourcename' /*case 'SourceName'*/: break;
  15071. case 'parentfield' /*case 'ParentField'*/: break;
  15072. case 'ptlineitems' /*case 'PTLineItems'*/: break;
  15073. case 'ptlineitem' /*case 'PTLineItem'*/: break;
  15074. case 'countofsameitems' /*case 'CountOfSameItems'*/: break;
  15075. case 'item' /*case 'Item'*/: break;
  15076. case 'itemtype' /*case 'ItemType'*/: break;
  15077. case 'ptsource' /*case 'PTSource'*/: break;
  15078. case 'cacheindex' /*case 'CacheIndex'*/: break;
  15079. case 'consolidationreference' /*case 'ConsolidationReference'*/: break;
  15080. case 'filename' /*case 'FileName'*/: break;
  15081. case 'reference' /*case 'Reference'*/: break;
  15082. case 'nocolumngrand' /*case 'NoColumnGrand'*/: break;
  15083. case 'norowgrand' /*case 'NoRowGrand'*/: break;
  15084. case 'blanklineafteritems' /*case 'BlankLineAfterItems'*/: break;
  15085. case 'hidden' /*case 'Hidden'*/: break;
  15086. case 'subtotal' /*case 'Subtotal'*/: break;
  15087. case 'basefield' /*case 'BaseField'*/: break;
  15088. case 'mapchilditems' /*case 'MapChildItems'*/: break;
  15089. case 'function' /*case 'Function'*/: break;
  15090. case 'refreshonfileopen' /*case 'RefreshOnFileOpen'*/: break;
  15091. case 'printsettitles' /*case 'PrintSetTitles'*/: break;
  15092. case 'mergelabels' /*case 'MergeLabels'*/: break;
  15093. case 'defaultversion' /*case 'DefaultVersion'*/: break;
  15094. case 'refreshname' /*case 'RefreshName'*/: break;
  15095. case 'refreshdate' /*case 'RefreshDate'*/: break;
  15096. case 'refreshdatecopy' /*case 'RefreshDateCopy'*/: break;
  15097. case 'versionlastrefresh' /*case 'VersionLastRefresh'*/: break;
  15098. case 'versionlastupdate' /*case 'VersionLastUpdate'*/: break;
  15099. case 'versionupdateablemin' /*case 'VersionUpdateableMin'*/: break;
  15100. case 'versionrefreshablemin' /*case 'VersionRefreshableMin'*/: break;
  15101. case 'calculation' /*case 'Calculation'*/: break;
  15102. default: seen = false;
  15103. } break;
  15104. /* PageBreaks */
  15105. case 'pagebreaks' /*case 'PageBreaks'*/: switch(Rn[3]) {
  15106. case 'colbreaks' /*case 'ColBreaks'*/: break;
  15107. case 'colbreak' /*case 'ColBreak'*/: break;
  15108. case 'rowbreaks' /*case 'RowBreaks'*/: break;
  15109. case 'rowbreak' /*case 'RowBreak'*/: break;
  15110. case 'colstart' /*case 'ColStart'*/: break;
  15111. case 'colend' /*case 'ColEnd'*/: break;
  15112. case 'rowend' /*case 'RowEnd'*/: break;
  15113. default: seen = false;
  15114. } break;
  15115. /* AutoFilter */
  15116. case 'autofilter' /*case 'AutoFilter'*/: switch(Rn[3]) {
  15117. case 'autofiltercolumn' /*case 'AutoFilterColumn'*/: break;
  15118. case 'autofiltercondition' /*case 'AutoFilterCondition'*/: break;
  15119. case 'autofilterand' /*case 'AutoFilterAnd'*/: break;
  15120. case 'autofilteror' /*case 'AutoFilterOr'*/: break;
  15121. default: seen = false;
  15122. } break;
  15123. /* QueryTable */
  15124. case 'querytable' /*case 'QueryTable'*/: switch(Rn[3]) {
  15125. case 'id' /*case 'Id'*/: break;
  15126. case 'autoformatfont' /*case 'AutoFormatFont'*/: break;
  15127. case 'autoformatpattern' /*case 'AutoFormatPattern'*/: break;
  15128. case 'querysource' /*case 'QuerySource'*/: break;
  15129. case 'querytype' /*case 'QueryType'*/: break;
  15130. case 'enableredirections' /*case 'EnableRedirections'*/: break;
  15131. case 'refreshedinxl9' /*case 'RefreshedInXl9'*/: break;
  15132. case 'urlstring' /*case 'URLString'*/: break;
  15133. case 'htmltables' /*case 'HTMLTables'*/: break;
  15134. case 'connection' /*case 'Connection'*/: break;
  15135. case 'commandtext' /*case 'CommandText'*/: break;
  15136. case 'refreshinfo' /*case 'RefreshInfo'*/: break;
  15137. case 'notitles' /*case 'NoTitles'*/: break;
  15138. case 'nextid' /*case 'NextId'*/: break;
  15139. case 'columninfo' /*case 'ColumnInfo'*/: break;
  15140. case 'overwritecells' /*case 'OverwriteCells'*/: break;
  15141. case 'donotpromptforfile' /*case 'DoNotPromptForFile'*/: break;
  15142. case 'textwizardsettings' /*case 'TextWizardSettings'*/: break;
  15143. case 'source' /*case 'Source'*/: break;
  15144. case 'number' /*case 'Number'*/: break;
  15145. case 'decimal' /*case 'Decimal'*/: break;
  15146. case 'thousandseparator' /*case 'ThousandSeparator'*/: break;
  15147. case 'trailingminusnumbers' /*case 'TrailingMinusNumbers'*/: break;
  15148. case 'formatsettings' /*case 'FormatSettings'*/: break;
  15149. case 'fieldtype' /*case 'FieldType'*/: break;
  15150. case 'delimiters' /*case 'Delimiters'*/: break;
  15151. case 'tab' /*case 'Tab'*/: break;
  15152. case 'comma' /*case 'Comma'*/: break;
  15153. case 'autoformatname' /*case 'AutoFormatName'*/: break;
  15154. case 'versionlastedit' /*case 'VersionLastEdit'*/: break;
  15155. case 'versionlastrefresh' /*case 'VersionLastRefresh'*/: break;
  15156. default: seen = false;
  15157. } break;
  15158. case 'datavalidation' /*case 'DataValidation'*/:
  15159. switch(Rn[3]) {
  15160. case 'range' /*case 'Range'*/: break;
  15161. case 'type' /*case 'Type'*/: break;
  15162. case 'min' /*case 'Min'*/: break;
  15163. case 'max' /*case 'Max'*/: break;
  15164. case 'sort' /*case 'Sort'*/: break;
  15165. case 'descending' /*case 'Descending'*/: break;
  15166. case 'order' /*case 'Order'*/: break;
  15167. case 'casesensitive' /*case 'CaseSensitive'*/: break;
  15168. case 'value' /*case 'Value'*/: break;
  15169. case 'errorstyle' /*case 'ErrorStyle'*/: break;
  15170. case 'errormessage' /*case 'ErrorMessage'*/: break;
  15171. case 'errortitle' /*case 'ErrorTitle'*/: break;
  15172. case 'inputmessage' /*case 'InputMessage'*/: break;
  15173. case 'inputtitle' /*case 'InputTitle'*/: break;
  15174. case 'combohide' /*case 'ComboHide'*/: break;
  15175. case 'inputhide' /*case 'InputHide'*/: break;
  15176. case 'condition' /*case 'Condition'*/: break;
  15177. case 'qualifier' /*case 'Qualifier'*/: break;
  15178. case 'useblank' /*case 'UseBlank'*/: break;
  15179. case 'value1' /*case 'Value1'*/: break;
  15180. case 'value2' /*case 'Value2'*/: break;
  15181. case 'format' /*case 'Format'*/: break;
  15182. case 'cellrangelist' /*case 'CellRangeList'*/: break;
  15183. default: seen = false;
  15184. } break;
  15185. case 'sorting' /*case 'Sorting'*/:
  15186. case 'conditionalformatting' /*case 'ConditionalFormatting'*/:
  15187. switch(Rn[3]) {
  15188. case 'range' /*case 'Range'*/: break;
  15189. case 'type' /*case 'Type'*/: break;
  15190. case 'min' /*case 'Min'*/: break;
  15191. case 'max' /*case 'Max'*/: break;
  15192. case 'sort' /*case 'Sort'*/: break;
  15193. case 'descending' /*case 'Descending'*/: break;
  15194. case 'order' /*case 'Order'*/: break;
  15195. case 'casesensitive' /*case 'CaseSensitive'*/: break;
  15196. case 'value' /*case 'Value'*/: break;
  15197. case 'errorstyle' /*case 'ErrorStyle'*/: break;
  15198. case 'errormessage' /*case 'ErrorMessage'*/: break;
  15199. case 'errortitle' /*case 'ErrorTitle'*/: break;
  15200. case 'cellrangelist' /*case 'CellRangeList'*/: break;
  15201. case 'inputmessage' /*case 'InputMessage'*/: break;
  15202. case 'inputtitle' /*case 'InputTitle'*/: break;
  15203. case 'combohide' /*case 'ComboHide'*/: break;
  15204. case 'inputhide' /*case 'InputHide'*/: break;
  15205. case 'condition' /*case 'Condition'*/: break;
  15206. case 'qualifier' /*case 'Qualifier'*/: break;
  15207. case 'useblank' /*case 'UseBlank'*/: break;
  15208. case 'value1' /*case 'Value1'*/: break;
  15209. case 'value2' /*case 'Value2'*/: break;
  15210. case 'format' /*case 'Format'*/: break;
  15211. default: seen = false;
  15212. } break;
  15213. /* MapInfo (schema) */
  15214. case 'mapinfo' /*case 'MapInfo'*/: case 'schema' /*case 'Schema'*/: case 'data' /*case 'data'*/: switch(Rn[3]) {
  15215. case 'map' /*case 'Map'*/: break;
  15216. case 'entry' /*case 'Entry'*/: break;
  15217. case 'range' /*case 'Range'*/: break;
  15218. case 'xpath' /*case 'XPath'*/: break;
  15219. case 'field' /*case 'Field'*/: break;
  15220. case 'xsdtype' /*case 'XSDType'*/: break;
  15221. case 'filteron' /*case 'FilterOn'*/: break;
  15222. case 'aggregate' /*case 'Aggregate'*/: break;
  15223. case 'elementtype' /*case 'ElementType'*/: break;
  15224. case 'attributetype' /*case 'AttributeType'*/: break;
  15225. /* These are from xsd (XML Schema Definition) */
  15226. case 'schema' /*case 'schema'*/:
  15227. case 'element' /*case 'element'*/:
  15228. case 'complextype' /*case 'complexType'*/:
  15229. case 'datatype' /*case 'datatype'*/:
  15230. case 'all' /*case 'all'*/:
  15231. case 'attribute' /*case 'attribute'*/:
  15232. case 'extends' /*case 'extends'*/: break;
  15233. case 'row' /*case 'row'*/: break;
  15234. default: seen = false;
  15235. } break;
  15236. /* SmartTags (can be anything) */
  15237. case 'smarttags' /*case 'SmartTags'*/: break;
  15238. default: seen = false; break;
  15239. }
  15240. if(seen) break;
  15241. /* CustomDocumentProperties */
  15242. if(Rn[3].match(/!\[CDATA/)) break;
  15243. if(!state[state.length-1][1]) throw 'Unrecognized tag: ' + Rn[3] + "|" + state.join("|");
  15244. if(state[state.length-1][0]===/*'CustomDocumentProperties'*/'customdocumentproperties') {
  15245. if(Rn[0].slice(-2) === "/>") break;
  15246. else if(Rn[1]==="/") xlml_set_custprop(Custprops, raw_Rn3, cp, str.slice(pidx, Rn.index));
  15247. else { cp = Rn; pidx = Rn.index + Rn[0].length; }
  15248. break;
  15249. }
  15250. if(opts.WTF) throw 'Unrecognized tag: ' + Rn[3] + "|" + state.join("|");
  15251. }
  15252. var out = ({});
  15253. if(!opts.bookSheets && !opts.bookProps) out.Sheets = sheets;
  15254. out.SheetNames = sheetnames;
  15255. out.Workbook = Workbook;
  15256. out.SSF = SSF.get_table();
  15257. out.Props = Props;
  15258. out.Custprops = Custprops;
  15259. return out;
  15260. }
  15261. function parse_xlml(data, opts) {
  15262. fix_read_opts(opts=opts||{});
  15263. switch(opts.type||"base64") {
  15264. case "base64": return parse_xlml_xml(Base64.decode(data), opts);
  15265. case "binary": case "buffer": case "file": return parse_xlml_xml(data, opts);
  15266. case "array": return parse_xlml_xml(a2s(data), opts);
  15267. }
  15268. }
  15269. /* TODO */
  15270. function write_props_xlml(wb, opts) {
  15271. var o = [];
  15272. /* DocumentProperties */
  15273. if(wb.Props) o.push(xlml_write_docprops(wb.Props, opts));
  15274. /* CustomDocumentProperties */
  15275. if(wb.Custprops) o.push(xlml_write_custprops(wb.Props, wb.Custprops, opts));
  15276. return o.join("");
  15277. }
  15278. /* TODO */
  15279. function write_wb_xlml() {
  15280. /* OfficeDocumentSettings */
  15281. /* ExcelWorkbook */
  15282. return "";
  15283. }
  15284. /* TODO */
  15285. function write_sty_xlml(wb, opts) {
  15286. /* Styles */
  15287. var styles = ['<Style ss:ID="Default" ss:Name="Normal"><NumberFormat/></Style>'];
  15288. opts.cellXfs.forEach(function(xf, id) {
  15289. var payload = [];
  15290. payload.push(writextag('NumberFormat', null, {"ss:Format": escapexml(SSF._table[xf.numFmtId])}));
  15291. var o = {"ss:ID": "s" + (21+id)};
  15292. styles.push(writextag('Style', payload.join(""), o));
  15293. });
  15294. return writextag("Styles", styles.join(""));
  15295. }
  15296. function write_name_xlml(n) { return writextag("NamedRange", null, {"ss:Name": n.Name, "ss:RefersTo":"=" + a1_to_rc(n.Ref, {r:0,c:0})}); }
  15297. function write_names_xlml(wb) {
  15298. if(!((wb||{}).Workbook||{}).Names) return "";
  15299. var names = wb.Workbook.Names;
  15300. var out = [];
  15301. for(var i = 0; i < names.length; ++i) {
  15302. var n = names[i];
  15303. if(n.Sheet != null) continue;
  15304. if(n.Name.match(/^_xlfn\./)) continue;
  15305. out.push(write_name_xlml(n));
  15306. }
  15307. return writextag("Names", out.join(""));
  15308. }
  15309. function write_ws_xlml_names(ws, opts, idx, wb) {
  15310. if(!ws) return "";
  15311. if(!((wb||{}).Workbook||{}).Names) return "";
  15312. var names = wb.Workbook.Names;
  15313. var out = [];
  15314. for(var i = 0; i < names.length; ++i) {
  15315. var n = names[i];
  15316. if(n.Sheet != idx) continue;
  15317. /*switch(n.Name) {
  15318. case "_": continue;
  15319. }*/
  15320. if(n.Name.match(/^_xlfn\./)) continue;
  15321. out.push(write_name_xlml(n));
  15322. }
  15323. return out.join("");
  15324. }
  15325. /* WorksheetOptions */
  15326. function write_ws_xlml_wsopts(ws, opts, idx, wb) {
  15327. if(!ws) return "";
  15328. var o = [];
  15329. /* NOTE: spec technically allows any order, but stick with implied order */
  15330. /* FitToPage */
  15331. /* DoNotDisplayColHeaders */
  15332. /* DoNotDisplayRowHeaders */
  15333. /* ViewableRange */
  15334. /* Selection */
  15335. /* GridlineColor */
  15336. /* Name */
  15337. /* ExcelWorksheetType */
  15338. /* IntlMacro */
  15339. /* Unsynced */
  15340. /* Selected */
  15341. /* CodeName */
  15342. if(ws['!margins']) {
  15343. o.push("<PageSetup>");
  15344. if(ws['!margins'].header) o.push(writextag("Header", null, {'x:Margin':ws['!margins'].header}));
  15345. if(ws['!margins'].footer) o.push(writextag("Footer", null, {'x:Margin':ws['!margins'].footer}));
  15346. o.push(writextag("PageMargins", null, {
  15347. 'x:Bottom': ws['!margins'].bottom || "0.75",
  15348. 'x:Left': ws['!margins'].left || "0.7",
  15349. 'x:Right': ws['!margins'].right || "0.7",
  15350. 'x:Top': ws['!margins'].top || "0.75"
  15351. }));
  15352. o.push("</PageSetup>");
  15353. }
  15354. /* PageSetup */
  15355. /* DisplayPageBreak */
  15356. /* TransitionExpressionEvaluation */
  15357. /* TransitionFormulaEntry */
  15358. /* Print */
  15359. /* Zoom */
  15360. /* PageLayoutZoom */
  15361. /* PageBreakZoom */
  15362. /* ShowPageBreakZoom */
  15363. /* DefaultRowHeight */
  15364. /* DefaultColumnWidth */
  15365. /* StandardWidth */
  15366. if(wb && wb.Workbook && wb.Workbook.Sheets && wb.Workbook.Sheets[idx]) {
  15367. /* Visible */
  15368. if(wb.Workbook.Sheets[idx].Hidden) o.push(writextag("Visible", (wb.Workbook.Sheets[idx].Hidden == 1 ? "SheetHidden" : "SheetVeryHidden"), {}));
  15369. else {
  15370. /* Selected */
  15371. for(var i = 0; i < idx; ++i) if(wb.Workbook.Sheets[i] && !wb.Workbook.Sheets[i].Hidden) break;
  15372. if(i == idx) o.push("<Selected/>");
  15373. }
  15374. }
  15375. /* LeftColumnVisible */
  15376. if(((((wb||{}).Workbook||{}).Views||[])[0]||{}).RTL) o.push("<DisplayRightToLeft/>");
  15377. /* GridlineColorIndex */
  15378. /* DisplayFormulas */
  15379. /* DoNotDisplayGridlines */
  15380. /* DoNotDisplayHeadings */
  15381. /* DoNotDisplayOutline */
  15382. /* ApplyAutomaticOutlineStyles */
  15383. /* NoSummaryRowsBelowDetail */
  15384. /* NoSummaryColumnsRightDetail */
  15385. /* DoNotDisplayZeros */
  15386. /* ActiveRow */
  15387. /* ActiveColumn */
  15388. /* FilterOn */
  15389. /* RangeSelection */
  15390. /* TopRowVisible */
  15391. /* TopRowBottomPane */
  15392. /* LeftColumnRightPane */
  15393. /* ActivePane */
  15394. /* SplitHorizontal */
  15395. /* SplitVertical */
  15396. /* FreezePanes */
  15397. /* FrozenNoSplit */
  15398. /* TabColorIndex */
  15399. /* Panes */
  15400. /* NOTE: Password not supported in XLML Format */
  15401. if(ws['!protect']) {
  15402. o.push(writetag("ProtectContents", "True"));
  15403. if(ws['!protect'].objects) o.push(writetag("ProtectObjects", "True"));
  15404. if(ws['!protect'].scenarios) o.push(writetag("ProtectScenarios", "True"));
  15405. if(ws['!protect'].selectLockedCells != null && !ws['!protect'].selectLockedCells) o.push(writetag("EnableSelection", "NoSelection"));
  15406. else if(ws['!protect'].selectUnlockedCells != null && !ws['!protect'].selectUnlockedCells) o.push(writetag("EnableSelection", "UnlockedCells"));
  15407. [
  15408. [ "formatCells", "AllowFormatCells" ],
  15409. [ "formatColumns", "AllowSizeCols" ],
  15410. [ "formatRows", "AllowSizeRows" ],
  15411. [ "insertColumns", "AllowInsertCols" ],
  15412. [ "insertRows", "AllowInsertRows" ],
  15413. [ "insertHyperlinks", "AllowInsertHyperlinks" ],
  15414. [ "deleteColumns", "AllowDeleteCols" ],
  15415. [ "deleteRows", "AllowDeleteRows" ],
  15416. [ "sort", "AllowSort" ],
  15417. [ "autoFilter", "AllowFilter" ],
  15418. [ "pivotTables", "AllowUsePivotTables" ]
  15419. ].forEach(function(x) { if(ws['!protect'][x[0]]) o.push("<"+x[1]+"/>"); });
  15420. }
  15421. if(o.length == 0) return "";
  15422. return writextag("WorksheetOptions", o.join(""), {xmlns:XLMLNS.x});
  15423. }
  15424. function write_ws_xlml_comment(comments) {
  15425. return comments.map(function(c) {
  15426. // TODO: formatted text
  15427. var t = xlml_unfixstr(c.t||"");
  15428. var d =writextag("ss:Data", t, {"xmlns":"http://www.w3.org/TR/REC-html40"});
  15429. return writextag("Comment", d, {"ss:Author":c.a});
  15430. }).join("");
  15431. }
  15432. function write_ws_xlml_cell(cell, ref, ws, opts, idx, wb, addr){
  15433. if(!cell || (cell.v == undefined && cell.f == undefined)) return "";
  15434. var attr = {};
  15435. if(cell.f) attr["ss:Formula"] = "=" + escapexml(a1_to_rc(cell.f, addr));
  15436. if(cell.F && cell.F.slice(0, ref.length) == ref) {
  15437. var end = decode_cell(cell.F.slice(ref.length + 1));
  15438. attr["ss:ArrayRange"] = "RC:R" + (end.r == addr.r ? "" : "[" + (end.r - addr.r) + "]") + "C" + (end.c == addr.c ? "" : "[" + (end.c - addr.c) + "]");
  15439. }
  15440. if(cell.l && cell.l.Target) {
  15441. attr["ss:HRef"] = escapexml(cell.l.Target);
  15442. if(cell.l.Tooltip) attr["x:HRefScreenTip"] = escapexml(cell.l.Tooltip);
  15443. }
  15444. if(ws['!merges']) {
  15445. var marr = ws['!merges'];
  15446. for(var mi = 0; mi != marr.length; ++mi) {
  15447. if(marr[mi].s.c != addr.c || marr[mi].s.r != addr.r) continue;
  15448. if(marr[mi].e.c > marr[mi].s.c) attr['ss:MergeAcross'] = marr[mi].e.c - marr[mi].s.c;
  15449. if(marr[mi].e.r > marr[mi].s.r) attr['ss:MergeDown'] = marr[mi].e.r - marr[mi].s.r;
  15450. }
  15451. }
  15452. var t = "", p = "";
  15453. switch(cell.t) {
  15454. case 'z': if(!opts.sheetStubs) return ""; break;
  15455. case 'n': t = 'Number'; p = String(cell.v); break;
  15456. case 'b': t = 'Boolean'; p = (cell.v ? "1" : "0"); break;
  15457. case 'e': t = 'Error'; p = BErr[cell.v]; break;
  15458. case 'd': t = 'DateTime'; p = new Date(cell.v).toISOString(); if(cell.z == null) cell.z = cell.z || SSF._table[14]; break;
  15459. case 's': t = 'String'; p = escapexlml(cell.v||""); break;
  15460. }
  15461. /* TODO: cell style */
  15462. var os = get_cell_style(opts.cellXfs, cell, opts);
  15463. attr["ss:StyleID"] = "s" + (21+os);
  15464. attr["ss:Index"] = addr.c + 1;
  15465. var _v = (cell.v != null ? p : "");
  15466. var m = cell.t == 'z' ? "" : ('<Data ss:Type="' + t + '">' + _v + '</Data>');
  15467. if((cell.c||[]).length > 0) m += write_ws_xlml_comment(cell.c);
  15468. return writextag("Cell", m, attr);
  15469. }
  15470. function write_ws_xlml_row(R, row) {
  15471. var o = '<Row ss:Index="' + (R+1) + '"';
  15472. if(row) {
  15473. if(row.hpt && !row.hpx) row.hpx = pt2px(row.hpt);
  15474. if(row.hpx) o += ' ss:AutoFitHeight="0" ss:Height="' + row.hpx + '"';
  15475. if(row.hidden) o += ' ss:Hidden="1"';
  15476. }
  15477. return o + '>';
  15478. }
  15479. /* TODO */
  15480. function write_ws_xlml_table(ws, opts, idx, wb) {
  15481. if(!ws['!ref']) return "";
  15482. var range = safe_decode_range(ws['!ref']);
  15483. var marr = ws['!merges'] || [], mi = 0;
  15484. var o = [];
  15485. if(ws['!cols']) ws['!cols'].forEach(function(n, i) {
  15486. process_col(n);
  15487. var w = !!n.width;
  15488. var p = col_obj_w(i, n);
  15489. var k = {"ss:Index":i+1};
  15490. if(w) k['ss:Width'] = width2px(p.width);
  15491. if(n.hidden) k['ss:Hidden']="1";
  15492. o.push(writextag("Column",null,k));
  15493. });
  15494. var dense = Array.isArray(ws);
  15495. for(var R = range.s.r; R <= range.e.r; ++R) {
  15496. var row = [write_ws_xlml_row(R, (ws['!rows']||[])[R])];
  15497. for(var C = range.s.c; C <= range.e.c; ++C) {
  15498. var skip = false;
  15499. for(mi = 0; mi != marr.length; ++mi) {
  15500. if(marr[mi].s.c > C) continue;
  15501. if(marr[mi].s.r > R) continue;
  15502. if(marr[mi].e.c < C) continue;
  15503. if(marr[mi].e.r < R) continue;
  15504. if(marr[mi].s.c != C || marr[mi].s.r != R) skip = true;
  15505. break;
  15506. }
  15507. if(skip) continue;
  15508. var addr = {r:R,c:C};
  15509. var ref = encode_cell(addr), cell = dense ? (ws[R]||[])[C] : ws[ref];
  15510. row.push(write_ws_xlml_cell(cell, ref, ws, opts, idx, wb, addr));
  15511. }
  15512. row.push("</Row>");
  15513. if(row.length > 2) o.push(row.join(""));
  15514. }
  15515. return o.join("");
  15516. }
  15517. function write_ws_xlml(idx, opts, wb) {
  15518. var o = [];
  15519. var s = wb.SheetNames[idx];
  15520. var ws = wb.Sheets[s];
  15521. var t = ws ? write_ws_xlml_names(ws, opts, idx, wb) : "";
  15522. if(t.length > 0) o.push("<Names>" + t + "</Names>");
  15523. /* Table */
  15524. t = ws ? write_ws_xlml_table(ws, opts, idx, wb) : "";
  15525. if(t.length > 0) o.push("<Table>" + t + "</Table>");
  15526. /* WorksheetOptions */
  15527. o.push(write_ws_xlml_wsopts(ws, opts, idx, wb));
  15528. return o.join("");
  15529. }
  15530. function write_xlml(wb, opts) {
  15531. if(!opts) opts = {};
  15532. if(!wb.SSF) wb.SSF = SSF.get_table();
  15533. if(wb.SSF) {
  15534. make_ssf(SSF); SSF.load_table(wb.SSF);
  15535. // $FlowIgnore
  15536. opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
  15537. opts.ssf = wb.SSF;
  15538. opts.cellXfs = [];
  15539. get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
  15540. }
  15541. var d = [];
  15542. d.push(write_props_xlml(wb, opts));
  15543. d.push(write_wb_xlml(wb, opts));
  15544. d.push("");
  15545. d.push("");
  15546. for(var i = 0; i < wb.SheetNames.length; ++i)
  15547. d.push(writextag("Worksheet", write_ws_xlml(i, opts, wb), {"ss:Name":escapexml(wb.SheetNames[i])}));
  15548. d[2] = write_sty_xlml(wb, opts);
  15549. d[3] = write_names_xlml(wb, opts);
  15550. return XML_HEADER + writextag("Workbook", d.join(""), {
  15551. 'xmlns': XLMLNS.ss,
  15552. 'xmlns:o': XLMLNS.o,
  15553. 'xmlns:x': XLMLNS.x,
  15554. 'xmlns:ss': XLMLNS.ss,
  15555. 'xmlns:dt': XLMLNS.dt,
  15556. 'xmlns:html': XLMLNS.html
  15557. });
  15558. }
  15559. /* [MS-OLEDS] 2.3.8 CompObjStream */
  15560. function parse_compobj(obj) {
  15561. var v = {};
  15562. var o = obj.content;
  15563. /* [MS-OLEDS] 2.3.7 CompObjHeader -- All fields MUST be ignored */
  15564. o.l = 28;
  15565. v.AnsiUserType = o.read_shift(0, "lpstr-ansi");
  15566. v.AnsiClipboardFormat = parse_ClipboardFormatOrAnsiString(o);
  15567. if(o.length - o.l <= 4) return v;
  15568. var m = o.read_shift(4);
  15569. if(m == 0 || m > 40) return v;
  15570. o.l-=4; v.Reserved1 = o.read_shift(0, "lpstr-ansi");
  15571. if(o.length - o.l <= 4) return v;
  15572. m = o.read_shift(4);
  15573. if(m !== 0x71b239f4) return v;
  15574. v.UnicodeClipboardFormat = parse_ClipboardFormatOrUnicodeString(o);
  15575. m = o.read_shift(4);
  15576. if(m == 0 || m > 40) return v;
  15577. o.l-=4; v.Reserved2 = o.read_shift(0, "lpwstr");
  15578. }
  15579. /*
  15580. Continue logic for:
  15581. - 2.4.58 Continue
  15582. - 2.4.59 ContinueBigName
  15583. - 2.4.60 ContinueFrt
  15584. - 2.4.61 ContinueFrt11
  15585. - 2.4.62 ContinueFrt12
  15586. */
  15587. function slurp(R, blob, length, opts) {
  15588. var l = length;
  15589. var bufs = [];
  15590. var d = blob.slice(blob.l,blob.l+l);
  15591. if(opts && opts.enc && opts.enc.insitu) switch(R.n) {
  15592. case 'BOF': case 'FilePass': case 'FileLock': case 'InterfaceHdr': case 'RRDInfo': case 'RRDHead': case 'UsrExcl': break;
  15593. default:
  15594. if(d.length === 0) break;
  15595. opts.enc.insitu(d);
  15596. }
  15597. bufs.push(d);
  15598. blob.l += l;
  15599. var next = (XLSRecordEnum[__readUInt16LE(blob,blob.l)]);
  15600. var start = 0;
  15601. while(next != null && next.n.slice(0,8) === 'Continue') {
  15602. l = __readUInt16LE(blob,blob.l+2);
  15603. start = blob.l + 4;
  15604. if(next.n == 'ContinueFrt') start += 4;
  15605. else if(next.n.slice(0,11) == 'ContinueFrt') start += 12;
  15606. bufs.push(blob.slice(start,blob.l+4+l));
  15607. blob.l += 4+l;
  15608. next = (XLSRecordEnum[__readUInt16LE(blob, blob.l)]);
  15609. }
  15610. var b = (bconcat(bufs));
  15611. prep_blob(b, 0);
  15612. var ll = 0; b.lens = [];
  15613. for(var j = 0; j < bufs.length; ++j) { b.lens.push(ll); ll += bufs[j].length; }
  15614. return R.f(b, b.length, opts);
  15615. }
  15616. function safe_format_xf(p, opts, date1904) {
  15617. if(p.t === 'z') return;
  15618. if(!p.XF) return;
  15619. var fmtid = 0;
  15620. try {
  15621. fmtid = p.z || p.XF.numFmtId || 0;
  15622. if(opts.cellNF) p.z = SSF._table[fmtid];
  15623. } catch(e) { if(opts.WTF) throw e; }
  15624. if(!opts || opts.cellText !== false) try {
  15625. if(p.t === 'e') { p.w = p.w || BErr[p.v]; }
  15626. else if(fmtid === 0 || fmtid == "General") {
  15627. if(p.t === 'n') {
  15628. if((p.v|0) === p.v) p.w = SSF._general_int(p.v);
  15629. else p.w = SSF._general_num(p.v);
  15630. }
  15631. else p.w = SSF._general(p.v);
  15632. }
  15633. else p.w = SSF.format(fmtid,p.v, {date1904:!!date1904});
  15634. } catch(e) { if(opts.WTF) throw e; }
  15635. if(opts.cellDates && fmtid && p.t == 'n' && SSF.is_date(SSF._table[fmtid] || String(fmtid))) {
  15636. var _d = SSF.parse_date_code(p.v); if(_d) { p.t = 'd'; p.v = new Date(_d.y, _d.m-1,_d.d,_d.H,_d.M,_d.S,_d.u); }
  15637. }
  15638. }
  15639. function make_cell(val, ixfe, t) {
  15640. return ({v:val, ixfe:ixfe, t:t});
  15641. }
  15642. // 2.3.2
  15643. function parse_workbook(blob, options) {
  15644. var wb = ({opts:{}});
  15645. var Sheets = {};
  15646. if(DENSE != null && options.dense == null) options.dense = DENSE;
  15647. var out = ((options.dense ? [] : {}));
  15648. var Directory = {};
  15649. var range = ({});
  15650. var last_formula = null;
  15651. var sst = ([]);
  15652. var cur_sheet = "";
  15653. var Preamble = {};
  15654. var lastcell, last_cell = "", cc, cmnt, rngC, rngR;
  15655. var sharedf = {};
  15656. var arrayf = [];
  15657. var temp_val;
  15658. var country;
  15659. var cell_valid = true;
  15660. var XFs = []; /* XF records */
  15661. var palette = [];
  15662. var Workbook = ({ Sheets:[], WBProps:{date1904:false}, Views:[{}] }), wsprops = {};
  15663. var get_rgb = function getrgb(icv) {
  15664. if(icv < 8) return XLSIcv[icv];
  15665. if(icv < 64) return palette[icv-8] || XLSIcv[icv];
  15666. return XLSIcv[icv];
  15667. };
  15668. var process_cell_style = function pcs(cell, line, options) {
  15669. var xfd = line.XF.data;
  15670. if(!xfd || !xfd.patternType || !options || !options.cellStyles) return;
  15671. line.s = ({});
  15672. line.s.patternType = xfd.patternType;
  15673. var t;
  15674. if((t = rgb2Hex(get_rgb(xfd.icvFore)))) { line.s.fgColor = {rgb:t}; }
  15675. if((t = rgb2Hex(get_rgb(xfd.icvBack)))) { line.s.bgColor = {rgb:t}; }
  15676. };
  15677. var addcell = function addcell(cell, line, options) {
  15678. if(file_depth > 1) return;
  15679. if(options.sheetRows && cell.r >= options.sheetRows) cell_valid = false;
  15680. if(!cell_valid) return;
  15681. if(options.cellStyles && line.XF && line.XF.data) process_cell_style(cell, line, options);
  15682. delete line.ixfe; delete line.XF;
  15683. lastcell = cell;
  15684. last_cell = encode_cell(cell);
  15685. if(!range || !range.s || !range.e) range = {s:{r:0,c:0},e:{r:0,c:0}};
  15686. if(cell.r < range.s.r) range.s.r = cell.r;
  15687. if(cell.c < range.s.c) range.s.c = cell.c;
  15688. if(cell.r + 1 > range.e.r) range.e.r = cell.r + 1;
  15689. if(cell.c + 1 > range.e.c) range.e.c = cell.c + 1;
  15690. if(options.cellFormula && line.f) {
  15691. for(var afi = 0; afi < arrayf.length; ++afi) {
  15692. if(arrayf[afi][0].s.c > cell.c || arrayf[afi][0].s.r > cell.r) continue;
  15693. if(arrayf[afi][0].e.c < cell.c || arrayf[afi][0].e.r < cell.r) continue;
  15694. line.F = encode_range(arrayf[afi][0]);
  15695. if(arrayf[afi][0].s.c != cell.c || arrayf[afi][0].s.r != cell.r) delete line.f;
  15696. if(line.f) line.f = "" + stringify_formula(arrayf[afi][1], range, cell, supbooks, opts);
  15697. break;
  15698. }
  15699. }
  15700. {
  15701. if(options.dense) {
  15702. if(!out[cell.r]) out[cell.r] = [];
  15703. out[cell.r][cell.c] = line;
  15704. } else out[last_cell] = line;
  15705. }
  15706. };
  15707. var opts = ({
  15708. enc: false, // encrypted
  15709. sbcch: 0, // cch in the preceding SupBook
  15710. snames: [], // sheetnames
  15711. sharedf: sharedf, // shared formulae by address
  15712. arrayf: arrayf, // array formulae array
  15713. rrtabid: [], // RRTabId
  15714. lastuser: "", // Last User from WriteAccess
  15715. biff: 8, // BIFF version
  15716. codepage: 0, // CP from CodePage record
  15717. winlocked: 0, // fLockWn from WinProtect
  15718. cellStyles: !!options && !!options.cellStyles,
  15719. WTF: !!options && !!options.wtf
  15720. });
  15721. if(options.password) opts.password = options.password;
  15722. var themes;
  15723. var merges = [];
  15724. var objects = [];
  15725. var colinfo = [], rowinfo = [];
  15726. // eslint-disable-next-line no-unused-vars
  15727. var defwidth = 0, defheight = 0; // twips / MDW respectively
  15728. var seencol = false;
  15729. var supbooks = ([]); // 1-indexed, will hold extern names
  15730. supbooks.SheetNames = opts.snames;
  15731. supbooks.sharedf = opts.sharedf;
  15732. supbooks.arrayf = opts.arrayf;
  15733. supbooks.names = [];
  15734. supbooks.XTI = [];
  15735. var last_Rn = '';
  15736. var file_depth = 0; /* TODO: make a real stack */
  15737. var BIFF2Fmt = 0, BIFF2FmtTable = [];
  15738. var FilterDatabases = []; /* TODO: sort out supbooks and process elsewhere */
  15739. var last_lbl;
  15740. /* explicit override for some broken writers */
  15741. opts.codepage = 1200;
  15742. set_cp(1200);
  15743. var seen_codepage = false;
  15744. while(blob.l < blob.length - 1) {
  15745. var s = blob.l;
  15746. var RecordType = blob.read_shift(2);
  15747. if(RecordType === 0 && last_Rn === 'EOF') break;
  15748. var length = (blob.l === blob.length ? 0 : blob.read_shift(2));
  15749. var R = XLSRecordEnum[RecordType];
  15750. //console.log(RecordType.toString(16), RecordType, R, blob.l, length, blob.length);
  15751. //if(!R) console.log(blob.slice(blob.l, blob.l + length));
  15752. if(R && R.f) {
  15753. if(options.bookSheets) {
  15754. if(last_Rn === 'BoundSheet8' && R.n !== 'BoundSheet8') break;
  15755. }
  15756. last_Rn = R.n;
  15757. if(R.r === 2 || R.r == 12) {
  15758. var rt = blob.read_shift(2); length -= 2;
  15759. if(!opts.enc && rt !== RecordType && (((rt&0xFF)<<8)|(rt>>8)) !== RecordType) throw new Error("rt mismatch: " + rt + "!=" + RecordType);
  15760. if(R.r == 12){ blob.l += 10; length -= 10; } // skip FRT
  15761. }
  15762. //console.error(R,blob.l,length,blob.length);
  15763. var val = ({});
  15764. if(R.n === 'EOF') val = R.f(blob, length, opts);
  15765. else val = slurp(R, blob, length, opts);
  15766. var Rn = R.n;
  15767. if(file_depth == 0 && Rn != 'BOF') continue;
  15768. /* nested switch statements to workaround V8 128 limit */
  15769. switch(Rn) {
  15770. /* Workbook Options */
  15771. case 'Date1904':
  15772. wb.opts.Date1904 = Workbook.WBProps.date1904 = val; break;
  15773. case 'WriteProtect': wb.opts.WriteProtect = true; break;
  15774. case 'FilePass':
  15775. if(!opts.enc) blob.l = 0;
  15776. opts.enc = val;
  15777. if(!options.password) throw new Error("File is password-protected");
  15778. if(val.valid == null) throw new Error("Encryption scheme unsupported");
  15779. if(!val.valid) throw new Error("Password is incorrect");
  15780. break;
  15781. case 'WriteAccess': opts.lastuser = val; break;
  15782. case 'FileSharing': break; //TODO
  15783. case 'CodePage':
  15784. var cpval = Number(val);
  15785. /* overrides based on test cases */
  15786. switch(cpval) {
  15787. case 0x5212: cpval = 1200; break;
  15788. case 0x8000: cpval = 10000; break;
  15789. case 0x8001: cpval = 1252; break;
  15790. }
  15791. set_cp(opts.codepage = cpval);
  15792. seen_codepage = true;
  15793. break;
  15794. case 'RRTabId': opts.rrtabid = val; break;
  15795. case 'WinProtect': opts.winlocked = val; break;
  15796. case 'Template': break; // TODO
  15797. case 'BookBool': break; // TODO
  15798. case 'UsesELFs': break;
  15799. case 'MTRSettings': break;
  15800. case 'RefreshAll':
  15801. case 'CalcCount':
  15802. case 'CalcDelta':
  15803. case 'CalcIter':
  15804. case 'CalcMode':
  15805. case 'CalcPrecision':
  15806. case 'CalcSaveRecalc':
  15807. wb.opts[Rn] = val; break;
  15808. case 'CalcRefMode': opts.CalcRefMode = val; break; // TODO: implement R1C1
  15809. case 'Uncalced': break;
  15810. case 'ForceFullCalculation': wb.opts.FullCalc = val; break;
  15811. case 'WsBool':
  15812. if(val.fDialog) out["!type"] = "dialog";
  15813. break; // TODO
  15814. case 'XF':
  15815. XFs.push(val); break;
  15816. case 'ExtSST': break; // TODO
  15817. case 'BookExt': break; // TODO
  15818. case 'RichTextStream': break;
  15819. case 'BkHim': break;
  15820. case 'SupBook':
  15821. supbooks.push([val]);
  15822. supbooks[supbooks.length-1].XTI = [];
  15823. break;
  15824. case 'ExternName':
  15825. supbooks[supbooks.length-1].push(val);
  15826. break;
  15827. case 'Index': break; // TODO
  15828. case 'Lbl':
  15829. last_lbl = ({
  15830. Name: val.Name,
  15831. Ref: stringify_formula(val.rgce,range,null,supbooks,opts)
  15832. });
  15833. if(val.itab > 0) last_lbl.Sheet = val.itab - 1;
  15834. supbooks.names.push(last_lbl);
  15835. if(!supbooks[0]) { supbooks[0] = []; supbooks[0].XTI = []; }
  15836. supbooks[supbooks.length-1].push(val);
  15837. if(val.Name == "_xlnm._FilterDatabase" && val.itab > 0)
  15838. if(val.rgce && val.rgce[0] && val.rgce[0][0] && val.rgce[0][0][0] == 'PtgArea3d')
  15839. FilterDatabases[val.itab - 1] = { ref: encode_range(val.rgce[0][0][1][2]) };
  15840. break;
  15841. case 'ExternCount': opts.ExternCount = val; break;
  15842. case 'ExternSheet':
  15843. if(supbooks.length == 0) { supbooks[0] = []; supbooks[0].XTI = []; }
  15844. supbooks[supbooks.length - 1].XTI = supbooks[supbooks.length - 1].XTI.concat(val); supbooks.XTI = supbooks.XTI.concat(val); break;
  15845. case 'NameCmt':
  15846. /* TODO: search for correct name */
  15847. if(opts.biff < 8) break;
  15848. if(last_lbl != null) last_lbl.Comment = val[1];
  15849. break;
  15850. case 'Protect': out["!protect"] = val; break; /* for sheet or book */
  15851. case 'Password': if(val !== 0 && opts.WTF) console.error("Password verifier: " + val); break;
  15852. case 'Prot4Rev': case 'Prot4RevPass': break; /*TODO: Revision Control*/
  15853. case 'BoundSheet8': {
  15854. Directory[val.pos] = val;
  15855. opts.snames.push(val.name);
  15856. } break;
  15857. case 'EOF': {
  15858. if(--file_depth) break;
  15859. if(range.e) {
  15860. if(range.e.r > 0 && range.e.c > 0) {
  15861. range.e.r--; range.e.c--;
  15862. out["!ref"] = encode_range(range);
  15863. if(options.sheetRows && options.sheetRows <= range.e.r) {
  15864. var tmpri = range.e.r;
  15865. range.e.r = options.sheetRows - 1;
  15866. out["!fullref"] = out["!ref"];
  15867. out["!ref"] = encode_range(range);
  15868. range.e.r = tmpri;
  15869. }
  15870. range.e.r++; range.e.c++;
  15871. }
  15872. if(merges.length > 0) out["!merges"] = merges;
  15873. if(objects.length > 0) out["!objects"] = objects;
  15874. if(colinfo.length > 0) out["!cols"] = colinfo;
  15875. if(rowinfo.length > 0) out["!rows"] = rowinfo;
  15876. Workbook.Sheets.push(wsprops);
  15877. }
  15878. if(cur_sheet === "") Preamble = out; else Sheets[cur_sheet] = out;
  15879. out = ((options.dense ? [] : {}));
  15880. } break;
  15881. case 'BOF': {
  15882. if(opts.biff === 8) opts.biff = {
  15883. 0x0009:2,
  15884. 0x0209:3,
  15885. 0x0409:4
  15886. }[RecordType] || {
  15887. 0x0200:2,
  15888. 0x0300:3,
  15889. 0x0400:4,
  15890. 0x0500:5,
  15891. 0x0600:8,
  15892. 0x0002:2,
  15893. 0x0007:2
  15894. }[val.BIFFVer] || 8;
  15895. if(opts.biff == 8 && val.BIFFVer == 0 && val.dt == 16) opts.biff = 2;
  15896. if(file_depth++) break;
  15897. cell_valid = true;
  15898. out = ((options.dense ? [] : {}));
  15899. if(opts.biff < 8 && !seen_codepage) { seen_codepage = true; set_cp(opts.codepage = options.codepage || 1252); }
  15900. if(opts.biff < 5) {
  15901. if(cur_sheet === "") cur_sheet = "Sheet1";
  15902. range = {s:{r:0,c:0},e:{r:0,c:0}};
  15903. /* fake BoundSheet8 */
  15904. var fakebs8 = {pos: blob.l - length, name:cur_sheet};
  15905. Directory[fakebs8.pos] = fakebs8;
  15906. opts.snames.push(cur_sheet);
  15907. }
  15908. else cur_sheet = (Directory[s] || {name:""}).name;
  15909. if(val.dt == 0x20) out["!type"] = "chart";
  15910. if(val.dt == 0x40) out["!type"] = "macro";
  15911. merges = [];
  15912. objects = [];
  15913. opts.arrayf = arrayf = [];
  15914. colinfo = []; rowinfo = [];
  15915. defwidth = defheight = 0;
  15916. seencol = false;
  15917. wsprops = {Hidden:(Directory[s]||{hs:0}).hs, name:cur_sheet };
  15918. } break;
  15919. case 'Number': case 'BIFF2NUM': case 'BIFF2INT': {
  15920. if(out["!type"] == "chart") if(options.dense ? (out[val.r]||[])[val.c]: out[encode_cell({c:val.c, r:val.r})]) ++val.c;
  15921. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe]||{}, v:val.val, t:'n'});
  15922. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15923. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15924. addcell({c:val.c, r:val.r}, temp_val, options);
  15925. } break;
  15926. case 'BoolErr': {
  15927. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], v:val.val, t:val.t});
  15928. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15929. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15930. addcell({c:val.c, r:val.r}, temp_val, options);
  15931. } break;
  15932. case 'RK': {
  15933. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], v:val.rknum, t:'n'});
  15934. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15935. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15936. addcell({c:val.c, r:val.r}, temp_val, options);
  15937. } break;
  15938. case 'MulRk': {
  15939. for(var j = val.c; j <= val.C; ++j) {
  15940. var ixfe = val.rkrec[j-val.c][0];
  15941. temp_val= ({ixfe:ixfe, XF:XFs[ixfe], v:val.rkrec[j-val.c][1], t:'n'});
  15942. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15943. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15944. addcell({c:j, r:val.r}, temp_val, options);
  15945. }
  15946. } break;
  15947. case 'Formula': {
  15948. if(val.val == 'String') { last_formula = val; break; }
  15949. temp_val = make_cell(val.val, val.cell.ixfe, val.tt);
  15950. temp_val.XF = XFs[temp_val.ixfe];
  15951. if(options.cellFormula) {
  15952. var _f = val.formula;
  15953. if(_f && _f[0] && _f[0][0] && _f[0][0][0] == 'PtgExp') {
  15954. var _fr = _f[0][0][1][0], _fc = _f[0][0][1][1];
  15955. var _fe = encode_cell({r:_fr, c:_fc});
  15956. if(sharedf[_fe]) temp_val.f = ""+stringify_formula(val.formula,range,val.cell,supbooks, opts);
  15957. else temp_val.F = ((options.dense ? (out[_fr]||[])[_fc]: out[_fe]) || {}).F;
  15958. } else temp_val.f = ""+stringify_formula(val.formula,range,val.cell,supbooks, opts);
  15959. }
  15960. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15961. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15962. addcell(val.cell, temp_val, options);
  15963. last_formula = val;
  15964. } break;
  15965. case 'String': {
  15966. if(last_formula) { /* technically always true */
  15967. last_formula.val = val;
  15968. temp_val = make_cell(val, last_formula.cell.ixfe, 's');
  15969. temp_val.XF = XFs[temp_val.ixfe];
  15970. if(options.cellFormula) {
  15971. temp_val.f = ""+stringify_formula(last_formula.formula, range, last_formula.cell, supbooks, opts);
  15972. }
  15973. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  15974. safe_format_xf(temp_val, options, wb.opts.Date1904);
  15975. addcell(last_formula.cell, temp_val, options);
  15976. last_formula = null;
  15977. } else throw new Error("String record expects Formula");
  15978. } break;
  15979. case 'Array': {
  15980. arrayf.push(val);
  15981. var _arraystart = encode_cell(val[0].s);
  15982. cc = options.dense ? (out[val[0].s.r]||[])[val[0].s.c] : out[_arraystart];
  15983. if(options.cellFormula && cc) {
  15984. if(!last_formula) break; /* technically unreachable */
  15985. if(!_arraystart || !cc) break;
  15986. cc.f = ""+stringify_formula(val[1], range, val[0], supbooks, opts);
  15987. cc.F = encode_range(val[0]);
  15988. }
  15989. } break;
  15990. case 'ShrFmla': {
  15991. if(!cell_valid) break;
  15992. if(!options.cellFormula) break;
  15993. if(last_cell) {
  15994. /* TODO: capture range */
  15995. if(!last_formula) break; /* technically unreachable */
  15996. sharedf[encode_cell(last_formula.cell)]= val[0];
  15997. cc = options.dense ? (out[last_formula.cell.r]||[])[last_formula.cell.c] : out[encode_cell(last_formula.cell)];
  15998. (cc||{}).f = ""+stringify_formula(val[0], range, lastcell, supbooks, opts);
  15999. }
  16000. } break;
  16001. case 'LabelSst':
  16002. temp_val=make_cell(sst[val.isst].t, val.ixfe, 's');
  16003. if(sst[val.isst].h) temp_val.h = sst[val.isst].h;
  16004. temp_val.XF = XFs[temp_val.ixfe];
  16005. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  16006. safe_format_xf(temp_val, options, wb.opts.Date1904);
  16007. addcell({c:val.c, r:val.r}, temp_val, options);
  16008. break;
  16009. case 'Blank': if(options.sheetStubs) {
  16010. temp_val = ({ixfe: val.ixfe, XF: XFs[val.ixfe], t:'z'});
  16011. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  16012. safe_format_xf(temp_val, options, wb.opts.Date1904);
  16013. addcell({c:val.c, r:val.r}, temp_val, options);
  16014. } break;
  16015. case 'MulBlank': if(options.sheetStubs) {
  16016. for(var _j = val.c; _j <= val.C; ++_j) {
  16017. var _ixfe = val.ixfe[_j-val.c];
  16018. temp_val= ({ixfe:_ixfe, XF:XFs[_ixfe], t:'z'});
  16019. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  16020. safe_format_xf(temp_val, options, wb.opts.Date1904);
  16021. addcell({c:_j, r:val.r}, temp_val, options);
  16022. }
  16023. } break;
  16024. case 'RString':
  16025. case 'Label': case 'BIFF2STR':
  16026. temp_val=make_cell(val.val, val.ixfe, 's');
  16027. temp_val.XF = XFs[temp_val.ixfe];
  16028. if(BIFF2Fmt > 0) temp_val.z = BIFF2FmtTable[(temp_val.ixfe>>8) & 0x1F];
  16029. safe_format_xf(temp_val, options, wb.opts.Date1904);
  16030. addcell({c:val.c, r:val.r}, temp_val, options);
  16031. break;
  16032. case 'Dimensions': {
  16033. if(file_depth === 1) range = val; /* TODO: stack */
  16034. } break;
  16035. case 'SST': {
  16036. sst = val;
  16037. } break;
  16038. case 'Format': { /* val = [id, fmt] */
  16039. if(opts.biff == 4) {
  16040. BIFF2FmtTable[BIFF2Fmt++] = val[1];
  16041. for(var b4idx = 0; b4idx < BIFF2Fmt + 163; ++b4idx) if(SSF._table[b4idx] == val[1]) break;
  16042. if(b4idx >= 163) SSF.load(val[1], BIFF2Fmt + 163);
  16043. }
  16044. else SSF.load(val[1], val[0]);
  16045. } break;
  16046. case 'BIFF2FORMAT': {
  16047. BIFF2FmtTable[BIFF2Fmt++] = val;
  16048. for(var b2idx = 0; b2idx < BIFF2Fmt + 163; ++b2idx) if(SSF._table[b2idx] == val) break;
  16049. if(b2idx >= 163) SSF.load(val, BIFF2Fmt + 163);
  16050. } break;
  16051. case 'MergeCells': merges = merges.concat(val); break;
  16052. case 'Obj': objects[val.cmo[0]] = opts.lastobj = val; break;
  16053. case 'TxO': opts.lastobj.TxO = val; break;
  16054. case 'ImData': opts.lastobj.ImData = val; break;
  16055. case 'HLink': {
  16056. for(rngR = val[0].s.r; rngR <= val[0].e.r; ++rngR)
  16057. for(rngC = val[0].s.c; rngC <= val[0].e.c; ++rngC) {
  16058. cc = options.dense ? (out[rngR]||[])[rngC] : out[encode_cell({c:rngC,r:rngR})];
  16059. if(cc) cc.l = val[1];
  16060. }
  16061. } break;
  16062. case 'HLinkTooltip': {
  16063. for(rngR = val[0].s.r; rngR <= val[0].e.r; ++rngR)
  16064. for(rngC = val[0].s.c; rngC <= val[0].e.c; ++rngC) {
  16065. cc = options.dense ? (out[rngR]||[])[rngC] : out[encode_cell({c:rngC,r:rngR})];
  16066. if(cc && cc.l) cc.l.Tooltip = val[1];
  16067. }
  16068. } break;
  16069. /* Comments */
  16070. case 'Note': {
  16071. if(opts.biff <= 5 && opts.biff >= 2) break; /* TODO: BIFF5 */
  16072. cc = options.dense ? (out[val[0].r]||[])[val[0].c] : out[encode_cell(val[0])];
  16073. var noteobj = objects[val[2]];
  16074. if(!cc) {
  16075. if(options.dense) {
  16076. if(!out[val[0].r]) out[val[0].r] = [];
  16077. cc = out[val[0].r][val[0].c] = ({t:"z"});
  16078. } else {
  16079. cc = out[encode_cell(val[0])] = ({t:"z"});
  16080. }
  16081. range.e.r = Math.max(range.e.r, val[0].r);
  16082. range.s.r = Math.min(range.s.r, val[0].r);
  16083. range.e.c = Math.max(range.e.c, val[0].c);
  16084. range.s.c = Math.min(range.s.c, val[0].c);
  16085. }
  16086. if(!cc.c) cc.c = [];
  16087. cmnt = {a:val[1],t:noteobj.TxO.t};
  16088. cc.c.push(cmnt);
  16089. } break;
  16090. default: switch(R.n) { /* nested */
  16091. case 'ClrtClient': break;
  16092. case 'XFExt': update_xfext(XFs[val.ixfe], val.ext); break;
  16093. case 'DefColWidth': defwidth = val; break;
  16094. case 'DefaultRowHeight': defheight = val[1]; break; // TODO: flags
  16095. case 'ColInfo': {
  16096. if(!opts.cellStyles) break;
  16097. while(val.e >= val.s) {
  16098. colinfo[val.e--] = { width: val.w/256 };
  16099. if(!seencol) { seencol = true; find_mdw_colw(val.w/256); }
  16100. process_col(colinfo[val.e+1]);
  16101. }
  16102. } break;
  16103. case 'Row': {
  16104. var rowobj = {};
  16105. if(val.level != null) { rowinfo[val.r] = rowobj; rowobj.level = val.level; }
  16106. if(val.hidden) { rowinfo[val.r] = rowobj; rowobj.hidden = true; }
  16107. if(val.hpt) {
  16108. rowinfo[val.r] = rowobj;
  16109. rowobj.hpt = val.hpt; rowobj.hpx = pt2px(val.hpt);
  16110. }
  16111. } break;
  16112. case 'LeftMargin':
  16113. case 'RightMargin':
  16114. case 'TopMargin':
  16115. case 'BottomMargin':
  16116. if(!out['!margins']) default_margins(out['!margins'] = {});
  16117. out['!margins'][Rn.slice(0,-6).toLowerCase()] = val;
  16118. break;
  16119. case 'Setup': // TODO
  16120. if(!out['!margins']) default_margins(out['!margins'] = {});
  16121. out['!margins'].header = val.header;
  16122. out['!margins'].footer = val.footer;
  16123. break;
  16124. case 'Window2': // TODO
  16125. // $FlowIgnore
  16126. if(val.RTL) Workbook.Views[0].RTL = true;
  16127. break;
  16128. case 'Header': break; // TODO
  16129. case 'Footer': break; // TODO
  16130. case 'HCenter': break; // TODO
  16131. case 'VCenter': break; // TODO
  16132. case 'Pls': break; // TODO
  16133. case 'GCW': break;
  16134. case 'LHRecord': break;
  16135. case 'DBCell': break; // TODO
  16136. case 'EntExU2': break; // TODO
  16137. case 'SxView': break; // TODO
  16138. case 'Sxvd': break; // TODO
  16139. case 'SXVI': break; // TODO
  16140. case 'SXVDEx': break; // TODO
  16141. case 'SxIvd': break; // TODO
  16142. case 'SXString': break; // TODO
  16143. case 'Sync': break;
  16144. case 'Addin': break;
  16145. case 'SXDI': break; // TODO
  16146. case 'SXLI': break; // TODO
  16147. case 'SXEx': break; // TODO
  16148. case 'QsiSXTag': break; // TODO
  16149. case 'Selection': break;
  16150. case 'Feat': break;
  16151. case 'FeatHdr': case 'FeatHdr11': break;
  16152. case 'Feature11': case 'Feature12': case 'List12': break;
  16153. case 'Country': country = val; break;
  16154. case 'RecalcId': break;
  16155. case 'DxGCol': break; // TODO: htmlify
  16156. case 'Fbi': case 'Fbi2': case 'GelFrame': break;
  16157. case 'Font': break; // TODO
  16158. case 'XFCRC': break; // TODO
  16159. case 'Style': break; // TODO
  16160. case 'StyleExt': break; // TODO
  16161. case 'Palette': palette = val; break;
  16162. case 'Theme': themes = val; break;
  16163. /* Protection */
  16164. case 'ScenarioProtect': break;
  16165. case 'ObjProtect': break;
  16166. /* Conditional Formatting */
  16167. case 'CondFmt12': break;
  16168. /* Table */
  16169. case 'Table': break; // TODO
  16170. case 'TableStyles': break; // TODO
  16171. case 'TableStyle': break; // TODO
  16172. case 'TableStyleElement': break; // TODO
  16173. /* PivotTable */
  16174. case 'SXStreamID': break; // TODO
  16175. case 'SXVS': break; // TODO
  16176. case 'DConRef': break; // TODO
  16177. case 'SXAddl': break; // TODO
  16178. case 'DConBin': break; // TODO
  16179. case 'DConName': break; // TODO
  16180. case 'SXPI': break; // TODO
  16181. case 'SxFormat': break; // TODO
  16182. case 'SxSelect': break; // TODO
  16183. case 'SxRule': break; // TODO
  16184. case 'SxFilt': break; // TODO
  16185. case 'SxItm': break; // TODO
  16186. case 'SxDXF': break; // TODO
  16187. /* Scenario Manager */
  16188. case 'ScenMan': break;
  16189. /* Data Consolidation */
  16190. case 'DCon': break;
  16191. /* Watched Cell */
  16192. case 'CellWatch': break;
  16193. /* Print Settings */
  16194. case 'PrintRowCol': break;
  16195. case 'PrintGrid': break;
  16196. case 'PrintSize': break;
  16197. case 'XCT': break;
  16198. case 'CRN': break;
  16199. case 'Scl': {
  16200. //console.log("Zoom Level:", val[0]/val[1],val);
  16201. } break;
  16202. case 'SheetExt': {
  16203. /* empty */
  16204. } break;
  16205. case 'SheetExtOptional': {
  16206. /* empty */
  16207. } break;
  16208. /* VBA */
  16209. case 'ObNoMacros': {
  16210. /* empty */
  16211. } break;
  16212. case 'ObProj': {
  16213. /* empty */
  16214. } break;
  16215. case 'CodeName': {
  16216. if(!cur_sheet) Workbook.WBProps.CodeName = val || "ThisWorkbook";
  16217. else wsprops.CodeName = val || wsprops.name;
  16218. } break;
  16219. case 'GUIDTypeLib': {
  16220. /* empty */
  16221. } break;
  16222. case 'WOpt': break; // TODO: WTF?
  16223. case 'PhoneticInfo': break;
  16224. case 'OleObjectSize': break;
  16225. /* Differential Formatting */
  16226. case 'DXF': case 'DXFN': case 'DXFN12': case 'DXFN12List': case 'DXFN12NoCB': break;
  16227. /* Data Validation */
  16228. case 'Dv': case 'DVal': break;
  16229. /* Data Series */
  16230. case 'BRAI': case 'Series': case 'SeriesText': break;
  16231. /* Data Connection */
  16232. case 'DConn': break;
  16233. case 'DbOrParamQry': break;
  16234. case 'DBQueryExt': break;
  16235. case 'OleDbConn': break;
  16236. case 'ExtString': break;
  16237. /* Formatting */
  16238. case 'IFmtRecord': break;
  16239. case 'CondFmt': case 'CF': case 'CF12': case 'CFEx': break;
  16240. /* Explicitly Ignored */
  16241. case 'Excel9File': break;
  16242. case 'Units': break;
  16243. case 'InterfaceHdr': case 'Mms': case 'InterfaceEnd': case 'DSF': break;
  16244. case 'BuiltInFnGroupCount': /* 2.4.30 0x0E or 0x10 but excel 2011 generates 0x11? */ break;
  16245. /* View Stuff */
  16246. case 'Window1': case 'HideObj': case 'GridSet': case 'Guts':
  16247. case 'UserBView': case 'UserSViewBegin': case 'UserSViewEnd': break;
  16248. case 'Pane': break;
  16249. default: switch(R.n) { /* nested */
  16250. /* Chart */
  16251. case 'Dat':
  16252. case 'Begin': case 'End':
  16253. case 'StartBlock': case 'EndBlock':
  16254. case 'Frame': case 'Area':
  16255. case 'Axis': case 'AxisLine': case 'Tick': break;
  16256. case 'AxesUsed':
  16257. case 'CrtLayout12': case 'CrtLayout12A': case 'CrtLink': case 'CrtLine': case 'CrtMlFrt': case 'CrtMlFrtContinue': break;
  16258. case 'LineFormat': case 'AreaFormat':
  16259. case 'Chart': case 'Chart3d': case 'Chart3DBarShape': case 'ChartFormat': case 'ChartFrtInfo': break;
  16260. case 'PlotArea': case 'PlotGrowth': break;
  16261. case 'SeriesList': case 'SerParent': case 'SerAuxTrend': break;
  16262. case 'DataFormat': case 'SerToCrt': case 'FontX': break;
  16263. case 'CatSerRange': case 'AxcExt': case 'SerFmt': break;
  16264. case 'ShtProps': break;
  16265. case 'DefaultText': case 'Text': case 'CatLab': break;
  16266. case 'DataLabExtContents': break;
  16267. case 'Legend': case 'LegendException': break;
  16268. case 'Pie': case 'Scatter': break;
  16269. case 'PieFormat': case 'MarkerFormat': break;
  16270. case 'StartObject': case 'EndObject': break;
  16271. case 'AlRuns': case 'ObjectLink': break;
  16272. case 'SIIndex': break;
  16273. case 'AttachedLabel': case 'YMult': break;
  16274. /* Chart Group */
  16275. case 'Line': case 'Bar': break;
  16276. case 'Surf': break;
  16277. /* Axis Group */
  16278. case 'AxisParent': break;
  16279. case 'Pos': break;
  16280. case 'ValueRange': break;
  16281. /* Pivot Chart */
  16282. case 'SXViewEx9': break; // TODO
  16283. case 'SXViewLink': break;
  16284. case 'PivotChartBits': break;
  16285. case 'SBaseRef': break;
  16286. case 'TextPropsStream': break;
  16287. /* Chart Misc */
  16288. case 'LnExt': break;
  16289. case 'MkrExt': break;
  16290. case 'CrtCoopt': break;
  16291. /* Query Table */
  16292. case 'Qsi': case 'Qsif': case 'Qsir': case 'QsiSXTag': break;
  16293. case 'TxtQry': break;
  16294. /* Filter */
  16295. case 'FilterMode': break;
  16296. case 'AutoFilter': case 'AutoFilterInfo': break;
  16297. case 'AutoFilter12': break;
  16298. case 'DropDownObjIds': break;
  16299. case 'Sort': break;
  16300. case 'SortData': break;
  16301. /* Drawing */
  16302. case 'ShapePropsStream': break;
  16303. case 'MsoDrawing': case 'MsoDrawingGroup': case 'MsoDrawingSelection': break;
  16304. /* Pub Stuff */
  16305. case 'WebPub': case 'AutoWebPub': break;
  16306. /* Print Stuff */
  16307. case 'HeaderFooter': case 'HFPicture': case 'PLV':
  16308. case 'HorizontalPageBreaks': case 'VerticalPageBreaks': break;
  16309. /* Behavioral */
  16310. case 'Backup': case 'CompressPictures': case 'Compat12': break;
  16311. /* Should not Happen */
  16312. case 'Continue': case 'ContinueFrt12': break;
  16313. /* Future Records */
  16314. case 'FrtFontList': case 'FrtWrapper': break;
  16315. default: switch(R.n) { /* nested */
  16316. /* BIFF5 records */
  16317. case 'TabIdConf': case 'Radar': case 'RadarArea': case 'DropBar': case 'Intl': case 'CoordList': case 'SerAuxErrBar': break;
  16318. /* BIFF2-4 records */
  16319. case 'BIFF2FONTCLR': case 'BIFF2FMTCNT': case 'BIFF2FONTXTRA': break;
  16320. case 'BIFF2XF': case 'BIFF3XF': case 'BIFF4XF': break;
  16321. case 'BIFF4FMTCNT': case 'BIFF2ROW': case 'BIFF2WINDOW2': break;
  16322. /* Miscellaneous */
  16323. case 'SCENARIO': case 'DConBin': case 'PicF': case 'DataLabExt':
  16324. case 'Lel': case 'BopPop': case 'BopPopCustom': case 'RealTimeData':
  16325. case 'Name': break;
  16326. case 'LHNGraph': case 'FnGroupName': case 'AddMenu': case 'LPr': break;
  16327. case 'ListObj': case 'ListField': break;
  16328. case 'RRSort': break;
  16329. case 'BigName': break;
  16330. case 'ToolbarHdr': case 'ToolbarEnd': break;
  16331. case 'DDEObjName': break;
  16332. case 'FRTArchId$': break;
  16333. default: if(options.WTF) throw 'Unrecognized Record ' + R.n;
  16334. }}}}
  16335. } else blob.l += length;
  16336. }
  16337. wb.SheetNames=keys(Directory).sort(function(a,b) { return Number(a) - Number(b); }).map(function(x){return Directory[x].name;});
  16338. if(!options.bookSheets) wb.Sheets=Sheets;
  16339. if(wb.Sheets) FilterDatabases.forEach(function(r,i) { wb.Sheets[wb.SheetNames[i]]['!autofilter'] = r; });
  16340. wb.Preamble=Preamble;
  16341. wb.Strings = sst;
  16342. wb.SSF = SSF.get_table();
  16343. if(opts.enc) wb.Encryption = opts.enc;
  16344. if(themes) wb.Themes = themes;
  16345. wb.Metadata = {};
  16346. if(country !== undefined) wb.Metadata.Country = country;
  16347. if(supbooks.names.length > 0) Workbook.Names = supbooks.names;
  16348. wb.Workbook = Workbook;
  16349. return wb;
  16350. }
  16351. /* TODO: split props*/
  16352. var PSCLSID = {
  16353. SI: "e0859ff2f94f6810ab9108002b27b3d9",
  16354. DSI: "02d5cdd59c2e1b10939708002b2cf9ae",
  16355. UDI: "05d5cdd59c2e1b10939708002b2cf9ae"
  16356. };
  16357. function parse_xls_props(cfb, props, o) {
  16358. /* [MS-OSHARED] 2.3.3.2.2 Document Summary Information Property Set */
  16359. var DSI = CFB.find(cfb, '!DocumentSummaryInformation');
  16360. if(DSI && DSI.size > 0) try {
  16361. var DocSummary = parse_PropertySetStream(DSI, DocSummaryPIDDSI, PSCLSID.DSI);
  16362. for(var d in DocSummary) props[d] = DocSummary[d];
  16363. } catch(e) {if(o.WTF) throw e;/* empty */}
  16364. /* [MS-OSHARED] 2.3.3.2.1 Summary Information Property Set*/
  16365. var SI = CFB.find(cfb, '!SummaryInformation');
  16366. if(SI && SI.size > 0) try {
  16367. var Summary = parse_PropertySetStream(SI, SummaryPIDSI, PSCLSID.SI);
  16368. for(var s in Summary) if(props[s] == null) props[s] = Summary[s];
  16369. } catch(e) {if(o.WTF) throw e;/* empty */}
  16370. if(props.HeadingPairs && props.TitlesOfParts) {
  16371. load_props_pairs(props.HeadingPairs, props.TitlesOfParts, props, o);
  16372. delete props.HeadingPairs; delete props.TitlesOfParts;
  16373. }
  16374. }
  16375. function write_xls_props(wb, cfb) {
  16376. var DSEntries = [], SEntries = [], CEntries = [];
  16377. var i = 0, Keys;
  16378. if(wb.Props) {
  16379. Keys = keys(wb.Props);
  16380. // $FlowIgnore
  16381. for(i = 0; i < Keys.length; ++i) (Object.prototype.hasOwnProperty.call(DocSummaryRE, Keys[i]) ? DSEntries : Object.prototype.hasOwnProperty.call(SummaryRE, Keys[i]) ? SEntries : CEntries).push([Keys[i], wb.Props[Keys[i]]]);
  16382. }
  16383. if(wb.Custprops) {
  16384. Keys = keys(wb.Custprops);
  16385. // $FlowIgnore
  16386. for(i = 0; i < Keys.length; ++i) if(!Object.prototype.hasOwnProperty.call((wb.Props||{}), Keys[i])) (Object.prototype.hasOwnProperty.call(DocSummaryRE, Keys[i]) ? DSEntries : Object.prototype.hasOwnProperty.call(SummaryRE, Keys[i]) ? SEntries : CEntries).push([Keys[i], wb.Custprops[Keys[i]]]);
  16387. }
  16388. var CEntries2 = [];
  16389. for(i = 0; i < CEntries.length; ++i) {
  16390. if(XLSPSSkip.indexOf(CEntries[i][0]) > -1) continue;
  16391. if(CEntries[i][1] == null) continue;
  16392. CEntries2.push(CEntries[i]);
  16393. }
  16394. if(SEntries.length) CFB.utils.cfb_add(cfb, "/\u0005SummaryInformation", write_PropertySetStream(SEntries, PSCLSID.SI, SummaryRE, SummaryPIDSI));
  16395. if(DSEntries.length || CEntries2.length) CFB.utils.cfb_add(cfb, "/\u0005DocumentSummaryInformation", write_PropertySetStream(DSEntries, PSCLSID.DSI, DocSummaryRE, DocSummaryPIDDSI, CEntries2.length ? CEntries2 : null, PSCLSID.UDI));
  16396. }
  16397. function parse_xlscfb(cfb, options) {
  16398. if(!options) options = {};
  16399. fix_read_opts(options);
  16400. reset_cp();
  16401. if(options.codepage) set_ansi(options.codepage);
  16402. var CompObj, WB;
  16403. if(cfb.FullPaths) {
  16404. if(CFB.find(cfb, '/encryption')) throw new Error("File is password-protected");
  16405. CompObj = CFB.find(cfb, '!CompObj');
  16406. WB = CFB.find(cfb, '/Workbook') || CFB.find(cfb, '/Book');
  16407. } else {
  16408. switch(options.type) {
  16409. case 'base64': cfb = s2a(Base64.decode(cfb)); break;
  16410. case 'binary': cfb = s2a(cfb); break;
  16411. case 'buffer': break;
  16412. case 'array': if(!Array.isArray(cfb)) cfb = Array.prototype.slice.call(cfb); break;
  16413. }
  16414. prep_blob(cfb, 0);
  16415. WB = ({content: cfb});
  16416. }
  16417. var WorkbookP;
  16418. var _data;
  16419. if(CompObj) parse_compobj(CompObj);
  16420. if(options.bookProps && !options.bookSheets) WorkbookP = ({});
  16421. else {
  16422. var T = has_buf ? 'buffer' : 'array';
  16423. if(WB && WB.content) WorkbookP = parse_workbook(WB.content, options);
  16424. /* Quattro Pro 7-8 */
  16425. else if((_data=CFB.find(cfb, 'PerfectOffice_MAIN')) && _data.content) WorkbookP = WK_.to_workbook(_data.content, (options.type = T, options));
  16426. /* Quattro Pro 9 */
  16427. else if((_data=CFB.find(cfb, 'NativeContent_MAIN')) && _data.content) WorkbookP = WK_.to_workbook(_data.content, (options.type = T, options));
  16428. else throw new Error("Cannot find Workbook stream");
  16429. if(options.bookVBA && cfb.FullPaths && CFB.find(cfb, '/_VBA_PROJECT_CUR/VBA/dir')) WorkbookP.vbaraw = make_vba_xls(cfb);
  16430. }
  16431. var props = {};
  16432. if(cfb.FullPaths) parse_xls_props(cfb, props, options);
  16433. WorkbookP.Props = WorkbookP.Custprops = props; /* TODO: split up properties */
  16434. if(options.bookFiles) WorkbookP.cfb = cfb;
  16435. /*WorkbookP.CompObjP = CompObjP; // TODO: storage? */
  16436. return WorkbookP;
  16437. }
  16438. function write_xlscfb(wb, opts) {
  16439. var o = opts || {};
  16440. var cfb = CFB.utils.cfb_new({root:"R"});
  16441. var wbpath = "/Workbook";
  16442. switch(o.bookType || "xls") {
  16443. case "xls": o.bookType = "biff8";
  16444. /* falls through */
  16445. case "xla": if(!o.bookType) o.bookType = "xla";
  16446. /* falls through */
  16447. case "biff8": wbpath = "/Workbook"; o.biff = 8; break;
  16448. case "biff5": wbpath = "/Book"; o.biff = 5; break;
  16449. default: throw new Error("invalid type " + o.bookType + " for XLS CFB");
  16450. }
  16451. CFB.utils.cfb_add(cfb, wbpath, write_biff_buf(wb, o));
  16452. if(o.biff == 8 && (wb.Props || wb.Custprops)) write_xls_props(wb, cfb);
  16453. // TODO: SI, DSI, CO
  16454. if(o.biff == 8 && wb.vbaraw) fill_vba_xls(cfb, CFB.read(wb.vbaraw, {type: typeof wb.vbaraw == "string" ? "binary" : "buffer"}));
  16455. return cfb;
  16456. }
  16457. /* [MS-XLSB] 2.3 Record Enumeration */
  16458. var XLSBRecordEnum = {
  16459. 0x0000: { n:"BrtRowHdr", f:parse_BrtRowHdr },
  16460. 0x0001: { n:"BrtCellBlank", f:parse_BrtCellBlank },
  16461. 0x0002: { n:"BrtCellRk", f:parse_BrtCellRk },
  16462. 0x0003: { n:"BrtCellError", f:parse_BrtCellError },
  16463. 0x0004: { n:"BrtCellBool", f:parse_BrtCellBool },
  16464. 0x0005: { n:"BrtCellReal", f:parse_BrtCellReal },
  16465. 0x0006: { n:"BrtCellSt", f:parse_BrtCellSt },
  16466. 0x0007: { n:"BrtCellIsst", f:parse_BrtCellIsst },
  16467. 0x0008: { n:"BrtFmlaString", f:parse_BrtFmlaString },
  16468. 0x0009: { n:"BrtFmlaNum", f:parse_BrtFmlaNum },
  16469. 0x000A: { n:"BrtFmlaBool", f:parse_BrtFmlaBool },
  16470. 0x000B: { n:"BrtFmlaError", f:parse_BrtFmlaError },
  16471. 0x0010: { n:"BrtFRTArchID$", f:parse_BrtFRTArchID$ },
  16472. 0x0013: { n:"BrtSSTItem", f:parse_RichStr },
  16473. 0x0014: { n:"BrtPCDIMissing" },
  16474. 0x0015: { n:"BrtPCDINumber" },
  16475. 0x0016: { n:"BrtPCDIBoolean" },
  16476. 0x0017: { n:"BrtPCDIError" },
  16477. 0x0018: { n:"BrtPCDIString" },
  16478. 0x0019: { n:"BrtPCDIDatetime" },
  16479. 0x001A: { n:"BrtPCDIIndex" },
  16480. 0x001B: { n:"BrtPCDIAMissing" },
  16481. 0x001C: { n:"BrtPCDIANumber" },
  16482. 0x001D: { n:"BrtPCDIABoolean" },
  16483. 0x001E: { n:"BrtPCDIAError" },
  16484. 0x001F: { n:"BrtPCDIAString" },
  16485. 0x0020: { n:"BrtPCDIADatetime" },
  16486. 0x0021: { n:"BrtPCRRecord" },
  16487. 0x0022: { n:"BrtPCRRecordDt" },
  16488. 0x0023: { n:"BrtFRTBegin" },
  16489. 0x0024: { n:"BrtFRTEnd" },
  16490. 0x0025: { n:"BrtACBegin" },
  16491. 0x0026: { n:"BrtACEnd" },
  16492. 0x0027: { n:"BrtName", f:parse_BrtName },
  16493. 0x0028: { n:"BrtIndexRowBlock" },
  16494. 0x002A: { n:"BrtIndexBlock" },
  16495. 0x002B: { n:"BrtFont", f:parse_BrtFont },
  16496. 0x002C: { n:"BrtFmt", f:parse_BrtFmt },
  16497. 0x002D: { n:"BrtFill", f:parse_BrtFill },
  16498. 0x002E: { n:"BrtBorder", f:parse_BrtBorder },
  16499. 0x002F: { n:"BrtXF", f:parse_BrtXF },
  16500. 0x0030: { n:"BrtStyle" },
  16501. 0x0031: { n:"BrtCellMeta" },
  16502. 0x0032: { n:"BrtValueMeta" },
  16503. 0x0033: { n:"BrtMdb" },
  16504. 0x0034: { n:"BrtBeginFmd" },
  16505. 0x0035: { n:"BrtEndFmd" },
  16506. 0x0036: { n:"BrtBeginMdx" },
  16507. 0x0037: { n:"BrtEndMdx" },
  16508. 0x0038: { n:"BrtBeginMdxTuple" },
  16509. 0x0039: { n:"BrtEndMdxTuple" },
  16510. 0x003A: { n:"BrtMdxMbrIstr" },
  16511. 0x003B: { n:"BrtStr" },
  16512. 0x003C: { n:"BrtColInfo", f:parse_ColInfo },
  16513. 0x003E: { n:"BrtCellRString" },
  16514. 0x003F: { n:"BrtCalcChainItem$", f:parse_BrtCalcChainItem$ },
  16515. 0x0040: { n:"BrtDVal", f:parse_BrtDVal },
  16516. 0x0041: { n:"BrtSxvcellNum" },
  16517. 0x0042: { n:"BrtSxvcellStr" },
  16518. 0x0043: { n:"BrtSxvcellBool" },
  16519. 0x0044: { n:"BrtSxvcellErr" },
  16520. 0x0045: { n:"BrtSxvcellDate" },
  16521. 0x0046: { n:"BrtSxvcellNil" },
  16522. 0x0080: { n:"BrtFileVersion" },
  16523. 0x0081: { n:"BrtBeginSheet" },
  16524. 0x0082: { n:"BrtEndSheet" },
  16525. 0x0083: { n:"BrtBeginBook", f:parsenoop, p:0 },
  16526. 0x0084: { n:"BrtEndBook" },
  16527. 0x0085: { n:"BrtBeginWsViews" },
  16528. 0x0086: { n:"BrtEndWsViews" },
  16529. 0x0087: { n:"BrtBeginBookViews" },
  16530. 0x0088: { n:"BrtEndBookViews" },
  16531. 0x0089: { n:"BrtBeginWsView", f:parse_BrtBeginWsView },
  16532. 0x008A: { n:"BrtEndWsView" },
  16533. 0x008B: { n:"BrtBeginCsViews" },
  16534. 0x008C: { n:"BrtEndCsViews" },
  16535. 0x008D: { n:"BrtBeginCsView" },
  16536. 0x008E: { n:"BrtEndCsView" },
  16537. 0x008F: { n:"BrtBeginBundleShs" },
  16538. 0x0090: { n:"BrtEndBundleShs" },
  16539. 0x0091: { n:"BrtBeginSheetData" },
  16540. 0x0092: { n:"BrtEndSheetData" },
  16541. 0x0093: { n:"BrtWsProp", f:parse_BrtWsProp },
  16542. 0x0094: { n:"BrtWsDim", f:parse_BrtWsDim, p:16 },
  16543. 0x0097: { n:"BrtPane", f:parse_BrtPane },
  16544. 0x0098: { n:"BrtSel" },
  16545. 0x0099: { n:"BrtWbProp", f:parse_BrtWbProp },
  16546. 0x009A: { n:"BrtWbFactoid" },
  16547. 0x009B: { n:"BrtFileRecover" },
  16548. 0x009C: { n:"BrtBundleSh", f:parse_BrtBundleSh },
  16549. 0x009D: { n:"BrtCalcProp" },
  16550. 0x009E: { n:"BrtBookView" },
  16551. 0x009F: { n:"BrtBeginSst", f:parse_BrtBeginSst },
  16552. 0x00A0: { n:"BrtEndSst" },
  16553. 0x00A1: { n:"BrtBeginAFilter", f:parse_UncheckedRfX },
  16554. 0x00A2: { n:"BrtEndAFilter" },
  16555. 0x00A3: { n:"BrtBeginFilterColumn" },
  16556. 0x00A4: { n:"BrtEndFilterColumn" },
  16557. 0x00A5: { n:"BrtBeginFilters" },
  16558. 0x00A6: { n:"BrtEndFilters" },
  16559. 0x00A7: { n:"BrtFilter" },
  16560. 0x00A8: { n:"BrtColorFilter" },
  16561. 0x00A9: { n:"BrtIconFilter" },
  16562. 0x00AA: { n:"BrtTop10Filter" },
  16563. 0x00AB: { n:"BrtDynamicFilter" },
  16564. 0x00AC: { n:"BrtBeginCustomFilters" },
  16565. 0x00AD: { n:"BrtEndCustomFilters" },
  16566. 0x00AE: { n:"BrtCustomFilter" },
  16567. 0x00AF: { n:"BrtAFilterDateGroupItem" },
  16568. 0x00B0: { n:"BrtMergeCell", f:parse_BrtMergeCell },
  16569. 0x00B1: { n:"BrtBeginMergeCells" },
  16570. 0x00B2: { n:"BrtEndMergeCells" },
  16571. 0x00B3: { n:"BrtBeginPivotCacheDef" },
  16572. 0x00B4: { n:"BrtEndPivotCacheDef" },
  16573. 0x00B5: { n:"BrtBeginPCDFields" },
  16574. 0x00B6: { n:"BrtEndPCDFields" },
  16575. 0x00B7: { n:"BrtBeginPCDField" },
  16576. 0x00B8: { n:"BrtEndPCDField" },
  16577. 0x00B9: { n:"BrtBeginPCDSource" },
  16578. 0x00BA: { n:"BrtEndPCDSource" },
  16579. 0x00BB: { n:"BrtBeginPCDSRange" },
  16580. 0x00BC: { n:"BrtEndPCDSRange" },
  16581. 0x00BD: { n:"BrtBeginPCDFAtbl" },
  16582. 0x00BE: { n:"BrtEndPCDFAtbl" },
  16583. 0x00BF: { n:"BrtBeginPCDIRun" },
  16584. 0x00C0: { n:"BrtEndPCDIRun" },
  16585. 0x00C1: { n:"BrtBeginPivotCacheRecords" },
  16586. 0x00C2: { n:"BrtEndPivotCacheRecords" },
  16587. 0x00C3: { n:"BrtBeginPCDHierarchies" },
  16588. 0x00C4: { n:"BrtEndPCDHierarchies" },
  16589. 0x00C5: { n:"BrtBeginPCDHierarchy" },
  16590. 0x00C6: { n:"BrtEndPCDHierarchy" },
  16591. 0x00C7: { n:"BrtBeginPCDHFieldsUsage" },
  16592. 0x00C8: { n:"BrtEndPCDHFieldsUsage" },
  16593. 0x00C9: { n:"BrtBeginExtConnection" },
  16594. 0x00CA: { n:"BrtEndExtConnection" },
  16595. 0x00CB: { n:"BrtBeginECDbProps" },
  16596. 0x00CC: { n:"BrtEndECDbProps" },
  16597. 0x00CD: { n:"BrtBeginECOlapProps" },
  16598. 0x00CE: { n:"BrtEndECOlapProps" },
  16599. 0x00CF: { n:"BrtBeginPCDSConsol" },
  16600. 0x00D0: { n:"BrtEndPCDSConsol" },
  16601. 0x00D1: { n:"BrtBeginPCDSCPages" },
  16602. 0x00D2: { n:"BrtEndPCDSCPages" },
  16603. 0x00D3: { n:"BrtBeginPCDSCPage" },
  16604. 0x00D4: { n:"BrtEndPCDSCPage" },
  16605. 0x00D5: { n:"BrtBeginPCDSCPItem" },
  16606. 0x00D6: { n:"BrtEndPCDSCPItem" },
  16607. 0x00D7: { n:"BrtBeginPCDSCSets" },
  16608. 0x00D8: { n:"BrtEndPCDSCSets" },
  16609. 0x00D9: { n:"BrtBeginPCDSCSet" },
  16610. 0x00DA: { n:"BrtEndPCDSCSet" },
  16611. 0x00DB: { n:"BrtBeginPCDFGroup" },
  16612. 0x00DC: { n:"BrtEndPCDFGroup" },
  16613. 0x00DD: { n:"BrtBeginPCDFGItems" },
  16614. 0x00DE: { n:"BrtEndPCDFGItems" },
  16615. 0x00DF: { n:"BrtBeginPCDFGRange" },
  16616. 0x00E0: { n:"BrtEndPCDFGRange" },
  16617. 0x00E1: { n:"BrtBeginPCDFGDiscrete" },
  16618. 0x00E2: { n:"BrtEndPCDFGDiscrete" },
  16619. 0x00E3: { n:"BrtBeginPCDSDTupleCache" },
  16620. 0x00E4: { n:"BrtEndPCDSDTupleCache" },
  16621. 0x00E5: { n:"BrtBeginPCDSDTCEntries" },
  16622. 0x00E6: { n:"BrtEndPCDSDTCEntries" },
  16623. 0x00E7: { n:"BrtBeginPCDSDTCEMembers" },
  16624. 0x00E8: { n:"BrtEndPCDSDTCEMembers" },
  16625. 0x00E9: { n:"BrtBeginPCDSDTCEMember" },
  16626. 0x00EA: { n:"BrtEndPCDSDTCEMember" },
  16627. 0x00EB: { n:"BrtBeginPCDSDTCQueries" },
  16628. 0x00EC: { n:"BrtEndPCDSDTCQueries" },
  16629. 0x00ED: { n:"BrtBeginPCDSDTCQuery" },
  16630. 0x00EE: { n:"BrtEndPCDSDTCQuery" },
  16631. 0x00EF: { n:"BrtBeginPCDSDTCSets" },
  16632. 0x00F0: { n:"BrtEndPCDSDTCSets" },
  16633. 0x00F1: { n:"BrtBeginPCDSDTCSet" },
  16634. 0x00F2: { n:"BrtEndPCDSDTCSet" },
  16635. 0x00F3: { n:"BrtBeginPCDCalcItems" },
  16636. 0x00F4: { n:"BrtEndPCDCalcItems" },
  16637. 0x00F5: { n:"BrtBeginPCDCalcItem" },
  16638. 0x00F6: { n:"BrtEndPCDCalcItem" },
  16639. 0x00F7: { n:"BrtBeginPRule" },
  16640. 0x00F8: { n:"BrtEndPRule" },
  16641. 0x00F9: { n:"BrtBeginPRFilters" },
  16642. 0x00FA: { n:"BrtEndPRFilters" },
  16643. 0x00FB: { n:"BrtBeginPRFilter" },
  16644. 0x00FC: { n:"BrtEndPRFilter" },
  16645. 0x00FD: { n:"BrtBeginPNames" },
  16646. 0x00FE: { n:"BrtEndPNames" },
  16647. 0x00FF: { n:"BrtBeginPName" },
  16648. 0x0100: { n:"BrtEndPName" },
  16649. 0x0101: { n:"BrtBeginPNPairs" },
  16650. 0x0102: { n:"BrtEndPNPairs" },
  16651. 0x0103: { n:"BrtBeginPNPair" },
  16652. 0x0104: { n:"BrtEndPNPair" },
  16653. 0x0105: { n:"BrtBeginECWebProps" },
  16654. 0x0106: { n:"BrtEndECWebProps" },
  16655. 0x0107: { n:"BrtBeginEcWpTables" },
  16656. 0x0108: { n:"BrtEndECWPTables" },
  16657. 0x0109: { n:"BrtBeginECParams" },
  16658. 0x010A: { n:"BrtEndECParams" },
  16659. 0x010B: { n:"BrtBeginECParam" },
  16660. 0x010C: { n:"BrtEndECParam" },
  16661. 0x010D: { n:"BrtBeginPCDKPIs" },
  16662. 0x010E: { n:"BrtEndPCDKPIs" },
  16663. 0x010F: { n:"BrtBeginPCDKPI" },
  16664. 0x0110: { n:"BrtEndPCDKPI" },
  16665. 0x0111: { n:"BrtBeginDims" },
  16666. 0x0112: { n:"BrtEndDims" },
  16667. 0x0113: { n:"BrtBeginDim" },
  16668. 0x0114: { n:"BrtEndDim" },
  16669. 0x0115: { n:"BrtIndexPartEnd" },
  16670. 0x0116: { n:"BrtBeginStyleSheet" },
  16671. 0x0117: { n:"BrtEndStyleSheet" },
  16672. 0x0118: { n:"BrtBeginSXView" },
  16673. 0x0119: { n:"BrtEndSXVI" },
  16674. 0x011A: { n:"BrtBeginSXVI" },
  16675. 0x011B: { n:"BrtBeginSXVIs" },
  16676. 0x011C: { n:"BrtEndSXVIs" },
  16677. 0x011D: { n:"BrtBeginSXVD" },
  16678. 0x011E: { n:"BrtEndSXVD" },
  16679. 0x011F: { n:"BrtBeginSXVDs" },
  16680. 0x0120: { n:"BrtEndSXVDs" },
  16681. 0x0121: { n:"BrtBeginSXPI" },
  16682. 0x0122: { n:"BrtEndSXPI" },
  16683. 0x0123: { n:"BrtBeginSXPIs" },
  16684. 0x0124: { n:"BrtEndSXPIs" },
  16685. 0x0125: { n:"BrtBeginSXDI" },
  16686. 0x0126: { n:"BrtEndSXDI" },
  16687. 0x0127: { n:"BrtBeginSXDIs" },
  16688. 0x0128: { n:"BrtEndSXDIs" },
  16689. 0x0129: { n:"BrtBeginSXLI" },
  16690. 0x012A: { n:"BrtEndSXLI" },
  16691. 0x012B: { n:"BrtBeginSXLIRws" },
  16692. 0x012C: { n:"BrtEndSXLIRws" },
  16693. 0x012D: { n:"BrtBeginSXLICols" },
  16694. 0x012E: { n:"BrtEndSXLICols" },
  16695. 0x012F: { n:"BrtBeginSXFormat" },
  16696. 0x0130: { n:"BrtEndSXFormat" },
  16697. 0x0131: { n:"BrtBeginSXFormats" },
  16698. 0x0132: { n:"BrtEndSxFormats" },
  16699. 0x0133: { n:"BrtBeginSxSelect" },
  16700. 0x0134: { n:"BrtEndSxSelect" },
  16701. 0x0135: { n:"BrtBeginISXVDRws" },
  16702. 0x0136: { n:"BrtEndISXVDRws" },
  16703. 0x0137: { n:"BrtBeginISXVDCols" },
  16704. 0x0138: { n:"BrtEndISXVDCols" },
  16705. 0x0139: { n:"BrtEndSXLocation" },
  16706. 0x013A: { n:"BrtBeginSXLocation" },
  16707. 0x013B: { n:"BrtEndSXView" },
  16708. 0x013C: { n:"BrtBeginSXTHs" },
  16709. 0x013D: { n:"BrtEndSXTHs" },
  16710. 0x013E: { n:"BrtBeginSXTH" },
  16711. 0x013F: { n:"BrtEndSXTH" },
  16712. 0x0140: { n:"BrtBeginISXTHRws" },
  16713. 0x0141: { n:"BrtEndISXTHRws" },
  16714. 0x0142: { n:"BrtBeginISXTHCols" },
  16715. 0x0143: { n:"BrtEndISXTHCols" },
  16716. 0x0144: { n:"BrtBeginSXTDMPS" },
  16717. 0x0145: { n:"BrtEndSXTDMPs" },
  16718. 0x0146: { n:"BrtBeginSXTDMP" },
  16719. 0x0147: { n:"BrtEndSXTDMP" },
  16720. 0x0148: { n:"BrtBeginSXTHItems" },
  16721. 0x0149: { n:"BrtEndSXTHItems" },
  16722. 0x014A: { n:"BrtBeginSXTHItem" },
  16723. 0x014B: { n:"BrtEndSXTHItem" },
  16724. 0x014C: { n:"BrtBeginMetadata" },
  16725. 0x014D: { n:"BrtEndMetadata" },
  16726. 0x014E: { n:"BrtBeginEsmdtinfo" },
  16727. 0x014F: { n:"BrtMdtinfo" },
  16728. 0x0150: { n:"BrtEndEsmdtinfo" },
  16729. 0x0151: { n:"BrtBeginEsmdb" },
  16730. 0x0152: { n:"BrtEndEsmdb" },
  16731. 0x0153: { n:"BrtBeginEsfmd" },
  16732. 0x0154: { n:"BrtEndEsfmd" },
  16733. 0x0155: { n:"BrtBeginSingleCells" },
  16734. 0x0156: { n:"BrtEndSingleCells" },
  16735. 0x0157: { n:"BrtBeginList" },
  16736. 0x0158: { n:"BrtEndList" },
  16737. 0x0159: { n:"BrtBeginListCols" },
  16738. 0x015A: { n:"BrtEndListCols" },
  16739. 0x015B: { n:"BrtBeginListCol" },
  16740. 0x015C: { n:"BrtEndListCol" },
  16741. 0x015D: { n:"BrtBeginListXmlCPr" },
  16742. 0x015E: { n:"BrtEndListXmlCPr" },
  16743. 0x015F: { n:"BrtListCCFmla" },
  16744. 0x0160: { n:"BrtListTrFmla" },
  16745. 0x0161: { n:"BrtBeginExternals" },
  16746. 0x0162: { n:"BrtEndExternals" },
  16747. 0x0163: { n:"BrtSupBookSrc", f:parse_RelID},
  16748. 0x0165: { n:"BrtSupSelf" },
  16749. 0x0166: { n:"BrtSupSame" },
  16750. 0x0167: { n:"BrtSupTabs" },
  16751. 0x0168: { n:"BrtBeginSupBook" },
  16752. 0x0169: { n:"BrtPlaceholderName" },
  16753. 0x016A: { n:"BrtExternSheet", f:parse_ExternSheet },
  16754. 0x016B: { n:"BrtExternTableStart" },
  16755. 0x016C: { n:"BrtExternTableEnd" },
  16756. 0x016E: { n:"BrtExternRowHdr" },
  16757. 0x016F: { n:"BrtExternCellBlank" },
  16758. 0x0170: { n:"BrtExternCellReal" },
  16759. 0x0171: { n:"BrtExternCellBool" },
  16760. 0x0172: { n:"BrtExternCellError" },
  16761. 0x0173: { n:"BrtExternCellString" },
  16762. 0x0174: { n:"BrtBeginEsmdx" },
  16763. 0x0175: { n:"BrtEndEsmdx" },
  16764. 0x0176: { n:"BrtBeginMdxSet" },
  16765. 0x0177: { n:"BrtEndMdxSet" },
  16766. 0x0178: { n:"BrtBeginMdxMbrProp" },
  16767. 0x0179: { n:"BrtEndMdxMbrProp" },
  16768. 0x017A: { n:"BrtBeginMdxKPI" },
  16769. 0x017B: { n:"BrtEndMdxKPI" },
  16770. 0x017C: { n:"BrtBeginEsstr" },
  16771. 0x017D: { n:"BrtEndEsstr" },
  16772. 0x017E: { n:"BrtBeginPRFItem" },
  16773. 0x017F: { n:"BrtEndPRFItem" },
  16774. 0x0180: { n:"BrtBeginPivotCacheIDs" },
  16775. 0x0181: { n:"BrtEndPivotCacheIDs" },
  16776. 0x0182: { n:"BrtBeginPivotCacheID" },
  16777. 0x0183: { n:"BrtEndPivotCacheID" },
  16778. 0x0184: { n:"BrtBeginISXVIs" },
  16779. 0x0185: { n:"BrtEndISXVIs" },
  16780. 0x0186: { n:"BrtBeginColInfos" },
  16781. 0x0187: { n:"BrtEndColInfos" },
  16782. 0x0188: { n:"BrtBeginRwBrk" },
  16783. 0x0189: { n:"BrtEndRwBrk" },
  16784. 0x018A: { n:"BrtBeginColBrk" },
  16785. 0x018B: { n:"BrtEndColBrk" },
  16786. 0x018C: { n:"BrtBrk" },
  16787. 0x018D: { n:"BrtUserBookView" },
  16788. 0x018E: { n:"BrtInfo" },
  16789. 0x018F: { n:"BrtCUsr" },
  16790. 0x0190: { n:"BrtUsr" },
  16791. 0x0191: { n:"BrtBeginUsers" },
  16792. 0x0193: { n:"BrtEOF" },
  16793. 0x0194: { n:"BrtUCR" },
  16794. 0x0195: { n:"BrtRRInsDel" },
  16795. 0x0196: { n:"BrtRREndInsDel" },
  16796. 0x0197: { n:"BrtRRMove" },
  16797. 0x0198: { n:"BrtRREndMove" },
  16798. 0x0199: { n:"BrtRRChgCell" },
  16799. 0x019A: { n:"BrtRREndChgCell" },
  16800. 0x019B: { n:"BrtRRHeader" },
  16801. 0x019C: { n:"BrtRRUserView" },
  16802. 0x019D: { n:"BrtRRRenSheet" },
  16803. 0x019E: { n:"BrtRRInsertSh" },
  16804. 0x019F: { n:"BrtRRDefName" },
  16805. 0x01A0: { n:"BrtRRNote" },
  16806. 0x01A1: { n:"BrtRRConflict" },
  16807. 0x01A2: { n:"BrtRRTQSIF" },
  16808. 0x01A3: { n:"BrtRRFormat" },
  16809. 0x01A4: { n:"BrtRREndFormat" },
  16810. 0x01A5: { n:"BrtRRAutoFmt" },
  16811. 0x01A6: { n:"BrtBeginUserShViews" },
  16812. 0x01A7: { n:"BrtBeginUserShView" },
  16813. 0x01A8: { n:"BrtEndUserShView" },
  16814. 0x01A9: { n:"BrtEndUserShViews" },
  16815. 0x01AA: { n:"BrtArrFmla", f:parse_BrtArrFmla },
  16816. 0x01AB: { n:"BrtShrFmla", f:parse_BrtShrFmla },
  16817. 0x01AC: { n:"BrtTable" },
  16818. 0x01AD: { n:"BrtBeginExtConnections" },
  16819. 0x01AE: { n:"BrtEndExtConnections" },
  16820. 0x01AF: { n:"BrtBeginPCDCalcMems" },
  16821. 0x01B0: { n:"BrtEndPCDCalcMems" },
  16822. 0x01B1: { n:"BrtBeginPCDCalcMem" },
  16823. 0x01B2: { n:"BrtEndPCDCalcMem" },
  16824. 0x01B3: { n:"BrtBeginPCDHGLevels" },
  16825. 0x01B4: { n:"BrtEndPCDHGLevels" },
  16826. 0x01B5: { n:"BrtBeginPCDHGLevel" },
  16827. 0x01B6: { n:"BrtEndPCDHGLevel" },
  16828. 0x01B7: { n:"BrtBeginPCDHGLGroups" },
  16829. 0x01B8: { n:"BrtEndPCDHGLGroups" },
  16830. 0x01B9: { n:"BrtBeginPCDHGLGroup" },
  16831. 0x01BA: { n:"BrtEndPCDHGLGroup" },
  16832. 0x01BB: { n:"BrtBeginPCDHGLGMembers" },
  16833. 0x01BC: { n:"BrtEndPCDHGLGMembers" },
  16834. 0x01BD: { n:"BrtBeginPCDHGLGMember" },
  16835. 0x01BE: { n:"BrtEndPCDHGLGMember" },
  16836. 0x01BF: { n:"BrtBeginQSI" },
  16837. 0x01C0: { n:"BrtEndQSI" },
  16838. 0x01C1: { n:"BrtBeginQSIR" },
  16839. 0x01C2: { n:"BrtEndQSIR" },
  16840. 0x01C3: { n:"BrtBeginDeletedNames" },
  16841. 0x01C4: { n:"BrtEndDeletedNames" },
  16842. 0x01C5: { n:"BrtBeginDeletedName" },
  16843. 0x01C6: { n:"BrtEndDeletedName" },
  16844. 0x01C7: { n:"BrtBeginQSIFs" },
  16845. 0x01C8: { n:"BrtEndQSIFs" },
  16846. 0x01C9: { n:"BrtBeginQSIF" },
  16847. 0x01CA: { n:"BrtEndQSIF" },
  16848. 0x01CB: { n:"BrtBeginAutoSortScope" },
  16849. 0x01CC: { n:"BrtEndAutoSortScope" },
  16850. 0x01CD: { n:"BrtBeginConditionalFormatting" },
  16851. 0x01CE: { n:"BrtEndConditionalFormatting" },
  16852. 0x01CF: { n:"BrtBeginCFRule" },
  16853. 0x01D0: { n:"BrtEndCFRule" },
  16854. 0x01D1: { n:"BrtBeginIconSet" },
  16855. 0x01D2: { n:"BrtEndIconSet" },
  16856. 0x01D3: { n:"BrtBeginDatabar" },
  16857. 0x01D4: { n:"BrtEndDatabar" },
  16858. 0x01D5: { n:"BrtBeginColorScale" },
  16859. 0x01D6: { n:"BrtEndColorScale" },
  16860. 0x01D7: { n:"BrtCFVO" },
  16861. 0x01D8: { n:"BrtExternValueMeta" },
  16862. 0x01D9: { n:"BrtBeginColorPalette" },
  16863. 0x01DA: { n:"BrtEndColorPalette" },
  16864. 0x01DB: { n:"BrtIndexedColor" },
  16865. 0x01DC: { n:"BrtMargins", f:parse_BrtMargins },
  16866. 0x01DD: { n:"BrtPrintOptions" },
  16867. 0x01DE: { n:"BrtPageSetup" },
  16868. 0x01DF: { n:"BrtBeginHeaderFooter" },
  16869. 0x01E0: { n:"BrtEndHeaderFooter" },
  16870. 0x01E1: { n:"BrtBeginSXCrtFormat" },
  16871. 0x01E2: { n:"BrtEndSXCrtFormat" },
  16872. 0x01E3: { n:"BrtBeginSXCrtFormats" },
  16873. 0x01E4: { n:"BrtEndSXCrtFormats" },
  16874. 0x01E5: { n:"BrtWsFmtInfo", f:parse_BrtWsFmtInfo },
  16875. 0x01E6: { n:"BrtBeginMgs" },
  16876. 0x01E7: { n:"BrtEndMGs" },
  16877. 0x01E8: { n:"BrtBeginMGMaps" },
  16878. 0x01E9: { n:"BrtEndMGMaps" },
  16879. 0x01EA: { n:"BrtBeginMG" },
  16880. 0x01EB: { n:"BrtEndMG" },
  16881. 0x01EC: { n:"BrtBeginMap" },
  16882. 0x01ED: { n:"BrtEndMap" },
  16883. 0x01EE: { n:"BrtHLink", f:parse_BrtHLink },
  16884. 0x01EF: { n:"BrtBeginDCon" },
  16885. 0x01F0: { n:"BrtEndDCon" },
  16886. 0x01F1: { n:"BrtBeginDRefs" },
  16887. 0x01F2: { n:"BrtEndDRefs" },
  16888. 0x01F3: { n:"BrtDRef" },
  16889. 0x01F4: { n:"BrtBeginScenMan" },
  16890. 0x01F5: { n:"BrtEndScenMan" },
  16891. 0x01F6: { n:"BrtBeginSct" },
  16892. 0x01F7: { n:"BrtEndSct" },
  16893. 0x01F8: { n:"BrtSlc" },
  16894. 0x01F9: { n:"BrtBeginDXFs" },
  16895. 0x01FA: { n:"BrtEndDXFs" },
  16896. 0x01FB: { n:"BrtDXF" },
  16897. 0x01FC: { n:"BrtBeginTableStyles" },
  16898. 0x01FD: { n:"BrtEndTableStyles" },
  16899. 0x01FE: { n:"BrtBeginTableStyle" },
  16900. 0x01FF: { n:"BrtEndTableStyle" },
  16901. 0x0200: { n:"BrtTableStyleElement" },
  16902. 0x0201: { n:"BrtTableStyleClient" },
  16903. 0x0202: { n:"BrtBeginVolDeps" },
  16904. 0x0203: { n:"BrtEndVolDeps" },
  16905. 0x0204: { n:"BrtBeginVolType" },
  16906. 0x0205: { n:"BrtEndVolType" },
  16907. 0x0206: { n:"BrtBeginVolMain" },
  16908. 0x0207: { n:"BrtEndVolMain" },
  16909. 0x0208: { n:"BrtBeginVolTopic" },
  16910. 0x0209: { n:"BrtEndVolTopic" },
  16911. 0x020A: { n:"BrtVolSubtopic" },
  16912. 0x020B: { n:"BrtVolRef" },
  16913. 0x020C: { n:"BrtVolNum" },
  16914. 0x020D: { n:"BrtVolErr" },
  16915. 0x020E: { n:"BrtVolStr" },
  16916. 0x020F: { n:"BrtVolBool" },
  16917. 0x0210: { n:"BrtBeginCalcChain$" },
  16918. 0x0211: { n:"BrtEndCalcChain$" },
  16919. 0x0212: { n:"BrtBeginSortState" },
  16920. 0x0213: { n:"BrtEndSortState" },
  16921. 0x0214: { n:"BrtBeginSortCond" },
  16922. 0x0215: { n:"BrtEndSortCond" },
  16923. 0x0216: { n:"BrtBookProtection" },
  16924. 0x0217: { n:"BrtSheetProtection" },
  16925. 0x0218: { n:"BrtRangeProtection" },
  16926. 0x0219: { n:"BrtPhoneticInfo" },
  16927. 0x021A: { n:"BrtBeginECTxtWiz" },
  16928. 0x021B: { n:"BrtEndECTxtWiz" },
  16929. 0x021C: { n:"BrtBeginECTWFldInfoLst" },
  16930. 0x021D: { n:"BrtEndECTWFldInfoLst" },
  16931. 0x021E: { n:"BrtBeginECTwFldInfo" },
  16932. 0x0224: { n:"BrtFileSharing" },
  16933. 0x0225: { n:"BrtOleSize" },
  16934. 0x0226: { n:"BrtDrawing", f:parse_RelID },
  16935. 0x0227: { n:"BrtLegacyDrawing" },
  16936. 0x0228: { n:"BrtLegacyDrawingHF" },
  16937. 0x0229: { n:"BrtWebOpt" },
  16938. 0x022A: { n:"BrtBeginWebPubItems" },
  16939. 0x022B: { n:"BrtEndWebPubItems" },
  16940. 0x022C: { n:"BrtBeginWebPubItem" },
  16941. 0x022D: { n:"BrtEndWebPubItem" },
  16942. 0x022E: { n:"BrtBeginSXCondFmt" },
  16943. 0x022F: { n:"BrtEndSXCondFmt" },
  16944. 0x0230: { n:"BrtBeginSXCondFmts" },
  16945. 0x0231: { n:"BrtEndSXCondFmts" },
  16946. 0x0232: { n:"BrtBkHim" },
  16947. 0x0234: { n:"BrtColor" },
  16948. 0x0235: { n:"BrtBeginIndexedColors" },
  16949. 0x0236: { n:"BrtEndIndexedColors" },
  16950. 0x0239: { n:"BrtBeginMRUColors" },
  16951. 0x023A: { n:"BrtEndMRUColors" },
  16952. 0x023C: { n:"BrtMRUColor" },
  16953. 0x023D: { n:"BrtBeginDVals" },
  16954. 0x023E: { n:"BrtEndDVals" },
  16955. 0x0241: { n:"BrtSupNameStart" },
  16956. 0x0242: { n:"BrtSupNameValueStart" },
  16957. 0x0243: { n:"BrtSupNameValueEnd" },
  16958. 0x0244: { n:"BrtSupNameNum" },
  16959. 0x0245: { n:"BrtSupNameErr" },
  16960. 0x0246: { n:"BrtSupNameSt" },
  16961. 0x0247: { n:"BrtSupNameNil" },
  16962. 0x0248: { n:"BrtSupNameBool" },
  16963. 0x0249: { n:"BrtSupNameFmla" },
  16964. 0x024A: { n:"BrtSupNameBits" },
  16965. 0x024B: { n:"BrtSupNameEnd" },
  16966. 0x024C: { n:"BrtEndSupBook" },
  16967. 0x024D: { n:"BrtCellSmartTagProperty" },
  16968. 0x024E: { n:"BrtBeginCellSmartTag" },
  16969. 0x024F: { n:"BrtEndCellSmartTag" },
  16970. 0x0250: { n:"BrtBeginCellSmartTags" },
  16971. 0x0251: { n:"BrtEndCellSmartTags" },
  16972. 0x0252: { n:"BrtBeginSmartTags" },
  16973. 0x0253: { n:"BrtEndSmartTags" },
  16974. 0x0254: { n:"BrtSmartTagType" },
  16975. 0x0255: { n:"BrtBeginSmartTagTypes" },
  16976. 0x0256: { n:"BrtEndSmartTagTypes" },
  16977. 0x0257: { n:"BrtBeginSXFilters" },
  16978. 0x0258: { n:"BrtEndSXFilters" },
  16979. 0x0259: { n:"BrtBeginSXFILTER" },
  16980. 0x025A: { n:"BrtEndSXFilter" },
  16981. 0x025B: { n:"BrtBeginFills" },
  16982. 0x025C: { n:"BrtEndFills" },
  16983. 0x025D: { n:"BrtBeginCellWatches" },
  16984. 0x025E: { n:"BrtEndCellWatches" },
  16985. 0x025F: { n:"BrtCellWatch" },
  16986. 0x0260: { n:"BrtBeginCRErrs" },
  16987. 0x0261: { n:"BrtEndCRErrs" },
  16988. 0x0262: { n:"BrtCrashRecErr" },
  16989. 0x0263: { n:"BrtBeginFonts" },
  16990. 0x0264: { n:"BrtEndFonts" },
  16991. 0x0265: { n:"BrtBeginBorders" },
  16992. 0x0266: { n:"BrtEndBorders" },
  16993. 0x0267: { n:"BrtBeginFmts" },
  16994. 0x0268: { n:"BrtEndFmts" },
  16995. 0x0269: { n:"BrtBeginCellXFs" },
  16996. 0x026A: { n:"BrtEndCellXFs" },
  16997. 0x026B: { n:"BrtBeginStyles" },
  16998. 0x026C: { n:"BrtEndStyles" },
  16999. 0x0271: { n:"BrtBigName" },
  17000. 0x0272: { n:"BrtBeginCellStyleXFs" },
  17001. 0x0273: { n:"BrtEndCellStyleXFs" },
  17002. 0x0274: { n:"BrtBeginComments" },
  17003. 0x0275: { n:"BrtEndComments" },
  17004. 0x0276: { n:"BrtBeginCommentAuthors" },
  17005. 0x0277: { n:"BrtEndCommentAuthors" },
  17006. 0x0278: { n:"BrtCommentAuthor", f:parse_BrtCommentAuthor },
  17007. 0x0279: { n:"BrtBeginCommentList" },
  17008. 0x027A: { n:"BrtEndCommentList" },
  17009. 0x027B: { n:"BrtBeginComment", f:parse_BrtBeginComment},
  17010. 0x027C: { n:"BrtEndComment" },
  17011. 0x027D: { n:"BrtCommentText", f:parse_BrtCommentText },
  17012. 0x027E: { n:"BrtBeginOleObjects" },
  17013. 0x027F: { n:"BrtOleObject" },
  17014. 0x0280: { n:"BrtEndOleObjects" },
  17015. 0x0281: { n:"BrtBeginSxrules" },
  17016. 0x0282: { n:"BrtEndSxRules" },
  17017. 0x0283: { n:"BrtBeginActiveXControls" },
  17018. 0x0284: { n:"BrtActiveX" },
  17019. 0x0285: { n:"BrtEndActiveXControls" },
  17020. 0x0286: { n:"BrtBeginPCDSDTCEMembersSortBy" },
  17021. 0x0288: { n:"BrtBeginCellIgnoreECs" },
  17022. 0x0289: { n:"BrtCellIgnoreEC" },
  17023. 0x028A: { n:"BrtEndCellIgnoreECs" },
  17024. 0x028B: { n:"BrtCsProp", f:parse_BrtCsProp },
  17025. 0x028C: { n:"BrtCsPageSetup" },
  17026. 0x028D: { n:"BrtBeginUserCsViews" },
  17027. 0x028E: { n:"BrtEndUserCsViews" },
  17028. 0x028F: { n:"BrtBeginUserCsView" },
  17029. 0x0290: { n:"BrtEndUserCsView" },
  17030. 0x0291: { n:"BrtBeginPcdSFCIEntries" },
  17031. 0x0292: { n:"BrtEndPCDSFCIEntries" },
  17032. 0x0293: { n:"BrtPCDSFCIEntry" },
  17033. 0x0294: { n:"BrtBeginListParts" },
  17034. 0x0295: { n:"BrtListPart" },
  17035. 0x0296: { n:"BrtEndListParts" },
  17036. 0x0297: { n:"BrtSheetCalcProp" },
  17037. 0x0298: { n:"BrtBeginFnGroup" },
  17038. 0x0299: { n:"BrtFnGroup" },
  17039. 0x029A: { n:"BrtEndFnGroup" },
  17040. 0x029B: { n:"BrtSupAddin" },
  17041. 0x029C: { n:"BrtSXTDMPOrder" },
  17042. 0x029D: { n:"BrtCsProtection" },
  17043. 0x029F: { n:"BrtBeginWsSortMap" },
  17044. 0x02A0: { n:"BrtEndWsSortMap" },
  17045. 0x02A1: { n:"BrtBeginRRSort" },
  17046. 0x02A2: { n:"BrtEndRRSort" },
  17047. 0x02A3: { n:"BrtRRSortItem" },
  17048. 0x02A4: { n:"BrtFileSharingIso" },
  17049. 0x02A5: { n:"BrtBookProtectionIso" },
  17050. 0x02A6: { n:"BrtSheetProtectionIso" },
  17051. 0x02A7: { n:"BrtCsProtectionIso" },
  17052. 0x02A8: { n:"BrtRangeProtectionIso" },
  17053. 0x02A9: { n:"BrtDValList" },
  17054. 0x0400: { n:"BrtRwDescent" },
  17055. 0x0401: { n:"BrtKnownFonts" },
  17056. 0x0402: { n:"BrtBeginSXTupleSet" },
  17057. 0x0403: { n:"BrtEndSXTupleSet" },
  17058. 0x0404: { n:"BrtBeginSXTupleSetHeader" },
  17059. 0x0405: { n:"BrtEndSXTupleSetHeader" },
  17060. 0x0406: { n:"BrtSXTupleSetHeaderItem" },
  17061. 0x0407: { n:"BrtBeginSXTupleSetData" },
  17062. 0x0408: { n:"BrtEndSXTupleSetData" },
  17063. 0x0409: { n:"BrtBeginSXTupleSetRow" },
  17064. 0x040A: { n:"BrtEndSXTupleSetRow" },
  17065. 0x040B: { n:"BrtSXTupleSetRowItem" },
  17066. 0x040C: { n:"BrtNameExt" },
  17067. 0x040D: { n:"BrtPCDH14" },
  17068. 0x040E: { n:"BrtBeginPCDCalcMem14" },
  17069. 0x040F: { n:"BrtEndPCDCalcMem14" },
  17070. 0x0410: { n:"BrtSXTH14" },
  17071. 0x0411: { n:"BrtBeginSparklineGroup" },
  17072. 0x0412: { n:"BrtEndSparklineGroup" },
  17073. 0x0413: { n:"BrtSparkline" },
  17074. 0x0414: { n:"BrtSXDI14" },
  17075. 0x0415: { n:"BrtWsFmtInfoEx14" },
  17076. 0x0416: { n:"BrtBeginConditionalFormatting14" },
  17077. 0x0417: { n:"BrtEndConditionalFormatting14" },
  17078. 0x0418: { n:"BrtBeginCFRule14" },
  17079. 0x0419: { n:"BrtEndCFRule14" },
  17080. 0x041A: { n:"BrtCFVO14" },
  17081. 0x041B: { n:"BrtBeginDatabar14" },
  17082. 0x041C: { n:"BrtBeginIconSet14" },
  17083. 0x041D: { n:"BrtDVal14", f: parse_BrtDVal14 },
  17084. 0x041E: { n:"BrtBeginDVals14" },
  17085. 0x041F: { n:"BrtColor14" },
  17086. 0x0420: { n:"BrtBeginSparklines" },
  17087. 0x0421: { n:"BrtEndSparklines" },
  17088. 0x0422: { n:"BrtBeginSparklineGroups" },
  17089. 0x0423: { n:"BrtEndSparklineGroups" },
  17090. 0x0425: { n:"BrtSXVD14" },
  17091. 0x0426: { n:"BrtBeginSXView14" },
  17092. 0x0427: { n:"BrtEndSXView14" },
  17093. 0x0428: { n:"BrtBeginSXView16" },
  17094. 0x0429: { n:"BrtEndSXView16" },
  17095. 0x042A: { n:"BrtBeginPCD14" },
  17096. 0x042B: { n:"BrtEndPCD14" },
  17097. 0x042C: { n:"BrtBeginExtConn14" },
  17098. 0x042D: { n:"BrtEndExtConn14" },
  17099. 0x042E: { n:"BrtBeginSlicerCacheIDs" },
  17100. 0x042F: { n:"BrtEndSlicerCacheIDs" },
  17101. 0x0430: { n:"BrtBeginSlicerCacheID" },
  17102. 0x0431: { n:"BrtEndSlicerCacheID" },
  17103. 0x0433: { n:"BrtBeginSlicerCache" },
  17104. 0x0434: { n:"BrtEndSlicerCache" },
  17105. 0x0435: { n:"BrtBeginSlicerCacheDef" },
  17106. 0x0436: { n:"BrtEndSlicerCacheDef" },
  17107. 0x0437: { n:"BrtBeginSlicersEx" },
  17108. 0x0438: { n:"BrtEndSlicersEx" },
  17109. 0x0439: { n:"BrtBeginSlicerEx" },
  17110. 0x043A: { n:"BrtEndSlicerEx" },
  17111. 0x043B: { n:"BrtBeginSlicer" },
  17112. 0x043C: { n:"BrtEndSlicer" },
  17113. 0x043D: { n:"BrtSlicerCachePivotTables" },
  17114. 0x043E: { n:"BrtBeginSlicerCacheOlapImpl" },
  17115. 0x043F: { n:"BrtEndSlicerCacheOlapImpl" },
  17116. 0x0440: { n:"BrtBeginSlicerCacheLevelsData" },
  17117. 0x0441: { n:"BrtEndSlicerCacheLevelsData" },
  17118. 0x0442: { n:"BrtBeginSlicerCacheLevelData" },
  17119. 0x0443: { n:"BrtEndSlicerCacheLevelData" },
  17120. 0x0444: { n:"BrtBeginSlicerCacheSiRanges" },
  17121. 0x0445: { n:"BrtEndSlicerCacheSiRanges" },
  17122. 0x0446: { n:"BrtBeginSlicerCacheSiRange" },
  17123. 0x0447: { n:"BrtEndSlicerCacheSiRange" },
  17124. 0x0448: { n:"BrtSlicerCacheOlapItem" },
  17125. 0x0449: { n:"BrtBeginSlicerCacheSelections" },
  17126. 0x044A: { n:"BrtSlicerCacheSelection" },
  17127. 0x044B: { n:"BrtEndSlicerCacheSelections" },
  17128. 0x044C: { n:"BrtBeginSlicerCacheNative" },
  17129. 0x044D: { n:"BrtEndSlicerCacheNative" },
  17130. 0x044E: { n:"BrtSlicerCacheNativeItem" },
  17131. 0x044F: { n:"BrtRangeProtection14" },
  17132. 0x0450: { n:"BrtRangeProtectionIso14" },
  17133. 0x0451: { n:"BrtCellIgnoreEC14" },
  17134. 0x0457: { n:"BrtList14" },
  17135. 0x0458: { n:"BrtCFIcon" },
  17136. 0x0459: { n:"BrtBeginSlicerCachesPivotCacheIDs" },
  17137. 0x045A: { n:"BrtEndSlicerCachesPivotCacheIDs" },
  17138. 0x045B: { n:"BrtBeginSlicers" },
  17139. 0x045C: { n:"BrtEndSlicers" },
  17140. 0x045D: { n:"BrtWbProp14" },
  17141. 0x045E: { n:"BrtBeginSXEdit" },
  17142. 0x045F: { n:"BrtEndSXEdit" },
  17143. 0x0460: { n:"BrtBeginSXEdits" },
  17144. 0x0461: { n:"BrtEndSXEdits" },
  17145. 0x0462: { n:"BrtBeginSXChange" },
  17146. 0x0463: { n:"BrtEndSXChange" },
  17147. 0x0464: { n:"BrtBeginSXChanges" },
  17148. 0x0465: { n:"BrtEndSXChanges" },
  17149. 0x0466: { n:"BrtSXTupleItems" },
  17150. 0x0468: { n:"BrtBeginSlicerStyle" },
  17151. 0x0469: { n:"BrtEndSlicerStyle" },
  17152. 0x046A: { n:"BrtSlicerStyleElement" },
  17153. 0x046B: { n:"BrtBeginStyleSheetExt14" },
  17154. 0x046C: { n:"BrtEndStyleSheetExt14" },
  17155. 0x046D: { n:"BrtBeginSlicerCachesPivotCacheID" },
  17156. 0x046E: { n:"BrtEndSlicerCachesPivotCacheID" },
  17157. 0x046F: { n:"BrtBeginConditionalFormattings" },
  17158. 0x0470: { n:"BrtEndConditionalFormattings" },
  17159. 0x0471: { n:"BrtBeginPCDCalcMemExt" },
  17160. 0x0472: { n:"BrtEndPCDCalcMemExt" },
  17161. 0x0473: { n:"BrtBeginPCDCalcMemsExt" },
  17162. 0x0474: { n:"BrtEndPCDCalcMemsExt" },
  17163. 0x0475: { n:"BrtPCDField14" },
  17164. 0x0476: { n:"BrtBeginSlicerStyles" },
  17165. 0x0477: { n:"BrtEndSlicerStyles" },
  17166. 0x0478: { n:"BrtBeginSlicerStyleElements" },
  17167. 0x0479: { n:"BrtEndSlicerStyleElements" },
  17168. 0x047A: { n:"BrtCFRuleExt" },
  17169. 0x047B: { n:"BrtBeginSXCondFmt14" },
  17170. 0x047C: { n:"BrtEndSXCondFmt14" },
  17171. 0x047D: { n:"BrtBeginSXCondFmts14" },
  17172. 0x047E: { n:"BrtEndSXCondFmts14" },
  17173. 0x0480: { n:"BrtBeginSortCond14" },
  17174. 0x0481: { n:"BrtEndSortCond14" },
  17175. 0x0482: { n:"BrtEndDVals14" },
  17176. 0x0483: { n:"BrtEndIconSet14" },
  17177. 0x0484: { n:"BrtEndDatabar14" },
  17178. 0x0485: { n:"BrtBeginColorScale14" },
  17179. 0x0486: { n:"BrtEndColorScale14" },
  17180. 0x0487: { n:"BrtBeginSxrules14" },
  17181. 0x0488: { n:"BrtEndSxrules14" },
  17182. 0x0489: { n:"BrtBeginPRule14" },
  17183. 0x048A: { n:"BrtEndPRule14" },
  17184. 0x048B: { n:"BrtBeginPRFilters14" },
  17185. 0x048C: { n:"BrtEndPRFilters14" },
  17186. 0x048D: { n:"BrtBeginPRFilter14" },
  17187. 0x048E: { n:"BrtEndPRFilter14" },
  17188. 0x048F: { n:"BrtBeginPRFItem14" },
  17189. 0x0490: { n:"BrtEndPRFItem14" },
  17190. 0x0491: { n:"BrtBeginCellIgnoreECs14" },
  17191. 0x0492: { n:"BrtEndCellIgnoreECs14" },
  17192. 0x0493: { n:"BrtDxf14" },
  17193. 0x0494: { n:"BrtBeginDxF14s" },
  17194. 0x0495: { n:"BrtEndDxf14s" },
  17195. 0x0499: { n:"BrtFilter14" },
  17196. 0x049A: { n:"BrtBeginCustomFilters14" },
  17197. 0x049C: { n:"BrtCustomFilter14" },
  17198. 0x049D: { n:"BrtIconFilter14" },
  17199. 0x049E: { n:"BrtPivotCacheConnectionName" },
  17200. 0x0800: { n:"BrtBeginDecoupledPivotCacheIDs" },
  17201. 0x0801: { n:"BrtEndDecoupledPivotCacheIDs" },
  17202. 0x0802: { n:"BrtDecoupledPivotCacheID" },
  17203. 0x0803: { n:"BrtBeginPivotTableRefs" },
  17204. 0x0804: { n:"BrtEndPivotTableRefs" },
  17205. 0x0805: { n:"BrtPivotTableRef" },
  17206. 0x0806: { n:"BrtSlicerCacheBookPivotTables" },
  17207. 0x0807: { n:"BrtBeginSxvcells" },
  17208. 0x0808: { n:"BrtEndSxvcells" },
  17209. 0x0809: { n:"BrtBeginSxRow" },
  17210. 0x080A: { n:"BrtEndSxRow" },
  17211. 0x080C: { n:"BrtPcdCalcMem15" },
  17212. 0x0813: { n:"BrtQsi15" },
  17213. 0x0814: { n:"BrtBeginWebExtensions" },
  17214. 0x0815: { n:"BrtEndWebExtensions" },
  17215. 0x0816: { n:"BrtWebExtension" },
  17216. 0x0817: { n:"BrtAbsPath15" },
  17217. 0x0818: { n:"BrtBeginPivotTableUISettings" },
  17218. 0x0819: { n:"BrtEndPivotTableUISettings" },
  17219. 0x081B: { n:"BrtTableSlicerCacheIDs" },
  17220. 0x081C: { n:"BrtTableSlicerCacheID" },
  17221. 0x081D: { n:"BrtBeginTableSlicerCache" },
  17222. 0x081E: { n:"BrtEndTableSlicerCache" },
  17223. 0x081F: { n:"BrtSxFilter15" },
  17224. 0x0820: { n:"BrtBeginTimelineCachePivotCacheIDs" },
  17225. 0x0821: { n:"BrtEndTimelineCachePivotCacheIDs" },
  17226. 0x0822: { n:"BrtTimelineCachePivotCacheID" },
  17227. 0x0823: { n:"BrtBeginTimelineCacheIDs" },
  17228. 0x0824: { n:"BrtEndTimelineCacheIDs" },
  17229. 0x0825: { n:"BrtBeginTimelineCacheID" },
  17230. 0x0826: { n:"BrtEndTimelineCacheID" },
  17231. 0x0827: { n:"BrtBeginTimelinesEx" },
  17232. 0x0828: { n:"BrtEndTimelinesEx" },
  17233. 0x0829: { n:"BrtBeginTimelineEx" },
  17234. 0x082A: { n:"BrtEndTimelineEx" },
  17235. 0x082B: { n:"BrtWorkBookPr15" },
  17236. 0x082C: { n:"BrtPCDH15" },
  17237. 0x082D: { n:"BrtBeginTimelineStyle" },
  17238. 0x082E: { n:"BrtEndTimelineStyle" },
  17239. 0x082F: { n:"BrtTimelineStyleElement" },
  17240. 0x0830: { n:"BrtBeginTimelineStylesheetExt15" },
  17241. 0x0831: { n:"BrtEndTimelineStylesheetExt15" },
  17242. 0x0832: { n:"BrtBeginTimelineStyles" },
  17243. 0x0833: { n:"BrtEndTimelineStyles" },
  17244. 0x0834: { n:"BrtBeginTimelineStyleElements" },
  17245. 0x0835: { n:"BrtEndTimelineStyleElements" },
  17246. 0x0836: { n:"BrtDxf15" },
  17247. 0x0837: { n:"BrtBeginDxfs15" },
  17248. 0x0838: { n:"brtEndDxfs15" },
  17249. 0x0839: { n:"BrtSlicerCacheHideItemsWithNoData" },
  17250. 0x083A: { n:"BrtBeginItemUniqueNames" },
  17251. 0x083B: { n:"BrtEndItemUniqueNames" },
  17252. 0x083C: { n:"BrtItemUniqueName" },
  17253. 0x083D: { n:"BrtBeginExtConn15" },
  17254. 0x083E: { n:"BrtEndExtConn15" },
  17255. 0x083F: { n:"BrtBeginOledbPr15" },
  17256. 0x0840: { n:"BrtEndOledbPr15" },
  17257. 0x0841: { n:"BrtBeginDataFeedPr15" },
  17258. 0x0842: { n:"BrtEndDataFeedPr15" },
  17259. 0x0843: { n:"BrtTextPr15" },
  17260. 0x0844: { n:"BrtRangePr15" },
  17261. 0x0845: { n:"BrtDbCommand15" },
  17262. 0x0846: { n:"BrtBeginDbTables15" },
  17263. 0x0847: { n:"BrtEndDbTables15" },
  17264. 0x0848: { n:"BrtDbTable15" },
  17265. 0x0849: { n:"BrtBeginDataModel" },
  17266. 0x084A: { n:"BrtEndDataModel" },
  17267. 0x084B: { n:"BrtBeginModelTables" },
  17268. 0x084C: { n:"BrtEndModelTables" },
  17269. 0x084D: { n:"BrtModelTable" },
  17270. 0x084E: { n:"BrtBeginModelRelationships" },
  17271. 0x084F: { n:"BrtEndModelRelationships" },
  17272. 0x0850: { n:"BrtModelRelationship" },
  17273. 0x0851: { n:"BrtBeginECTxtWiz15" },
  17274. 0x0852: { n:"BrtEndECTxtWiz15" },
  17275. 0x0853: { n:"BrtBeginECTWFldInfoLst15" },
  17276. 0x0854: { n:"BrtEndECTWFldInfoLst15" },
  17277. 0x0855: { n:"BrtBeginECTWFldInfo15" },
  17278. 0x0856: { n:"BrtFieldListActiveItem" },
  17279. 0x0857: { n:"BrtPivotCacheIdVersion" },
  17280. 0x0858: { n:"BrtSXDI15" },
  17281. 0x0859: { n:"BrtBeginModelTimeGroupings" },
  17282. 0x085A: { n:"BrtEndModelTimeGroupings" },
  17283. 0x085B: { n:"BrtBeginModelTimeGrouping" },
  17284. 0x085C: { n:"BrtEndModelTimeGrouping" },
  17285. 0x085D: { n:"BrtModelTimeGroupingCalcCol" },
  17286. 0x0C00: { n:"BrtUid" },
  17287. 0x0C01: { n:"BrtRevisionPtr" },
  17288. 0x13e7: { n:"BrtBeginCalcFeatures" },
  17289. 0x13e8: { n:"BrtEndCalcFeatures" },
  17290. 0x13e9: { n:"BrtCalcFeature" },
  17291. 0xFFFF: { n:"" }
  17292. };
  17293. var XLSBRE = evert_key(XLSBRecordEnum, 'n');
  17294. /* [MS-XLS] 2.3 Record Enumeration */
  17295. var XLSRecordEnum = {
  17296. 0x0003: { n:"BIFF2NUM", f:parse_BIFF2NUM },
  17297. 0x0004: { n:"BIFF2STR", f:parse_BIFF2STR },
  17298. 0x0006: { n:"Formula", f:parse_Formula },
  17299. 0x0009: { n:'BOF', f:parse_BOF },
  17300. 0x000a: { n:'EOF', f:parsenoop2 },
  17301. 0x000c: { n:"CalcCount", f:parseuint16 },
  17302. 0x000d: { n:"CalcMode", f:parseuint16 },
  17303. 0x000e: { n:"CalcPrecision", f:parsebool },
  17304. 0x000f: { n:"CalcRefMode", f:parsebool },
  17305. 0x0010: { n:"CalcDelta", f:parse_Xnum },
  17306. 0x0011: { n:"CalcIter", f:parsebool },
  17307. 0x0012: { n:"Protect", f:parsebool },
  17308. 0x0013: { n:"Password", f:parseuint16 },
  17309. 0x0014: { n:"Header", f:parse_XLHeaderFooter },
  17310. 0x0015: { n:"Footer", f:parse_XLHeaderFooter },
  17311. 0x0017: { n:"ExternSheet", f:parse_ExternSheet },
  17312. 0x0018: { n:"Lbl", f:parse_Lbl },
  17313. 0x0019: { n:"WinProtect", f:parsebool },
  17314. 0x001a: { n:"VerticalPageBreaks" },
  17315. 0x001b: { n:"HorizontalPageBreaks" },
  17316. 0x001c: { n:"Note", f:parse_Note },
  17317. 0x001d: { n:"Selection" },
  17318. 0x0022: { n:"Date1904", f:parsebool },
  17319. 0x0023: { n:"ExternName", f:parse_ExternName },
  17320. 0x0024: { n:"COLWIDTH" },
  17321. 0x0026: { n:"LeftMargin", f:parse_Xnum },
  17322. 0x0027: { n:"RightMargin", f:parse_Xnum },
  17323. 0x0028: { n:"TopMargin", f:parse_Xnum },
  17324. 0x0029: { n:"BottomMargin", f:parse_Xnum },
  17325. 0x002a: { n:"PrintRowCol", f:parsebool },
  17326. 0x002b: { n:"PrintGrid", f:parsebool },
  17327. 0x002f: { n:"FilePass", f:parse_FilePass },
  17328. 0x0031: { n:"Font", f:parse_Font },
  17329. 0x0033: { n:"PrintSize", f:parseuint16 },
  17330. 0x003c: { n:"Continue" },
  17331. 0x003d: { n:"Window1", f:parse_Window1 },
  17332. 0x0040: { n:"Backup", f:parsebool },
  17333. 0x0041: { n:"Pane", f:parse_Pane },
  17334. 0x0042: { n:'CodePage', f:parseuint16 },
  17335. 0x004d: { n:"Pls" },
  17336. 0x0050: { n:"DCon" },
  17337. 0x0051: { n:"DConRef" },
  17338. 0x0052: { n:"DConName" },
  17339. 0x0055: { n:"DefColWidth", f:parseuint16 },
  17340. 0x0059: { n:"XCT" },
  17341. 0x005a: { n:"CRN" },
  17342. 0x005b: { n:"FileSharing" },
  17343. 0x005c: { n:'WriteAccess', f:parse_WriteAccess },
  17344. 0x005d: { n:"Obj", f:parse_Obj },
  17345. 0x005e: { n:"Uncalced" },
  17346. 0x005f: { n:"CalcSaveRecalc", f:parsebool },
  17347. 0x0060: { n:"Template" },
  17348. 0x0061: { n:"Intl" },
  17349. 0x0063: { n:"ObjProtect", f:parsebool },
  17350. 0x007d: { n:"ColInfo", f:parse_ColInfo },
  17351. 0x0080: { n:"Guts", f:parse_Guts },
  17352. 0x0081: { n:"WsBool", f:parse_WsBool },
  17353. 0x0082: { n:"GridSet", f:parseuint16 },
  17354. 0x0083: { n:"HCenter", f:parsebool },
  17355. 0x0084: { n:"VCenter", f:parsebool },
  17356. 0x0085: { n:'BoundSheet8', f:parse_BoundSheet8 },
  17357. 0x0086: { n:"WriteProtect" },
  17358. 0x008c: { n:"Country", f:parse_Country },
  17359. 0x008d: { n:"HideObj", f:parseuint16 },
  17360. 0x0090: { n:"Sort" },
  17361. 0x0092: { n:"Palette", f:parse_Palette },
  17362. 0x0097: { n:"Sync" },
  17363. 0x0098: { n:"LPr" },
  17364. 0x0099: { n:"DxGCol" },
  17365. 0x009a: { n:"FnGroupName" },
  17366. 0x009b: { n:"FilterMode" },
  17367. 0x009c: { n:"BuiltInFnGroupCount", f:parseuint16 },
  17368. 0x009d: { n:"AutoFilterInfo" },
  17369. 0x009e: { n:"AutoFilter" },
  17370. 0x00a0: { n:"Scl", f:parse_Scl },
  17371. 0x00a1: { n:"Setup", f:parse_Setup },
  17372. 0x00ae: { n:"ScenMan" },
  17373. 0x00af: { n:"SCENARIO" },
  17374. 0x00b0: { n:"SxView" },
  17375. 0x00b1: { n:"Sxvd" },
  17376. 0x00b2: { n:"SXVI" },
  17377. 0x00b4: { n:"SxIvd" },
  17378. 0x00b5: { n:"SXLI" },
  17379. 0x00b6: { n:"SXPI" },
  17380. 0x00b8: { n:"DocRoute" },
  17381. 0x00b9: { n:"RecipName" },
  17382. 0x00bd: { n:"MulRk", f:parse_MulRk },
  17383. 0x00be: { n:"MulBlank", f:parse_MulBlank },
  17384. 0x00c1: { n:'Mms', f:parsenoop2 },
  17385. 0x00c5: { n:"SXDI" },
  17386. 0x00c6: { n:"SXDB" },
  17387. 0x00c7: { n:"SXFDB" },
  17388. 0x00c8: { n:"SXDBB" },
  17389. 0x00c9: { n:"SXNum" },
  17390. 0x00ca: { n:"SxBool", f:parsebool },
  17391. 0x00cb: { n:"SxErr" },
  17392. 0x00cc: { n:"SXInt" },
  17393. 0x00cd: { n:"SXString" },
  17394. 0x00ce: { n:"SXDtr" },
  17395. 0x00cf: { n:"SxNil" },
  17396. 0x00d0: { n:"SXTbl" },
  17397. 0x00d1: { n:"SXTBRGIITM" },
  17398. 0x00d2: { n:"SxTbpg" },
  17399. 0x00d3: { n:"ObProj" },
  17400. 0x00d5: { n:"SXStreamID" },
  17401. 0x00d7: { n:"DBCell" },
  17402. 0x00d8: { n:"SXRng" },
  17403. 0x00d9: { n:"SxIsxoper" },
  17404. 0x00da: { n:"BookBool", f:parseuint16 },
  17405. 0x00dc: { n:"DbOrParamQry" },
  17406. 0x00dd: { n:"ScenarioProtect", f:parsebool },
  17407. 0x00de: { n:"OleObjectSize" },
  17408. 0x00e0: { n:"XF", f:parse_XF },
  17409. 0x00e1: { n:'InterfaceHdr', f:parse_InterfaceHdr },
  17410. 0x00e2: { n:'InterfaceEnd', f:parsenoop2 },
  17411. 0x00e3: { n:"SXVS" },
  17412. 0x00e5: { n:"MergeCells", f:parse_MergeCells },
  17413. 0x00e9: { n:"BkHim" },
  17414. 0x00eb: { n:"MsoDrawingGroup" },
  17415. 0x00ec: { n:"MsoDrawing" },
  17416. 0x00ed: { n:"MsoDrawingSelection" },
  17417. 0x00ef: { n:"PhoneticInfo" },
  17418. 0x00f0: { n:"SxRule" },
  17419. 0x00f1: { n:"SXEx" },
  17420. 0x00f2: { n:"SxFilt" },
  17421. 0x00f4: { n:"SxDXF" },
  17422. 0x00f5: { n:"SxItm" },
  17423. 0x00f6: { n:"SxName" },
  17424. 0x00f7: { n:"SxSelect" },
  17425. 0x00f8: { n:"SXPair" },
  17426. 0x00f9: { n:"SxFmla" },
  17427. 0x00fb: { n:"SxFormat" },
  17428. 0x00fc: { n:"SST", f:parse_SST },
  17429. 0x00fd: { n:"LabelSst", f:parse_LabelSst },
  17430. 0x00ff: { n:"ExtSST", f:parse_ExtSST },
  17431. 0x0100: { n:"SXVDEx" },
  17432. 0x0103: { n:"SXFormula" },
  17433. 0x0122: { n:"SXDBEx" },
  17434. 0x0137: { n:"RRDInsDel" },
  17435. 0x0138: { n:"RRDHead" },
  17436. 0x013b: { n:"RRDChgCell" },
  17437. 0x013d: { n:"RRTabId", f:parseuint16a },
  17438. 0x013e: { n:"RRDRenSheet" },
  17439. 0x013f: { n:"RRSort" },
  17440. 0x0140: { n:"RRDMove" },
  17441. 0x014a: { n:"RRFormat" },
  17442. 0x014b: { n:"RRAutoFmt" },
  17443. 0x014d: { n:"RRInsertSh" },
  17444. 0x014e: { n:"RRDMoveBegin" },
  17445. 0x014f: { n:"RRDMoveEnd" },
  17446. 0x0150: { n:"RRDInsDelBegin" },
  17447. 0x0151: { n:"RRDInsDelEnd" },
  17448. 0x0152: { n:"RRDConflict" },
  17449. 0x0153: { n:"RRDDefName" },
  17450. 0x0154: { n:"RRDRstEtxp" },
  17451. 0x015f: { n:"LRng" },
  17452. 0x0160: { n:"UsesELFs", f:parsebool },
  17453. 0x0161: { n:"DSF", f:parsenoop2 },
  17454. 0x0191: { n:"CUsr" },
  17455. 0x0192: { n:"CbUsr" },
  17456. 0x0193: { n:"UsrInfo" },
  17457. 0x0194: { n:"UsrExcl" },
  17458. 0x0195: { n:"FileLock" },
  17459. 0x0196: { n:"RRDInfo" },
  17460. 0x0197: { n:"BCUsrs" },
  17461. 0x0198: { n:"UsrChk" },
  17462. 0x01a9: { n:"UserBView" },
  17463. 0x01aa: { n:"UserSViewBegin" },
  17464. 0x01ab: { n:"UserSViewEnd" },
  17465. 0x01ac: { n:"RRDUserView" },
  17466. 0x01ad: { n:"Qsi" },
  17467. 0x01ae: { n:"SupBook", f:parse_SupBook },
  17468. 0x01af: { n:"Prot4Rev", f:parsebool },
  17469. 0x01b0: { n:"CondFmt" },
  17470. 0x01b1: { n:"CF" },
  17471. 0x01b2: { n:"DVal" },
  17472. 0x01b5: { n:"DConBin" },
  17473. 0x01b6: { n:"TxO", f:parse_TxO },
  17474. 0x01b7: { n:"RefreshAll", f:parsebool },
  17475. 0x01b8: { n:"HLink", f:parse_HLink },
  17476. 0x01b9: { n:"Lel" },
  17477. 0x01ba: { n:"CodeName", f:parse_XLUnicodeString },
  17478. 0x01bb: { n:"SXFDBType" },
  17479. 0x01bc: { n:"Prot4RevPass", f:parseuint16 },
  17480. 0x01bd: { n:"ObNoMacros" },
  17481. 0x01be: { n:"Dv" },
  17482. 0x01c0: { n:"Excel9File", f:parsenoop2 },
  17483. 0x01c1: { n:"RecalcId", f:parse_RecalcId, r:2},
  17484. 0x01c2: { n:"EntExU2", f:parsenoop2 },
  17485. 0x0200: { n:"Dimensions", f:parse_Dimensions },
  17486. 0x0201: { n:"Blank", f:parse_Blank },
  17487. 0x0203: { n:"Number", f:parse_Number },
  17488. 0x0204: { n:"Label", f:parse_Label },
  17489. 0x0205: { n:"BoolErr", f:parse_BoolErr },
  17490. 0x0206: { n:"Formula", f:parse_Formula },
  17491. 0x0207: { n:"String", f:parse_String },
  17492. 0x0208: { n:'Row', f:parse_Row },
  17493. 0x020b: { n:"Index" },
  17494. 0x0221: { n:"Array", f:parse_Array },
  17495. 0x0225: { n:"DefaultRowHeight", f:parse_DefaultRowHeight },
  17496. 0x0236: { n:"Table" },
  17497. 0x023e: { n:"Window2", f:parse_Window2 },
  17498. 0x027e: { n:"RK", f:parse_RK },
  17499. 0x0293: { n:"Style" },
  17500. 0x0406: { n:"Formula", f:parse_Formula },
  17501. 0x0418: { n:"BigName" },
  17502. 0x041e: { n:"Format", f:parse_Format },
  17503. 0x043c: { n:"ContinueBigName" },
  17504. 0x04bc: { n:"ShrFmla", f:parse_ShrFmla },
  17505. 0x0800: { n:"HLinkTooltip", f:parse_HLinkTooltip },
  17506. 0x0801: { n:"WebPub" },
  17507. 0x0802: { n:"QsiSXTag" },
  17508. 0x0803: { n:"DBQueryExt" },
  17509. 0x0804: { n:"ExtString" },
  17510. 0x0805: { n:"TxtQry" },
  17511. 0x0806: { n:"Qsir" },
  17512. 0x0807: { n:"Qsif" },
  17513. 0x0808: { n:"RRDTQSIF" },
  17514. 0x0809: { n:'BOF', f:parse_BOF },
  17515. 0x080a: { n:"OleDbConn" },
  17516. 0x080b: { n:"WOpt" },
  17517. 0x080c: { n:"SXViewEx" },
  17518. 0x080d: { n:"SXTH" },
  17519. 0x080e: { n:"SXPIEx" },
  17520. 0x080f: { n:"SXVDTEx" },
  17521. 0x0810: { n:"SXViewEx9" },
  17522. 0x0812: { n:"ContinueFrt" },
  17523. 0x0813: { n:"RealTimeData" },
  17524. 0x0850: { n:"ChartFrtInfo" },
  17525. 0x0851: { n:"FrtWrapper" },
  17526. 0x0852: { n:"StartBlock" },
  17527. 0x0853: { n:"EndBlock" },
  17528. 0x0854: { n:"StartObject" },
  17529. 0x0855: { n:"EndObject" },
  17530. 0x0856: { n:"CatLab" },
  17531. 0x0857: { n:"YMult" },
  17532. 0x0858: { n:"SXViewLink" },
  17533. 0x0859: { n:"PivotChartBits" },
  17534. 0x085a: { n:"FrtFontList" },
  17535. 0x0862: { n:"SheetExt" },
  17536. 0x0863: { n:"BookExt", r:12},
  17537. 0x0864: { n:"SXAddl" },
  17538. 0x0865: { n:"CrErr" },
  17539. 0x0866: { n:"HFPicture" },
  17540. 0x0867: { n:'FeatHdr', f:parsenoop2 },
  17541. 0x0868: { n:"Feat" },
  17542. 0x086a: { n:"DataLabExt" },
  17543. 0x086b: { n:"DataLabExtContents" },
  17544. 0x086c: { n:"CellWatch" },
  17545. 0x0871: { n:"FeatHdr11" },
  17546. 0x0872: { n:"Feature11" },
  17547. 0x0874: { n:"DropDownObjIds" },
  17548. 0x0875: { n:"ContinueFrt11" },
  17549. 0x0876: { n:"DConn" },
  17550. 0x0877: { n:"List12" },
  17551. 0x0878: { n:"Feature12" },
  17552. 0x0879: { n:"CondFmt12" },
  17553. 0x087a: { n:"CF12" },
  17554. 0x087b: { n:"CFEx" },
  17555. 0x087c: { n:"XFCRC", f:parse_XFCRC, r:12 },
  17556. 0x087d: { n:"XFExt", f:parse_XFExt, r:12 },
  17557. 0x087e: { n:"AutoFilter12" },
  17558. 0x087f: { n:"ContinueFrt12" },
  17559. 0x0884: { n:"MDTInfo" },
  17560. 0x0885: { n:"MDXStr" },
  17561. 0x0886: { n:"MDXTuple" },
  17562. 0x0887: { n:"MDXSet" },
  17563. 0x0888: { n:"MDXProp" },
  17564. 0x0889: { n:"MDXKPI" },
  17565. 0x088a: { n:"MDB" },
  17566. 0x088b: { n:"PLV" },
  17567. 0x088c: { n:"Compat12", f:parsebool, r:12 },
  17568. 0x088d: { n:"DXF" },
  17569. 0x088e: { n:"TableStyles", r:12 },
  17570. 0x088f: { n:"TableStyle" },
  17571. 0x0890: { n:"TableStyleElement" },
  17572. 0x0892: { n:"StyleExt" },
  17573. 0x0893: { n:"NamePublish" },
  17574. 0x0894: { n:"NameCmt", f:parse_NameCmt, r:12 },
  17575. 0x0895: { n:"SortData" },
  17576. 0x0896: { n:"Theme", f:parse_Theme, r:12 },
  17577. 0x0897: { n:"GUIDTypeLib" },
  17578. 0x0898: { n:"FnGrp12" },
  17579. 0x0899: { n:"NameFnGrp12" },
  17580. 0x089a: { n:"MTRSettings", f:parse_MTRSettings, r:12 },
  17581. 0x089b: { n:"CompressPictures", f:parsenoop2 },
  17582. 0x089c: { n:"HeaderFooter" },
  17583. 0x089d: { n:"CrtLayout12" },
  17584. 0x089e: { n:"CrtMlFrt" },
  17585. 0x089f: { n:"CrtMlFrtContinue" },
  17586. 0x08a3: { n:"ForceFullCalculation", f:parse_ForceFullCalculation },
  17587. 0x08a4: { n:"ShapePropsStream" },
  17588. 0x08a5: { n:"TextPropsStream" },
  17589. 0x08a6: { n:"RichTextStream" },
  17590. 0x08a7: { n:"CrtLayout12A" },
  17591. 0x1001: { n:"Units" },
  17592. 0x1002: { n:"Chart" },
  17593. 0x1003: { n:"Series" },
  17594. 0x1006: { n:"DataFormat" },
  17595. 0x1007: { n:"LineFormat" },
  17596. 0x1009: { n:"MarkerFormat" },
  17597. 0x100a: { n:"AreaFormat" },
  17598. 0x100b: { n:"PieFormat" },
  17599. 0x100c: { n:"AttachedLabel" },
  17600. 0x100d: { n:"SeriesText" },
  17601. 0x1014: { n:"ChartFormat" },
  17602. 0x1015: { n:"Legend" },
  17603. 0x1016: { n:"SeriesList" },
  17604. 0x1017: { n:"Bar" },
  17605. 0x1018: { n:"Line" },
  17606. 0x1019: { n:"Pie" },
  17607. 0x101a: { n:"Area" },
  17608. 0x101b: { n:"Scatter" },
  17609. 0x101c: { n:"CrtLine" },
  17610. 0x101d: { n:"Axis" },
  17611. 0x101e: { n:"Tick" },
  17612. 0x101f: { n:"ValueRange" },
  17613. 0x1020: { n:"CatSerRange" },
  17614. 0x1021: { n:"AxisLine" },
  17615. 0x1022: { n:"CrtLink" },
  17616. 0x1024: { n:"DefaultText" },
  17617. 0x1025: { n:"Text" },
  17618. 0x1026: { n:"FontX", f:parseuint16 },
  17619. 0x1027: { n:"ObjectLink" },
  17620. 0x1032: { n:"Frame" },
  17621. 0x1033: { n:"Begin" },
  17622. 0x1034: { n:"End" },
  17623. 0x1035: { n:"PlotArea" },
  17624. 0x103a: { n:"Chart3d" },
  17625. 0x103c: { n:"PicF" },
  17626. 0x103d: { n:"DropBar" },
  17627. 0x103e: { n:"Radar" },
  17628. 0x103f: { n:"Surf" },
  17629. 0x1040: { n:"RadarArea" },
  17630. 0x1041: { n:"AxisParent" },
  17631. 0x1043: { n:"LegendException" },
  17632. 0x1044: { n:"ShtProps", f:parse_ShtProps },
  17633. 0x1045: { n:"SerToCrt" },
  17634. 0x1046: { n:"AxesUsed" },
  17635. 0x1048: { n:"SBaseRef" },
  17636. 0x104a: { n:"SerParent" },
  17637. 0x104b: { n:"SerAuxTrend" },
  17638. 0x104e: { n:"IFmtRecord" },
  17639. 0x104f: { n:"Pos" },
  17640. 0x1050: { n:"AlRuns" },
  17641. 0x1051: { n:"BRAI" },
  17642. 0x105b: { n:"SerAuxErrBar" },
  17643. 0x105c: { n:"ClrtClient", f:parse_ClrtClient },
  17644. 0x105d: { n:"SerFmt" },
  17645. 0x105f: { n:"Chart3DBarShape" },
  17646. 0x1060: { n:"Fbi" },
  17647. 0x1061: { n:"BopPop" },
  17648. 0x1062: { n:"AxcExt" },
  17649. 0x1063: { n:"Dat" },
  17650. 0x1064: { n:"PlotGrowth" },
  17651. 0x1065: { n:"SIIndex" },
  17652. 0x1066: { n:"GelFrame" },
  17653. 0x1067: { n:"BopPopCustom" },
  17654. 0x1068: { n:"Fbi2" },
  17655. 0x0000: { n:"Dimensions", f:parse_Dimensions },
  17656. 0x0002: { n:"BIFF2INT", f:parse_BIFF2INT },
  17657. 0x0005: { n:"BoolErr", f:parse_BoolErr },
  17658. 0x0007: { n:"String", f:parse_BIFF2STRING },
  17659. 0x0008: { n:"BIFF2ROW" },
  17660. 0x000b: { n:"Index" },
  17661. 0x0016: { n:"ExternCount", f:parseuint16 },
  17662. 0x001e: { n:"BIFF2FORMAT", f:parse_BIFF2Format },
  17663. 0x001f: { n:"BIFF2FMTCNT" }, /* 16-bit cnt of BIFF2FORMAT records */
  17664. 0x0020: { n:"BIFF2COLINFO" },
  17665. 0x0021: { n:"Array", f:parse_Array },
  17666. 0x0025: { n:"DefaultRowHeight", f:parse_DefaultRowHeight },
  17667. 0x0032: { n:"BIFF2FONTXTRA", f:parse_BIFF2FONTXTRA },
  17668. 0x0034: { n:"DDEObjName" },
  17669. 0x003e: { n:"BIFF2WINDOW2" },
  17670. 0x0043: { n:"BIFF2XF" },
  17671. 0x0045: { n:"BIFF2FONTCLR" },
  17672. 0x0056: { n:"BIFF4FMTCNT" }, /* 16-bit cnt, similar to BIFF2 */
  17673. 0x007e: { n:"RK" }, /* Not necessarily same as 0x027e */
  17674. 0x007f: { n:"ImData", f:parse_ImData },
  17675. 0x0087: { n:"Addin" },
  17676. 0x0088: { n:"Edg" },
  17677. 0x0089: { n:"Pub" },
  17678. 0x0091: { n:"Sub" },
  17679. 0x0094: { n:"LHRecord" },
  17680. 0x0095: { n:"LHNGraph" },
  17681. 0x0096: { n:"Sound" },
  17682. 0x00a9: { n:"CoordList" },
  17683. 0x00ab: { n:"GCW" },
  17684. 0x00bc: { n:"ShrFmla" }, /* Not necessarily same as 0x04bc */
  17685. 0x00bf: { n:"ToolbarHdr" },
  17686. 0x00c0: { n:"ToolbarEnd" },
  17687. 0x00c2: { n:"AddMenu" },
  17688. 0x00c3: { n:"DelMenu" },
  17689. 0x00d6: { n:"RString", f:parse_RString },
  17690. 0x00df: { n:"UDDesc" },
  17691. 0x00ea: { n:"TabIdConf" },
  17692. 0x0162: { n:"XL5Modify" },
  17693. 0x01a5: { n:"FileSharing2" },
  17694. 0x0209: { n:'BOF', f:parse_BOF },
  17695. 0x0218: { n:"Lbl", f:parse_Lbl },
  17696. 0x0223: { n:"ExternName", f:parse_ExternName },
  17697. 0x0231: { n:"Font" },
  17698. 0x0243: { n:"BIFF3XF" },
  17699. 0x0409: { n:'BOF', f:parse_BOF },
  17700. 0x0443: { n:"BIFF4XF" },
  17701. 0x086d: { n:"FeatInfo" },
  17702. 0x0873: { n:"FeatInfo11" },
  17703. 0x0881: { n:"SXAddl12" },
  17704. 0x08c0: { n:"AutoWebPub" },
  17705. 0x08c1: { n:"ListObj" },
  17706. 0x08c2: { n:"ListField" },
  17707. 0x08c3: { n:"ListDV" },
  17708. 0x08c4: { n:"ListCondFmt" },
  17709. 0x08c5: { n:"ListCF" },
  17710. 0x08c6: { n:"FMQry" },
  17711. 0x08c7: { n:"FMSQry" },
  17712. 0x08c8: { n:"PLV" },
  17713. 0x08c9: { n:"LnExt" },
  17714. 0x08ca: { n:"MkrExt" },
  17715. 0x08cb: { n:"CrtCoopt" },
  17716. 0x08d6: { n:"FRTArchId$", r:12 },
  17717. 0x7262: {}
  17718. };
  17719. var XLSRE = evert_key(XLSRecordEnum, 'n');
  17720. function write_biff_rec(ba, type, payload, length) {
  17721. var t = +type || +XLSRE[type];
  17722. if(isNaN(t)) return;
  17723. var len = length || (payload||[]).length || 0;
  17724. var o = ba.next(4);
  17725. o.write_shift(2, t);
  17726. o.write_shift(2, len);
  17727. if(len > 0 && is_buf(payload)) ba.push(payload);
  17728. }
  17729. function write_biff_continue(ba, type, payload, length) {
  17730. var len = length || (payload||[]).length || 0;
  17731. if(len <= 8224) return write_biff_rec(ba, type, payload, len);
  17732. var t = +type || +XLSRE[type];
  17733. if(isNaN(t)) return;
  17734. var parts = payload.parts || [], sidx = 0;
  17735. var i = 0, w = 0;
  17736. while(w + (parts[sidx] || 8224) <= 8224) { w+= (parts[sidx] || 8224); sidx++; }
  17737. var o = ba.next(4);
  17738. o.write_shift(2, t);
  17739. o.write_shift(2, w);
  17740. ba.push(payload.slice(i, i + w));
  17741. i += w;
  17742. while(i < len) {
  17743. o = ba.next(4);
  17744. o.write_shift(2, 0x3c); // TODO: figure out correct continue type
  17745. w = 0;
  17746. while(w + (parts[sidx] || 8224) <= 8224) { w+= (parts[sidx] || 8224); sidx++; }
  17747. o.write_shift(2, w);
  17748. ba.push(payload.slice(i, i+w)); i+= w;
  17749. }
  17750. }
  17751. function write_BIFF2Cell(out, r, c) {
  17752. if(!out) out = new_buf(7);
  17753. out.write_shift(2, r);
  17754. out.write_shift(2, c);
  17755. out.write_shift(2, 0);
  17756. out.write_shift(1, 0);
  17757. return out;
  17758. }
  17759. function write_BIFF2BERR(r, c, val, t) {
  17760. var out = new_buf(9);
  17761. write_BIFF2Cell(out, r, c);
  17762. if(t == 'e') { out.write_shift(1, val); out.write_shift(1, 1); }
  17763. else { out.write_shift(1, val?1:0); out.write_shift(1, 0); }
  17764. return out;
  17765. }
  17766. /* TODO: codepage, large strings */
  17767. function write_BIFF2LABEL(r, c, val) {
  17768. var out = new_buf(8 + 2*val.length);
  17769. write_BIFF2Cell(out, r, c);
  17770. out.write_shift(1, val.length);
  17771. out.write_shift(val.length, val, 'sbcs');
  17772. return out.l < out.length ? out.slice(0, out.l) : out;
  17773. }
  17774. function write_ws_biff2_cell(ba, cell, R, C) {
  17775. if(cell.v != null) switch(cell.t) {
  17776. case 'd': case 'n':
  17777. var v = cell.t == 'd' ? datenum(parseDate(cell.v)) : cell.v;
  17778. if((v == (v|0)) && (v >= 0) && (v < 65536))
  17779. write_biff_rec(ba, 0x0002, write_BIFF2INT(R, C, v));
  17780. else
  17781. write_biff_rec(ba, 0x0003, write_BIFF2NUM(R,C, v));
  17782. return;
  17783. case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return;
  17784. /* TODO: codepage, sst */
  17785. case 's': case 'str':
  17786. write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v));
  17787. return;
  17788. }
  17789. write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C));
  17790. }
  17791. function write_ws_biff2(ba, ws, idx, opts) {
  17792. var dense = Array.isArray(ws);
  17793. var range = safe_decode_range(ws['!ref'] || "A1"), ref, rr = "", cols = [];
  17794. if(range.e.c > 0xFF || range.e.r > 0x3FFF) {
  17795. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:IV16384");
  17796. range.e.c = Math.min(range.e.c, 0xFF);
  17797. range.e.r = Math.min(range.e.c, 0x3FFF);
  17798. ref = encode_range(range);
  17799. }
  17800. for(var R = range.s.r; R <= range.e.r; ++R) {
  17801. rr = encode_row(R);
  17802. for(var C = range.s.c; C <= range.e.c; ++C) {
  17803. if(R === range.s.r) cols[C] = encode_col(C);
  17804. ref = cols[C] + rr;
  17805. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  17806. if(!cell) continue;
  17807. /* write cell */
  17808. write_ws_biff2_cell(ba, cell, R, C, opts);
  17809. }
  17810. }
  17811. }
  17812. /* Based on test files */
  17813. function write_biff2_buf(wb, opts) {
  17814. var o = opts || {};
  17815. if(DENSE != null && o.dense == null) o.dense = DENSE;
  17816. var ba = buf_array();
  17817. var idx = 0;
  17818. for(var i=0;i<wb.SheetNames.length;++i) if(wb.SheetNames[i] == o.sheet) idx=i;
  17819. if(idx == 0 && !!o.sheet && wb.SheetNames[0] != o.sheet) throw new Error("Sheet not found: " + o.sheet);
  17820. write_biff_rec(ba, 0x0009, write_BOF(wb, 0x10, o));
  17821. /* ... */
  17822. write_ws_biff2(ba, wb.Sheets[wb.SheetNames[idx]], idx, o, wb);
  17823. /* ... */
  17824. write_biff_rec(ba, 0x000A);
  17825. return ba.end();
  17826. }
  17827. function write_FONTS_biff8(ba, data, opts) {
  17828. write_biff_rec(ba, "Font", write_Font({
  17829. sz:12,
  17830. color: {theme:1},
  17831. name: "Arial",
  17832. family: 2,
  17833. scheme: "minor"
  17834. }, opts));
  17835. }
  17836. function write_FMTS_biff8(ba, NF, opts) {
  17837. if(!NF) return;
  17838. [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
  17839. for(var i = r[0]; i <= r[1]; ++i) if(NF[i] != null) write_biff_rec(ba, "Format", write_Format(i, NF[i], opts));
  17840. });
  17841. }
  17842. function write_FEAT(ba, ws) {
  17843. /* [MS-XLS] 2.4.112 */
  17844. var o = new_buf(19);
  17845. o.write_shift(4, 0x867); o.write_shift(4, 0); o.write_shift(4, 0);
  17846. o.write_shift(2, 3); o.write_shift(1, 1); o.write_shift(4, 0);
  17847. write_biff_rec(ba, "FeatHdr", o);
  17848. /* [MS-XLS] 2.4.111 */
  17849. o = new_buf(39);
  17850. o.write_shift(4, 0x868); o.write_shift(4, 0); o.write_shift(4, 0);
  17851. o.write_shift(2, 3); o.write_shift(1, 0); o.write_shift(4, 0);
  17852. o.write_shift(2, 1); o.write_shift(4, 4); o.write_shift(2, 0);
  17853. write_Ref8U(safe_decode_range(ws['!ref']||"A1"), o);
  17854. o.write_shift(4, 4);
  17855. write_biff_rec(ba, "Feat", o);
  17856. }
  17857. function write_CELLXFS_biff8(ba, opts) {
  17858. for(var i = 0; i < 16; ++i) write_biff_rec(ba, "XF", write_XF({numFmtId:0, style:true}, 0, opts));
  17859. opts.cellXfs.forEach(function(c) {
  17860. write_biff_rec(ba, "XF", write_XF(c, 0, opts));
  17861. });
  17862. }
  17863. function write_ws_biff8_hlinks(ba, ws) {
  17864. for(var R=0; R<ws['!links'].length; ++R) {
  17865. var HL = ws['!links'][R];
  17866. write_biff_rec(ba, "HLink", write_HLink(HL));
  17867. if(HL[1].Tooltip) write_biff_rec(ba, "HLinkTooltip", write_HLinkTooltip(HL));
  17868. }
  17869. delete ws['!links'];
  17870. }
  17871. function write_ws_biff8_cell(ba, cell, R, C, opts) {
  17872. var os = 16 + get_cell_style(opts.cellXfs, cell, opts);
  17873. if(cell.v == null && !cell.bf) {
  17874. write_biff_rec(ba, "Blank", write_XLSCell(R, C, os));
  17875. return;
  17876. }
  17877. if(cell.bf) write_biff_rec(ba, "Formula", write_Formula(cell, R, C, opts, os));
  17878. else switch(cell.t) {
  17879. case 'd': case 'n':
  17880. var v = cell.t == 'd' ? datenum(parseDate(cell.v)) : cell.v;
  17881. /* TODO: emit RK as appropriate */
  17882. write_biff_rec(ba, "Number", write_Number(R, C, v, os, opts));
  17883. break;
  17884. case 'b': case 'e':
  17885. write_biff_rec(ba, 0x0205, write_BoolErr(R, C, cell.v, os, opts, cell.t));
  17886. break;
  17887. /* TODO: codepage, sst */
  17888. case 's': case 'str':
  17889. if(opts.bookSST) {
  17890. var isst = get_sst_id(opts.Strings, cell.v, opts.revStrings);
  17891. write_biff_rec(ba, "LabelSst", write_LabelSst(R, C, isst, os, opts));
  17892. } else write_biff_rec(ba, "Label", write_Label(R, C, cell.v, os, opts));
  17893. break;
  17894. default:
  17895. write_biff_rec(ba, "Blank", write_XLSCell(R, C, os));
  17896. }
  17897. }
  17898. /* [MS-XLS] 2.1.7.20.5 */
  17899. function write_ws_biff8(idx, opts, wb) {
  17900. var ba = buf_array();
  17901. var s = wb.SheetNames[idx], ws = wb.Sheets[s] || {};
  17902. var _WB = ((wb||{}).Workbook||{});
  17903. var _sheet = ((_WB.Sheets||[])[idx]||{});
  17904. var dense = Array.isArray(ws);
  17905. var b8 = opts.biff == 8;
  17906. var ref, rr = "", cols = [];
  17907. var range = safe_decode_range(ws['!ref'] || "A1");
  17908. var MAX_ROWS = b8 ? 65536 : 16384;
  17909. if(range.e.c > 0xFF || range.e.r >= MAX_ROWS) {
  17910. if(opts.WTF) throw new Error("Range " + (ws['!ref'] || "A1") + " exceeds format limit A1:IV16384");
  17911. range.e.c = Math.min(range.e.c, 0xFF);
  17912. range.e.r = Math.min(range.e.c, MAX_ROWS-1);
  17913. }
  17914. write_biff_rec(ba, 0x0809, write_BOF(wb, 0x10, opts));
  17915. /* [Uncalced] Index */
  17916. write_biff_rec(ba, "CalcMode", writeuint16(1));
  17917. write_biff_rec(ba, "CalcCount", writeuint16(100));
  17918. write_biff_rec(ba, "CalcRefMode", writebool(true));
  17919. write_biff_rec(ba, "CalcIter", writebool(false));
  17920. write_biff_rec(ba, "CalcDelta", write_Xnum(0.001));
  17921. write_biff_rec(ba, "CalcSaveRecalc", writebool(true));
  17922. write_biff_rec(ba, "PrintRowCol", writebool(false));
  17923. write_biff_rec(ba, "PrintGrid", writebool(false));
  17924. write_biff_rec(ba, "GridSet", writeuint16(1));
  17925. write_biff_rec(ba, "Guts", write_Guts([0,0]));
  17926. /* DefaultRowHeight WsBool [Sync] [LPr] [HorizontalPageBreaks] [VerticalPageBreaks] */
  17927. /* Header (string) */
  17928. /* Footer (string) */
  17929. write_biff_rec(ba, "HCenter", writebool(false));
  17930. write_biff_rec(ba, "VCenter", writebool(false));
  17931. /* ... */
  17932. write_biff_rec(ba, 0x200, write_Dimensions(range, opts));
  17933. /* ... */
  17934. if(b8) ws['!links'] = [];
  17935. for(var R = range.s.r; R <= range.e.r; ++R) {
  17936. rr = encode_row(R);
  17937. for(var C = range.s.c; C <= range.e.c; ++C) {
  17938. if(R === range.s.r) cols[C] = encode_col(C);
  17939. ref = cols[C] + rr;
  17940. var cell = dense ? (ws[R]||[])[C] : ws[ref];
  17941. if(!cell) continue;
  17942. /* write cell */
  17943. write_ws_biff8_cell(ba, cell, R, C, opts);
  17944. if(b8 && cell.l) ws['!links'].push([ref, cell.l]);
  17945. }
  17946. }
  17947. var cname = _sheet.CodeName || _sheet.name || s;
  17948. /* ... */
  17949. if(b8) write_biff_rec(ba, "Window2", write_Window2((_WB.Views||[])[0]));
  17950. /* ... */
  17951. if(b8 && (ws['!merges']||[]).length) write_biff_rec(ba, "MergeCells", write_MergeCells(ws['!merges']));
  17952. /* [LRng] *QUERYTABLE [PHONETICINFO] CONDFMTS */
  17953. if(b8) write_ws_biff8_hlinks(ba, ws);
  17954. /* [DVAL] */
  17955. write_biff_rec(ba, "CodeName", write_XLUnicodeString(cname, opts));
  17956. /* *WebPub *CellWatch [SheetExt] */
  17957. if(b8) write_FEAT(ba, ws);
  17958. /* *FEAT11 *RECORD12 */
  17959. write_biff_rec(ba, "EOF");
  17960. return ba.end();
  17961. }
  17962. /* [MS-XLS] 2.1.7.20.3 */
  17963. function write_biff8_global(wb, bufs, opts) {
  17964. var A = buf_array();
  17965. var _WB = ((wb||{}).Workbook||{});
  17966. var _sheets = (_WB.Sheets||[]);
  17967. var _wb = _WB.WBProps||{};
  17968. var b8 = opts.biff == 8, b5 = opts.biff == 5;
  17969. write_biff_rec(A, 0x0809, write_BOF(wb, 0x05, opts));
  17970. if(opts.bookType == "xla") write_biff_rec(A, "Addin");
  17971. write_biff_rec(A, "InterfaceHdr", b8 ? writeuint16(0x04b0) : null);
  17972. write_biff_rec(A, "Mms", writezeroes(2));
  17973. if(b5) write_biff_rec(A, "ToolbarHdr");
  17974. if(b5) write_biff_rec(A, "ToolbarEnd");
  17975. write_biff_rec(A, "InterfaceEnd");
  17976. write_biff_rec(A, "WriteAccess", write_WriteAccess("SheetJS", opts));
  17977. /* [FileSharing] */
  17978. write_biff_rec(A, "CodePage", writeuint16(b8 ? 0x04b0 : 0x04E4));
  17979. /* *2047 Lel */
  17980. if(b8) write_biff_rec(A, "DSF", writeuint16(0));
  17981. if(b8) write_biff_rec(A, "Excel9File");
  17982. write_biff_rec(A, "RRTabId", write_RRTabId(wb.SheetNames.length));
  17983. if(b8 && wb.vbaraw) write_biff_rec(A, "ObProj");
  17984. /* [ObNoMacros] */
  17985. if(b8 && wb.vbaraw) {
  17986. var cname = _wb.CodeName || "ThisWorkbook";
  17987. write_biff_rec(A, "CodeName", write_XLUnicodeString(cname, opts));
  17988. }
  17989. write_biff_rec(A, "BuiltInFnGroupCount", writeuint16(0x11));
  17990. /* *FnGroupName *FnGrp12 */
  17991. /* *Lbl */
  17992. /* [OleObjectSize] */
  17993. write_biff_rec(A, "WinProtect", writebool(false));
  17994. write_biff_rec(A, "Protect", writebool(false));
  17995. write_biff_rec(A, "Password", writeuint16(0));
  17996. if(b8) write_biff_rec(A, "Prot4Rev", writebool(false));
  17997. if(b8) write_biff_rec(A, "Prot4RevPass", writeuint16(0));
  17998. write_biff_rec(A, "Window1", write_Window1(opts));
  17999. write_biff_rec(A, "Backup", writebool(false));
  18000. write_biff_rec(A, "HideObj", writeuint16(0));
  18001. write_biff_rec(A, "Date1904", writebool(safe1904(wb)=="true"));
  18002. write_biff_rec(A, "CalcPrecision", writebool(true));
  18003. if(b8) write_biff_rec(A, "RefreshAll", writebool(false));
  18004. write_biff_rec(A, "BookBool", writeuint16(0));
  18005. /* ... */
  18006. write_FONTS_biff8(A, wb, opts);
  18007. write_FMTS_biff8(A, wb.SSF, opts);
  18008. write_CELLXFS_biff8(A, opts);
  18009. /* ... */
  18010. if(b8) write_biff_rec(A, "UsesELFs", writebool(false));
  18011. var a = A.end();
  18012. var C = buf_array();
  18013. /* METADATA [MTRSettings] [ForceFullCalculation] */
  18014. if(b8) write_biff_rec(C, "Country", write_Country());
  18015. /* *SUPBOOK *LBL *RTD [RecalcId] *HFPicture *MSODRAWINGGROUP */
  18016. /* BIFF8: [SST *Continue] ExtSST */
  18017. if(b8 && opts.Strings) write_biff_continue(C, "SST", write_SST(opts.Strings, opts));
  18018. /* *WebPub [WOpt] [CrErr] [BookExt] *FeatHdr *DConn [THEME] [CompressPictures] [Compat12] [GUIDTypeLib] */
  18019. write_biff_rec(C, "EOF");
  18020. var c = C.end();
  18021. var B = buf_array();
  18022. var blen = 0, j = 0;
  18023. for(j = 0; j < wb.SheetNames.length; ++j) blen += (b8 ? 12 : 11) + (b8 ? 2 : 1) * wb.SheetNames[j].length;
  18024. var start = a.length + blen + c.length;
  18025. for(j = 0; j < wb.SheetNames.length; ++j) {
  18026. var _sheet = _sheets[j] || ({});
  18027. write_biff_rec(B, "BoundSheet8", write_BoundSheet8({pos:start, hs:_sheet.Hidden||0, dt:0, name:wb.SheetNames[j]}, opts));
  18028. start += bufs[j].length;
  18029. }
  18030. /* 1*BoundSheet8 */
  18031. var b = B.end();
  18032. if(blen != b.length) throw new Error("BS8 " + blen + " != " + b.length);
  18033. var out = [];
  18034. if(a.length) out.push(a);
  18035. if(b.length) out.push(b);
  18036. if(c.length) out.push(c);
  18037. return __toBuffer([out]);
  18038. }
  18039. /* [MS-XLS] 2.1.7.20 Workbook Stream */
  18040. function write_biff8_buf(wb, opts) {
  18041. var o = opts || {};
  18042. var bufs = [];
  18043. if(wb && !wb.SSF) {
  18044. wb.SSF = SSF.get_table();
  18045. }
  18046. if(wb && wb.SSF) {
  18047. make_ssf(SSF); SSF.load_table(wb.SSF);
  18048. // $FlowIgnore
  18049. o.revssf = evert_num(wb.SSF); o.revssf[wb.SSF[65535]] = 0;
  18050. o.ssf = wb.SSF;
  18051. }
  18052. o.Strings = []; o.Strings.Count = 0; o.Strings.Unique = 0;
  18053. fix_write_opts(o);
  18054. o.cellXfs = [];
  18055. get_cell_style(o.cellXfs, {}, {revssf:{"General":0}});
  18056. if(!wb.Props) wb.Props = {};
  18057. for(var i = 0; i < wb.SheetNames.length; ++i) bufs[bufs.length] = write_ws_biff8(i, o, wb);
  18058. bufs.unshift(write_biff8_global(wb, bufs, o));
  18059. return __toBuffer([bufs]);
  18060. }
  18061. function write_biff_buf(wb, opts) {
  18062. var o = opts || {};
  18063. switch(o.biff || 2) {
  18064. case 8: case 5: return write_biff8_buf(wb, opts);
  18065. case 4: case 3: case 2: return write_biff2_buf(wb, opts);
  18066. }
  18067. throw new Error("invalid type " + o.bookType + " for BIFF");
  18068. }
  18069. /* note: browser DOM element cannot see mso- style attrs, must parse */
  18070. var HTML_ = (function() {
  18071. function html_to_sheet(str, _opts) {
  18072. var opts = _opts || {};
  18073. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  18074. var ws = opts.dense ? ([]) : ({});
  18075. str = str.replace(/<!--.*?-->/g, "");
  18076. var mtch = str.match(/<table/i);
  18077. if(!mtch) throw new Error("Invalid HTML: could not find <table>");
  18078. var mtch2 = str.match(/<\/table/i);
  18079. var i = mtch.index, j = mtch2 && mtch2.index || str.length;
  18080. var rows = split_regex(str.slice(i, j), /(:?<tr[^>]*>)/i, "<tr>");
  18081. var R = -1, C = 0, RS = 0, CS = 0;
  18082. var range = {s:{r:10000000, c:10000000},e:{r:0,c:0}};
  18083. var merges = [];
  18084. for(i = 0; i < rows.length; ++i) {
  18085. var row = rows[i].trim();
  18086. var hd = row.slice(0,3).toLowerCase();
  18087. if(hd == "<tr") { ++R; if(opts.sheetRows && opts.sheetRows <= R) { --R; break; } C = 0; continue; }
  18088. if(hd != "<td" && hd != "<th") continue;
  18089. var cells = row.split(/<\/t[dh]>/i);
  18090. for(j = 0; j < cells.length; ++j) {
  18091. var cell = cells[j].trim();
  18092. if(!cell.match(/<t[dh]/i)) continue;
  18093. var m = cell, cc = 0;
  18094. /* TODO: parse styles etc */
  18095. while(m.charAt(0) == "<" && (cc = m.indexOf(">")) > -1) m = m.slice(cc+1);
  18096. for(var midx = 0; midx < merges.length; ++midx) {
  18097. var _merge = merges[midx];
  18098. if(_merge.s.c == C && _merge.s.r < R && R <= _merge.e.r) { C = _merge.e.c + 1; midx = -1; }
  18099. }
  18100. var tag = parsexmltag(cell.slice(0, cell.indexOf(">")));
  18101. CS = tag.colspan ? +tag.colspan : 1;
  18102. if((RS = +tag.rowspan)>1 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
  18103. var _t = tag.t || "";
  18104. /* TODO: generate stub cells */
  18105. if(!m.length) { C += CS; continue; }
  18106. m = htmldecode(m);
  18107. if(range.s.r > R) range.s.r = R; if(range.e.r < R) range.e.r = R;
  18108. if(range.s.c > C) range.s.c = C; if(range.e.c < C) range.e.c = C;
  18109. if(!m.length) continue;
  18110. var o = {t:'s', v:m};
  18111. if(opts.raw || !m.trim().length || _t == 's'){}
  18112. else if(m === 'TRUE') o = {t:'b', v:true};
  18113. else if(m === 'FALSE') o = {t:'b', v:false};
  18114. else if(!isNaN(fuzzynum(m))) o = {t:'n', v:fuzzynum(m)};
  18115. else if(!isNaN(fuzzydate(m).getDate())) {
  18116. o = ({t:'d', v:parseDate(m)});
  18117. if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
  18118. o.z = opts.dateNF || SSF._table[14];
  18119. }
  18120. if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = o; }
  18121. else ws[encode_cell({r:R, c:C})] = o;
  18122. C += CS;
  18123. }
  18124. }
  18125. ws['!ref'] = encode_range(range);
  18126. if(merges.length) ws["!merges"] = merges;
  18127. return ws;
  18128. }
  18129. function html_to_book(str, opts) {
  18130. return sheet_to_workbook(html_to_sheet(str, opts), opts);
  18131. }
  18132. function make_html_row(ws, r, R, o) {
  18133. var M = (ws['!merges'] ||[]);
  18134. var oo = [];
  18135. for(var C = r.s.c; C <= r.e.c; ++C) {
  18136. var RS = 0, CS = 0;
  18137. for(var j = 0; j < M.length; ++j) {
  18138. if(M[j].s.r > R || M[j].s.c > C) continue;
  18139. if(M[j].e.r < R || M[j].e.c < C) continue;
  18140. if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
  18141. RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
  18142. }
  18143. if(RS < 0) continue;
  18144. var coord = encode_cell({r:R,c:C});
  18145. var cell = o.dense ? (ws[R]||[])[C] : ws[coord];
  18146. /* TODO: html entities */
  18147. var w = (cell && cell.v != null) && (cell.h || escapehtml(cell.w || (format_cell(cell), cell.w) || "")) || "";
  18148. var sp = ({});
  18149. if(RS > 1) sp.rowspan = RS;
  18150. if(CS > 1) sp.colspan = CS;
  18151. sp.t = cell && cell.t || 'z';
  18152. if(o.editable) w = '<span contenteditable="true">' + w + '</span>';
  18153. sp.id = (o.id || "sjs") + "-" + coord;
  18154. if(sp.t != "z") { sp.v = cell.v; if(cell.z != null) sp.z = cell.z; }
  18155. oo.push(writextag('td', w, sp));
  18156. }
  18157. var preamble = "<tr>";
  18158. return preamble + oo.join("") + "</tr>";
  18159. }
  18160. function make_html_preamble(ws, R, o) {
  18161. var out = [];
  18162. return out.join("") + '<table' + (o && o.id ? ' id="' + o.id + '"' : "") + '>';
  18163. }
  18164. var _BEGIN = '<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body>';
  18165. var _END = '</body></html>';
  18166. function sheet_to_html(ws, opts/*, wb:?Workbook*/) {
  18167. var o = opts || {};
  18168. var header = o.header != null ? o.header : _BEGIN;
  18169. var footer = o.footer != null ? o.footer : _END;
  18170. var out = [header];
  18171. var r = decode_range(ws['!ref']);
  18172. o.dense = Array.isArray(ws);
  18173. out.push(make_html_preamble(ws, r, o));
  18174. for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o));
  18175. out.push("</table>" + footer);
  18176. return out.join("");
  18177. }
  18178. return {
  18179. to_workbook: html_to_book,
  18180. to_sheet: html_to_sheet,
  18181. _row: make_html_row,
  18182. BEGIN: _BEGIN,
  18183. END: _END,
  18184. _preamble: make_html_preamble,
  18185. from_sheet: sheet_to_html
  18186. };
  18187. })();
  18188. function sheet_add_dom(ws, table, _opts) {
  18189. var opts = _opts || {};
  18190. if(DENSE != null) opts.dense = DENSE;
  18191. var or_R = 0, or_C = 0;
  18192. if(opts.origin != null) {
  18193. if(typeof opts.origin == 'number') or_R = opts.origin;
  18194. else {
  18195. var _origin = typeof opts.origin == "string" ? decode_cell(opts.origin) : opts.origin;
  18196. or_R = _origin.r; or_C = _origin.c;
  18197. }
  18198. }
  18199. var rows = table.getElementsByTagName('tr');
  18200. var sheetRows = Math.min(opts.sheetRows||10000000, rows.length);
  18201. var range = {s:{r:0,c:0},e:{r:or_R,c:or_C}};
  18202. if(ws["!ref"]) {
  18203. var _range = decode_range(ws["!ref"]);
  18204. range.s.r = Math.min(range.s.r, _range.s.r);
  18205. range.s.c = Math.min(range.s.c, _range.s.c);
  18206. range.e.r = Math.max(range.e.r, _range.e.r);
  18207. range.e.c = Math.max(range.e.c, _range.e.c);
  18208. if(or_R == -1) range.e.r = or_R = _range.e.r + 1;
  18209. }
  18210. var merges = [], midx = 0;
  18211. var rowinfo = ws["!rows"] || (ws["!rows"] = []);
  18212. var _R = 0, R = 0, _C = 0, C = 0, RS = 0, CS = 0;
  18213. if(!ws["!cols"]) ws['!cols'] = [];
  18214. for(; _R < rows.length && R < sheetRows; ++_R) {
  18215. var row = rows[_R];
  18216. if (is_dom_element_hidden(row)) {
  18217. if (opts.display) continue;
  18218. rowinfo[R] = {hidden: true};
  18219. }
  18220. var elts = (row.children);
  18221. for(_C = C = 0; _C < elts.length; ++_C) {
  18222. var elt = elts[_C];
  18223. if (opts.display && is_dom_element_hidden(elt)) continue;
  18224. var v = elt.hasAttribute('v') ? elt.getAttribute('v') : htmldecode(elt.innerHTML);
  18225. var z = elt.getAttribute('z');
  18226. for(midx = 0; midx < merges.length; ++midx) {
  18227. var m = merges[midx];
  18228. if(m.s.c == C + or_C && m.s.r < R + or_R && R + or_R <= m.e.r) { C = m.e.c+1 - or_C; midx = -1; }
  18229. }
  18230. /* TODO: figure out how to extract nonstandard mso- style */
  18231. CS = +elt.getAttribute("colspan") || 1;
  18232. if( ((RS = (+elt.getAttribute("rowspan") || 1)))>1 || CS>1) merges.push({s:{r:R + or_R,c:C + or_C},e:{r:R + or_R + (RS||1) - 1, c:C + or_C + (CS||1) - 1}});
  18233. var o = {t:'s', v:v};
  18234. var _t = elt.getAttribute("t") || "";
  18235. if(v != null) {
  18236. if(v.length == 0) o.t = _t || 'z';
  18237. else if(opts.raw || v.trim().length == 0 || _t == "s"){}
  18238. else if(v === 'TRUE') o = {t:'b', v:true};
  18239. else if(v === 'FALSE') o = {t:'b', v:false};
  18240. else if(!isNaN(fuzzynum(v))) o = {t:'n', v:fuzzynum(v)};
  18241. else if(!isNaN(fuzzydate(v).getDate())) {
  18242. o = ({t:'d', v:parseDate(v)});
  18243. if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
  18244. o.z = opts.dateNF || SSF._table[14];
  18245. }
  18246. }
  18247. if(o.z === undefined && z != null) o.z = z;
  18248. if(opts.dense) { if(!ws[R + or_R]) ws[R + or_R] = []; ws[R + or_R][C + or_C] = o; }
  18249. else ws[encode_cell({c:C + or_C, r:R + or_R})] = o;
  18250. if(range.e.c < C + or_C) range.e.c = C + or_C;
  18251. C += CS;
  18252. }
  18253. ++R;
  18254. }
  18255. if(merges.length) ws['!merges'] = (ws["!merges"] || []).concat(merges);
  18256. range.e.r = Math.max(range.e.r, R - 1 + or_R);
  18257. ws['!ref'] = encode_range(range);
  18258. if(R >= sheetRows) ws['!fullref'] = encode_range((range.e.r = rows.length-_R+R-1 + or_R,range)); // We can count the real number of rows to parse but we don't to improve the performance
  18259. return ws;
  18260. }
  18261. function parse_dom_table(table, _opts) {
  18262. var opts = _opts || {};
  18263. var ws = opts.dense ? ([]) : ({});
  18264. return sheet_add_dom(ws, table, _opts);
  18265. }
  18266. function table_to_book(table, opts) {
  18267. return sheet_to_workbook(parse_dom_table(table, opts), opts);
  18268. }
  18269. function is_dom_element_hidden(element) {
  18270. var display = '';
  18271. var get_computed_style = get_get_computed_style_function(element);
  18272. if(get_computed_style) display = get_computed_style(element).getPropertyValue('display');
  18273. if(!display) display = element.style.display; // Fallback for cases when getComputedStyle is not available (e.g. an old browser or some Node.js environments) or doesn't work (e.g. if the element is not inserted to a document)
  18274. return display === 'none';
  18275. }
  18276. /* global getComputedStyle */
  18277. function get_get_computed_style_function(element) {
  18278. // The proper getComputedStyle implementation is the one defined in the element window
  18279. if(element.ownerDocument.defaultView && typeof element.ownerDocument.defaultView.getComputedStyle === 'function') return element.ownerDocument.defaultView.getComputedStyle;
  18280. // If it is not available, try to get one from the global namespace
  18281. if(typeof getComputedStyle === 'function') return getComputedStyle;
  18282. return null;
  18283. }
  18284. /* OpenDocument */
  18285. var parse_content_xml = (function() {
  18286. var parse_text_p = function(text) {
  18287. /* 6.1.2 White Space Characters */
  18288. var fixed = text
  18289. .replace(/[\t\r\n]/g, " ").trim().replace(/ +/g, " ")
  18290. .replace(/<text:s\/>/g," ")
  18291. .replace(/<text:s text:c="(\d+)"\/>/g, function($$,$1) { return Array(parseInt($1,10)+1).join(" "); })
  18292. .replace(/<text:tab[^>]*\/>/g,"\t")
  18293. .replace(/<text:line-break\/>/g,"\n");
  18294. var v = unescapexml(fixed.replace(/<[^>]*>/g,""));
  18295. return [v];
  18296. };
  18297. var number_formats = {
  18298. /* ods name: [short ssf fmt, long ssf fmt] */
  18299. day: ["d", "dd"],
  18300. month: ["m", "mm"],
  18301. year: ["y", "yy"],
  18302. hours: ["h", "hh"],
  18303. minutes: ["m", "mm"],
  18304. seconds: ["s", "ss"],
  18305. "am-pm": ["A/P", "AM/PM"],
  18306. "day-of-week": ["ddd", "dddd"],
  18307. era: ["e", "ee"],
  18308. /* there is no native representation of LO "Q" format */
  18309. quarter: ["\\Qm", "m\\\"th quarter\""]
  18310. };
  18311. return function pcx(d, _opts) {
  18312. var opts = _opts || {};
  18313. if(DENSE != null && opts.dense == null) opts.dense = DENSE;
  18314. var str = xlml_normalize(d);
  18315. var state = [], tmp;
  18316. var tag;
  18317. var NFtag = {name:""}, NF = "", pidx = 0;
  18318. var sheetag;
  18319. var rowtag;
  18320. var Sheets = {}, SheetNames = [];
  18321. var ws = opts.dense ? ([]) : ({});
  18322. var Rn, q;
  18323. var ctag = ({value:""});
  18324. var textp = "", textpidx = 0, textptag;
  18325. var textR = [];
  18326. var R = -1, C = -1, range = {s: {r:1000000,c:10000000}, e: {r:0, c:0}};
  18327. var row_ol = 0;
  18328. var number_format_map = {};
  18329. var merges = [], mrange = {}, mR = 0, mC = 0;
  18330. var rowinfo = [], rowpeat = 1, colpeat = 1;
  18331. var arrayf = [];
  18332. var WB = {Names:[]};
  18333. var atag = ({});
  18334. var _Ref = ["", ""];
  18335. var comments = [], comment = ({});
  18336. var creator = "", creatoridx = 0;
  18337. var isstub = false, intable = false;
  18338. var i = 0;
  18339. xlmlregex.lastIndex = 0;
  18340. str = str.replace(/<!--([\s\S]*?)-->/mg,"").replace(/<!DOCTYPE[^\[]*\[[^\]]*\]>/gm,"");
  18341. while((Rn = xlmlregex.exec(str))) switch((Rn[3]=Rn[3].replace(/_.*$/,""))) {
  18342. case 'table': case '工作表': // 9.1.2 <table:table>
  18343. if(Rn[1]==='/') {
  18344. if(range.e.c >= range.s.c && range.e.r >= range.s.r) ws['!ref'] = encode_range(range);
  18345. else ws['!ref'] = "A1:A1";
  18346. if(opts.sheetRows > 0 && opts.sheetRows <= range.e.r) {
  18347. ws['!fullref'] = ws['!ref'];
  18348. range.e.r = opts.sheetRows - 1;
  18349. ws['!ref'] = encode_range(range);
  18350. }
  18351. if(merges.length) ws['!merges'] = merges;
  18352. if(rowinfo.length) ws["!rows"] = rowinfo;
  18353. sheetag.name = sheetag['名称'] || sheetag.name;
  18354. if(typeof JSON !== 'undefined') JSON.stringify(sheetag);
  18355. SheetNames.push(sheetag.name);
  18356. Sheets[sheetag.name] = ws;
  18357. intable = false;
  18358. }
  18359. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  18360. sheetag = parsexmltag(Rn[0], false);
  18361. R = C = -1;
  18362. range.s.r = range.s.c = 10000000; range.e.r = range.e.c = 0;
  18363. ws = opts.dense ? ([]) : ({}); merges = [];
  18364. rowinfo = [];
  18365. intable = true;
  18366. }
  18367. break;
  18368. case 'table-row-group': // 9.1.9 <table:table-row-group>
  18369. if(Rn[1] === "/") --row_ol; else ++row_ol;
  18370. break;
  18371. case 'table-row': case '行': // 9.1.3 <table:table-row>
  18372. if(Rn[1] === '/') { R+=rowpeat; rowpeat = 1; break; }
  18373. rowtag = parsexmltag(Rn[0], false);
  18374. if(rowtag['行号']) R = rowtag['行号'] - 1; else if(R == -1) R = 0;
  18375. rowpeat = +rowtag['number-rows-repeated'] || 1;
  18376. /* TODO: remove magic */
  18377. if(rowpeat < 10) for(i = 0; i < rowpeat; ++i) if(row_ol > 0) rowinfo[R + i] = {level: row_ol};
  18378. C = -1; break;
  18379. case 'covered-table-cell': // 9.1.5 <table:covered-table-cell>
  18380. if(Rn[1] !== '/') ++C;
  18381. if(opts.sheetStubs) {
  18382. if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = {t:'z'}; }
  18383. else ws[encode_cell({r:R,c:C})] = {t:'z'};
  18384. }
  18385. textp = ""; textR = [];
  18386. break; /* stub */
  18387. case 'table-cell': case '数据':
  18388. if(Rn[0].charAt(Rn[0].length-2) === '/') {
  18389. ++C;
  18390. ctag = parsexmltag(Rn[0], false);
  18391. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  18392. q = ({t:'z', v:null});
  18393. if(ctag.formula && opts.cellFormula != false) q.f = ods_to_csf_formula(unescapexml(ctag.formula));
  18394. if((ctag['数据类型'] || ctag['value-type']) == "string") {
  18395. q.t = "s"; q.v = unescapexml(ctag['string-value'] || "");
  18396. if(opts.dense) {
  18397. if(!ws[R]) ws[R] = [];
  18398. ws[R][C] = q;
  18399. } else {
  18400. ws[encode_cell({r:R,c:C})] = q;
  18401. }
  18402. }
  18403. C+= colpeat-1;
  18404. } else if(Rn[1]!=='/') {
  18405. ++C;
  18406. colpeat = 1;
  18407. var rptR = rowpeat ? R + rowpeat - 1 : R;
  18408. if(C > range.e.c) range.e.c = C;
  18409. if(C < range.s.c) range.s.c = C;
  18410. if(R < range.s.r) range.s.r = R;
  18411. if(rptR > range.e.r) range.e.r = rptR;
  18412. ctag = parsexmltag(Rn[0], false);
  18413. comments = []; comment = ({});
  18414. q = ({t:ctag['数据类型'] || ctag['value-type'], v:null});
  18415. if(opts.cellFormula) {
  18416. if(ctag.formula) ctag.formula = unescapexml(ctag.formula);
  18417. if(ctag['number-matrix-columns-spanned'] && ctag['number-matrix-rows-spanned']) {
  18418. mR = parseInt(ctag['number-matrix-rows-spanned'],10) || 0;
  18419. mC = parseInt(ctag['number-matrix-columns-spanned'],10) || 0;
  18420. mrange = {s: {r:R,c:C}, e:{r:R + mR-1,c:C + mC-1}};
  18421. q.F = encode_range(mrange);
  18422. arrayf.push([mrange, q.F]);
  18423. }
  18424. if(ctag.formula) q.f = ods_to_csf_formula(ctag.formula);
  18425. else for(i = 0; i < arrayf.length; ++i)
  18426. if(R >= arrayf[i][0].s.r && R <= arrayf[i][0].e.r)
  18427. if(C >= arrayf[i][0].s.c && C <= arrayf[i][0].e.c)
  18428. q.F = arrayf[i][1];
  18429. }
  18430. if(ctag['number-columns-spanned'] || ctag['number-rows-spanned']) {
  18431. mR = parseInt(ctag['number-rows-spanned'],10) || 0;
  18432. mC = parseInt(ctag['number-columns-spanned'],10) || 0;
  18433. mrange = {s: {r:R,c:C}, e:{r:R + mR-1,c:C + mC-1}};
  18434. merges.push(mrange);
  18435. }
  18436. /* 19.675.2 table:number-columns-repeated */
  18437. if(ctag['number-columns-repeated']) colpeat = parseInt(ctag['number-columns-repeated'], 10);
  18438. /* 19.385 office:value-type */
  18439. switch(q.t) {
  18440. case 'boolean': q.t = 'b'; q.v = parsexmlbool(ctag['boolean-value']); break;
  18441. case 'float': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  18442. case 'percentage': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  18443. case 'currency': q.t = 'n'; q.v = parseFloat(ctag.value); break;
  18444. case 'date': q.t = 'd'; q.v = parseDate(ctag['date-value']);
  18445. if(!opts.cellDates) { q.t = 'n'; q.v = datenum(q.v); }
  18446. q.z = 'm/d/yy'; break;
  18447. case 'time': q.t = 'n'; q.v = parse_isodur(ctag['time-value'])/86400; break;
  18448. case 'number': q.t = 'n'; q.v = parseFloat(ctag['数据数值']); break;
  18449. default:
  18450. if(q.t === 'string' || q.t === 'text' || !q.t) {
  18451. q.t = 's';
  18452. if(ctag['string-value'] != null) { textp = unescapexml(ctag['string-value']); textR = []; }
  18453. } else throw new Error('Unsupported value type ' + q.t);
  18454. }
  18455. } else {
  18456. isstub = false;
  18457. if(q.t === 's') {
  18458. q.v = textp || '';
  18459. if(textR.length) q.R = textR;
  18460. isstub = textpidx == 0;
  18461. }
  18462. if(atag.Target) q.l = atag;
  18463. if(comments.length > 0) { q.c = comments; comments = []; }
  18464. if(textp && opts.cellText !== false) q.w = textp;
  18465. if(isstub) { q.t = "z"; delete q.v; }
  18466. if(!isstub || opts.sheetStubs) {
  18467. if(!(opts.sheetRows && opts.sheetRows <= R)) {
  18468. for(var rpt = 0; rpt < rowpeat; ++rpt) {
  18469. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  18470. if(opts.dense) {
  18471. if(!ws[R + rpt]) ws[R + rpt] = [];
  18472. ws[R + rpt][C] = rpt == 0 ? q : dup(q);
  18473. while(--colpeat > 0) ws[R + rpt][C + colpeat] = dup(q);
  18474. } else {
  18475. ws[encode_cell({r:R + rpt,c:C})] = q;
  18476. while(--colpeat > 0) ws[encode_cell({r:R + rpt,c:C + colpeat})] = dup(q);
  18477. }
  18478. if(range.e.c <= C) range.e.c = C;
  18479. }
  18480. }
  18481. }
  18482. colpeat = parseInt(ctag['number-columns-repeated']||"1", 10);
  18483. C += colpeat-1; colpeat = 0;
  18484. q = {};
  18485. textp = ""; textR = [];
  18486. }
  18487. atag = ({});
  18488. break; // 9.1.4 <table:table-cell>
  18489. /* pure state */
  18490. case 'document': // TODO: <office:document> is the root for FODS
  18491. case 'document-content': case '电子表格文档': // 3.1.3.2 <office:document-content>
  18492. case 'spreadsheet': case '主体': // 3.7 <office:spreadsheet>
  18493. case 'scripts': // 3.12 <office:scripts>
  18494. case 'styles': // TODO <office:styles>
  18495. case 'font-face-decls': // 3.14 <office:font-face-decls>
  18496. case 'master-styles': //3.15.4 <office:master-styles> -- relevant for FODS
  18497. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;}
  18498. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], true]);
  18499. break;
  18500. case 'annotation': // 14.1 <office:annotation>
  18501. if(Rn[1]==='/'){
  18502. if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;
  18503. comment.t = textp;
  18504. if(textR.length) comment.R = textR;
  18505. comment.a = creator;
  18506. comments.push(comment);
  18507. }
  18508. else if(Rn[0].charAt(Rn[0].length-2) !== '/') {state.push([Rn[3], false]);}
  18509. creator = ""; creatoridx = 0;
  18510. textp = ""; textpidx = 0; textR = [];
  18511. break;
  18512. case 'creator': // 4.3.2.7 <dc:creator>
  18513. if(Rn[1]==='/') { creator = str.slice(creatoridx,Rn.index); }
  18514. else creatoridx = Rn.index + Rn[0].length;
  18515. break;
  18516. /* ignore state */
  18517. case 'meta': case '元数据': // TODO: <office:meta> <uof:元数据> FODS/UOF
  18518. case 'settings': // TODO: <office:settings>
  18519. case 'config-item-set': // TODO: <office:config-item-set>
  18520. case 'config-item-map-indexed': // TODO: <office:config-item-map-indexed>
  18521. case 'config-item-map-entry': // TODO: <office:config-item-map-entry>
  18522. case 'config-item-map-named': // TODO: <office:config-item-map-entry>
  18523. case 'shapes': // 9.2.8 <table:shapes>
  18524. case 'frame': // 10.4.2 <draw:frame>
  18525. case 'text-box': // 10.4.3 <draw:text-box>
  18526. case 'image': // 10.4.4 <draw:image>
  18527. case 'data-pilot-tables': // 9.6.2 <table:data-pilot-tables>
  18528. case 'list-style': // 16.30 <text:list-style>
  18529. case 'form': // 13.13 <form:form>
  18530. case 'dde-links': // 9.8 <table:dde-links>
  18531. case 'event-listeners': // TODO
  18532. case 'chart': // TODO
  18533. if(Rn[1]==='/'){if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;}
  18534. else if(Rn[0].charAt(Rn[0].length-2) !== '/') state.push([Rn[3], false]);
  18535. textp = ""; textpidx = 0; textR = [];
  18536. break;
  18537. case 'scientific-number': // TODO: <number:scientific-number>
  18538. break;
  18539. case 'currency-symbol': // TODO: <number:currency-symbol>
  18540. break;
  18541. case 'currency-style': // TODO: <number:currency-style>
  18542. break;
  18543. case 'number-style': // 16.27.2 <number:number-style>
  18544. case 'percentage-style': // 16.27.9 <number:percentage-style>
  18545. case 'date-style': // 16.27.10 <number:date-style>
  18546. case 'time-style': // 16.27.18 <number:time-style>
  18547. if(Rn[1]==='/'){
  18548. number_format_map[NFtag.name] = NF;
  18549. if((tmp=state.pop())[0]!==Rn[3]) throw "Bad state: "+tmp;
  18550. } else if(Rn[0].charAt(Rn[0].length-2) !== '/') {
  18551. NF = "";
  18552. NFtag = parsexmltag(Rn[0], false);
  18553. state.push([Rn[3], true]);
  18554. } break;
  18555. case 'script': break; // 3.13 <office:script>
  18556. case 'libraries': break; // TODO: <ooo:libraries>
  18557. case 'automatic-styles': break; // 3.15.3 <office:automatic-styles>
  18558. case 'default-style': // TODO: <style:default-style>
  18559. case 'page-layout': break; // TODO: <style:page-layout>
  18560. case 'style': // 16.2 <style:style>
  18561. break;
  18562. case 'map': break; // 16.3 <style:map>
  18563. case 'font-face': break; // 16.21 <style:font-face>
  18564. case 'paragraph-properties': break; // 17.6 <style:paragraph-properties>
  18565. case 'table-properties': break; // 17.15 <style:table-properties>
  18566. case 'table-column-properties': break; // 17.16 <style:table-column-properties>
  18567. case 'table-row-properties': break; // 17.17 <style:table-row-properties>
  18568. case 'table-cell-properties': break; // 17.18 <style:table-cell-properties>
  18569. case 'number': // 16.27.3 <number:number>
  18570. switch(state[state.length-1][0]) {
  18571. case 'time-style':
  18572. case 'date-style':
  18573. tag = parsexmltag(Rn[0], false);
  18574. NF += number_formats[Rn[3]][tag.style==='long'?1:0]; break;
  18575. } break;
  18576. case 'fraction': break; // TODO 16.27.6 <number:fraction>
  18577. case 'day': // 16.27.11 <number:day>
  18578. case 'month': // 16.27.12 <number:month>
  18579. case 'year': // 16.27.13 <number:year>
  18580. case 'era': // 16.27.14 <number:era>
  18581. case 'day-of-week': // 16.27.15 <number:day-of-week>
  18582. case 'week-of-year': // 16.27.16 <number:week-of-year>
  18583. case 'quarter': // 16.27.17 <number:quarter>
  18584. case 'hours': // 16.27.19 <number:hours>
  18585. case 'minutes': // 16.27.20 <number:minutes>
  18586. case 'seconds': // 16.27.21 <number:seconds>
  18587. case 'am-pm': // 16.27.22 <number:am-pm>
  18588. switch(state[state.length-1][0]) {
  18589. case 'time-style':
  18590. case 'date-style':
  18591. tag = parsexmltag(Rn[0], false);
  18592. NF += number_formats[Rn[3]][tag.style==='long'?1:0]; break;
  18593. } break;
  18594. case 'boolean-style': break; // 16.27.23 <number:boolean-style>
  18595. case 'boolean': break; // 16.27.24 <number:boolean>
  18596. case 'text-style': break; // 16.27.25 <number:text-style>
  18597. case 'text': // 16.27.26 <number:text>
  18598. if(Rn[0].slice(-2) === "/>") break;
  18599. else if(Rn[1]==="/") switch(state[state.length-1][0]) {
  18600. case 'number-style':
  18601. case 'date-style':
  18602. case 'time-style':
  18603. NF += str.slice(pidx, Rn.index);
  18604. break;
  18605. }
  18606. else pidx = Rn.index + Rn[0].length;
  18607. break;
  18608. case 'named-range': // 9.4.12 <table:named-range>
  18609. tag = parsexmltag(Rn[0], false);
  18610. _Ref = ods_to_csf_3D(tag['cell-range-address']);
  18611. var nrange = ({Name:tag.name, Ref:_Ref[0] + '!' + _Ref[1]});
  18612. if(intable) nrange.Sheet = SheetNames.length;
  18613. WB.Names.push(nrange);
  18614. break;
  18615. case 'text-content': break; // 16.27.27 <number:text-content>
  18616. case 'text-properties': break; // 16.27.27 <style:text-properties>
  18617. case 'embedded-text': break; // 16.27.4 <number:embedded-text>
  18618. case 'body': case '电子表格': break; // 3.3 16.9.6 19.726.3
  18619. case 'forms': break; // 12.25.2 13.2
  18620. case 'table-column': break; // 9.1.6 <table:table-column>
  18621. case 'table-header-rows': break; // 9.1.7 <table:table-header-rows>
  18622. case 'table-rows': break; // 9.1.12 <table:table-rows>
  18623. /* TODO: outline levels */
  18624. case 'table-column-group': break; // 9.1.10 <table:table-column-group>
  18625. case 'table-header-columns': break; // 9.1.11 <table:table-header-columns>
  18626. case 'table-columns': break; // 9.1.12 <table:table-columns>
  18627. case 'null-date': break; // 9.4.2 <table:null-date> TODO: date1904
  18628. case 'graphic-properties': break; // 17.21 <style:graphic-properties>
  18629. case 'calculation-settings': break; // 9.4.1 <table:calculation-settings>
  18630. case 'named-expressions': break; // 9.4.11 <table:named-expressions>
  18631. case 'label-range': break; // 9.4.9 <table:label-range>
  18632. case 'label-ranges': break; // 9.4.10 <table:label-ranges>
  18633. case 'named-expression': break; // 9.4.13 <table:named-expression>
  18634. case 'sort': break; // 9.4.19 <table:sort>
  18635. case 'sort-by': break; // 9.4.20 <table:sort-by>
  18636. case 'sort-groups': break; // 9.4.22 <table:sort-groups>
  18637. case 'tab': break; // 6.1.4 <text:tab>
  18638. case 'line-break': break; // 6.1.5 <text:line-break>
  18639. case 'span': break; // 6.1.7 <text:span>
  18640. case 'p': case '文本串': // 5.1.3 <text:p>
  18641. if(['master-styles'].indexOf(state[state.length-1][0]) > -1) break;
  18642. if(Rn[1]==='/' && (!ctag || !ctag['string-value'])) {
  18643. var ptp = parse_text_p(str.slice(textpidx,Rn.index), textptag);
  18644. textp = (textp.length > 0 ? textp + "\n" : "") + ptp[0];
  18645. } else { textptag = parsexmltag(Rn[0], false); textpidx = Rn.index + Rn[0].length; }
  18646. break; // <text:p>
  18647. case 's': break; // <text:s>
  18648. case 'database-range': // 9.4.15 <table:database-range>
  18649. if(Rn[1]==='/') break;
  18650. try {
  18651. _Ref = ods_to_csf_3D(parsexmltag(Rn[0])['target-range-address']);
  18652. Sheets[_Ref[0]]['!autofilter'] = { ref:_Ref[1] };
  18653. } catch(e) {/* empty */}
  18654. break;
  18655. case 'date': break; // <*:date>
  18656. case 'object': break; // 10.4.6.2 <draw:object>
  18657. case 'title': case '标题': break; // <*:title> OR <uof:标题>
  18658. case 'desc': break; // <*:desc>
  18659. case 'binary-data': break; // 10.4.5 TODO: b64 blob
  18660. /* 9.2 Advanced Tables */
  18661. case 'table-source': break; // 9.2.6
  18662. case 'scenario': break; // 9.2.6
  18663. case 'iteration': break; // 9.4.3 <table:iteration>
  18664. case 'content-validations': break; // 9.4.4 <table:
  18665. case 'content-validation': break; // 9.4.5 <table:
  18666. case 'help-message': break; // 9.4.6 <table:
  18667. case 'error-message': break; // 9.4.7 <table:
  18668. case 'database-ranges': break; // 9.4.14 <table:database-ranges>
  18669. case 'filter': break; // 9.5.2 <table:filter>
  18670. case 'filter-and': break; // 9.5.3 <table:filter-and>
  18671. case 'filter-or': break; // 9.5.4 <table:filter-or>
  18672. case 'filter-condition': break; // 9.5.5 <table:filter-condition>
  18673. case 'list-level-style-bullet': break; // 16.31 <text:
  18674. case 'list-level-style-number': break; // 16.32 <text:
  18675. case 'list-level-properties': break; // 17.19 <style:
  18676. /* 7.3 Document Fields */
  18677. case 'sender-firstname': // 7.3.6.2
  18678. case 'sender-lastname': // 7.3.6.3
  18679. case 'sender-initials': // 7.3.6.4
  18680. case 'sender-title': // 7.3.6.5
  18681. case 'sender-position': // 7.3.6.6
  18682. case 'sender-email': // 7.3.6.7
  18683. case 'sender-phone-private': // 7.3.6.8
  18684. case 'sender-fax': // 7.3.6.9
  18685. case 'sender-company': // 7.3.6.10
  18686. case 'sender-phone-work': // 7.3.6.11
  18687. case 'sender-street': // 7.3.6.12
  18688. case 'sender-city': // 7.3.6.13
  18689. case 'sender-postal-code': // 7.3.6.14
  18690. case 'sender-country': // 7.3.6.15
  18691. case 'sender-state-or-province': // 7.3.6.16
  18692. case 'author-name': // 7.3.7.1
  18693. case 'author-initials': // 7.3.7.2
  18694. case 'chapter': // 7.3.8
  18695. case 'file-name': // 7.3.9
  18696. case 'template-name': // 7.3.9
  18697. case 'sheet-name': // 7.3.9
  18698. break;
  18699. case 'event-listener':
  18700. break;
  18701. /* TODO: FODS Properties */
  18702. case 'initial-creator':
  18703. case 'creation-date':
  18704. case 'print-date':
  18705. case 'generator':
  18706. case 'document-statistic':
  18707. case 'user-defined':
  18708. case 'editing-duration':
  18709. case 'editing-cycles':
  18710. break;
  18711. /* TODO: FODS Config */
  18712. case 'config-item':
  18713. break;
  18714. /* TODO: style tokens */
  18715. case 'page-number': break; // TODO <text:page-number>
  18716. case 'page-count': break; // TODO <text:page-count>
  18717. case 'time': break; // TODO <text:time>
  18718. /* 9.3 Advanced Table Cells */
  18719. case 'cell-range-source': break; // 9.3.1 <table:
  18720. case 'detective': break; // 9.3.2 <table:
  18721. case 'operation': break; // 9.3.3 <table:
  18722. case 'highlighted-range': break; // 9.3.4 <table:
  18723. /* 9.6 Data Pilot Tables <table: */
  18724. case 'data-pilot-table': // 9.6.3
  18725. case 'source-cell-range': // 9.6.5
  18726. case 'source-service': // 9.6.6
  18727. case 'data-pilot-field': // 9.6.7
  18728. case 'data-pilot-level': // 9.6.8
  18729. case 'data-pilot-subtotals': // 9.6.9
  18730. case 'data-pilot-subtotal': // 9.6.10
  18731. case 'data-pilot-members': // 9.6.11
  18732. case 'data-pilot-member': // 9.6.12
  18733. case 'data-pilot-display-info': // 9.6.13
  18734. case 'data-pilot-sort-info': // 9.6.14
  18735. case 'data-pilot-layout-info': // 9.6.15
  18736. case 'data-pilot-field-reference': // 9.6.16
  18737. case 'data-pilot-groups': // 9.6.17
  18738. case 'data-pilot-group': // 9.6.18
  18739. case 'data-pilot-group-member': // 9.6.19
  18740. break;
  18741. /* 10.3 Drawing Shapes */
  18742. case 'rect': // 10.3.2
  18743. break;
  18744. /* 14.6 DDE Connections */
  18745. case 'dde-connection-decls': // 14.6.2 <text:
  18746. case 'dde-connection-decl': // 14.6.3 <text:
  18747. case 'dde-link': // 14.6.4 <table:
  18748. case 'dde-source': // 14.6.5 <office:
  18749. break;
  18750. case 'properties': break; // 13.7 <form:properties>
  18751. case 'property': break; // 13.8 <form:property>
  18752. case 'a': // 6.1.8 hyperlink
  18753. if(Rn[1]!== '/') {
  18754. atag = parsexmltag(Rn[0], false);
  18755. if(!atag.href) break;
  18756. atag.Target = atag.href; delete atag.href;
  18757. if(atag.Target.charAt(0) == "#" && atag.Target.indexOf(".") > -1) {
  18758. _Ref = ods_to_csf_3D(atag.Target.slice(1));
  18759. atag.Target = "#" + _Ref[0] + "!" + _Ref[1];
  18760. }
  18761. }
  18762. break;
  18763. /* non-standard */
  18764. case 'table-protection': break;
  18765. case 'data-pilot-grand-total': break; // <table:
  18766. case 'office-document-common-attrs': break; // bare
  18767. default: switch(Rn[2]) {
  18768. case 'dc:': // TODO: properties
  18769. case 'calcext:': // ignore undocumented extensions
  18770. case 'loext:': // ignore undocumented extensions
  18771. case 'ooo:': // ignore undocumented extensions
  18772. case 'chartooo:': // ignore undocumented extensions
  18773. case 'draw:': // TODO: drawing
  18774. case 'style:': // TODO: styles
  18775. case 'chart:': // TODO: charts
  18776. case 'form:': // TODO: forms
  18777. case 'uof:': // TODO: uof
  18778. case '表:': // TODO: uof
  18779. case '字:': // TODO: uof
  18780. break;
  18781. default: if(opts.WTF) throw new Error(Rn);
  18782. }
  18783. }
  18784. var out = ({
  18785. Sheets: Sheets,
  18786. SheetNames: SheetNames,
  18787. Workbook: WB
  18788. });
  18789. if(opts.bookSheets) delete out.Sheets;
  18790. return out;
  18791. };
  18792. })();
  18793. function parse_ods(zip, opts) {
  18794. opts = opts || ({});
  18795. var ods = !!safegetzipfile(zip, 'objectdata');
  18796. if(ods) parse_manifest(getzipdata(zip, 'META-INF/manifest.xml'), opts);
  18797. var content = getzipstr(zip, 'content.xml');
  18798. if(!content) throw new Error("Missing content.xml in " + (ods ? "ODS" : "UOF")+ " file");
  18799. var wb = parse_content_xml(ods ? content : utf8read(content), opts);
  18800. if(safegetzipfile(zip, 'meta.xml')) wb.Props = parse_core_props(getzipdata(zip, 'meta.xml'));
  18801. return wb;
  18802. }
  18803. function parse_fods(data, opts) {
  18804. return parse_content_xml(data, opts);
  18805. }
  18806. /* OpenDocument */
  18807. var write_styles_ods = (function() {
  18808. var payload = '<office:document-styles ' + wxt_helper({
  18809. 'xmlns:office': "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
  18810. 'xmlns:table': "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
  18811. 'xmlns:style': "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  18812. 'xmlns:text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
  18813. 'xmlns:draw': "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
  18814. 'xmlns:fo': "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
  18815. 'xmlns:xlink': "http://www.w3.org/1999/xlink",
  18816. 'xmlns:dc': "http://purl.org/dc/elements/1.1/",
  18817. 'xmlns:number': "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
  18818. 'xmlns:svg': "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
  18819. 'xmlns:of': "urn:oasis:names:tc:opendocument:xmlns:of:1.2",
  18820. 'office:version': "1.2"
  18821. }) + '></office:document-styles>';
  18822. return function wso() {
  18823. return XML_HEADER + payload;
  18824. };
  18825. })();
  18826. var write_content_ods = (function() {
  18827. /* 6.1.2 White Space Characters */
  18828. var write_text_p = function(text) {
  18829. return escapexml(text)
  18830. .replace(/ +/g, function($$){return '<text:s text:c="'+$$.length+'"/>';})
  18831. .replace(/\t/g, "<text:tab/>")
  18832. .replace(/\n/g, "<text:line-break/>")
  18833. .replace(/^ /, "<text:s/>").replace(/ $/, "<text:s/>");
  18834. };
  18835. var null_cell_xml = ' <table:table-cell />\n';
  18836. var covered_cell_xml = ' <table:covered-table-cell/>\n';
  18837. var write_ws = function(ws, wb, i) {
  18838. /* Section 9 Tables */
  18839. var o = [];
  18840. o.push(' <table:table table:name="' + escapexml(wb.SheetNames[i]) + '" table:style-name="ta1">\n');
  18841. var R=0,C=0, range = decode_range(ws['!ref']);
  18842. var marr = ws['!merges'] || [], mi = 0;
  18843. var dense = Array.isArray(ws);
  18844. for(R = 0; R < range.s.r; ++R) o.push(' <table:table-row></table:table-row>\n');
  18845. for(; R <= range.e.r; ++R) {
  18846. o.push(' <table:table-row>\n');
  18847. for(C=0; C < range.s.c; ++C) o.push(null_cell_xml);
  18848. for(; C <= range.e.c; ++C) {
  18849. var skip = false, ct = {}, textp = "";
  18850. for(mi = 0; mi != marr.length; ++mi) {
  18851. if(marr[mi].s.c > C) continue;
  18852. if(marr[mi].s.r > R) continue;
  18853. if(marr[mi].e.c < C) continue;
  18854. if(marr[mi].e.r < R) continue;
  18855. if(marr[mi].s.c != C || marr[mi].s.r != R) skip = true;
  18856. ct['table:number-columns-spanned'] = (marr[mi].e.c - marr[mi].s.c + 1);
  18857. ct['table:number-rows-spanned'] = (marr[mi].e.r - marr[mi].s.r + 1);
  18858. break;
  18859. }
  18860. if(skip) { o.push(covered_cell_xml); continue; }
  18861. var ref = encode_cell({r:R, c:C}), cell = dense ? (ws[R]||[])[C]: ws[ref];
  18862. if(cell && cell.f) {
  18863. ct['table:formula'] = escapexml(csf_to_ods_formula(cell.f));
  18864. if(cell.F) {
  18865. if(cell.F.slice(0, ref.length) == ref) {
  18866. var _Fref = decode_range(cell.F);
  18867. ct['table:number-matrix-columns-spanned'] = (_Fref.e.c - _Fref.s.c + 1);
  18868. ct['table:number-matrix-rows-spanned'] = (_Fref.e.r - _Fref.s.r + 1);
  18869. }
  18870. }
  18871. }
  18872. if(!cell) { o.push(null_cell_xml); continue; }
  18873. switch(cell.t) {
  18874. case 'b':
  18875. textp = (cell.v ? 'TRUE' : 'FALSE');
  18876. ct['office:value-type'] = "boolean";
  18877. ct['office:boolean-value'] = (cell.v ? 'true' : 'false');
  18878. break;
  18879. case 'n':
  18880. textp = (cell.w||String(cell.v||0));
  18881. ct['office:value-type'] = "float";
  18882. ct['office:value'] = (cell.v||0);
  18883. break;
  18884. case 's': case 'str':
  18885. textp = cell.v == null ? "" : cell.v;
  18886. ct['office:value-type'] = "string";
  18887. break;
  18888. case 'd':
  18889. textp = (cell.w||(parseDate(cell.v).toISOString()));
  18890. ct['office:value-type'] = "date";
  18891. ct['office:date-value'] = (parseDate(cell.v).toISOString());
  18892. ct['table:style-name'] = "ce1";
  18893. break;
  18894. //case 'e':
  18895. default: o.push(null_cell_xml); continue;
  18896. }
  18897. var text_p = write_text_p(textp);
  18898. if(cell.l && cell.l.Target) {
  18899. var _tgt = cell.l.Target; _tgt = _tgt.charAt(0) == "#" ? "#" + csf_to_ods_3D(_tgt.slice(1)) : _tgt;
  18900. text_p = writextag('text:a', text_p, {'xlink:href': _tgt});
  18901. }
  18902. o.push(' ' + writextag('table:table-cell', writextag('text:p', text_p, {}), ct) + '\n');
  18903. }
  18904. o.push(' </table:table-row>\n');
  18905. }
  18906. o.push(' </table:table>\n');
  18907. return o.join("");
  18908. };
  18909. var write_automatic_styles_ods = function(o) {
  18910. o.push(' <office:automatic-styles>\n');
  18911. o.push(' <number:date-style style:name="N37" number:automatic-order="true">\n');
  18912. o.push(' <number:month number:style="long"/>\n');
  18913. o.push(' <number:text>/</number:text>\n');
  18914. o.push(' <number:day number:style="long"/>\n');
  18915. o.push(' <number:text>/</number:text>\n');
  18916. o.push(' <number:year/>\n');
  18917. o.push(' </number:date-style>\n');
  18918. /* table */
  18919. o.push(' <style:style style:name="ta1" style:family="table">\n'); // style:master-page-name="mp1">\n');
  18920. o.push(' <style:table-properties table:display="true" style:writing-mode="lr-tb"/>\n');
  18921. o.push(' </style:style>\n');
  18922. /* table cells, text */
  18923. o.push(' <style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N37"/>\n');
  18924. /* page-layout */
  18925. o.push(' </office:automatic-styles>\n');
  18926. };
  18927. return function wcx(wb, opts) {
  18928. var o = [XML_HEADER];
  18929. /* 3.1.3.2 */
  18930. var attr = wxt_helper({
  18931. 'xmlns:office': "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
  18932. 'xmlns:table': "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
  18933. 'xmlns:style': "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  18934. 'xmlns:text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
  18935. 'xmlns:draw': "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
  18936. 'xmlns:fo': "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
  18937. 'xmlns:xlink': "http://www.w3.org/1999/xlink",
  18938. 'xmlns:dc': "http://purl.org/dc/elements/1.1/",
  18939. 'xmlns:meta': "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
  18940. 'xmlns:number': "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
  18941. 'xmlns:presentation': "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
  18942. 'xmlns:svg': "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
  18943. 'xmlns:chart': "urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
  18944. 'xmlns:dr3d': "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
  18945. 'xmlns:math': "http://www.w3.org/1998/Math/MathML",
  18946. 'xmlns:form': "urn:oasis:names:tc:opendocument:xmlns:form:1.0",
  18947. 'xmlns:script': "urn:oasis:names:tc:opendocument:xmlns:script:1.0",
  18948. 'xmlns:ooo': "http://openoffice.org/2004/office",
  18949. 'xmlns:ooow': "http://openoffice.org/2004/writer",
  18950. 'xmlns:oooc': "http://openoffice.org/2004/calc",
  18951. 'xmlns:dom': "http://www.w3.org/2001/xml-events",
  18952. 'xmlns:xforms': "http://www.w3.org/2002/xforms",
  18953. 'xmlns:xsd': "http://www.w3.org/2001/XMLSchema",
  18954. 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
  18955. 'xmlns:sheet': "urn:oasis:names:tc:opendocument:sh33tjs:1.0",
  18956. 'xmlns:rpt': "http://openoffice.org/2005/report",
  18957. 'xmlns:of': "urn:oasis:names:tc:opendocument:xmlns:of:1.2",
  18958. 'xmlns:xhtml': "http://www.w3.org/1999/xhtml",
  18959. 'xmlns:grddl': "http://www.w3.org/2003/g/data-view#",
  18960. 'xmlns:tableooo': "http://openoffice.org/2009/table",
  18961. 'xmlns:drawooo': "http://openoffice.org/2010/draw",
  18962. 'xmlns:calcext': "urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0",
  18963. 'xmlns:loext': "urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0",
  18964. 'xmlns:field': "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0",
  18965. 'xmlns:formx': "urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0",
  18966. 'xmlns:css3t': "http://www.w3.org/TR/css3-text/",
  18967. 'office:version': "1.2"
  18968. });
  18969. var fods = wxt_helper({
  18970. 'xmlns:config': "urn:oasis:names:tc:opendocument:xmlns:config:1.0",
  18971. 'office:mimetype': "application/vnd.oasis.opendocument.spreadsheet"
  18972. });
  18973. if(opts.bookType == "fods") o.push('<office:document' + attr + fods + '>\n');
  18974. else o.push('<office:document-content' + attr + '>\n');
  18975. write_automatic_styles_ods(o);
  18976. o.push(' <office:body>\n');
  18977. o.push(' <office:spreadsheet>\n');
  18978. for(var i = 0; i != wb.SheetNames.length; ++i) o.push(write_ws(wb.Sheets[wb.SheetNames[i]], wb, i, opts));
  18979. o.push(' </office:spreadsheet>\n');
  18980. o.push(' </office:body>\n');
  18981. if(opts.bookType == "fods") o.push('</office:document>');
  18982. else o.push('</office:document-content>');
  18983. return o.join("");
  18984. };
  18985. })();
  18986. function write_ods(wb, opts) {
  18987. if(opts.bookType == "fods") return write_content_ods(wb, opts);
  18988. var zip = zip_new();
  18989. var f = "";
  18990. var manifest = [];
  18991. var rdf = [];
  18992. /* Part 3 Section 3.3 MIME Media Type */
  18993. f = "mimetype";
  18994. zip_add_file(zip, f, "application/vnd.oasis.opendocument.spreadsheet");
  18995. /* Part 1 Section 2.2 Documents */
  18996. f = "content.xml";
  18997. zip_add_file(zip, f, write_content_ods(wb, opts));
  18998. manifest.push([f, "text/xml"]);
  18999. rdf.push([f, "ContentFile"]);
  19000. /* TODO: these are hard-coded styles to satiate excel */
  19001. f = "styles.xml";
  19002. zip_add_file(zip, f, write_styles_ods(wb, opts));
  19003. manifest.push([f, "text/xml"]);
  19004. rdf.push([f, "StylesFile"]);
  19005. /* TODO: this is hard-coded to satiate excel */
  19006. f = "meta.xml";
  19007. zip_add_file(zip, f, write_meta_ods());
  19008. manifest.push([f, "text/xml"]);
  19009. rdf.push([f, "MetadataFile"]);
  19010. /* Part 3 Section 6 Metadata Manifest File */
  19011. f = "manifest.rdf";
  19012. zip_add_file(zip, f, write_rdf(rdf/*, opts*/));
  19013. manifest.push([f, "application/rdf+xml"]);
  19014. /* Part 3 Section 4 Manifest File */
  19015. f = "META-INF/manifest.xml";
  19016. zip_add_file(zip, f, write_manifest(manifest/*, opts*/));
  19017. return zip;
  19018. }
  19019. function write_sheet_index(wb, sheet) {
  19020. if(!sheet) return 0;
  19021. var idx = wb.SheetNames.indexOf(sheet);
  19022. if(idx == -1) throw new Error("Sheet not found: " + sheet);
  19023. return idx;
  19024. }
  19025. function write_obj_str(factory) {
  19026. return function write_str(wb, o) {
  19027. var idx = write_sheet_index(wb, o.sheet);
  19028. return factory.from_sheet(wb.Sheets[wb.SheetNames[idx]], o, wb);
  19029. };
  19030. }
  19031. var write_htm_str = write_obj_str(HTML_);
  19032. var write_csv_str = write_obj_str({from_sheet:sheet_to_csv});
  19033. var write_slk_str = write_obj_str(typeof SYLK !== "undefined" ? SYLK : {});
  19034. var write_dif_str = write_obj_str(typeof DIF !== "undefined" ? DIF : {});
  19035. var write_prn_str = write_obj_str(typeof PRN !== "undefined" ? PRN : {});
  19036. var write_rtf_str = write_obj_str(typeof RTF !== "undefined" ? RTF : {});
  19037. var write_txt_str = write_obj_str({from_sheet:sheet_to_txt});
  19038. var write_dbf_buf = write_obj_str(typeof DBF !== "undefined" ? DBF : {});
  19039. var write_eth_str = write_obj_str(typeof ETH !== "undefined" ? ETH : {});
  19040. function fix_opts_func(defaults) {
  19041. return function fix_opts(opts) {
  19042. for(var i = 0; i != defaults.length; ++i) {
  19043. var d = defaults[i];
  19044. if(opts[d[0]] === undefined) opts[d[0]] = d[1];
  19045. if(d[2] === 'n') opts[d[0]] = Number(opts[d[0]]);
  19046. }
  19047. };
  19048. }
  19049. var fix_read_opts = function(opts) {
  19050. fix_opts_func([
  19051. ['cellNF', false], /* emit cell number format string as .z */
  19052. ['cellHTML', true], /* emit html string as .h */
  19053. ['cellFormula', true], /* emit formulae as .f */
  19054. ['cellStyles', false], /* emits style/theme as .s */
  19055. ['cellText', true], /* emit formatted text as .w */
  19056. ['cellDates', false], /* emit date cells with type `d` */
  19057. ['sheetStubs', false], /* emit empty cells */
  19058. ['sheetRows', 0, 'n'], /* read n rows (0 = read all rows) */
  19059. ['bookDeps', false], /* parse calculation chains */
  19060. ['bookSheets', false], /* only try to get sheet names (no Sheets) */
  19061. ['bookProps', false], /* only try to get properties (no Sheets) */
  19062. ['bookFiles', false], /* include raw file structure (keys, files, cfb) */
  19063. ['bookVBA', false], /* include vba raw data (vbaraw) */
  19064. ['password',''], /* password */
  19065. ['WTF', false] /* WTF mode (throws errors) */
  19066. ])(opts);
  19067. };
  19068. var fix_write_opts = fix_opts_func([
  19069. ['cellDates', false], /* write date cells with type `d` */
  19070. ['bookSST', false], /* Generate Shared String Table */
  19071. ['bookType', 'xlsx'], /* Type of workbook (xlsx/m/b) */
  19072. ['compression', false], /* Use file compression */
  19073. ['WTF', false] /* WTF mode (throws errors) */
  19074. ]);
  19075. function get_sheet_type(n) {
  19076. if(RELS.WS.indexOf(n) > -1) return "sheet";
  19077. if(RELS.CS && n == RELS.CS) return "chart";
  19078. if(RELS.DS && n == RELS.DS) return "dialog";
  19079. if(RELS.MS && n == RELS.MS) return "macro";
  19080. return (n && n.length) ? n : "sheet";
  19081. }
  19082. function safe_parse_wbrels(wbrels, sheets) {
  19083. if(!wbrels) return 0;
  19084. try {
  19085. wbrels = sheets.map(function pwbr(w) { if(!w.id) w.id = w.strRelID; return [w.name, wbrels['!id'][w.id].Target, get_sheet_type(wbrels['!id'][w.id].Type)]; });
  19086. } catch(e) { return null; }
  19087. return !wbrels || wbrels.length === 0 ? null : wbrels;
  19088. }
  19089. function safe_parse_sheet(zip, path, relsPath, sheet, idx, sheetRels, sheets, stype, opts, wb, themes, styles) {
  19090. try {
  19091. sheetRels[sheet]=parse_rels(getzipstr(zip, relsPath, true), path);
  19092. var data = getzipdata(zip, path);
  19093. var _ws;
  19094. switch(stype) {
  19095. case 'sheet': _ws = parse_ws(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  19096. case 'chart': _ws = parse_cs(data, path, idx, opts, sheetRels[sheet], wb, themes, styles);
  19097. if(!_ws || !_ws['!drawel']) break;
  19098. var dfile = resolve_path(_ws['!drawel'].Target, path);
  19099. var drelsp = get_rels_path(dfile);
  19100. var draw = parse_drawing(getzipstr(zip, dfile, true), parse_rels(getzipstr(zip, drelsp, true), dfile));
  19101. var chartp = resolve_path(draw, dfile);
  19102. var crelsp = get_rels_path(chartp);
  19103. _ws = parse_chart(getzipstr(zip, chartp, true), chartp, opts, parse_rels(getzipstr(zip, crelsp, true), chartp), wb, _ws);
  19104. break;
  19105. case 'macro': _ws = parse_ms(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  19106. case 'dialog': _ws = parse_ds(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
  19107. default: throw new Error("Unrecognized sheet type " + stype);
  19108. }
  19109. sheets[sheet] = _ws;
  19110. /* scan rels for comments */
  19111. var comments = [];
  19112. if(sheetRels && sheetRels[sheet]) keys(sheetRels[sheet]).forEach(function(n) {
  19113. if(sheetRels[sheet][n].Type == RELS.CMNT) {
  19114. var dfile = resolve_path(sheetRels[sheet][n].Target, path);
  19115. comments = parse_cmnt(getzipdata(zip, dfile, true), dfile, opts);
  19116. if(!comments || !comments.length) return;
  19117. sheet_insert_comments(_ws, comments);
  19118. }
  19119. });
  19120. } catch(e) { if(opts.WTF) throw e; }
  19121. }
  19122. function strip_front_slash(x) { return x.charAt(0) == '/' ? x.slice(1) : x; }
  19123. function parse_zip(zip, opts) {
  19124. make_ssf(SSF);
  19125. opts = opts || {};
  19126. fix_read_opts(opts);
  19127. /* OpenDocument Part 3 Section 2.2.1 OpenDocument Package */
  19128. if(safegetzipfile(zip, 'META-INF/manifest.xml')) return parse_ods(zip, opts);
  19129. /* UOC */
  19130. if(safegetzipfile(zip, 'objectdata.xml')) return parse_ods(zip, opts);
  19131. /* Numbers */
  19132. if(safegetzipfile(zip, 'Index/Document.iwa')) throw new Error('Unsupported NUMBERS file');
  19133. var entries = zipentries(zip);
  19134. var dir = parse_ct((getzipstr(zip, '[Content_Types].xml')));
  19135. var xlsb = false;
  19136. var sheets, binname;
  19137. if(dir.workbooks.length === 0) {
  19138. binname = "xl/workbook.xml";
  19139. if(getzipdata(zip,binname, true)) dir.workbooks.push(binname);
  19140. }
  19141. if(dir.workbooks.length === 0) {
  19142. binname = "xl/workbook.bin";
  19143. if(!getzipdata(zip,binname,true)) throw new Error("Could not find workbook");
  19144. dir.workbooks.push(binname);
  19145. xlsb = true;
  19146. }
  19147. if(dir.workbooks[0].slice(-3) == "bin") xlsb = true;
  19148. var themes = ({});
  19149. var styles = ({});
  19150. if(!opts.bookSheets && !opts.bookProps) {
  19151. strs = [];
  19152. if(dir.sst) try { strs=parse_sst(getzipdata(zip, strip_front_slash(dir.sst)), dir.sst, opts); } catch(e) { if(opts.WTF) throw e; }
  19153. if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipstr(zip, dir.themes[0].replace(/^\//,''), true)||"",dir.themes[0], opts);
  19154. if(dir.style) styles = parse_sty(getzipdata(zip, strip_front_slash(dir.style)), dir.style, themes, opts);
  19155. }
  19156. /*var externbooks = */dir.links.map(function(link) {
  19157. try {
  19158. var rels = parse_rels(getzipstr(zip, get_rels_path(strip_front_slash(link))), link);
  19159. return parse_xlink(getzipdata(zip, strip_front_slash(link)), rels, link, opts);
  19160. } catch(e) {}
  19161. });
  19162. var wb = parse_wb(getzipdata(zip, strip_front_slash(dir.workbooks[0])), dir.workbooks[0], opts);
  19163. var props = {}, propdata = "";
  19164. if(dir.coreprops.length) {
  19165. propdata = getzipdata(zip, strip_front_slash(dir.coreprops[0]), true);
  19166. if(propdata) props = parse_core_props(propdata);
  19167. if(dir.extprops.length !== 0) {
  19168. propdata = getzipdata(zip, strip_front_slash(dir.extprops[0]), true);
  19169. if(propdata) parse_ext_props(propdata, props, opts);
  19170. }
  19171. }
  19172. var custprops = {};
  19173. if(!opts.bookSheets || opts.bookProps) {
  19174. if (dir.custprops.length !== 0) {
  19175. propdata = getzipstr(zip, strip_front_slash(dir.custprops[0]), true);
  19176. if(propdata) custprops = parse_cust_props(propdata, opts);
  19177. }
  19178. }
  19179. var out = ({});
  19180. if(opts.bookSheets || opts.bookProps) {
  19181. if(wb.Sheets) sheets = wb.Sheets.map(function pluck(x){ return x.name; });
  19182. else if(props.Worksheets && props.SheetNames.length > 0) sheets=props.SheetNames;
  19183. if(opts.bookProps) { out.Props = props; out.Custprops = custprops; }
  19184. if(opts.bookSheets && typeof sheets !== 'undefined') out.SheetNames = sheets;
  19185. if(opts.bookSheets ? out.SheetNames : opts.bookProps) return out;
  19186. }
  19187. sheets = {};
  19188. var deps = {};
  19189. if(opts.bookDeps && dir.calcchain) deps=parse_cc(getzipdata(zip, strip_front_slash(dir.calcchain)),dir.calcchain,opts);
  19190. var i=0;
  19191. var sheetRels = ({});
  19192. var path, relsPath;
  19193. {
  19194. var wbsheets = wb.Sheets;
  19195. props.Worksheets = wbsheets.length;
  19196. props.SheetNames = [];
  19197. for(var j = 0; j != wbsheets.length; ++j) {
  19198. props.SheetNames[j] = wbsheets[j].name;
  19199. }
  19200. }
  19201. var wbext = xlsb ? "bin" : "xml";
  19202. var wbrelsi = dir.workbooks[0].lastIndexOf("/");
  19203. var wbrelsfile = (dir.workbooks[0].slice(0, wbrelsi+1) + "_rels/" + dir.workbooks[0].slice(wbrelsi+1) + ".rels").replace(/^\//,"");
  19204. if(!safegetzipfile(zip, wbrelsfile)) wbrelsfile = 'xl/_rels/workbook.' + wbext + '.rels';
  19205. var wbrels = parse_rels(getzipstr(zip, wbrelsfile, true), wbrelsfile);
  19206. if(wbrels) wbrels = safe_parse_wbrels(wbrels, wb.Sheets);
  19207. /* Numbers iOS hack */
  19208. var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0;
  19209. wsloop: for(i = 0; i != props.Worksheets; ++i) {
  19210. var stype = "sheet";
  19211. if(wbrels && wbrels[i]) {
  19212. path = 'xl/' + (wbrels[i][1]).replace(/[\/]?xl\//, "");
  19213. if(!safegetzipfile(zip, path)) path = wbrels[i][1];
  19214. if(!safegetzipfile(zip, path)) path = wbrelsfile.replace(/_rels\/.*$/,"") + wbrels[i][1];
  19215. stype = wbrels[i][2];
  19216. } else {
  19217. path = 'xl/worksheets/sheet'+(i+1-nmode)+"." + wbext;
  19218. path = path.replace(/sheet0\./,"sheet.");
  19219. }
  19220. relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
  19221. if(opts && opts.sheets != null) switch(typeof opts.sheets) {
  19222. case "number": if(i != opts.sheets) continue wsloop; break;
  19223. case "string": if(props.SheetNames[i].toLowerCase() != opts.sheets.toLowerCase()) continue wsloop; break;
  19224. default: if(Array.isArray && Array.isArray(opts.sheets)) {
  19225. var snjseen = false;
  19226. for(var snj = 0; snj != opts.sheets.length; ++snj) {
  19227. if(typeof opts.sheets[snj] == "number" && opts.sheets[snj] == i) snjseen=1;
  19228. if(typeof opts.sheets[snj] == "string" && opts.sheets[snj].toLowerCase() == props.SheetNames[i].toLowerCase()) snjseen = 1;
  19229. }
  19230. if(!snjseen) continue wsloop;
  19231. }
  19232. }
  19233. safe_parse_sheet(zip, path, relsPath, props.SheetNames[i], i, sheetRels, sheets, stype, opts, wb, themes, styles);
  19234. }
  19235. out = ({
  19236. Directory: dir,
  19237. Workbook: wb,
  19238. Props: props,
  19239. Custprops: custprops,
  19240. Deps: deps,
  19241. Sheets: sheets,
  19242. SheetNames: props.SheetNames,
  19243. Strings: strs,
  19244. Styles: styles,
  19245. Themes: themes,
  19246. SSF: SSF.get_table()
  19247. });
  19248. if(opts && opts.bookFiles) {
  19249. out.keys = entries;
  19250. out.files = zip.files;
  19251. }
  19252. if(opts && opts.bookVBA) {
  19253. if(dir.vba.length > 0) out.vbaraw = getzipdata(zip,strip_front_slash(dir.vba[0]),true);
  19254. else if(dir.defaults && dir.defaults.bin === CT_VBA) out.vbaraw = getzipdata(zip, 'xl/vbaProject.bin',true);
  19255. }
  19256. return out;
  19257. }
  19258. /* [MS-OFFCRYPTO] 2.1.1 */
  19259. function parse_xlsxcfb(cfb, _opts) {
  19260. var opts = _opts || {};
  19261. var f = 'Workbook', data = CFB.find(cfb, f);
  19262. try {
  19263. f = '/!DataSpaces/Version';
  19264. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19265. /*var version = */parse_DataSpaceVersionInfo(data.content);
  19266. /* 2.3.4.1 */
  19267. f = '/!DataSpaces/DataSpaceMap';
  19268. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19269. var dsm = parse_DataSpaceMap(data.content);
  19270. if(dsm.length !== 1 || dsm[0].comps.length !== 1 || dsm[0].comps[0].t !== 0 || dsm[0].name !== "StrongEncryptionDataSpace" || dsm[0].comps[0].v !== "EncryptedPackage")
  19271. throw new Error("ECMA-376 Encrypted file bad " + f);
  19272. /* 2.3.4.2 */
  19273. f = '/!DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace';
  19274. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19275. var seds = parse_DataSpaceDefinition(data.content);
  19276. if(seds.length != 1 || seds[0] != "StrongEncryptionTransform")
  19277. throw new Error("ECMA-376 Encrypted file bad " + f);
  19278. /* 2.3.4.3 */
  19279. f = '/!DataSpaces/TransformInfo/StrongEncryptionTransform/!Primary';
  19280. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19281. /*var hdr = */parse_Primary(data.content);
  19282. } catch(e) {}
  19283. f = '/EncryptionInfo';
  19284. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19285. var einfo = parse_EncryptionInfo(data.content);
  19286. /* 2.3.4.4 */
  19287. f = '/EncryptedPackage';
  19288. data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
  19289. /*global decrypt_agile */
  19290. if(einfo[0] == 0x04 && typeof decrypt_agile !== 'undefined') return decrypt_agile(einfo[1], data.content, opts.password || "", opts);
  19291. /*global decrypt_std76 */
  19292. if(einfo[0] == 0x02 && typeof decrypt_std76 !== 'undefined') return decrypt_std76(einfo[1], data.content, opts.password || "", opts);
  19293. throw new Error("File is password-protected");
  19294. }
  19295. function write_zip(wb, opts) {
  19296. _shapeid = 1024;
  19297. if(opts.bookType == "ods") return write_ods(wb, opts);
  19298. if(wb && !wb.SSF) {
  19299. wb.SSF = SSF.get_table();
  19300. }
  19301. if(wb && wb.SSF) {
  19302. make_ssf(SSF); SSF.load_table(wb.SSF);
  19303. // $FlowIgnore
  19304. opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
  19305. opts.ssf = wb.SSF;
  19306. }
  19307. opts.rels = {}; opts.wbrels = {};
  19308. opts.Strings = []; opts.Strings.Count = 0; opts.Strings.Unique = 0;
  19309. if(browser_has_Map) opts.revStrings = new Map();
  19310. else { opts.revStrings = {}; opts.revStrings.foo = []; delete opts.revStrings.foo; }
  19311. var wbext = opts.bookType == "xlsb" ? "bin" : "xml";
  19312. var vbafmt = VBAFMTS.indexOf(opts.bookType) > -1;
  19313. var ct = new_ct();
  19314. fix_write_opts(opts = opts || {});
  19315. var zip = zip_new();
  19316. var f = "", rId = 0;
  19317. opts.cellXfs = [];
  19318. get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
  19319. if(!wb.Props) wb.Props = {};
  19320. f = "docProps/core.xml";
  19321. zip_add_file(zip, f, write_core_props(wb.Props, opts));
  19322. ct.coreprops.push(f);
  19323. add_rels(opts.rels, 2, f, RELS.CORE_PROPS);
  19324. f = "docProps/app.xml";
  19325. if(wb.Props && wb.Props.SheetNames){/* empty */}
  19326. else if(!wb.Workbook || !wb.Workbook.Sheets) wb.Props.SheetNames = wb.SheetNames;
  19327. else {
  19328. var _sn = [];
  19329. for(var _i = 0; _i < wb.SheetNames.length; ++_i)
  19330. if((wb.Workbook.Sheets[_i]||{}).Hidden != 2) _sn.push(wb.SheetNames[_i]);
  19331. wb.Props.SheetNames = _sn;
  19332. }
  19333. wb.Props.Worksheets = wb.Props.SheetNames.length;
  19334. zip_add_file(zip, f, write_ext_props(wb.Props, opts));
  19335. ct.extprops.push(f);
  19336. add_rels(opts.rels, 3, f, RELS.EXT_PROPS);
  19337. if(wb.Custprops !== wb.Props && keys(wb.Custprops||{}).length > 0) {
  19338. f = "docProps/custom.xml";
  19339. zip_add_file(zip, f, write_cust_props(wb.Custprops, opts));
  19340. ct.custprops.push(f);
  19341. add_rels(opts.rels, 4, f, RELS.CUST_PROPS);
  19342. }
  19343. for(rId=1;rId <= wb.SheetNames.length; ++rId) {
  19344. var wsrels = {'!id':{}};
  19345. var ws = wb.Sheets[wb.SheetNames[rId-1]];
  19346. var _type = (ws || {})["!type"] || "sheet";
  19347. switch(_type) {
  19348. case "chart":
  19349. /* falls through */
  19350. default:
  19351. f = "xl/worksheets/sheet" + rId + "." + wbext;
  19352. zip_add_file(zip, f, write_ws(rId-1, f, opts, wb, wsrels));
  19353. ct.sheets.push(f);
  19354. add_rels(opts.wbrels, -1, "worksheets/sheet" + rId + "." + wbext, RELS.WS[0]);
  19355. }
  19356. if(ws) {
  19357. var comments = ws['!comments'];
  19358. var need_vml = false;
  19359. if(comments && comments.length > 0) {
  19360. var cf = "xl/comments" + rId + "." + wbext;
  19361. zip_add_file(zip, cf, write_cmnt(comments, cf, opts));
  19362. ct.comments.push(cf);
  19363. add_rels(wsrels, -1, "../comments" + rId + "." + wbext, RELS.CMNT);
  19364. need_vml = true;
  19365. }
  19366. if(ws['!legacy']) {
  19367. if(need_vml) zip_add_file(zip, "xl/drawings/vmlDrawing" + (rId) + ".vml", write_comments_vml(rId, ws['!comments']));
  19368. }
  19369. delete ws['!comments'];
  19370. delete ws['!legacy'];
  19371. }
  19372. if(wsrels['!id'].rId1) zip_add_file(zip, get_rels_path(f), write_rels(wsrels));
  19373. }
  19374. if(opts.Strings != null && opts.Strings.length > 0) {
  19375. f = "xl/sharedStrings." + wbext;
  19376. zip_add_file(zip, f, write_sst(opts.Strings, f, opts));
  19377. ct.strs.push(f);
  19378. add_rels(opts.wbrels, -1, "sharedStrings." + wbext, RELS.SST);
  19379. }
  19380. f = "xl/workbook." + wbext;
  19381. zip_add_file(zip, f, write_wb(wb, f, opts));
  19382. ct.workbooks.push(f);
  19383. add_rels(opts.rels, 1, f, RELS.WB);
  19384. /* TODO: something more intelligent with themes */
  19385. f = "xl/theme/theme1.xml";
  19386. zip_add_file(zip, f, write_theme(wb.Themes, opts));
  19387. ct.themes.push(f);
  19388. add_rels(opts.wbrels, -1, "theme/theme1.xml", RELS.THEME);
  19389. /* TODO: something more intelligent with styles */
  19390. f = "xl/styles." + wbext;
  19391. zip_add_file(zip, f, write_sty(wb, f, opts));
  19392. ct.styles.push(f);
  19393. add_rels(opts.wbrels, -1, "styles." + wbext, RELS.STY);
  19394. if(wb.vbaraw && vbafmt) {
  19395. f = "xl/vbaProject.bin";
  19396. zip_add_file(zip, f, wb.vbaraw);
  19397. ct.vba.push(f);
  19398. add_rels(opts.wbrels, -1, "vbaProject.bin", RELS.VBA);
  19399. }
  19400. zip_add_file(zip, "[Content_Types].xml", write_ct(ct, opts));
  19401. zip_add_file(zip, '_rels/.rels', write_rels(opts.rels));
  19402. zip_add_file(zip, 'xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
  19403. delete opts.revssf; delete opts.ssf;
  19404. return zip;
  19405. }
  19406. function firstbyte(f,o) {
  19407. var x = "";
  19408. switch((o||{}).type || "base64") {
  19409. case 'buffer': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
  19410. case 'base64': x = Base64.decode(f.slice(0,12)); break;
  19411. case 'binary': x = f; break;
  19412. case 'array': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
  19413. default: throw new Error("Unrecognized type " + (o && o.type || "undefined"));
  19414. }
  19415. return [x.charCodeAt(0), x.charCodeAt(1), x.charCodeAt(2), x.charCodeAt(3), x.charCodeAt(4), x.charCodeAt(5), x.charCodeAt(6), x.charCodeAt(7)];
  19416. }
  19417. function read_cfb(cfb, opts) {
  19418. if(CFB.find(cfb, "EncryptedPackage")) return parse_xlsxcfb(cfb, opts);
  19419. return parse_xlscfb(cfb, opts);
  19420. }
  19421. function read_zip(data, opts) {
  19422. var zip, d = data;
  19423. var o = opts||{};
  19424. if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
  19425. zip = zip_read(d, o);
  19426. return parse_zip(zip, o);
  19427. }
  19428. function read_plaintext(data, o) {
  19429. var i = 0;
  19430. main: while(i < data.length) switch(data.charCodeAt(i)) {
  19431. case 0x0A: case 0x0D: case 0x20: ++i; break;
  19432. case 0x3C: return parse_xlml(data.slice(i),o);
  19433. default: break main;
  19434. }
  19435. return PRN.to_workbook(data, o);
  19436. }
  19437. function read_plaintext_raw(data, o) {
  19438. var str = "", bytes = firstbyte(data, o);
  19439. switch(o.type) {
  19440. case 'base64': str = Base64.decode(data); break;
  19441. case 'binary': str = data; break;
  19442. case 'buffer': str = data.toString('binary'); break;
  19443. case 'array': str = cc2str(data); break;
  19444. default: throw new Error("Unrecognized type " + o.type);
  19445. }
  19446. if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str);
  19447. return read_plaintext(str, o);
  19448. }
  19449. function read_utf16(data, o) {
  19450. var d = data;
  19451. if(o.type == 'base64') d = Base64.decode(d);
  19452. d = cptable.utils.decode(1200, d.slice(2), 'str');
  19453. o.type = "binary";
  19454. return read_plaintext(d, o);
  19455. }
  19456. function bstrify(data) {
  19457. return !data.match(/[^\x00-\x7F]/) ? data : utf8write(data);
  19458. }
  19459. function read_prn(data, d, o, str) {
  19460. if(str) { o.type = "string"; return PRN.to_workbook(data, o); }
  19461. return PRN.to_workbook(d, o);
  19462. }
  19463. function readSync(data, opts) {
  19464. reset_cp();
  19465. if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data), opts);
  19466. var d = data, n = [0,0,0,0], str = false;
  19467. var o = opts||{};
  19468. if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
  19469. _ssfopts = {};
  19470. if(o.dateNF) _ssfopts.dateNF = o.dateNF;
  19471. if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
  19472. if(o.type == "file") { o.type = has_buf ? "buffer" : "binary"; d = read_binary(data); }
  19473. if(o.type == "string") { str = true; o.type = "binary"; o.codepage = 65001; d = bstrify(data); }
  19474. if(o.type == 'array' && typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && typeof ArrayBuffer !== 'undefined') {
  19475. // $FlowIgnore
  19476. var ab=new ArrayBuffer(3), vu=new Uint8Array(ab); vu.foo="bar";
  19477. // $FlowIgnore
  19478. if(!vu.foo) {o=dup(o); o.type='array'; return readSync(ab2a(d), o);}
  19479. }
  19480. switch((n = firstbyte(d, o))[0]) {
  19481. case 0xD0: if(n[1] === 0xCF && n[2] === 0x11 && n[3] === 0xE0 && n[4] === 0xA1 && n[5] === 0xB1 && n[6] === 0x1A && n[7] === 0xE1) return read_cfb(CFB.read(d, o), o); break;
  19482. case 0x09: if(n[1] <= 0x04) return parse_xlscfb(d, o); break;
  19483. case 0x3C: return parse_xlml(d, o);
  19484. case 0x49: if(n[1] === 0x44) return read_wb_ID(d, o); break;
  19485. case 0x54: if(n[1] === 0x41 && n[2] === 0x42 && n[3] === 0x4C) return DIF.to_workbook(d, o); break;
  19486. case 0x50: return (n[1] === 0x4B && n[2] < 0x09 && n[3] < 0x09) ? read_zip(d, o) : read_prn(data, d, o, str);
  19487. case 0xEF: return n[3] === 0x3C ? parse_xlml(d, o) : read_prn(data, d, o, str);
  19488. case 0xFF: if(n[1] === 0xFE) { return read_utf16(d, o); } break;
  19489. case 0x00: if(n[1] === 0x00 && n[2] >= 0x02 && n[3] === 0x00) return WK_.to_workbook(d, o); break;
  19490. case 0x03: case 0x83: case 0x8B: case 0x8C: return DBF.to_workbook(d, o);
  19491. case 0x7B: if(n[1] === 0x5C && n[2] === 0x72 && n[3] === 0x74) return RTF.to_workbook(d, o); break;
  19492. case 0x0A: case 0x0D: case 0x20: return read_plaintext_raw(d, o);
  19493. }
  19494. if(DBF.versions.indexOf(n[0]) > -1 && n[2] <= 12 && n[3] <= 31) return DBF.to_workbook(d, o);
  19495. return read_prn(data, d, o, str);
  19496. }
  19497. function readFileSync(filename, opts) {
  19498. var o = opts||{}; o.type = 'file';
  19499. return readSync(filename, o);
  19500. }
  19501. function write_cfb_ctr(cfb, o) {
  19502. switch(o.type) {
  19503. case "base64": case "binary": break;
  19504. case "buffer": case "array": o.type = ""; break;
  19505. case "file": return write_dl(o.file, CFB.write(cfb, {type:has_buf ? 'buffer' : ""}));
  19506. case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
  19507. default: throw new Error("Unrecognized type " + o.type);
  19508. }
  19509. return CFB.write(cfb, o);
  19510. }
  19511. function write_zip_type(wb, opts) {
  19512. var o = opts||{};
  19513. var z = write_zip(wb, o);
  19514. var oopts = {};
  19515. if(o.compression) oopts.compression = 'DEFLATE';
  19516. if(o.password) oopts.type = has_buf ? "nodebuffer" : "string";
  19517. else switch(o.type) {
  19518. case "base64": oopts.type = "base64"; break;
  19519. case "binary": oopts.type = "string"; break;
  19520. case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
  19521. case "buffer":
  19522. case "file": oopts.type = has_buf ? "nodebuffer" : "string"; break;
  19523. default: throw new Error("Unrecognized type " + o.type);
  19524. }
  19525. var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: {"nodebuffer": "buffer", "string": "binary"}[oopts.type] || oopts.type}) : z.generate(oopts);
  19526. /*jshint -W083 */
  19527. if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o); // eslint-disable-line no-undef
  19528. /*jshint +W083 */
  19529. if(o.type === "file") return write_dl(o.file, out);
  19530. return o.type == "string" ? utf8read(out) : out;
  19531. }
  19532. function write_cfb_type(wb, opts) {
  19533. var o = opts||{};
  19534. var cfb = write_xlscfb(wb, o);
  19535. return write_cfb_ctr(cfb, o);
  19536. }
  19537. function write_string_type(out, opts, bom) {
  19538. if(!bom) bom = "";
  19539. var o = bom + out;
  19540. switch(opts.type) {
  19541. case "base64": return Base64.encode(utf8write(o));
  19542. case "binary": return utf8write(o);
  19543. case "string": return out;
  19544. case "file": return write_dl(opts.file, o, 'utf8');
  19545. case "buffer": {
  19546. if(has_buf) return Buffer_from(o, 'utf8');
  19547. else return write_string_type(o, {type:'binary'}).split("").map(function(c) { return c.charCodeAt(0); });
  19548. }
  19549. }
  19550. throw new Error("Unrecognized type " + opts.type);
  19551. }
  19552. function write_stxt_type(out, opts) {
  19553. switch(opts.type) {
  19554. case "base64": return Base64.encode(out);
  19555. case "binary": return out;
  19556. case "string": return out; /* override in sheet_to_txt */
  19557. case "file": return write_dl(opts.file, out, 'binary');
  19558. case "buffer": {
  19559. if(has_buf) return Buffer_from(out, 'binary');
  19560. else return out.split("").map(function(c) { return c.charCodeAt(0); });
  19561. }
  19562. }
  19563. throw new Error("Unrecognized type " + opts.type);
  19564. }
  19565. /* TODO: test consistency */
  19566. function write_binary_type(out, opts) {
  19567. switch(opts.type) {
  19568. case "string":
  19569. case "base64":
  19570. case "binary":
  19571. var bstr = "";
  19572. // $FlowIgnore
  19573. for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
  19574. return opts.type == 'base64' ? Base64.encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
  19575. case "file": return write_dl(opts.file, out);
  19576. case "buffer": return out;
  19577. default: throw new Error("Unrecognized type " + opts.type);
  19578. }
  19579. }
  19580. function writeSync(wb, opts) {
  19581. reset_cp();
  19582. check_wb(wb);
  19583. var o = opts||{};
  19584. if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
  19585. if(o.type == "array") { o.type = "binary"; var out = (writeSync(wb, o)); o.type = "array"; return s2ab(out); }
  19586. switch(o.bookType || 'xlsb') {
  19587. case 'xml':
  19588. case 'xlml': return write_string_type(write_xlml(wb, o), o);
  19589. case 'slk':
  19590. case 'sylk': return write_string_type(write_slk_str(wb, o), o);
  19591. case 'htm':
  19592. case 'html': return write_string_type(write_htm_str(wb, o), o);
  19593. case 'txt': return write_stxt_type(write_txt_str(wb, o), o);
  19594. case 'csv': return write_string_type(write_csv_str(wb, o), o, "\ufeff");
  19595. case 'dif': return write_string_type(write_dif_str(wb, o), o);
  19596. case 'dbf': return write_binary_type(write_dbf_buf(wb, o), o);
  19597. case 'prn': return write_string_type(write_prn_str(wb, o), o);
  19598. case 'rtf': return write_string_type(write_rtf_str(wb, o), o);
  19599. case 'eth': return write_string_type(write_eth_str(wb, o), o);
  19600. case 'fods': return write_string_type(write_ods(wb, o), o);
  19601. case 'biff2': if(!o.biff) o.biff = 2; /* falls through */
  19602. case 'biff3': if(!o.biff) o.biff = 3; /* falls through */
  19603. case 'biff4': if(!o.biff) o.biff = 4; return write_binary_type(write_biff_buf(wb, o), o);
  19604. case 'biff5': if(!o.biff) o.biff = 5; /* falls through */
  19605. case 'biff8':
  19606. case 'xla':
  19607. case 'xls': if(!o.biff) o.biff = 8; return write_cfb_type(wb, o);
  19608. case 'xlsx':
  19609. case 'xlsm':
  19610. case 'xlam':
  19611. case 'xlsb':
  19612. case 'ods': return write_zip_type(wb, o);
  19613. default: throw new Error ("Unrecognized bookType |" + o.bookType + "|");
  19614. }
  19615. }
  19616. function resolve_book_type(o) {
  19617. if(o.bookType) return;
  19618. var _BT = {
  19619. "xls": "biff8",
  19620. "htm": "html",
  19621. "slk": "sylk",
  19622. "socialcalc": "eth",
  19623. "Sh33tJS": "WTF"
  19624. };
  19625. var ext = o.file.slice(o.file.lastIndexOf(".")).toLowerCase();
  19626. if(ext.match(/^\.[a-z]+$/)) o.bookType = ext.slice(1);
  19627. o.bookType = _BT[o.bookType] || o.bookType;
  19628. }
  19629. function writeFileSync(wb, filename, opts) {
  19630. var o = opts||{}; o.type = 'file';
  19631. o.file = filename;
  19632. resolve_book_type(o);
  19633. return writeSync(wb, o);
  19634. }
  19635. function writeFileAsync(filename, wb, opts, cb) {
  19636. var o = opts||{}; o.type = 'file';
  19637. o.file = filename;
  19638. resolve_book_type(o);
  19639. o.type = 'buffer';
  19640. var _cb = cb; if(!(_cb instanceof Function)) _cb = (opts);
  19641. return _fs.writeFile(filename, writeSync(wb, o), _cb);
  19642. }
  19643. function make_json_row(sheet, r, R, cols, header, hdr, dense, o) {
  19644. var rr = encode_row(R);
  19645. var defval = o.defval, raw = o.raw || !Object.prototype.hasOwnProperty.call(o, "raw");
  19646. var isempty = true;
  19647. var row = (header === 1) ? [] : {};
  19648. if(header !== 1) {
  19649. if(Object.defineProperty) try { Object.defineProperty(row, '__rowNum__', {value:R, enumerable:false}); } catch(e) { row.__rowNum__ = R; }
  19650. else row.__rowNum__ = R;
  19651. }
  19652. if(!dense || sheet[R]) for (var C = r.s.c; C <= r.e.c; ++C) {
  19653. var val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  19654. if(val === undefined || val.t === undefined) {
  19655. if(defval === undefined) continue;
  19656. if(hdr[C] != null) { row[hdr[C]] = defval; }
  19657. continue;
  19658. }
  19659. var v = val.v;
  19660. switch(val.t){
  19661. case 'z': if(v == null) break; continue;
  19662. case 'e': v = void 0; break;
  19663. case 's': case 'd': case 'b': case 'n': break;
  19664. default: throw new Error('unrecognized type ' + val.t);
  19665. }
  19666. if(hdr[C] != null) {
  19667. if(v == null) {
  19668. if(defval !== undefined) row[hdr[C]] = defval;
  19669. else if(raw && v === null) row[hdr[C]] = null;
  19670. else continue;
  19671. } else {
  19672. row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o);
  19673. }
  19674. if(v != null) isempty = false;
  19675. }
  19676. }
  19677. return { row: row, isempty: isempty };
  19678. }
  19679. function sheet_to_json(sheet, opts) {
  19680. if(sheet == null || sheet["!ref"] == null) return [];
  19681. var val = {t:'n',v:0}, header = 0, offset = 1, hdr = [], v=0, vv="";
  19682. var r = {s:{r:0,c:0},e:{r:0,c:0}};
  19683. var o = opts || {};
  19684. var range = o.range != null ? o.range : sheet["!ref"];
  19685. if(o.header === 1) header = 1;
  19686. else if(o.header === "A") header = 2;
  19687. else if(Array.isArray(o.header)) header = 3;
  19688. else if(o.header == null) header = 0;
  19689. switch(typeof range) {
  19690. case 'string': r = safe_decode_range(range); break;
  19691. case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
  19692. default: r = range;
  19693. }
  19694. if(header > 0) offset = 0;
  19695. var rr = encode_row(r.s.r);
  19696. var cols = [];
  19697. var out = [];
  19698. var outi = 0, counter = 0;
  19699. var dense = Array.isArray(sheet);
  19700. var R = r.s.r, C = 0, CC = 0;
  19701. if(dense && !sheet[R]) sheet[R] = [];
  19702. for(C = r.s.c; C <= r.e.c; ++C) {
  19703. cols[C] = encode_col(C);
  19704. val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  19705. switch(header) {
  19706. case 1: hdr[C] = C - r.s.c; break;
  19707. case 2: hdr[C] = cols[C]; break;
  19708. case 3: hdr[C] = o.header[C - r.s.c]; break;
  19709. default:
  19710. if(val == null) val = {w: "__EMPTY", t: "s"};
  19711. vv = v = format_cell(val, null, o);
  19712. counter = 0;
  19713. for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
  19714. hdr[C] = vv;
  19715. }
  19716. }
  19717. for (R = r.s.r + offset; R <= r.e.r; ++R) {
  19718. var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
  19719. if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row.row;
  19720. }
  19721. out.length = outi;
  19722. return out;
  19723. }
  19724. var qreg = /"/g;
  19725. function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) {
  19726. var isempty = true;
  19727. var row = [], txt = "", rr = encode_row(R);
  19728. for(var C = r.s.c; C <= r.e.c; ++C) {
  19729. if (!cols[C]) continue;
  19730. var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
  19731. if(val == null) txt = "";
  19732. else if(val.v != null) {
  19733. isempty = false;
  19734. txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o));
  19735. for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; }
  19736. if(txt == "ID") txt = '"ID"';
  19737. } else if(val.f != null && !val.F) {
  19738. isempty = false;
  19739. txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
  19740. } else txt = "";
  19741. /* NOTE: Excel CSV does not support array formulae */
  19742. row.push(txt);
  19743. }
  19744. if(o.blankrows === false && isempty) return null;
  19745. return row.join(FS);
  19746. }
  19747. function sheet_to_csv(sheet, opts) {
  19748. var out = [];
  19749. var o = opts == null ? {} : opts;
  19750. if(sheet == null || sheet["!ref"] == null) return "";
  19751. var r = safe_decode_range(sheet["!ref"]);
  19752. var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
  19753. var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
  19754. var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
  19755. var row = "", cols = [];
  19756. o.dense = Array.isArray(sheet);
  19757. var colinfo = o.skipHidden && sheet["!cols"] || [];
  19758. var rowinfo = o.skipHidden && sheet["!rows"] || [];
  19759. for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
  19760. for(var R = r.s.r; R <= r.e.r; ++R) {
  19761. if ((rowinfo[R]||{}).hidden) continue;
  19762. row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
  19763. if(row == null) { continue; }
  19764. if(o.strip) row = row.replace(endregex,"");
  19765. out.push(row + RS);
  19766. }
  19767. delete o.dense;
  19768. return out.join("");
  19769. }
  19770. function sheet_to_txt(sheet, opts) {
  19771. if(!opts) opts = {}; opts.FS = "\t"; opts.RS = "\n";
  19772. var s = sheet_to_csv(sheet, opts);
  19773. if(typeof cptable == 'undefined' || opts.type == 'string') return s;
  19774. var o = cptable.utils.encode(1200, s, 'str');
  19775. return String.fromCharCode(255) + String.fromCharCode(254) + o;
  19776. }
  19777. function sheet_to_formulae(sheet) {
  19778. var y = "", x, val="";
  19779. if(sheet == null || sheet["!ref"] == null) return [];
  19780. var r = safe_decode_range(sheet['!ref']), rr = "", cols = [], C;
  19781. var cmds = [];
  19782. var dense = Array.isArray(sheet);
  19783. for(C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
  19784. for(var R = r.s.r; R <= r.e.r; ++R) {
  19785. rr = encode_row(R);
  19786. for(C = r.s.c; C <= r.e.c; ++C) {
  19787. y = cols[C] + rr;
  19788. x = dense ? (sheet[R]||[])[C] : sheet[y];
  19789. val = "";
  19790. if(x === undefined) continue;
  19791. else if(x.F != null) {
  19792. y = x.F;
  19793. if(!x.f) continue;
  19794. val = x.f;
  19795. if(y.indexOf(":") == -1) y = y + ":" + y;
  19796. }
  19797. if(x.f != null) val = x.f;
  19798. else if(x.t == 'z') continue;
  19799. else if(x.t == 'n' && x.v != null) val = "" + x.v;
  19800. else if(x.t == 'b') val = x.v ? "TRUE" : "FALSE";
  19801. else if(x.w !== undefined) val = "'" + x.w;
  19802. else if(x.v === undefined) continue;
  19803. else if(x.t == 's') val = "'" + x.v;
  19804. else val = ""+x.v;
  19805. cmds[cmds.length] = y + "=" + val;
  19806. }
  19807. }
  19808. return cmds;
  19809. }
  19810. function sheet_add_json(_ws, js, opts) {
  19811. var o = opts || {};
  19812. var offset = +!o.skipHeader;
  19813. var ws = _ws || ({});
  19814. var _R = 0, _C = 0;
  19815. if(ws && o.origin != null) {
  19816. if(typeof o.origin == 'number') _R = o.origin;
  19817. else {
  19818. var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
  19819. _R = _origin.r; _C = _origin.c;
  19820. }
  19821. }
  19822. var cell;
  19823. var range = ({s: {c:0, r:0}, e: {c:_C, r:_R + js.length - 1 + offset}});
  19824. if(ws['!ref']) {
  19825. var _range = safe_decode_range(ws['!ref']);
  19826. range.e.c = Math.max(range.e.c, _range.e.c);
  19827. range.e.r = Math.max(range.e.r, _range.e.r);
  19828. if(_R == -1) { _R = _range.e.r + 1; range.e.r = _R + js.length - 1 + offset; }
  19829. } else {
  19830. if(_R == -1) { _R = 0; range.e.r = js.length - 1 + offset; }
  19831. }
  19832. var hdr = o.header || [], C = 0;
  19833. js.forEach(function (JS, R) {
  19834. keys(JS).forEach(function(k) {
  19835. if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
  19836. var v = JS[k];
  19837. var t = 'z';
  19838. var z = "";
  19839. var ref = encode_cell({c:_C + C,r:_R + R + offset});
  19840. cell = utils.sheet_get_cell(ws, ref);
  19841. if(v && typeof v === 'object' && !(v instanceof Date)){
  19842. ws[ref] = v;
  19843. } else {
  19844. if(typeof v == 'number') t = 'n';
  19845. else if(typeof v == 'boolean') t = 'b';
  19846. else if(typeof v == 'string') t = 's';
  19847. else if(v instanceof Date) {
  19848. t = 'd';
  19849. if(!o.cellDates) { t = 'n'; v = datenum(v); }
  19850. z = (o.dateNF || SSF._table[14]);
  19851. }
  19852. if(!cell) ws[ref] = cell = ({t:t, v:v});
  19853. else {
  19854. cell.t = t; cell.v = v;
  19855. delete cell.w; delete cell.R;
  19856. if(z) cell.z = z;
  19857. }
  19858. if(z) cell.z = z;
  19859. }
  19860. });
  19861. });
  19862. range.e.c = Math.max(range.e.c, _C + hdr.length - 1);
  19863. var __R = encode_row(_R);
  19864. if(offset) for(C = 0; C < hdr.length; ++C) ws[encode_col(C + _C) + __R] = {t:'s', v:hdr[C]};
  19865. ws['!ref'] = encode_range(range);
  19866. return ws;
  19867. }
  19868. function json_to_sheet(js, opts) { return sheet_add_json(null, js, opts); }
  19869. var utils = {
  19870. encode_col: encode_col,
  19871. encode_row: encode_row,
  19872. encode_cell: encode_cell,
  19873. encode_range: encode_range,
  19874. decode_col: decode_col,
  19875. decode_row: decode_row,
  19876. split_cell: split_cell,
  19877. decode_cell: decode_cell,
  19878. decode_range: decode_range,
  19879. format_cell: format_cell,
  19880. get_formulae: sheet_to_formulae,
  19881. make_csv: sheet_to_csv,
  19882. make_json: sheet_to_json,
  19883. make_formulae: sheet_to_formulae,
  19884. sheet_add_aoa: sheet_add_aoa,
  19885. sheet_add_json: sheet_add_json,
  19886. sheet_add_dom: sheet_add_dom,
  19887. aoa_to_sheet: aoa_to_sheet,
  19888. json_to_sheet: json_to_sheet,
  19889. table_to_sheet: parse_dom_table,
  19890. table_to_book: table_to_book,
  19891. sheet_to_csv: sheet_to_csv,
  19892. sheet_to_txt: sheet_to_txt,
  19893. sheet_to_json: sheet_to_json,
  19894. sheet_to_html: HTML_.from_sheet,
  19895. sheet_to_formulae: sheet_to_formulae,
  19896. sheet_to_row_object_array: sheet_to_json
  19897. };
  19898. (function(utils) {
  19899. utils.consts = utils.consts || {};
  19900. function add_consts(R/*Array<any>*/) { R.forEach(function(a){ utils.consts[a[0]] = a[1]; }); }
  19901. function get_default(x, y, z) { return x[y] != null ? x[y] : (x[y] = z); }
  19902. /* get cell, creating a stub if necessary */
  19903. function ws_get_cell_stub(ws, R, C) {
  19904. /* A1 cell address */
  19905. if(typeof R == "string") {
  19906. /* dense */
  19907. if(Array.isArray(ws)) {
  19908. var RC = decode_cell(R);
  19909. if(!ws[RC.r]) ws[RC.r] = [];
  19910. return ws[RC.r][RC.c] || (ws[RC.r][RC.c] = {t:'z'});
  19911. }
  19912. return ws[R] || (ws[R] = {t:'z'});
  19913. }
  19914. /* cell address object */
  19915. if(typeof R != "number") return ws_get_cell_stub(ws, encode_cell(R));
  19916. /* R and C are 0-based indices */
  19917. return ws_get_cell_stub(ws, encode_cell({r:R,c:C||0}));
  19918. }
  19919. utils.sheet_get_cell = ws_get_cell_stub;
  19920. /* find sheet index for given name / validate index */
  19921. function wb_sheet_idx(wb, sh) {
  19922. if(typeof sh == "number") {
  19923. if(sh >= 0 && wb.SheetNames.length > sh) return sh;
  19924. throw new Error("Cannot find sheet # " + sh);
  19925. } else if(typeof sh == "string") {
  19926. var idx = wb.SheetNames.indexOf(sh);
  19927. if(idx > -1) return idx;
  19928. throw new Error("Cannot find sheet name |" + sh + "|");
  19929. } else throw new Error("Cannot find sheet |" + sh + "|");
  19930. }
  19931. /* simple blank workbook object */
  19932. utils.book_new = function() {
  19933. return { SheetNames: [], Sheets: {} };
  19934. };
  19935. /* add a worksheet to the end of a given workbook */
  19936. utils.book_append_sheet = function(wb, ws, name) {
  19937. if(!name) for(var i = 1; i <= 0xFFFF; ++i, name = undefined) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
  19938. if(!name || wb.SheetNames.length >= 0xFFFF) throw new Error("Too many worksheets");
  19939. check_ws_name(name);
  19940. if(wb.SheetNames.indexOf(name) >= 0) throw new Error("Worksheet with name |" + name + "| already exists!");
  19941. wb.SheetNames.push(name);
  19942. wb.Sheets[name] = ws;
  19943. };
  19944. /* set sheet visibility (visible/hidden/very hidden) */
  19945. utils.book_set_sheet_visibility = function(wb, sh, vis) {
  19946. get_default(wb,"Workbook",{});
  19947. get_default(wb.Workbook,"Sheets",[]);
  19948. var idx = wb_sheet_idx(wb, sh);
  19949. // $FlowIgnore
  19950. get_default(wb.Workbook.Sheets,idx, {});
  19951. switch(vis) {
  19952. case 0: case 1: case 2: break;
  19953. default: throw new Error("Bad sheet visibility setting " + vis);
  19954. }
  19955. // $FlowIgnore
  19956. wb.Workbook.Sheets[idx].Hidden = vis;
  19957. };
  19958. add_consts([
  19959. ["SHEET_VISIBLE", 0],
  19960. ["SHEET_HIDDEN", 1],
  19961. ["SHEET_VERY_HIDDEN", 2]
  19962. ]);
  19963. /* set number format */
  19964. utils.cell_set_number_format = function(cell, fmt) {
  19965. cell.z = fmt;
  19966. return cell;
  19967. };
  19968. /* set cell hyperlink */
  19969. utils.cell_set_hyperlink = function(cell, target, tooltip) {
  19970. if(!target) {
  19971. delete cell.l;
  19972. } else {
  19973. cell.l = ({ Target: target });
  19974. if(tooltip) cell.l.Tooltip = tooltip;
  19975. }
  19976. return cell;
  19977. };
  19978. utils.cell_set_internal_link = function(cell, range, tooltip) { return utils.cell_set_hyperlink(cell, "#" + range, tooltip); };
  19979. /* add to cell comments */
  19980. utils.cell_add_comment = function(cell, text, author) {
  19981. if(!cell.c) cell.c = [];
  19982. cell.c.push({t:text, a:author||"SheetJS"});
  19983. };
  19984. /* set array formula and flush related cells */
  19985. utils.sheet_set_array_formula = function(ws, range, formula) {
  19986. var rng = typeof range != "string" ? range : safe_decode_range(range);
  19987. var rngstr = typeof range == "string" ? range : encode_range(range);
  19988. for(var R = rng.s.r; R <= rng.e.r; ++R) for(var C = rng.s.c; C <= rng.e.c; ++C) {
  19989. var cell = ws_get_cell_stub(ws, R, C);
  19990. cell.t = 'n';
  19991. cell.F = rngstr;
  19992. delete cell.v;
  19993. if(R == rng.s.r && C == rng.s.c) cell.f = formula;
  19994. }
  19995. return ws;
  19996. };
  19997. return utils;
  19998. })(utils);
  19999. if(has_buf && typeof require != 'undefined') (function() {
  20000. var Readable = require('stream').Readable;
  20001. var write_csv_stream = function(sheet, opts) {
  20002. var stream = Readable();
  20003. var o = opts == null ? {} : opts;
  20004. if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
  20005. var r = safe_decode_range(sheet["!ref"]);
  20006. var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
  20007. var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
  20008. var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
  20009. var row = "", cols = [];
  20010. o.dense = Array.isArray(sheet);
  20011. var colinfo = o.skipHidden && sheet["!cols"] || [];
  20012. var rowinfo = o.skipHidden && sheet["!rows"] || [];
  20013. for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
  20014. var R = r.s.r;
  20015. var BOM = false;
  20016. stream._read = function() {
  20017. if(!BOM) { BOM = true; return stream.push("\uFEFF"); }
  20018. while(R <= r.e.r) {
  20019. ++R;
  20020. if ((rowinfo[R-1]||{}).hidden) continue;
  20021. row = make_csv_row(sheet, r, R-1, cols, fs, rs, FS, o);
  20022. if(row != null) {
  20023. if(o.strip) row = row.replace(endregex,"");
  20024. stream.push(row + RS);
  20025. break;
  20026. }
  20027. }
  20028. if(R > r.e.r) return stream.push(null);
  20029. };
  20030. return stream;
  20031. };
  20032. var write_html_stream = function(ws, opts) {
  20033. var stream = Readable();
  20034. var o = opts || {};
  20035. var header = o.header != null ? o.header : HTML_.BEGIN;
  20036. var footer = o.footer != null ? o.footer : HTML_.END;
  20037. stream.push(header);
  20038. var r = decode_range(ws['!ref']);
  20039. o.dense = Array.isArray(ws);
  20040. stream.push(HTML_._preamble(ws, r, o));
  20041. var R = r.s.r;
  20042. var end = false;
  20043. stream._read = function() {
  20044. if(R > r.e.r) {
  20045. if(!end) { end = true; stream.push("</table>" + footer); }
  20046. return stream.push(null);
  20047. }
  20048. while(R <= r.e.r) {
  20049. stream.push(HTML_._row(ws, r, R, o));
  20050. ++R;
  20051. break;
  20052. }
  20053. };
  20054. return stream;
  20055. };
  20056. var write_json_stream = function(sheet, opts) {
  20057. var stream = Readable({objectMode:true});
  20058. if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
  20059. var val = {t:'n',v:0}, header = 0, offset = 1, hdr = [], v=0, vv="";
  20060. var r = {s:{r:0,c:0},e:{r:0,c:0}};
  20061. var o = opts || {};
  20062. var range = o.range != null ? o.range : sheet["!ref"];
  20063. if(o.header === 1) header = 1;
  20064. else if(o.header === "A") header = 2;
  20065. else if(Array.isArray(o.header)) header = 3;
  20066. switch(typeof range) {
  20067. case 'string': r = safe_decode_range(range); break;
  20068. case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
  20069. default: r = range;
  20070. }
  20071. if(header > 0) offset = 0;
  20072. var rr = encode_row(r.s.r);
  20073. var cols = [];
  20074. var counter = 0;
  20075. var dense = Array.isArray(sheet);
  20076. var R = r.s.r, C = 0, CC = 0;
  20077. if(dense && !sheet[R]) sheet[R] = [];
  20078. for(C = r.s.c; C <= r.e.c; ++C) {
  20079. cols[C] = encode_col(C);
  20080. val = dense ? sheet[R][C] : sheet[cols[C] + rr];
  20081. switch(header) {
  20082. case 1: hdr[C] = C - r.s.c; break;
  20083. case 2: hdr[C] = cols[C]; break;
  20084. case 3: hdr[C] = o.header[C - r.s.c]; break;
  20085. default:
  20086. if(val == null) val = {w: "__EMPTY", t: "s"};
  20087. vv = v = format_cell(val, null, o);
  20088. counter = 0;
  20089. for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
  20090. hdr[C] = vv;
  20091. }
  20092. }
  20093. R = r.s.r + offset;
  20094. stream._read = function() {
  20095. if(R > r.e.r) return stream.push(null);
  20096. while(R <= r.e.r) {
  20097. //if ((rowinfo[R-1]||{}).hidden) continue;
  20098. var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
  20099. ++R;
  20100. if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) {
  20101. stream.push(row.row);
  20102. break;
  20103. }
  20104. }
  20105. };
  20106. return stream;
  20107. };
  20108. XLSX.stream = {
  20109. to_json: write_json_stream,
  20110. to_html: write_html_stream,
  20111. to_csv: write_csv_stream
  20112. };
  20113. })();
  20114. if(typeof parse_xlscfb !== "undefined") XLSX.parse_xlscfb = parse_xlscfb;
  20115. XLSX.parse_zip = parse_zip;
  20116. XLSX.read = readSync; //xlsread
  20117. XLSX.readFile = readFileSync; //readFile
  20118. XLSX.readFileSync = readFileSync;
  20119. XLSX.write = writeSync;
  20120. XLSX.writeFile = writeFileSync;
  20121. XLSX.writeFileSync = writeFileSync;
  20122. XLSX.writeFileAsync = writeFileAsync;
  20123. XLSX.utils = utils;
  20124. XLSX.SSF = SSF;
  20125. if(typeof CFB !== "undefined") XLSX.CFB = CFB;
  20126. }
  20127. /*global define */
  20128. if(typeof exports !== 'undefined') make_xlsx_lib(exports);
  20129. else if(typeof module !== 'undefined' && module.exports) make_xlsx_lib(module.exports);
  20130. else if(typeof define === 'function' && define.amd) define(function() { if(!XLSX.version) make_xlsx_lib(XLSX); return XLSX; });
  20131. else make_xlsx_lib(XLSX);
  20132. /*exported XLS, ODS */
  20133. var XLS = XLSX, ODS = XLSX;