12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710 |
- /*! xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
- /* vim: set ts=2: */
- /*exported XLSX */
- /*global global, exports, module, require:false, process:false, Buffer:false, ArrayBuffer:false */
- var XLSX = {};
- function make_xlsx_lib(XLSX){
- XLSX.version = '0.16.9';
- var current_codepage = 1200, current_ansi = 1252;
- var VALID_ANSI = [ 874, 932, 936, 949, 950 ];
- for(var i = 0; i <= 8; ++i) VALID_ANSI.push(1250 + i);
- /* ECMA-376 Part I 18.4.1 charset to codepage mapping */
- var CS2CP = ({
- 0: 1252, /* ANSI */
- 1: 65001, /* DEFAULT */
- 2: 65001, /* SYMBOL */
- 77: 10000, /* MAC */
- 128: 932, /* SHIFTJIS */
- 129: 949, /* HANGUL */
- 130: 1361, /* JOHAB */
- 134: 936, /* GB2312 */
- 136: 950, /* CHINESEBIG5 */
- 161: 1253, /* GREEK */
- 162: 1254, /* TURKISH */
- 163: 1258, /* VIETNAMESE */
- 177: 1255, /* HEBREW */
- 178: 1256, /* ARABIC */
- 186: 1257, /* BALTIC */
- 204: 1251, /* RUSSIAN */
- 222: 874, /* THAI */
- 238: 1250, /* EASTEUROPE */
- 255: 1252, /* OEM */
- 69: 6969 /* MISC */
- });
- var set_ansi = function(cp) { if(VALID_ANSI.indexOf(cp) == -1) return; current_ansi = CS2CP[0] = cp; };
- function reset_ansi() { set_ansi(1252); }
- var set_cp = function(cp) { current_codepage = cp; set_ansi(cp); };
- function reset_cp() { set_cp(1200); reset_ansi(); }
- function char_codes(data) { var o = []; for(var i = 0, len = data.length; i < len; ++i) o[i] = data.charCodeAt(i); return o; }
- function utf16leread(data) {
- var o = [];
- for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i) + (data.charCodeAt(2*i+1)<<8));
- return o.join("");
- }
- function utf16beread(data) {
- var o = [];
- for(var i = 0; i < (data.length>>1); ++i) o[i] = String.fromCharCode(data.charCodeAt(2*i+1) + (data.charCodeAt(2*i)<<8));
- return o.join("");
- }
- var debom = function(data) {
- var c1 = data.charCodeAt(0), c2 = data.charCodeAt(1);
- if(c1 == 0xFF && c2 == 0xFE) return utf16leread(data.slice(2));
- if(c1 == 0xFE && c2 == 0xFF) return utf16beread(data.slice(2));
- if(c1 == 0xFEFF) return data.slice(1);
- return data;
- };
- var _getchar = function _gc1(x) { return String.fromCharCode(x); };
- var _getansi = function _ga1(x) { return String.fromCharCode(x); };
- var DENSE = null;
- var DIF_XL = true;
- var Base64 = (function make_b64(){
- var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- return {
- encode: function(input) {
- var o = "";
- var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
- for(var i = 0; i < input.length; ) {
- c1 = input.charCodeAt(i++);
- e1 = (c1 >> 2);
- c2 = input.charCodeAt(i++);
- e2 = ((c1 & 3) << 4) | (c2 >> 4);
- c3 = input.charCodeAt(i++);
- e3 = ((c2 & 15) << 2) | (c3 >> 6);
- e4 = (c3 & 63);
- if (isNaN(c2)) { e3 = e4 = 64; }
- else if (isNaN(c3)) { e4 = 64; }
- o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4);
- }
- return o;
- },
- decode: function b64_decode(input) {
- var o = "";
- var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
- input = input.replace(/[^\w\+\/\=]/g, "");
- for(var i = 0; i < input.length;) {
- e1 = map.indexOf(input.charAt(i++));
- e2 = map.indexOf(input.charAt(i++));
- c1 = (e1 << 2) | (e2 >> 4);
- o += String.fromCharCode(c1);
- e3 = map.indexOf(input.charAt(i++));
- c2 = ((e2 & 15) << 4) | (e3 >> 2);
- if (e3 !== 64) { o += String.fromCharCode(c2); }
- e4 = map.indexOf(input.charAt(i++));
- c3 = ((e3 & 3) << 6) | e4;
- if (e4 !== 64) { o += String.fromCharCode(c3); }
- }
- return o;
- }
- };
- })();
- var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node);
- var Buffer_from = function(){};
- if(typeof Buffer !== 'undefined') {
- var nbfs = !Buffer.from;
- if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
- Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
- // $FlowIgnore
- if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); };
- // $FlowIgnore
- if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
- }
- function new_raw_buf(len) {
- /* jshint -W056 */
- return has_buf ? Buffer.alloc(len) : new Array(len);
- /* jshint +W056 */
- }
- function new_unsafe_buf(len) {
- /* jshint -W056 */
- return has_buf ? Buffer.allocUnsafe(len) : new Array(len);
- /* jshint +W056 */
- }
- var s2a = function s2a(s) {
- if(has_buf) return Buffer_from(s, "binary");
- return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; });
- };
- function s2ab(s) {
- if(typeof ArrayBuffer === 'undefined') return s2a(s);
- var buf = new ArrayBuffer(s.length), view = new Uint8Array(buf);
- for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
- return buf;
- }
- function a2s(data) {
- if(Array.isArray(data)) return data.map(function(c) { return String.fromCharCode(c); }).join("");
- var o = []; for(var i = 0; i < data.length; ++i) o[i] = String.fromCharCode(data[i]); return o.join("");
- }
- function a2u(data) {
- if(typeof Uint8Array === 'undefined') throw new Error("Unsupported");
- return new Uint8Array(data);
- }
- function ab2a(data) {
- if(typeof ArrayBuffer == 'undefined') throw new Error("Unsupported");
- if(data instanceof ArrayBuffer) return ab2a(new Uint8Array(data));
- var o = new Array(data.length);
- for(var i = 0; i < data.length; ++i) o[i] = data[i];
- return o;
- }
- var bconcat = function(bufs) { return [].concat.apply([], bufs); };
- var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/g;
- /* ssf.js (C) 2013-present SheetJS -- http://sheetjs.com */
- /*jshint -W041 */
- var SSF = ({});
- var make_ssf = function make_ssf(SSF){
- SSF.version = '0.11.2';
- function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
- function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
- function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
- function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
- function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
- function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
- function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
- var p2_32 = Math.pow(2,32);
- function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
- 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; }
- var days = [
- ['Sun', 'Sunday'],
- ['Mon', 'Monday'],
- ['Tue', 'Tuesday'],
- ['Wed', 'Wednesday'],
- ['Thu', 'Thursday'],
- ['Fri', 'Friday'],
- ['Sat', 'Saturday']
- ];
- var months = [
- ['J', 'Jan', 'January'],
- ['F', 'Feb', 'February'],
- ['M', 'Mar', 'March'],
- ['A', 'Apr', 'April'],
- ['M', 'May', 'May'],
- ['J', 'Jun', 'June'],
- ['J', 'Jul', 'July'],
- ['A', 'Aug', 'August'],
- ['S', 'Sep', 'September'],
- ['O', 'Oct', 'October'],
- ['N', 'Nov', 'November'],
- ['D', 'Dec', 'December']
- ];
- function init_table(t) {
- t[0]= 'General';
- t[1]= '0';
- t[2]= '0.00';
- t[3]= '#,##0';
- t[4]= '#,##0.00';
- t[9]= '0%';
- t[10]= '0.00%';
- t[11]= '0.00E+00';
- t[12]= '# ?/?';
- t[13]= '# ??/??';
- t[14]= 'm/d/yy';
- t[15]= 'd-mmm-yy';
- t[16]= 'd-mmm';
- t[17]= 'mmm-yy';
- t[18]= 'h:mm AM/PM';
- t[19]= 'h:mm:ss AM/PM';
- t[20]= 'h:mm';
- t[21]= 'h:mm:ss';
- t[22]= 'm/d/yy h:mm';
- t[37]= '#,##0 ;(#,##0)';
- t[38]= '#,##0 ;[Red](#,##0)';
- t[39]= '#,##0.00;(#,##0.00)';
- t[40]= '#,##0.00;[Red](#,##0.00)';
- t[45]= 'mm:ss';
- t[46]= '[h]:mm:ss';
- t[47]= 'mmss.0';
- t[48]= '##0.0E+0';
- t[49]= '@';
- t[56]= '"上午/下午 "hh"時"mm"分"ss"秒 "';
- }
- var table_fmt = {};
- init_table(table_fmt);
- /* Defaults determined by systematically testing in Excel 2019 */
- /* These formats appear to default to other formats in the table */
- var default_map = [];
- var defi = 0;
- // 5 -> 37 ... 8 -> 40
- for(defi = 5; defi <= 8; ++defi) default_map[defi] = 32 + defi;
- // 23 -> 0 ... 26 -> 0
- for(defi = 23; defi <= 26; ++defi) default_map[defi] = 0;
- // 27 -> 14 ... 31 -> 14
- for(defi = 27; defi <= 31; ++defi) default_map[defi] = 14;
- // 50 -> 14 ... 58 -> 14
- for(defi = 50; defi <= 58; ++defi) default_map[defi] = 14;
- // 59 -> 1 ... 62 -> 4
- for(defi = 59; defi <= 62; ++defi) default_map[defi] = defi - 58;
- // 67 -> 9 ... 68 -> 10
- for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 58;
- // 72 -> 14 ... 75 -> 17
- for(defi = 72; defi <= 75; ++defi) default_map[defi] = defi - 58;
- // 69 -> 12 ... 71 -> 14
- for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 57;
- // 76 -> 20 ... 78 -> 22
- for(defi = 76; defi <= 78; ++defi) default_map[defi] = defi - 56;
- // 79 -> 45 ... 81 -> 47
- for(defi = 79; defi <= 81; ++defi) default_map[defi] = defi - 34;
- // 82 -> 0 ... 65536 -> 0 (omitted)
- /* These formats technically refer to Accounting formats with no equivalent */
- var default_str = [];
- // 5 -- Currency, 0 decimal, black negative
- default_str[5] = default_str[63] = '"$"#,##0_);\\("$"#,##0\\)';
- // 6 -- Currency, 0 decimal, red negative
- default_str[6] = default_str[64] = '"$"#,##0_);[Red]\\("$"#,##0\\)';
- // 7 -- Currency, 2 decimal, black negative
- default_str[7] = default_str[65] = '"$"#,##0.00_);\\("$"#,##0.00\\)';
- // 8 -- Currency, 2 decimal, red negative
- default_str[8] = default_str[66] = '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)';
- // 41 -- Accounting, 0 decimal, No Symbol
- default_str[41] = '_(* #,##0_);_(* \\(#,##0\\);_(* "-"_);_(@_)';
- // 42 -- Accounting, 0 decimal, $ Symbol
- default_str[42] = '_("$"* #,##0_);_("$"* \\(#,##0\\);_("$"* "-"_);_(@_)';
- // 43 -- Accounting, 2 decimal, No Symbol
- default_str[43] = '_(* #,##0.00_);_(* \\(#,##0.00\\);_(* "-"??_);_(@_)';
- // 44 -- Accounting, 2 decimal, $ Symbol
- default_str[44] = '_("$"* #,##0.00_);_("$"* \\(#,##0.00\\);_("$"* "-"??_);_(@_)';
- function frac(x, D, mixed) {
- var sgn = x < 0 ? -1 : 1;
- var B = x * sgn;
- var P_2 = 0, P_1 = 1, P = 0;
- var Q_2 = 1, Q_1 = 0, Q = 0;
- var A = Math.floor(B);
- while(Q_1 < D) {
- A = Math.floor(B);
- P = A * P_1 + P_2;
- Q = A * Q_1 + Q_2;
- if((B - A) < 0.00000005) break;
- B = 1 / (B - A);
- P_2 = P_1; P_1 = P;
- Q_2 = Q_1; Q_1 = Q;
- }
- if(Q > D) { if(Q_1 > D) { Q = Q_2; P = P_2; } else { Q = Q_1; P = P_1; } }
- if(!mixed) return [0, sgn * P, Q];
- var q = Math.floor(sgn * P/Q);
- return [q, sgn*P - q*Q, Q];
- }
- function parse_date_code(v,opts,b2) {
- if(v > 2958465 || v < 0) return null;
- var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
- var dout=[];
- var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};
- if(Math.abs(out.u) < 1e-6) out.u = 0;
- if(opts && opts.date1904) date += 1462;
- if(out.u > 0.9999) {
- out.u = 0;
- if(++time == 86400) { out.T = time = 0; ++date; ++out.D; }
- }
- if(date === 60) {dout = b2 ? [1317,10,29] : [1900,2,29]; dow=3;}
- else if(date === 0) {dout = b2 ? [1317,8,29] : [1900,1,0]; dow=6;}
- else {
- if(date > 60) --date;
- /* 1 = Jan 1 1900 in Gregorian */
- var d = new Date(1900, 0, 1);
- d.setDate(d.getDate() + date - 1);
- dout = [d.getFullYear(), d.getMonth()+1,d.getDate()];
- dow = d.getDay();
- if(date < 60) dow = (dow + 6) % 7;
- if(b2) dow = fix_hijri(d, dout);
- }
- out.y = dout[0]; out.m = dout[1]; out.d = dout[2];
- out.S = time % 60; time = Math.floor(time / 60);
- out.M = time % 60; time = Math.floor(time / 60);
- out.H = time;
- out.q = dow;
- return out;
- }
- SSF.parse_date_code = parse_date_code;
- var basedate = new Date(1899, 11, 31, 0, 0, 0);
- var dnthresh = basedate.getTime();
- var base1904 = new Date(1900, 2, 1, 0, 0, 0);
- function datenum_local(v, date1904) {
- var epoch = v.getTime();
- if(date1904) epoch -= 1461*24*60*60*1000;
- else if(v >= base1904) epoch += 24*60*60*1000;
- return (epoch - (dnthresh + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000)) / (24 * 60 * 60 * 1000);
- }
- /* The longest 32-bit integer text is "-4294967296", exactly 11 chars */
- function general_fmt_int(v) { return v.toString(10); }
- SSF._general_int = general_fmt_int;
- /* ECMA-376 18.8.30 numFmt*/
- /* Note: `toPrecision` uses standard form when prec > E and E >= -6 */
- var general_fmt_num = (function make_general_fmt_num() {
- var trailing_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)$/;
- function strip_decimal(o) {
- return (o.indexOf(".") == -1) ? o : o.replace(trailing_zeroes_and_decimal, "$1");
- }
- /* General Exponential always shows 2 digits exp and trims the mantissa */
- var mantissa_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)[Ee]/;
- var exp_with_single_digit = /(E[+-])(\d)$/;
- function normalize_exp(o) {
- if(o.indexOf("E") == -1) return o;
- return o.replace(mantissa_zeroes_and_decimal,"$1E").replace(exp_with_single_digit,"$10$2");
- }
- /* exponent >= -9 and <= 9 */
- function small_exp(v) {
- var w = (v<0?12:11);
- var o = strip_decimal(v.toFixed(12)); if(o.length <= w) return o;
- o = v.toPrecision(10); if(o.length <= w) return o;
- return v.toExponential(5);
- }
- /* exponent >= 11 or <= -10 likely exponential */
- function large_exp(v) {
- var o = strip_decimal(v.toFixed(11));
- return (o.length > (v<0?12:11) || o === "0" || o === "-0") ? v.toPrecision(6) : o;
- }
- function general_fmt_num_base(v) {
- var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
- if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
- else if(Math.abs(V) <= 9) o = small_exp(v);
- else if(V === 10) o = v.toFixed(10).substr(0,12);
- else o = large_exp(v);
- return strip_decimal(normalize_exp(o.toUpperCase()));
- }
- return general_fmt_num_base;
- })();
- SSF._general_num = general_fmt_num;
- /*
- "General" rules:
- - text is passed through ("@")
- - booleans are rendered as TRUE/FALSE
- - "up to 11 characters" displayed for numbers
- - Default date format (code 14) used for Dates
- TODO: technically the display depends on the width of the cell
- */
- function general_fmt(v, opts) {
- switch(typeof v) {
- case 'string': return v;
- case 'boolean': return v ? "TRUE" : "FALSE";
- case 'number': return (v|0) === v ? v.toString(10) : general_fmt_num(v);
- case 'undefined': return "";
- case 'object':
- if(v == null) return "";
- if(v instanceof Date) return format(14, datenum_local(v, opts && opts.date1904), opts);
- }
- throw new Error("unsupported value in General format: " + v);
- }
- SSF._general = general_fmt;
- function fix_hijri(date, o) {
- /* TODO: properly adjust y/m/d and */
- o[0] -= 581;
- var dow = date.getDay();
- if(date < 60) dow = (dow + 6) % 7;
- return dow;
- }
- //var THAI_DIGITS = "\u0E50\u0E51\u0E52\u0E53\u0E54\u0E55\u0E56\u0E57\u0E58\u0E59".split("");
- /*jshint -W086 */
- function write_date(type, fmt, val, ss0) {
- var o="", ss=0, tt=0, y = val.y, out, outl = 0;
- switch(type) {
- case 98: /* 'b' buddhist year */
- y = val.y + 543;
- /* falls through */
- case 121: /* 'y' year */
- switch(fmt.length) {
- case 1: case 2: out = y % 100; outl = 2; break;
- default: out = y % 10000; outl = 4; break;
- } break;
- case 109: /* 'm' month */
- switch(fmt.length) {
- case 1: case 2: out = val.m; outl = fmt.length; break;
- case 3: return months[val.m-1][1];
- case 5: return months[val.m-1][0];
- default: return months[val.m-1][2];
- } break;
- case 100: /* 'd' day */
- switch(fmt.length) {
- case 1: case 2: out = val.d; outl = fmt.length; break;
- case 3: return days[val.q][0];
- default: return days[val.q][1];
- } break;
- case 104: /* 'h' 12-hour */
- switch(fmt.length) {
- case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
- default: throw 'bad hour format: ' + fmt;
- } break;
- case 72: /* 'H' 24-hour */
- switch(fmt.length) {
- case 1: case 2: out = val.H; outl = fmt.length; break;
- default: throw 'bad hour format: ' + fmt;
- } break;
- case 77: /* 'M' minutes */
- switch(fmt.length) {
- case 1: case 2: out = val.M; outl = fmt.length; break;
- default: throw 'bad minute format: ' + fmt;
- } break;
- case 115: /* 's' seconds */
- if(fmt != 's' && fmt != 'ss' && fmt != '.0' && fmt != '.00' && fmt != '.000') throw 'bad second format: ' + fmt;
- if(val.u === 0 && (fmt == "s" || fmt == "ss")) return pad0(val.S, fmt.length);
- if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
- else tt = ss0 === 1 ? 10 : 1;
- ss = Math.round((tt)*(val.S + val.u));
- if(ss >= 60*tt) ss = 0;
- if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
- o = pad0(ss,2 + ss0);
- if(fmt === 'ss') return o.substr(0,2);
- return "." + o.substr(2,fmt.length-1);
- case 90: /* 'Z' absolute time */
- switch(fmt) {
- case '[h]': case '[hh]': out = val.D*24+val.H; break;
- case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
- case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
- default: throw 'bad abstime format: ' + fmt;
- } outl = fmt.length === 3 ? 1 : 2; break;
- case 101: /* 'e' era */
- out = y; outl = 1; break;
- }
- var outstr = outl > 0 ? pad0(out, outl) : "";
- return outstr;
- }
- /*jshint +W086 */
- function commaify(s) {
- var w = 3;
- if(s.length <= w) return s;
- var j = (s.length % w), o = s.substr(0,j);
- for(; j!=s.length; j+=w) o+=(o.length > 0 ? "," : "") + s.substr(j,w);
- return o;
- }
- var write_num = (function make_write_num(){
- var pct1 = /%/g;
- function write_num_pct(type, fmt, val){
- var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
- return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
- }
- function write_num_cm(type, fmt, val){
- var idx = fmt.length - 1;
- while(fmt.charCodeAt(idx-1) === 44) --idx;
- return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
- }
- function write_num_exp(fmt, val){
- var o;
- var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
- if(fmt.match(/^#+0.0E\+0$/)) {
- if(val == 0) return "0.0E+0";
- else if(val < 0) return "-" + write_num_exp(fmt, -val);
- var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
- var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
- if(ee < 0) ee += period;
- o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
- if(o.indexOf("e") === -1) {
- var fakee = Math.floor(Math.log(val)*Math.LOG10E);
- if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
- else o += "E+" + (fakee - ee);
- while(o.substr(0,2) === "0.") {
- o = o.charAt(0) + o.substr(2,period) + "." + o.substr(2+period);
- o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
- }
- o = o.replace(/\+-/,"-");
- }
- o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
- } else o = val.toExponential(idx);
- if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
- if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
- return o.replace("e","E");
- }
- var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
- function write_num_f1(r, aval, sign) {
- var den = parseInt(r[4],10), rr = Math.round(aval * den), base = Math.floor(rr/den);
- var myn = (rr - base*den), myd = den;
- 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));
- }
- function write_num_f2(r, aval, sign) {
- return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
- }
- var dec1 = /^#*0*\.([0#]+)/;
- var closeparen = /\).*[0#]/;
- var phone = /\(###\) ###\\?-####/;
- function hashq(str) {
- var o = "", cc;
- for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
- case 35: break;
- case 63: o+= " "; break;
- case 48: o+= "0"; break;
- default: o+= String.fromCharCode(cc);
- }
- return o;
- }
- function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
- function dec(val, d) {
- var _frac = val - Math.floor(val), dd = Math.pow(10,d);
- if (d < ('' + Math.round(_frac * dd)).length) return 0;
- return Math.round(_frac * dd);
- }
- function carry(val, d) {
- if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
- return 1;
- }
- return 0;
- }
- function flr(val) {
- if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0));
- return ""+Math.floor(val);
- }
- function write_num_flt(type, fmt, val) {
- if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
- var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
- if(val >= 0) return write_num_flt('n', ffmt, val);
- return '(' + write_num_flt('n', ffmt, -val) + ')';
- }
- if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
- if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
- if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
- if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
- var o;
- var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
- if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
- if(fmt.match(/^[#?]+$/)) {
- o = pad0r(val,0); if(o === "0") o = "";
- return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
- }
- if((r = fmt.match(frac1))) return write_num_f1(r, aval, sign);
- if(fmt.match(/^#+0+$/)) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
- if((r = fmt.match(dec1))) {
- 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); });
- return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
- }
- fmt = fmt.replace(/^#+([0.])/, "$1");
- if((r = fmt.match(/^(0*)\.(#*)$/))) {
- return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
- }
- if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
- if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
- 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);
- }
- if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
- if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
- o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
- ri = 0;
- return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
- }
- if(fmt.match(phone)) {
- o = write_num_flt(type, "##########", val);
- return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
- }
- var oa = "";
- if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
- ri = Math.min(r[4].length,7);
- ff = frac(aval, Math.pow(10,ri)-1, false);
- o = "" + sign;
- oa = write_num("n", r[1], ff[1]);
- if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
- o += oa + r[2] + "/" + r[3];
- oa = rpad_(ff[2],ri);
- if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
- o += oa;
- return o;
- }
- if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
- ri = Math.min(Math.max(r[1].length, r[4].length),7);
- ff = frac(aval, Math.pow(10,ri)-1, true);
- 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));
- }
- if((r = fmt.match(/^[#0?]+$/))) {
- o = pad0r(val, 0);
- if(fmt.length <= o.length) return o;
- return hashq(fmt.substr(0,fmt.length-o.length)) + o;
- }
- if((r = fmt.match(/^([#0?]+)\.([#0]+)$/))) {
- o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
- ri = o.indexOf(".");
- var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
- return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
- }
- if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
- ri = dec(val, r[1].length);
- 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);
- }
- switch(fmt) {
- case "###,##0.00": return write_num_flt(type, "#,##0.00", val);
- case "###,###":
- case "##,###":
- case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
- case "###,###.00": return write_num_flt(type, "###,##0.00",val).replace(/^0\./,".");
- case "#,###.00": return write_num_flt(type, "#,##0.00",val).replace(/^0\./,".");
- default:
- }
- throw new Error("unsupported format |" + fmt + "|");
- }
- function write_num_cm2(type, fmt, val){
- var idx = fmt.length - 1;
- while(fmt.charCodeAt(idx-1) === 44) --idx;
- return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
- }
- function write_num_pct2(type, fmt, val){
- var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
- return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
- }
- function write_num_exp2(fmt, val){
- var o;
- var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
- if(fmt.match(/^#+0.0E\+0$/)) {
- if(val == 0) return "0.0E+0";
- else if(val < 0) return "-" + write_num_exp2(fmt, -val);
- var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
- var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
- if(ee < 0) ee += period;
- o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
- if(!o.match(/[Ee]/)) {
- var fakee = Math.floor(Math.log(val)*Math.LOG10E);
- if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
- else o += "E+" + (fakee - ee);
- o = o.replace(/\+-/,"-");
- }
- o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
- } else o = val.toExponential(idx);
- if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
- if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
- return o.replace("e","E");
- }
- function write_num_int(type, fmt, val) {
- if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
- var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
- if(val >= 0) return write_num_int('n', ffmt, val);
- return '(' + write_num_int('n', ffmt, -val) + ')';
- }
- if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
- if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
- if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
- if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
- var o;
- var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
- if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
- if(fmt.match(/^[#?]+$/)) {
- o = (""+val); if(val === 0) o = "";
- return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
- }
- if((r = fmt.match(frac1))) return write_num_f2(r, aval, sign);
- if(fmt.match(/^#+0+$/)) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
- if((r = fmt.match(dec1))) {
- o = (""+val).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1]));
- o = o.replace(/\.(\d*)$/,function($$, $1) {
- return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
- return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
- }
- fmt = fmt.replace(/^#+([0.])/, "$1");
- if((r = fmt.match(/^(0*)\.(#*)$/))) {
- return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
- }
- if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify((""+aval));
- if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
- return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
- }
- if((r = fmt.match(/^#,#*,#0/))) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
- if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
- o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
- ri = 0;
- return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
- }
- if(fmt.match(phone)) {
- o = write_num_int(type, "##########", val);
- return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
- }
- var oa = "";
- if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
- ri = Math.min(r[4].length,7);
- ff = frac(aval, Math.pow(10,ri)-1, false);
- o = "" + sign;
- oa = write_num("n", r[1], ff[1]);
- if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
- o += oa + r[2] + "/" + r[3];
- oa = rpad_(ff[2],ri);
- if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
- o += oa;
- return o;
- }
- if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
- ri = Math.min(Math.max(r[1].length, r[4].length),7);
- ff = frac(aval, Math.pow(10,ri)-1, true);
- 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));
- }
- if((r = fmt.match(/^[#0?]+$/))) {
- o = "" + val;
- if(fmt.length <= o.length) return o;
- return hashq(fmt.substr(0,fmt.length-o.length)) + o;
- }
- if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
- o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
- ri = o.indexOf(".");
- var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
- return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
- }
- if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
- 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);
- }
- switch(fmt) {
- case "###,###":
- case "##,###":
- case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
- default:
- if(fmt.match(/\.[0#?]*$/)) return write_num_int(type, fmt.slice(0,fmt.lastIndexOf(".")), val) + hashq(fmt.slice(fmt.lastIndexOf(".")));
- }
- throw new Error("unsupported format |" + fmt + "|");
- }
- return function write_num(type, fmt, val) {
- return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
- };})();
- function split_fmt(fmt) {
- var out = [];
- var in_str = false/*, cc*/;
- for(var i = 0, j = 0; i < fmt.length; ++i) switch((/*cc=*/fmt.charCodeAt(i))) {
- case 34: /* '"' */
- in_str = !in_str; break;
- case 95: case 42: case 92: /* '_' '*' '\\' */
- ++i; break;
- case 59: /* ';' */
- out[out.length] = fmt.substr(j,i-j);
- j = i+1;
- }
- out[out.length] = fmt.substr(j);
- if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
- return out;
- }
- SSF._split = split_fmt;
- var abstime = /\[[HhMmSs\u0E0A\u0E19\u0E17]*\]/;
- function fmt_is_date(fmt) {
- var i = 0, /*cc = 0,*/ c = "", o = "";
- while(i < fmt.length) {
- switch((c = fmt.charAt(i))) {
- case 'G': if(isgeneral(fmt, i)) i+= 6; i++; break;
- case '"': for(;(/*cc=*/fmt.charCodeAt(++i)) !== 34 && i < fmt.length;){/*empty*/} ++i; break;
- case '\\': i+=2; break;
- case '_': i+=2; break;
- case '@': ++i; break;
- case 'B': case 'b':
- if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") return true;
- /* falls through */
- case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
- /* falls through */
- case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g': return true;
- case 'A': case 'a': case '上':
- if(fmt.substr(i, 3).toUpperCase() === "A/P") return true;
- if(fmt.substr(i, 5).toUpperCase() === "AM/PM") return true;
- if(fmt.substr(i, 5).toUpperCase() === "上午/下午") return true;
- ++i; break;
- case '[':
- o = c;
- while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
- if(o.match(abstime)) return true;
- break;
- case '.':
- /* falls through */
- case '0': case '#':
- 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 */}
- break;
- case '?': while(fmt.charAt(++i) === c){/* empty */} break;
- case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break;
- case '(': case ')': ++i; break;
- case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
- while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1){/* empty */} break;
- case ' ': ++i; break;
- default: ++i; break;
- }
- }
- return false;
- }
- SSF.is_date = fmt_is_date;
- function eval_fmt(fmt, v, opts, flen) {
- var out = [], o = "", i = 0, c = "", lst='t', dt, j, cc;
- var hr='H';
- /* Tokenize */
- while(i < fmt.length) {
- switch((c = fmt.charAt(i))) {
- case 'G': /* General */
- if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
- out[out.length] = {t:'G', v:'General'}; i+=7; break;
- case '"': /* Literal text */
- for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
- out[out.length] = {t:'t', v:o}; ++i; break;
- case '\\': var w = fmt.charAt(++i), t = (w === "(" || w === ")") ? w : 't';
- out[out.length] = {t:t, v:w}; ++i; break;
- case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
- case '@': /* Text Placeholder */
- out[out.length] = {t:'T', v:v}; ++i; break;
- case 'B': case 'b':
- if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") {
- if(dt==null) { dt=parse_date_code(v, opts, fmt.charAt(i+1) === "2"); if(dt==null) return ""; }
- out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
- }
- /* falls through */
- case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
- c = c.toLowerCase();
- /* falls through */
- case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
- if(v < 0) return "";
- if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
- o = c; while(++i < fmt.length && fmt.charAt(i).toLowerCase() === c) o+=c;
- if(c === 'm' && lst.toLowerCase() === 'h') c = 'M';
- if(c === 'h') c = hr;
- out[out.length] = {t:c, v:o}; lst = c; break;
- case 'A': case 'a': case '上':
- var q={t:c, v:c};
- if(dt==null) dt=parse_date_code(v, opts);
- 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;}
- 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'; }
- else if(fmt.substr(i,5).toUpperCase() === "上午/下午") { if(dt!=null) q.v = dt.H >= 12 ? "下午" : "上午"; q.t = 'T'; i+=5; hr='h'; }
- else { q.t = "t"; ++i; }
- if(dt==null && q.t === 'T') return "";
- out[out.length] = q; lst = c; break;
- case '[':
- o = c;
- while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
- if(o.slice(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
- if(o.match(abstime)) {
- if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
- out[out.length] = {t:'Z', v:o.toLowerCase()};
- lst = o.charAt(1);
- } else if(o.indexOf("$") > -1) {
- o = (o.match(/\$([^-\[\]]*)/)||[])[1]||"$";
- if(!fmt_is_date(fmt)) out[out.length] = {t:'t',v:o};
- }
- break;
- /* Numbers */
- case '.':
- if(dt != null) {
- o = c; while(++i < fmt.length && (c=fmt.charAt(i)) === "0") o += c;
- out[out.length] = {t:'s', v:o}; break;
- }
- /* falls through */
- case '0': case '#':
- o = c; while(++i < fmt.length && "0#?.,E+-%".indexOf(c=fmt.charAt(i)) > -1) o += c;
- out[out.length] = {t:'n', v:o}; break;
- case '?':
- o = c; while(fmt.charAt(++i) === c) o+=c;
- out[out.length] = {t:c, v:o}; lst = c; break;
- case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break; // **
- case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
- case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
- o = c; while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1) o+=fmt.charAt(i);
- out[out.length] = {t:'D', v:o}; break;
- case ' ': out[out.length] = {t:c, v:c}; ++i; break;
- case '$': out[out.length] = {t:'t', v:'$'}; ++i; break;
- default:
- if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
- out[out.length] = {t:'t', v:c}; ++i; break;
- }
- }
- /* Scan for date/time parts */
- var bt = 0, ss0 = 0, ssm;
- for(i=out.length-1, lst='t'; i >= 0; --i) {
- switch(out[i].t) {
- case 'h': case 'H': out[i].t = hr; lst='h'; if(bt < 1) bt = 1; break;
- case 's':
- if((ssm=out[i].v.match(/\.0+$/))) ss0=Math.max(ss0,ssm[0].length-1);
- if(bt < 3) bt = 3;
- /* falls through */
- case 'd': case 'y': case 'M': case 'e': lst=out[i].t; break;
- case 'm': if(lst === 's') { out[i].t = 'M'; if(bt < 2) bt = 2; } break;
- case 'X': /*if(out[i].v === "B2");*/
- break;
- case 'Z':
- if(bt < 1 && out[i].v.match(/[Hh]/)) bt = 1;
- if(bt < 2 && out[i].v.match(/[Mm]/)) bt = 2;
- if(bt < 3 && out[i].v.match(/[Ss]/)) bt = 3;
- }
- }
- /* time rounding depends on presence of minute / second / usec fields */
- switch(bt) {
- case 0: break;
- case 1:
- if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
- if(dt.S >= 60) { dt.S = 0; ++dt.M; }
- if(dt.M >= 60) { dt.M = 0; ++dt.H; }
- break;
- case 2:
- if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
- if(dt.S >= 60) { dt.S = 0; ++dt.M; }
- break;
- }
- /* replace fields */
- var nstr = "", jj;
- for(i=0; i < out.length; ++i) {
- switch(out[i].t) {
- case 't': case 'T': case ' ': case 'D': break;
- case 'X': out[i].v = ""; out[i].t = ";"; break;
- case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
- out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
- out[i].t = 't'; break;
- case 'n': case '?':
- jj = i+1;
- while(out[jj] != null && (
- (c=out[jj].t) === "?" || c === "D" ||
- ((c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/')) ||
- (out[i].t === '(' && (c === ' ' || c === 'n' || c === ')')) ||
- (c === 't' && (out[jj].v === '/' || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?'))
- )) {
- out[i].v += out[jj].v;
- out[jj] = {v:"", t:";"}; ++jj;
- }
- nstr += out[i].v;
- i = jj-1; break;
- case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
- }
- }
- var vv = "", myv, ostr;
- if(nstr.length > 0) {
- if(nstr.charCodeAt(0) == 40) /* '(' */ {
- myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v);
- ostr = write_num('n', nstr, myv);
- } else {
- myv = (v<0 && flen > 1 ? -v : v);
- ostr = write_num('n', nstr, myv);
- if(myv < 0 && out[0] && out[0].t == 't') {
- ostr = ostr.substr(1);
- out[0].v = "-" + out[0].v;
- }
- }
- jj=ostr.length-1;
- var decpt = out.length;
- for(i=0; i < out.length; ++i) if(out[i] != null && out[i].t != 't' && out[i].v.indexOf(".") > -1) { decpt = i; break; }
- var lasti=out.length;
- if(decpt === out.length && ostr.indexOf("E") === -1) {
- for(i=out.length-1; i>= 0;--i) {
- if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
- if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
- else if(jj < 0) out[i].v = "";
- else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
- out[i].t = 't';
- lasti = i;
- }
- if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
- }
- else if(decpt !== out.length && ostr.indexOf("E") === -1) {
- jj = ostr.indexOf(".")-1;
- for(i=decpt; i>= 0; --i) {
- if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
- j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
- vv = out[i].v.substr(j+1);
- for(; j>=0; --j) {
- if(jj>=0 && (out[i].v.charAt(j) === "0" || out[i].v.charAt(j) === "#")) vv = ostr.charAt(jj--) + vv;
- }
- out[i].v = vv;
- out[i].t = 't';
- lasti = i;
- }
- if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
- jj = ostr.indexOf(".")+1;
- for(i=decpt; i<out.length; ++i) {
- if(out[i] == null || ('n?('.indexOf(out[i].t) === -1 && i !== decpt)) continue;
- j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
- vv = out[i].v.substr(0,j);
- for(; j<out[i].v.length; ++j) {
- if(jj<ostr.length) vv += ostr.charAt(jj++);
- }
- out[i].v = vv;
- out[i].t = 't';
- lasti = i;
- }
- }
- }
- for(i=0; i<out.length; ++i) if(out[i] != null && 'n?'.indexOf(out[i].t)>-1) {
- myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
- out[i].v = write_num(out[i].t, out[i].v, myv);
- out[i].t = 't';
- }
- var retval = "";
- for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
- return retval;
- }
- SSF._eval = eval_fmt;
- var cfregex = /\[[=<>]/;
- var cfregex2 = /\[(=|>[=]?|<[>=]?)(-?\d+(?:\.\d*)?)\]/;
- function chkcond(v, rr) {
- if(rr == null) return false;
- var thresh = parseFloat(rr[2]);
- switch(rr[1]) {
- case "=": if(v == thresh) return true; break;
- case ">": if(v > thresh) return true; break;
- case "<": if(v < thresh) return true; break;
- case "<>": if(v != thresh) return true; break;
- case ">=": if(v >= thresh) return true; break;
- case "<=": if(v <= thresh) return true; break;
- }
- return false;
- }
- function choose_fmt(f, v) {
- var fmt = split_fmt(f);
- var l = fmt.length, lat = fmt[l-1].indexOf("@");
- if(l<4 && lat>-1) --l;
- if(fmt.length > 4) throw new Error("cannot find right format for |" + fmt.join("|") + "|");
- if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
- switch(fmt.length) {
- case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
- case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
- case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
- case 4: break;
- }
- var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
- if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
- if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
- var m1 = fmt[0].match(cfregex2);
- var m2 = fmt[1].match(cfregex2);
- return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
- }
- return [l, ff];
- }
- function format(fmt,v,o) {
- if(o == null) o = {};
- var sfmt = "";
- switch(typeof fmt) {
- case "string":
- if(fmt == "m/d/yy" && o.dateNF) sfmt = o.dateNF;
- else sfmt = fmt;
- break;
- case "number":
- if(fmt == 14 && o.dateNF) sfmt = o.dateNF;
- else sfmt = (o.table != null ? (o.table) : table_fmt)[fmt];
- if(sfmt == null) sfmt = (o.table && o.table[default_map[fmt]]) || table_fmt[default_map[fmt]];
- if(sfmt == null) sfmt = default_str[fmt] || "General";
- break;
- }
- if(isgeneral(sfmt,0)) return general_fmt(v, o);
- if(v instanceof Date) v = datenum_local(v, o.date1904);
- var f = choose_fmt(sfmt, v);
- if(isgeneral(f[1])) return general_fmt(v, o);
- if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
- else if(v === "" || v == null) return "";
- return eval_fmt(f[1], v, o, f[0]);
- }
- function load_entry(fmt, idx) {
- if(typeof idx != 'number') {
- idx = +idx || -1;
- for(var i = 0; i < 0x0188; ++i) {
- if(table_fmt[i] == undefined) { if(idx < 0) idx = i; continue; }
- if(table_fmt[i] == fmt) { idx = i; break; }
- }
- if(idx < 0) idx = 0x187;
- }
- table_fmt[idx] = fmt;
- return idx;
- }
- SSF.load = load_entry;
- SSF._table = table_fmt;
- SSF.get_table = function get_table() { return table_fmt; };
- SSF.load_table = function load_table(tbl) {
- for(var i=0; i!=0x0188; ++i)
- if(tbl[i] !== undefined) load_entry(tbl[i], i);
- };
- SSF.init_table = init_table;
- SSF.format = format;
- };
- make_ssf(SSF);
- /* map from xlml named formats to SSF TODO: localize */
- var XLMLFormatMap/*{[string]:string}*/ = ({
- "General Number": "General",
- "General Date": SSF._table[22],
- "Long Date": "dddd, mmmm dd, yyyy",
- "Medium Date": SSF._table[15],
- "Short Date": SSF._table[14],
- "Long Time": SSF._table[19],
- "Medium Time": SSF._table[18],
- "Short Time": SSF._table[20],
- "Currency": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
- "Fixed": SSF._table[2],
- "Standard": SSF._table[4],
- "Percent": SSF._table[10],
- "Scientific": SSF._table[11],
- "Yes/No": '"Yes";"Yes";"No";@',
- "True/False": '"True";"True";"False";@',
- "On/Off": '"Yes";"Yes";"No";@'
- });
- var SSFImplicit/*{[number]:string}*/ = ({
- "5": '"$"#,##0_);\\("$"#,##0\\)',
- "6": '"$"#,##0_);[Red]\\("$"#,##0\\)',
- "7": '"$"#,##0.00_);\\("$"#,##0.00\\)',
- "8": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
- "23": 'General', "24": 'General', "25": 'General', "26": 'General',
- "27": 'm/d/yy', "28": 'm/d/yy', "29": 'm/d/yy', "30": 'm/d/yy', "31": 'm/d/yy',
- "32": 'h:mm:ss', "33": 'h:mm:ss', "34": 'h:mm:ss', "35": 'h:mm:ss',
- "36": 'm/d/yy',
- "41": '_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)',
- "42": '_("$"* #,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)',
- "43": '_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)',
- "44": '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
- "50": 'm/d/yy', "51": 'm/d/yy', "52": 'm/d/yy', "53": 'm/d/yy', "54": 'm/d/yy',
- "55": 'm/d/yy', "56": 'm/d/yy', "57": 'm/d/yy', "58": 'm/d/yy',
- "59": '0',
- "60": '0.00',
- "61": '#,##0',
- "62": '#,##0.00',
- "63": '"$"#,##0_);\\("$"#,##0\\)',
- "64": '"$"#,##0_);[Red]\\("$"#,##0\\)',
- "65": '"$"#,##0.00_);\\("$"#,##0.00\\)',
- "66": '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)',
- "67": '0%',
- "68": '0.00%',
- "69": '# ?/?',
- "70": '# ??/??',
- "71": 'm/d/yy',
- "72": 'm/d/yy',
- "73": 'd-mmm-yy',
- "74": 'd-mmm',
- "75": 'mmm-yy',
- "76": 'h:mm',
- "77": 'h:mm:ss',
- "78": 'm/d/yy h:mm',
- "79": 'mm:ss',
- "80": '[h]:mm:ss',
- "81": 'mmss.0'
- });
- /* dateNF parse TODO: move to SSF */
- var dateNFregex = /[dD]+|[mM]+|[yYeE]+|[Hh]+|[Ss]+/g;
- function dateNF_regex(dateNF) {
- var fmt = typeof dateNF == "number" ? SSF._table[dateNF] : dateNF;
- fmt = fmt.replace(dateNFregex, "(\\d+)");
- return new RegExp("^" + fmt + "$");
- }
- function dateNF_fix(str, dateNF, match) {
- var Y = -1, m = -1, d = -1, H = -1, M = -1, S = -1;
- (dateNF.match(dateNFregex)||[]).forEach(function(n, i) {
- var v = parseInt(match[i+1], 10);
- switch(n.toLowerCase().charAt(0)) {
- case 'y': Y = v; break; case 'd': d = v; break;
- case 'h': H = v; break; case 's': S = v; break;
- case 'm': if(H >= 0) M = v; else m = v; break;
- }
- });
- if(S >= 0 && M == -1 && m >= 0) { M = m; m = -1; }
- var datestr = (("" + (Y>=0?Y: new Date().getFullYear())).slice(-4) + "-" + ("00" + (m>=1?m:1)).slice(-2) + "-" + ("00" + (d>=1?d:1)).slice(-2));
- if(datestr.length == 7) datestr = "0" + datestr;
- if(datestr.length == 8) datestr = "20" + datestr;
- var timestr = (("00" + (H>=0?H:0)).slice(-2) + ":" + ("00" + (M>=0?M:0)).slice(-2) + ":" + ("00" + (S>=0?S:0)).slice(-2));
- if(H == -1 && M == -1 && S == -1) return datestr;
- if(Y == -1 && m == -1 && d == -1) return timestr;
- return datestr + "T" + timestr;
- }
- var DO_NOT_EXPORT_CFB = true;
- /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
- /* vim: set ts=2: */
- /*jshint eqnull:true */
- /*exported CFB */
- /*global Uint8Array:false, Uint16Array:false */
- /* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
- /* vim: set ts=2: */
- /*exported CRC32 */
- var CRC32;
- (function (factory) {
- /*jshint ignore:start */
- /*eslint-disable */
- factory(CRC32 = {});
- /*eslint-enable */
- /*jshint ignore:end */
- }(function(CRC32) {
- CRC32.version = '1.2.0';
- /* see perf/crc32table.js */
- /*global Int32Array */
- function signed_crc_table() {
- var c = 0, table = new Array(256);
- for(var n =0; n != 256; ++n){
- c = n;
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
- table[n] = c;
- }
- return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
- }
- var T = signed_crc_table();
- function crc32_bstr(bstr, seed) {
- var C = seed ^ -1, L = bstr.length - 1;
- for(var i = 0; i < L;) {
- C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
- C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
- }
- if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
- return C ^ -1;
- }
- function crc32_buf(buf, seed) {
- if(buf.length > 10000) return crc32_buf_8(buf, seed);
- var C = seed ^ -1, L = buf.length - 3;
- for(var i = 0; i < L;) {
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- }
- while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- return C ^ -1;
- }
- function crc32_buf_8(buf, seed) {
- var C = seed ^ -1, L = buf.length - 7;
- for(var i = 0; i < L;) {
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- }
- while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
- return C ^ -1;
- }
- function crc32_str(str, seed) {
- var C = seed ^ -1;
- for(var i = 0, L=str.length, c, d; i < L;) {
- c = str.charCodeAt(i++);
- if(c < 0x80) {
- C = (C>>>8) ^ T[(C ^ c)&0xFF];
- } else if(c < 0x800) {
- C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
- } else if(c >= 0xD800 && c < 0xE000) {
- c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
- C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
- } else {
- C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
- C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
- }
- }
- return C ^ -1;
- }
- CRC32.table = T;
- CRC32.bstr = crc32_bstr;
- CRC32.buf = crc32_buf;
- CRC32.str = crc32_str;
- }));
- /* [MS-CFB] v20171201 */
- var CFB = (function _CFB(){
- var exports = {};
- exports.version = '1.1.4';
- /* [MS-CFB] 2.6.4 */
- function namecmp(l, r) {
- var L = l.split("/"), R = r.split("/");
- for(var i = 0, c = 0, Z = Math.min(L.length, R.length); i < Z; ++i) {
- if((c = L[i].length - R[i].length)) return c;
- if(L[i] != R[i]) return L[i] < R[i] ? -1 : 1;
- }
- return L.length - R.length;
- }
- function dirname(p) {
- if(p.charAt(p.length - 1) == "/") return (p.slice(0,-1).indexOf("/") === -1) ? p : dirname(p.slice(0, -1));
- var c = p.lastIndexOf("/");
- return (c === -1) ? p : p.slice(0, c+1);
- }
- function filename(p) {
- if(p.charAt(p.length - 1) == "/") return filename(p.slice(0, -1));
- var c = p.lastIndexOf("/");
- return (c === -1) ? p : p.slice(c+1);
- }
- /* -------------------------------------------------------------------------- */
- /* DOS Date format:
- high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
- add 1980 to stored year
- stored second should be doubled
- */
- /* write JS date to buf as a DOS date */
- function write_dos_date(buf, date) {
- if(typeof date === "string") date = new Date(date);
- var hms = date.getHours();
- hms = hms << 6 | date.getMinutes();
- hms = hms << 5 | (date.getSeconds()>>>1);
- buf.write_shift(2, hms);
- var ymd = (date.getFullYear() - 1980);
- ymd = ymd << 4 | (date.getMonth()+1);
- ymd = ymd << 5 | date.getDate();
- buf.write_shift(2, ymd);
- }
- /* read four bytes from buf and interpret as a DOS date */
- function parse_dos_date(buf) {
- var hms = buf.read_shift(2) & 0xFFFF;
- var ymd = buf.read_shift(2) & 0xFFFF;
- var val = new Date();
- var d = ymd & 0x1F; ymd >>>= 5;
- var m = ymd & 0x0F; ymd >>>= 4;
- val.setMilliseconds(0);
- val.setFullYear(ymd + 1980);
- val.setMonth(m-1);
- val.setDate(d);
- var S = hms & 0x1F; hms >>>= 5;
- var M = hms & 0x3F; hms >>>= 6;
- val.setHours(hms);
- val.setMinutes(M);
- val.setSeconds(S<<1);
- return val;
- }
- function parse_extra_field(blob) {
- prep_blob(blob, 0);
- var o = {};
- var flags = 0;
- while(blob.l <= blob.length - 4) {
- var type = blob.read_shift(2);
- var sz = blob.read_shift(2), tgt = blob.l + sz;
- var p = {};
- switch(type) {
- /* UNIX-style Timestamps */
- case 0x5455: {
- flags = blob.read_shift(1);
- if(flags & 1) p.mtime = blob.read_shift(4);
- /* for some reason, CD flag corresponds to LFH */
- if(sz > 5) {
- if(flags & 2) p.atime = blob.read_shift(4);
- if(flags & 4) p.ctime = blob.read_shift(4);
- }
- if(p.mtime) p.mt = new Date(p.mtime*1000);
- }
- break;
- }
- blob.l = tgt;
- o[type] = p;
- }
- return o;
- }
- var fs;
- function get_fs() { return fs || (fs = require('fs')); }
- function parse(file, options) {
- if(file[0] == 0x50 && file[1] == 0x4b) return parse_zip(file, options);
- if(file.length < 512) throw new Error("CFB file size " + file.length + " < 512");
- var mver = 3;
- var ssz = 512;
- var nmfs = 0; // number of mini FAT sectors
- var difat_sec_cnt = 0;
- var dir_start = 0;
- var minifat_start = 0;
- var difat_start = 0;
- var fat_addrs = []; // locations of FAT sectors
- /* [MS-CFB] 2.2 Compound File Header */
- var blob = file.slice(0,512);
- prep_blob(blob, 0);
- /* major version */
- var mv = check_get_mver(blob);
- mver = mv[0];
- switch(mver) {
- case 3: ssz = 512; break; case 4: ssz = 4096; break;
- case 0: if(mv[1] == 0) return parse_zip(file, options);
- /* falls through */
- default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
- }
- /* reprocess header */
- if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
- /* Save header for final object */
- var header = file.slice(0,ssz);
- check_shifts(blob, mver);
- // Number of Directory Sectors
- var dir_cnt = blob.read_shift(4, 'i');
- if(mver === 3 && dir_cnt !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + dir_cnt);
- // Number of FAT Sectors
- blob.l += 4;
- // First Directory Sector Location
- dir_start = blob.read_shift(4, 'i');
- // Transaction Signature
- blob.l += 4;
- // Mini Stream Cutoff Size
- blob.chk('00100000', 'Mini Stream Cutoff Size: ');
- // First Mini FAT Sector Location
- minifat_start = blob.read_shift(4, 'i');
- // Number of Mini FAT Sectors
- nmfs = blob.read_shift(4, 'i');
- // First DIFAT sector location
- difat_start = blob.read_shift(4, 'i');
- // Number of DIFAT Sectors
- difat_sec_cnt = blob.read_shift(4, 'i');
- // Grab FAT Sector Locations
- for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
- q = blob.read_shift(4, 'i');
- if(q<0) break;
- fat_addrs[j] = q;
- }
- /** Break the file up into sectors */
- var sectors = sectorify(file, ssz);
- sleuth_fat(difat_start, difat_sec_cnt, sectors, ssz, fat_addrs);
- /** Chains */
- var sector_list = make_sector_list(sectors, dir_start, fat_addrs, ssz);
- sector_list[dir_start].name = "!Directory";
- if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
- sector_list[fat_addrs[0]].name = "!FAT";
- sector_list.fat_addrs = fat_addrs;
- sector_list.ssz = ssz;
- /* [MS-CFB] 2.6.1 Compound File Directory Entry */
- var files = {}, Paths = [], FileIndex = [], FullPaths = [];
- read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, minifat_start);
- build_full_paths(FileIndex, FullPaths, Paths);
- Paths.shift();
- var o = {
- FileIndex: FileIndex,
- FullPaths: FullPaths
- };
- // $FlowIgnore
- if(options && options.raw) o.raw = {header: header, sectors: sectors};
- return o;
- } // parse
- /* [MS-CFB] 2.2 Compound File Header -- read up to major version */
- function check_get_mver(blob) {
- if(blob[blob.l] == 0x50 && blob[blob.l + 1] == 0x4b) return [0, 0];
- // header signature 8
- blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
- // clsid 16
- //blob.chk(HEADER_CLSID, 'CLSID: ');
- blob.l += 16;
- // minor version 2
- var mver = blob.read_shift(2, 'u');
- return [blob.read_shift(2,'u'), mver];
- }
- function check_shifts(blob, mver) {
- var shift = 0x09;
- // Byte Order
- //blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
- blob.l += 2;
- // Sector Shift
- switch((shift = blob.read_shift(2))) {
- case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
- case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
- default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
- }
- // Mini Sector Shift
- blob.chk('0600', 'Mini Sector Shift: ');
- // Reserved
- blob.chk('000000000000', 'Reserved: ');
- }
- /** Break the file up into sectors */
- function sectorify(file, ssz) {
- var nsectors = Math.ceil(file.length/ssz)-1;
- var sectors = [];
- for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
- sectors[nsectors-1] = file.slice(nsectors*ssz);
- return sectors;
- }
- /* [MS-CFB] 2.6.4 Red-Black Tree */
- function build_full_paths(FI, FP, Paths) {
- var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
- var dad = [], q = [];
- for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
- for(; j < q.length; ++j) {
- i = q[j];
- L = FI[i].L; R = FI[i].R; C = FI[i].C;
- if(dad[i] === i) {
- if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
- if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
- }
- if(C !== -1 /*NOSTREAM*/) dad[C] = i;
- if(L !== -1 && i != dad[i]) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); }
- if(R !== -1 && i != dad[i]) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); }
- }
- for(i=1; i < pl; ++i) if(dad[i] === i) {
- if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
- else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
- }
- for(i=1; i < pl; ++i) {
- if(FI[i].type === 0 /* unknown */) continue;
- j = i;
- if(j != dad[j]) do {
- j = dad[j];
- FP[i] = FP[j] + "/" + FP[i];
- } while (j !== 0 && -1 !== dad[j] && j != dad[j]);
- dad[i] = -1;
- }
- FP[0] += "/";
- for(i=1; i < pl; ++i) {
- if(FI[i].type !== 2 /* stream */) FP[i] += "/";
- }
- }
- function get_mfat_entry(entry, payload, mini) {
- var start = entry.start, size = entry.size;
- //return (payload.slice(start*MSSZ, start*MSSZ + size));
- var o = [];
- var idx = start;
- while(mini && size > 0 && idx >= 0) {
- o.push(payload.slice(idx * MSSZ, idx * MSSZ + MSSZ));
- size -= MSSZ;
- idx = __readInt32LE(mini, idx * 4);
- }
- if(o.length === 0) return (new_buf(0));
- return (bconcat(o).slice(0, entry.size));
- }
- /** Chase down the rest of the DIFAT chain to build a comprehensive list
- DIFAT chains by storing the next sector number as the last 32 bits */
- function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
- var q = ENDOFCHAIN;
- if(idx === ENDOFCHAIN) {
- if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
- } else if(idx !== -1 /*FREESECT*/) {
- var sector = sectors[idx], m = (ssz>>>2)-1;
- if(!sector) return;
- for(var i = 0; i < m; ++i) {
- if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
- fat_addrs.push(q);
- }
- sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs);
- }
- }
- /** Follow the linked list of sectors for a given starting point */
- function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
- var buf = [], buf_chain = [];
- if(!chkd) chkd = [];
- var modulus = ssz - 1, j = 0, jj = 0;
- for(j=start; j>=0;) {
- chkd[j] = true;
- buf[buf.length] = j;
- buf_chain.push(sectors[j]);
- var addr = fat_addrs[Math.floor(j*4/ssz)];
- jj = ((j*4) & modulus);
- if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
- if(!sectors[addr]) break;
- j = __readInt32LE(sectors[addr], jj);
- }
- return {nodes: buf, data:__toBuffer([buf_chain])};
- }
- /** Chase down the sector linked lists */
- function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
- var sl = sectors.length, sector_list = ([]);
- var chkd = [], buf = [], buf_chain = [];
- var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
- for(i=0; i < sl; ++i) {
- buf = ([]);
- k = (i + dir_start); if(k >= sl) k-=sl;
- if(chkd[k]) continue;
- buf_chain = [];
- var seen = [];
- for(j=k; j>=0;) {
- seen[j] = true;
- chkd[j] = true;
- buf[buf.length] = j;
- buf_chain.push(sectors[j]);
- var addr = fat_addrs[Math.floor(j*4/ssz)];
- jj = ((j*4) & modulus);
- if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
- if(!sectors[addr]) break;
- j = __readInt32LE(sectors[addr], jj);
- if(seen[j]) break;
- }
- sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
- }
- return sector_list;
- }
- /* [MS-CFB] 2.6.1 Compound File Directory Entry */
- function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex, mini) {
- var minifat_store = 0, pl = (Paths.length?2:0);
- var sector = sector_list[dir_start].data;
- var i = 0, namelen = 0, name;
- for(; i < sector.length; i+= 128) {
- var blob = sector.slice(i, i+128);
- prep_blob(blob, 64);
- namelen = blob.read_shift(2);
- name = __utf16le(blob,0,namelen-pl);
- Paths.push(name);
- var o = ({
- name: name,
- type: blob.read_shift(1),
- color: blob.read_shift(1),
- L: blob.read_shift(4, 'i'),
- R: blob.read_shift(4, 'i'),
- C: blob.read_shift(4, 'i'),
- clsid: blob.read_shift(16),
- state: blob.read_shift(4, 'i'),
- start: 0,
- size: 0
- });
- var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
- if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
- var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
- if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
- o.start = blob.read_shift(4, 'i');
- o.size = blob.read_shift(4, 'i');
- if(o.size < 0 && o.start < 0) { o.size = o.type = 0; o.start = ENDOFCHAIN; o.name = ""; }
- if(o.type === 5) { /* root */
- minifat_store = o.start;
- if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
- /*minifat_size = o.size;*/
- } else if(o.size >= 4096 /* MSCSZ */) {
- o.storage = 'fat';
- if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
- sector_list[o.start].name = o.name;
- o.content = (sector_list[o.start].data.slice(0,o.size));
- } else {
- o.storage = 'minifat';
- if(o.size < 0) o.size = 0;
- else if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN && sector_list[minifat_store]) {
- o.content = get_mfat_entry(o, sector_list[minifat_store].data, (sector_list[mini]||{}).data);
- }
- }
- if(o.content) prep_blob(o.content, 0);
- files[name] = o;
- FileIndex.push(o);
- }
- }
- function read_date(blob, offset) {
- return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
- }
- function read_file(filename, options) {
- get_fs();
- return parse(fs.readFileSync(filename), options);
- }
- function read(blob, options) {
- switch(options && options.type || "base64") {
- case "file": return read_file(blob, options);
- case "base64": return parse(s2a(Base64.decode(blob)), options);
- case "binary": return parse(s2a(blob), options);
- }
- return parse(blob, options);
- }
- function init_cfb(cfb, opts) {
- var o = opts || {}, root = o.root || "Root Entry";
- if(!cfb.FullPaths) cfb.FullPaths = [];
- if(!cfb.FileIndex) cfb.FileIndex = [];
- if(cfb.FullPaths.length !== cfb.FileIndex.length) throw new Error("inconsistent CFB structure");
- if(cfb.FullPaths.length === 0) {
- cfb.FullPaths[0] = root + "/";
- cfb.FileIndex[0] = ({ name: root, type: 5 });
- }
- if(o.CLSID) cfb.FileIndex[0].clsid = o.CLSID;
- seed_cfb(cfb);
- }
- function seed_cfb(cfb) {
- var nm = "\u0001Sh33tJ5";
- if(CFB.find(cfb, "/" + nm)) return;
- var p = new_buf(4); p[0] = 55; p[1] = p[3] = 50; p[2] = 54;
- cfb.FileIndex.push(({ name: nm, type: 2, content:p, size:4, L:69, R:69, C:69 }));
- cfb.FullPaths.push(cfb.FullPaths[0] + nm);
- rebuild_cfb(cfb);
- }
- function rebuild_cfb(cfb, f) {
- init_cfb(cfb);
- var gc = false, s = false;
- for(var i = cfb.FullPaths.length - 1; i >= 0; --i) {
- var _file = cfb.FileIndex[i];
- switch(_file.type) {
- case 0:
- if(s) gc = true;
- else { cfb.FileIndex.pop(); cfb.FullPaths.pop(); }
- break;
- case 1: case 2: case 5:
- s = true;
- if(isNaN(_file.R * _file.L * _file.C)) gc = true;
- if(_file.R > -1 && _file.L > -1 && _file.R == _file.L) gc = true;
- break;
- default: gc = true; break;
- }
- }
- if(!gc && !f) return;
- var now = new Date(1987, 1, 19), j = 0;
- var data = [];
- for(i = 0; i < cfb.FullPaths.length; ++i) {
- if(cfb.FileIndex[i].type === 0) continue;
- data.push([cfb.FullPaths[i], cfb.FileIndex[i]]);
- }
- for(i = 0; i < data.length; ++i) {
- var dad = dirname(data[i][0]);
- s = false;
- for(j = 0; j < data.length; ++j) if(data[j][0] === dad) s = true;
- if(!s) data.push([dad, ({
- name: filename(dad).replace("/",""),
- type: 1,
- clsid: HEADER_CLSID,
- ct: now, mt: now,
- content: null
- })]);
- }
- data.sort(function(x,y) { return namecmp(x[0], y[0]); });
- cfb.FullPaths = []; cfb.FileIndex = [];
- for(i = 0; i < data.length; ++i) { cfb.FullPaths[i] = data[i][0]; cfb.FileIndex[i] = data[i][1]; }
- for(i = 0; i < data.length; ++i) {
- var elt = cfb.FileIndex[i];
- var nm = cfb.FullPaths[i];
- elt.name = filename(nm).replace("/","");
- elt.L = elt.R = elt.C = -(elt.color = 1);
- elt.size = elt.content ? elt.content.length : 0;
- elt.start = 0;
- elt.clsid = (elt.clsid || HEADER_CLSID);
- if(i === 0) {
- elt.C = data.length > 1 ? 1 : -1;
- elt.size = 0;
- elt.type = 5;
- } else if(nm.slice(-1) == "/") {
- for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==nm) break;
- elt.C = j >= data.length ? -1 : j;
- for(j=i+1;j < data.length; ++j) if(dirname(cfb.FullPaths[j])==dirname(nm)) break;
- elt.R = j >= data.length ? -1 : j;
- elt.type = 1;
- } else {
- if(dirname(cfb.FullPaths[i+1]||"") == dirname(nm)) elt.R = i + 1;
- elt.type = 2;
- }
- }
- }
- function _write(cfb, options) {
- var _opts = options || {};
- rebuild_cfb(cfb);
- if(_opts.fileType == 'zip') return write_zip(cfb, _opts);
- var L = (function(cfb){
- var mini_size = 0, fat_size = 0;
- for(var i = 0; i < cfb.FileIndex.length; ++i) {
- var file = cfb.FileIndex[i];
- if(!file.content) continue;
- var flen = file.content.length;
- if(flen > 0){
- if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6;
- else fat_size += (flen + 0x01FF) >> 9;
- }
- }
- var dir_cnt = (cfb.FullPaths.length +3) >> 2;
- var mini_cnt = (mini_size + 7) >> 3;
- var mfat_cnt = (mini_size + 0x7F) >> 7;
- var fat_base = mini_cnt + fat_size + dir_cnt + mfat_cnt;
- var fat_cnt = (fat_base + 0x7F) >> 7;
- var difat_cnt = fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
- while(((fat_base + fat_cnt + difat_cnt + 0x7F) >> 7) > fat_cnt) difat_cnt = ++fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
- var L = [1, difat_cnt, fat_cnt, mfat_cnt, dir_cnt, fat_size, mini_size, 0];
- cfb.FileIndex[0].size = mini_size << 6;
- L[7] = (cfb.FileIndex[0].start=L[0]+L[1]+L[2]+L[3]+L[4]+L[5])+((L[6]+7) >> 3);
- return L;
- })(cfb);
- var o = new_buf(L[7] << 9);
- var i = 0, T = 0;
- {
- for(i = 0; i < 8; ++i) o.write_shift(1, HEADER_SIG[i]);
- for(i = 0; i < 8; ++i) o.write_shift(2, 0);
- o.write_shift(2, 0x003E);
- o.write_shift(2, 0x0003);
- o.write_shift(2, 0xFFFE);
- o.write_shift(2, 0x0009);
- o.write_shift(2, 0x0006);
- for(i = 0; i < 3; ++i) o.write_shift(2, 0);
- o.write_shift(4, 0);
- o.write_shift(4, L[2]);
- o.write_shift(4, L[0] + L[1] + L[2] + L[3] - 1);
- o.write_shift(4, 0);
- o.write_shift(4, 1<<12);
- o.write_shift(4, L[3] ? L[0] + L[1] + L[2] - 1: ENDOFCHAIN);
- o.write_shift(4, L[3]);
- o.write_shift(-4, L[1] ? L[0] - 1: ENDOFCHAIN);
- o.write_shift(4, L[1]);
- for(i = 0; i < 109; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
- }
- if(L[1]) {
- for(T = 0; T < L[1]; ++T) {
- for(; i < 236 + T * 127; ++i) o.write_shift(-4, i < L[2] ? L[1] + i : -1);
- o.write_shift(-4, T === L[1] - 1 ? ENDOFCHAIN : T + 1);
- }
- }
- var chainit = function(w) {
- for(T += w; i<T-1; ++i) o.write_shift(-4, i+1);
- if(w) { ++i; o.write_shift(-4, ENDOFCHAIN); }
- };
- T = i = 0;
- for(T+=L[1]; i<T; ++i) o.write_shift(-4, consts.DIFSECT);
- for(T+=L[2]; i<T; ++i) o.write_shift(-4, consts.FATSECT);
- chainit(L[3]);
- chainit(L[4]);
- var j = 0, flen = 0;
- var file = cfb.FileIndex[0];
- for(; j < cfb.FileIndex.length; ++j) {
- file = cfb.FileIndex[j];
- if(!file.content) continue;
- flen = file.content.length;
- if(flen < 0x1000) continue;
- file.start = T;
- chainit((flen + 0x01FF) >> 9);
- }
- chainit((L[6] + 7) >> 3);
- while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
- T = i = 0;
- for(j = 0; j < cfb.FileIndex.length; ++j) {
- file = cfb.FileIndex[j];
- if(!file.content) continue;
- flen = file.content.length;
- if(!flen || flen >= 0x1000) continue;
- file.start = T;
- chainit((flen + 0x3F) >> 6);
- }
- while(o.l & 0x1FF) o.write_shift(-4, consts.ENDOFCHAIN);
- for(i = 0; i < L[4]<<2; ++i) {
- var nm = cfb.FullPaths[i];
- if(!nm || nm.length === 0) {
- for(j = 0; j < 17; ++j) o.write_shift(4, 0);
- for(j = 0; j < 3; ++j) o.write_shift(4, -1);
- for(j = 0; j < 12; ++j) o.write_shift(4, 0);
- continue;
- }
- file = cfb.FileIndex[i];
- if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN;
- var _nm = (i === 0 && _opts.root) || file.name;
- flen = 2*(_nm.length+1);
- o.write_shift(64, _nm, "utf16le");
- o.write_shift(2, flen);
- o.write_shift(1, file.type);
- o.write_shift(1, file.color);
- o.write_shift(-4, file.L);
- o.write_shift(-4, file.R);
- o.write_shift(-4, file.C);
- if(!file.clsid) for(j = 0; j < 4; ++j) o.write_shift(4, 0);
- else o.write_shift(16, file.clsid, "hex");
- o.write_shift(4, file.state || 0);
- o.write_shift(4, 0); o.write_shift(4, 0);
- o.write_shift(4, 0); o.write_shift(4, 0);
- o.write_shift(4, file.start);
- o.write_shift(4, file.size); o.write_shift(4, 0);
- }
- for(i = 1; i < cfb.FileIndex.length; ++i) {
- file = cfb.FileIndex[i];
- if(file.size >= 0x1000) {
- o.l = (file.start+1) << 9;
- for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
- for(; j & 0x1FF; ++j) o.write_shift(1, 0);
- }
- }
- for(i = 1; i < cfb.FileIndex.length; ++i) {
- file = cfb.FileIndex[i];
- if(file.size > 0 && file.size < 0x1000) {
- for(j = 0; j < file.size; ++j) o.write_shift(1, file.content[j]);
- for(; j & 0x3F; ++j) o.write_shift(1, 0);
- }
- }
- while(o.l < o.length) o.write_shift(1, 0);
- return o;
- }
- /* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
- function find(cfb, path) {
- var UCFullPaths = cfb.FullPaths.map(function(x) { return x.toUpperCase(); });
- var UCPaths = UCFullPaths.map(function(x) { var y = x.split("/"); return y[y.length - (x.slice(-1) == "/" ? 2 : 1)]; });
- var k = false;
- if(path.charCodeAt(0) === 47 /* "/" */) { k = true; path = UCFullPaths[0].slice(0, -1) + path; }
- else k = path.indexOf("/") !== -1;
- var UCPath = path.toUpperCase();
- var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
- if(w !== -1) return cfb.FileIndex[w];
- var m = !UCPath.match(chr1);
- UCPath = UCPath.replace(chr0,'');
- if(m) UCPath = UCPath.replace(chr1,'!');
- for(w = 0; w < UCFullPaths.length; ++w) {
- if((m ? UCFullPaths[w].replace(chr1,'!') : UCFullPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
- if((m ? UCPaths[w].replace(chr1,'!') : UCPaths[w]).replace(chr0,'') == UCPath) return cfb.FileIndex[w];
- }
- return null;
- }
- /** CFB Constants */
- var MSSZ = 64; /* Mini Sector Size = 1<<6 */
- //var MSCSZ = 4096; /* Mini Stream Cutoff Size */
- /* 2.1 Compound File Sector Numbers and Types */
- var ENDOFCHAIN = -2;
- /* 2.2 Compound File Header */
- var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
- var HEADER_SIG = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
- var HEADER_CLSID = '00000000000000000000000000000000';
- var consts = {
- /* 2.1 Compund File Sector Numbers and Types */
- MAXREGSECT: -6,
- DIFSECT: -4,
- FATSECT: -3,
- ENDOFCHAIN: ENDOFCHAIN,
- FREESECT: -1,
- /* 2.2 Compound File Header */
- HEADER_SIGNATURE: HEADER_SIGNATURE,
- HEADER_MINOR_VERSION: '3e00',
- MAXREGSID: -6,
- NOSTREAM: -1,
- HEADER_CLSID: HEADER_CLSID,
- /* 2.6.1 Compound File Directory Entry */
- EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
- };
- function write_file(cfb, filename, options) {
- get_fs();
- var o = _write(cfb, options);
- fs.writeFileSync(filename, o);
- }
- function a2s(o) {
- var out = new Array(o.length);
- for(var i = 0; i < o.length; ++i) out[i] = String.fromCharCode(o[i]);
- return out.join("");
- }
- function write(cfb, options) {
- var o = _write(cfb, options);
- switch(options && options.type) {
- case "file": get_fs(); fs.writeFileSync(options.filename, (o)); return o;
- case "binary": return a2s(o);
- case "base64": return Base64.encode(a2s(o));
- }
- return o;
- }
- /* node < 8.1 zlib does not expose bytesRead, so default to pure JS */
- var _zlib;
- function use_zlib(zlib) { try {
- var InflateRaw = zlib.InflateRaw;
- var InflRaw = new InflateRaw();
- InflRaw._processChunk(new Uint8Array([3, 0]), InflRaw._finishFlushFlag);
- if(InflRaw.bytesRead) _zlib = zlib;
- else throw new Error("zlib does not expose bytesRead");
- } catch(e) {console.error("cannot use native zlib: " + (e.message || e)); } }
- function _inflateRawSync(payload, usz) {
- if(!_zlib) return _inflate(payload, usz);
- var InflateRaw = _zlib.InflateRaw;
- var InflRaw = new InflateRaw();
- var out = InflRaw._processChunk(payload.slice(payload.l), InflRaw._finishFlushFlag);
- payload.l += InflRaw.bytesRead;
- return out;
- }
- function _deflateRawSync(payload) {
- return _zlib ? _zlib.deflateRawSync(payload) : _deflate(payload);
- }
- var CLEN_ORDER = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
- /* 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 ]; */
- 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 ];
- /* 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 ]; */
- 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 ];
- function bit_swap_8(n) { var t = (((((n<<1)|(n<<11)) & 0x22110) | (((n<<5)|(n<<15)) & 0x88440))); return ((t>>16) | (t>>8) |t)&0xFF; }
- var use_typed_arrays = typeof Uint8Array !== 'undefined';
- var bitswap8 = use_typed_arrays ? new Uint8Array(1<<8) : [];
- for(var q = 0; q < (1<<8); ++q) bitswap8[q] = bit_swap_8(q);
- function bit_swap_n(n, b) {
- var rev = bitswap8[n & 0xFF];
- if(b <= 8) return rev >>> (8-b);
- rev = (rev << 8) | bitswap8[(n>>8)&0xFF];
- if(b <= 16) return rev >>> (16-b);
- rev = (rev << 8) | bitswap8[(n>>16)&0xFF];
- return rev >>> (24-b);
- }
- /* helpers for unaligned bit reads */
- 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; }
- 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; }
- 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; }
- 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; }
- 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; }
- /* works up to n = 3 * 8 + 1 = 25 */
- function read_bits_n(buf, bl, n) {
- var w = (bl&7), h = (bl>>>3), f = ((1<<n)-1);
- var v = buf[h] >>> w;
- if(n < 8 - w) return v & f;
- v |= buf[h+1]<<(8-w);
- if(n < 16 - w) return v & f;
- v |= buf[h+2]<<(16-w);
- if(n < 24 - w) return v & f;
- v |= buf[h+3]<<(24-w);
- return v & f;
- }
- /* until ArrayBuffer#realloc is a thing, fake a realloc */
- function realloc(b, sz) {
- var L = b.length, M = 2*L > sz ? 2*L : sz + 5, i = 0;
- if(L >= sz) return b;
- if(has_buf) {
- var o = new_unsafe_buf(M);
- // $FlowIgnore
- if(b.copy) b.copy(o);
- else for(; i < b.length; ++i) o[i] = b[i];
- return o;
- } else if(use_typed_arrays) {
- var a = new Uint8Array(M);
- if(a.set) a.set(b);
- else for(; i < b.length; ++i) a[i] = b[i];
- return a;
- }
- b.length = M;
- return b;
- }
- /* zero-filled arrays for older browsers */
- function zero_fill_array(n) {
- var o = new Array(n);
- for(var i = 0; i < n; ++i) o[i] = 0;
- return o;
- }var _deflate = (function() {
- var _deflateRaw = (function() {
- return function deflateRaw(data, out) {
- var boff = 0;
- while(boff < data.length) {
- var L = Math.min(0xFFFF, data.length - boff);
- var h = boff + L == data.length;
- /* TODO: this is only type 0 stored */
- out.write_shift(1, +h);
- out.write_shift(2, L);
- out.write_shift(2, (~L) & 0xFFFF);
- while(L-- > 0) out[out.l++] = data[boff++];
- }
- return out.l;
- };
- })();
- return function(data) {
- var buf = new_buf(50+Math.floor(data.length*1.1));
- var off = _deflateRaw(data, buf);
- return buf.slice(0, off);
- };
- })();
- /* modified inflate function also moves original read head */
- /* build tree (used for literals and lengths) */
- function build_tree(clens, cmap, MAX) {
- var maxlen = 1, w = 0, i = 0, j = 0, ccode = 0, L = clens.length;
- var bl_count = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
- for(i = 0; i < 32; ++i) bl_count[i] = 0;
- for(i = L; i < MAX; ++i) clens[i] = 0;
- L = clens.length;
- var ctree = use_typed_arrays ? new Uint16Array(L) : zero_fill_array(L); // []
- /* build code tree */
- for(i = 0; i < L; ++i) {
- bl_count[(w = clens[i])]++;
- if(maxlen < w) maxlen = w;
- ctree[i] = 0;
- }
- bl_count[0] = 0;
- for(i = 1; i <= maxlen; ++i) bl_count[i+16] = (ccode = (ccode + bl_count[i-1])<<1);
- for(i = 0; i < L; ++i) {
- ccode = clens[i];
- if(ccode != 0) ctree[i] = bl_count[ccode+16]++;
- }
- /* cmap[maxlen + 4 bits] = (off&15) + (lit<<4) reverse mapping */
- var cleni = 0;
- for(i = 0; i < L; ++i) {
- cleni = clens[i];
- if(cleni != 0) {
- ccode = bit_swap_n(ctree[i], maxlen)>>(maxlen-cleni);
- for(j = (1<<(maxlen + 4 - cleni)) - 1; j>=0; --j)
- cmap[ccode|(j<<cleni)] = (cleni&15) | (i<<4);
- }
- }
- return maxlen;
- }
- var fix_lmap = use_typed_arrays ? new Uint16Array(512) : zero_fill_array(512);
- var fix_dmap = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
- if(!use_typed_arrays) {
- for(var i = 0; i < 512; ++i) fix_lmap[i] = 0;
- for(i = 0; i < 32; ++i) fix_dmap[i] = 0;
- }
- (function() {
- var dlens = [];
- var i = 0;
- for(;i<32; i++) dlens.push(5);
- build_tree(dlens, fix_dmap, 32);
- var clens = [];
- i = 0;
- for(; i<=143; i++) clens.push(8);
- for(; i<=255; i++) clens.push(9);
- for(; i<=279; i++) clens.push(7);
- for(; i<=287; i++) clens.push(8);
- build_tree(clens, fix_lmap, 288);
- })();
- var dyn_lmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
- var dyn_dmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
- var dyn_cmap = use_typed_arrays ? new Uint16Array(128) : zero_fill_array(128);
- var dyn_len_1 = 1, dyn_len_2 = 1;
- /* 5.5.3 Expanding Huffman Codes */
- function dyn(data, boff) {
- /* nomenclature from RFC1951 refers to bit values; these are offset by the implicit constant */
- var _HLIT = read_bits_5(data, boff) + 257; boff += 5;
- var _HDIST = read_bits_5(data, boff) + 1; boff += 5;
- var _HCLEN = read_bits_4(data, boff) + 4; boff += 4;
- var w = 0;
- /* grab and store code lengths */
- var clens = use_typed_arrays ? new Uint8Array(19) : zero_fill_array(19);
- var ctree = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
- var maxlen = 1;
- var bl_count = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
- var next_code = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
- var L = clens.length; /* 19 */
- for(var i = 0; i < _HCLEN; ++i) {
- clens[CLEN_ORDER[i]] = w = read_bits_3(data, boff);
- if(maxlen < w) maxlen = w;
- bl_count[w]++;
- boff += 3;
- }
- /* build code tree */
- var ccode = 0;
- bl_count[0] = 0;
- for(i = 1; i <= maxlen; ++i) next_code[i] = ccode = (ccode + bl_count[i-1])<<1;
- for(i = 0; i < L; ++i) if((ccode = clens[i]) != 0) ctree[i] = next_code[ccode]++;
- /* cmap[7 bits from stream] = (off&7) + (lit<<3) */
- var cleni = 0;
- for(i = 0; i < L; ++i) {
- cleni = clens[i];
- if(cleni != 0) {
- ccode = bitswap8[ctree[i]]>>(8-cleni);
- for(var j = (1<<(7-cleni))-1; j>=0; --j) dyn_cmap[ccode|(j<<cleni)] = (cleni&7) | (i<<3);
- }
- }
- /* read literal and dist codes at once */
- var hcodes = [];
- maxlen = 1;
- for(; hcodes.length < _HLIT + _HDIST;) {
- ccode = dyn_cmap[read_bits_7(data, boff)];
- boff += ccode & 7;
- switch((ccode >>>= 3)) {
- case 16:
- w = 3 + read_bits_2(data, boff); boff += 2;
- ccode = hcodes[hcodes.length - 1];
- while(w-- > 0) hcodes.push(ccode);
- break;
- case 17:
- w = 3 + read_bits_3(data, boff); boff += 3;
- while(w-- > 0) hcodes.push(0);
- break;
- case 18:
- w = 11 + read_bits_7(data, boff); boff += 7;
- while(w -- > 0) hcodes.push(0);
- break;
- default:
- hcodes.push(ccode);
- if(maxlen < ccode) maxlen = ccode;
- break;
- }
- }
- /* build literal / length trees */
- var h1 = hcodes.slice(0, _HLIT), h2 = hcodes.slice(_HLIT);
- for(i = _HLIT; i < 286; ++i) h1[i] = 0;
- for(i = _HDIST; i < 30; ++i) h2[i] = 0;
- dyn_len_1 = build_tree(h1, dyn_lmap, 286);
- dyn_len_2 = build_tree(h2, dyn_dmap, 30);
- return boff;
- }
- /* return [ data, bytesRead ] */
- function inflate(data, usz) {
- /* shortcircuit for empty buffer [0x03, 0x00] */
- if(data[0] == 3 && !(data[1] & 0x3)) { return [new_raw_buf(usz), 2]; }
- /* bit offset */
- var boff = 0;
- /* header includes final bit and type bits */
- var header = 0;
- var outbuf = new_unsafe_buf(usz ? usz : (1<<18));
- var woff = 0;
- var OL = outbuf.length>>>0;
- var max_len_1 = 0, max_len_2 = 0;
- while((header&1) == 0) {
- header = read_bits_3(data, boff); boff += 3;
- if((header >>> 1) == 0) {
- /* Stored block */
- if(boff & 7) boff += 8 - (boff&7);
- /* 2 bytes sz, 2 bytes bit inverse */
- var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8;
- boff += 32;
- /* push sz bytes */
- if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; }
- if(typeof data.copy === 'function') {
- // $FlowIgnore
- data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz);
- woff += sz; boff += 8*sz;
- } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; }
- continue;
- } else if((header >>> 1) == 1) {
- /* Fixed Huffman */
- max_len_1 = 9; max_len_2 = 5;
- } else {
- /* Dynamic Huffman */
- boff = dyn(data, boff);
- max_len_1 = dyn_len_1; max_len_2 = dyn_len_2;
- }
- if(!usz && (OL < woff + 32767)) { outbuf = realloc(outbuf, woff + 32767); OL = outbuf.length; }
- for(;;) { // while(true) is apparently out of vogue in modern JS circles
- /* ingest code and move read head */
- var bits = read_bits_n(data, boff, max_len_1);
- var code = (header>>>1) == 1 ? fix_lmap[bits] : dyn_lmap[bits];
- boff += code & 15; code >>>= 4;
- /* 0-255 are literals, 256 is end of block token, 257+ are copy tokens */
- if(((code>>>8)&0xFF) === 0) outbuf[woff++] = code;
- else if(code == 256) break;
- else {
- code -= 257;
- var len_eb = (code < 8) ? 0 : ((code-4)>>2); if(len_eb > 5) len_eb = 0;
- var tgt = woff + LEN_LN[code];
- /* length extra bits */
- if(len_eb > 0) {
- tgt += read_bits_n(data, boff, len_eb);
- boff += len_eb;
- }
- /* dist code */
- bits = read_bits_n(data, boff, max_len_2);
- code = (header>>>1) == 1 ? fix_dmap[bits] : dyn_dmap[bits];
- boff += code & 15; code >>>= 4;
- var dst_eb = (code < 4 ? 0 : (code-2)>>1);
- var dst = DST_LN[code];
- /* dist extra bits */
- if(dst_eb > 0) {
- dst += read_bits_n(data, boff, dst_eb);
- boff += dst_eb;
- }
- /* in the common case, manual byte copy is faster than TA set / Buffer copy */
- if(!usz && OL < tgt) { outbuf = realloc(outbuf, tgt); OL = outbuf.length; }
- while(woff < tgt) { outbuf[woff] = outbuf[woff - dst]; ++woff; }
- }
- }
- }
- return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3];
- }
- function _inflate(payload, usz) {
- var data = payload.slice(payload.l||0);
- var out = inflate(data, usz);
- payload.l += out[1];
- return out[0];
- }
- function warn_or_throw(wrn, msg) {
- if(wrn) { if(typeof console !== 'undefined') console.error(msg); }
- else throw new Error(msg);
- }
- function parse_zip(file, options) {
- var blob = file;
- prep_blob(blob, 0);
- var FileIndex = [], FullPaths = [];
- var o = {
- FileIndex: FileIndex,
- FullPaths: FullPaths
- };
- init_cfb(o, { root: options.root });
- /* find end of central directory, start just after signature */
- var i = blob.length - 4;
- while((blob[i] != 0x50 || blob[i+1] != 0x4b || blob[i+2] != 0x05 || blob[i+3] != 0x06) && i >= 0) --i;
- blob.l = i + 4;
- /* parse end of central directory */
- blob.l += 4;
- var fcnt = blob.read_shift(2);
- blob.l += 6;
- var start_cd = blob.read_shift(4);
- /* parse central directory */
- blob.l = start_cd;
- for(i = 0; i < fcnt; ++i) {
- /* trust local file header instead of CD entry */
- blob.l += 20;
- var csz = blob.read_shift(4);
- var usz = blob.read_shift(4);
- var namelen = blob.read_shift(2);
- var efsz = blob.read_shift(2);
- var fcsz = blob.read_shift(2);
- blob.l += 8;
- var offset = blob.read_shift(4);
- var EF = parse_extra_field(blob.slice(blob.l+namelen, blob.l+namelen+efsz));
- blob.l += namelen + efsz + fcsz;
- var L = blob.l;
- blob.l = offset + 4;
- parse_local_file(blob, csz, usz, o, EF);
- blob.l = L;
- }
- return o;
- }
- /* head starts just after local file header signature */
- function parse_local_file(blob, csz, usz, o, EF) {
- /* [local file header] */
- blob.l += 2;
- var flags = blob.read_shift(2);
- var meth = blob.read_shift(2);
- var date = parse_dos_date(blob);
- if(flags & 0x2041) throw new Error("Unsupported ZIP encryption");
- var crc32 = blob.read_shift(4);
- var _csz = blob.read_shift(4);
- var _usz = blob.read_shift(4);
- var namelen = blob.read_shift(2);
- var efsz = blob.read_shift(2);
- // TODO: flags & (1<<11) // UTF8
- var name = ""; for(var i = 0; i < namelen; ++i) name += String.fromCharCode(blob[blob.l++]);
- if(efsz) {
- var ef = parse_extra_field(blob.slice(blob.l, blob.l + efsz));
- if((ef[0x5455]||{}).mt) date = ef[0x5455].mt;
- if(((EF||{})[0x5455]||{}).mt) date = EF[0x5455].mt;
- }
- blob.l += efsz;
- /* [encryption header] */
- /* [file data] */
- var data = blob.slice(blob.l, blob.l + _csz);
- switch(meth) {
- case 8: data = _inflateRawSync(blob, _usz); break;
- case 0: break;
- default: throw new Error("Unsupported ZIP Compression method " + meth);
- }
- /* [data descriptor] */
- var wrn = false;
- if(flags & 8) {
- crc32 = blob.read_shift(4);
- if(crc32 == 0x08074b50) { crc32 = blob.read_shift(4); wrn = true; }
- _csz = blob.read_shift(4);
- _usz = blob.read_shift(4);
- }
- if(_csz != csz) warn_or_throw(wrn, "Bad compressed size: " + csz + " != " + _csz);
- if(_usz != usz) warn_or_throw(wrn, "Bad uncompressed size: " + usz + " != " + _usz);
- var _crc32 = CRC32.buf(data, 0);
- if((crc32>>0) != (_crc32>>0)) warn_or_throw(wrn, "Bad CRC32 checksum: " + crc32 + " != " + _crc32);
- cfb_add(o, name, data, {unsafe: true, mt: date});
- }
- function write_zip(cfb, options) {
- var _opts = options || {};
- var out = [], cdirs = [];
- var o = new_buf(1);
- var method = (_opts.compression ? 8 : 0), flags = 0;
- var desc = false;
- if(desc) flags |= 8;
- var i = 0, j = 0;
- var start_cd = 0, fcnt = 0;
- var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
- var crcs = [];
- var sz_cd = 0;
- for(i = 1; i < cfb.FullPaths.length; ++i) {
- fp = cfb.FullPaths[i].slice(root.length); fi = cfb.FileIndex[i];
- if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
- var start = start_cd;
- /* TODO: CP437 filename */
- var namebuf = new_buf(fp.length);
- for(j = 0; j < fp.length; ++j) namebuf.write_shift(1, fp.charCodeAt(j) & 0x7F);
- namebuf = namebuf.slice(0, namebuf.l);
- crcs[fcnt] = CRC32.buf(fi.content, 0);
- var outbuf = fi.content;
- if(method == 8) outbuf = _deflateRawSync(outbuf);
- /* local file header */
- o = new_buf(30);
- o.write_shift(4, 0x04034b50);
- o.write_shift(2, 20);
- o.write_shift(2, flags);
- o.write_shift(2, method);
- /* TODO: last mod file time/date */
- if(fi.mt) write_dos_date(o, fi.mt);
- else o.write_shift(4, 0);
- o.write_shift(-4, (flags & 8) ? 0 : crcs[fcnt]);
- o.write_shift(4, (flags & 8) ? 0 : outbuf.length);
- o.write_shift(4, (flags & 8) ? 0 : fi.content.length);
- o.write_shift(2, namebuf.length);
- o.write_shift(2, 0);
- start_cd += o.length;
- out.push(o);
- start_cd += namebuf.length;
- out.push(namebuf);
- /* TODO: encryption header ? */
- start_cd += outbuf.length;
- out.push(outbuf);
- /* data descriptor */
- if(flags & 8) {
- o = new_buf(12);
- o.write_shift(-4, crcs[fcnt]);
- o.write_shift(4, outbuf.length);
- o.write_shift(4, fi.content.length);
- start_cd += o.l;
- out.push(o);
- }
- /* central directory */
- o = new_buf(46);
- o.write_shift(4, 0x02014b50);
- o.write_shift(2, 0);
- o.write_shift(2, 20);
- o.write_shift(2, flags);
- o.write_shift(2, method);
- o.write_shift(4, 0); /* TODO: last mod file time/date */
- o.write_shift(-4, crcs[fcnt]);
- o.write_shift(4, outbuf.length);
- o.write_shift(4, fi.content.length);
- o.write_shift(2, namebuf.length);
- o.write_shift(2, 0);
- o.write_shift(2, 0);
- o.write_shift(2, 0);
- o.write_shift(2, 0);
- o.write_shift(4, 0);
- o.write_shift(4, start);
- sz_cd += o.l;
- cdirs.push(o);
- sz_cd += namebuf.length;
- cdirs.push(namebuf);
- ++fcnt;
- }
- /* end of central directory */
- o = new_buf(22);
- o.write_shift(4, 0x06054b50);
- o.write_shift(2, 0);
- o.write_shift(2, 0);
- o.write_shift(2, fcnt);
- o.write_shift(2, fcnt);
- o.write_shift(4, sz_cd);
- o.write_shift(4, start_cd);
- o.write_shift(2, 0);
- return bconcat(([bconcat((out)), bconcat(cdirs), o]));
- }
- function cfb_new(opts) {
- var o = ({});
- init_cfb(o, opts);
- return o;
- }
- function cfb_add(cfb, name, content, opts) {
- var unsafe = opts && opts.unsafe;
- if(!unsafe) init_cfb(cfb);
- var file = !unsafe && CFB.find(cfb, name);
- if(!file) {
- var fpath = cfb.FullPaths[0];
- if(name.slice(0, fpath.length) == fpath) fpath = name;
- else {
- if(fpath.slice(-1) != "/") fpath += "/";
- fpath = (fpath + name).replace("//","/");
- }
- file = ({name: filename(name), type: 2});
- cfb.FileIndex.push(file);
- cfb.FullPaths.push(fpath);
- if(!unsafe) CFB.utils.cfb_gc(cfb);
- }
- file.content = (content);
- file.size = content ? content.length : 0;
- if(opts) {
- if(opts.CLSID) file.clsid = opts.CLSID;
- if(opts.mt) file.mt = opts.mt;
- if(opts.ct) file.ct = opts.ct;
- }
- return file;
- }
- function cfb_del(cfb, name) {
- init_cfb(cfb);
- var file = CFB.find(cfb, name);
- if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
- cfb.FileIndex.splice(j, 1);
- cfb.FullPaths.splice(j, 1);
- return true;
- }
- return false;
- }
- function cfb_mov(cfb, old_name, new_name) {
- init_cfb(cfb);
- var file = CFB.find(cfb, old_name);
- if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
- cfb.FileIndex[j].name = filename(new_name);
- cfb.FullPaths[j] = new_name;
- return true;
- }
- return false;
- }
- function cfb_gc(cfb) { rebuild_cfb(cfb, true); }
- exports.find = find;
- exports.read = read;
- exports.parse = parse;
- exports.write = write;
- exports.writeFile = write_file;
- exports.utils = {
- cfb_new: cfb_new,
- cfb_add: cfb_add,
- cfb_del: cfb_del,
- cfb_mov: cfb_mov,
- cfb_gc: cfb_gc,
- ReadShift: ReadShift,
- CheckField: CheckField,
- prep_blob: prep_blob,
- bconcat: bconcat,
- use_zlib: use_zlib,
- _deflateRaw: _deflate,
- _inflateRaw: _inflate,
- consts: consts
- };
- return exports;
- })();
- if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
- var _fs;
- if(typeof require !== 'undefined') try { _fs = require('fs'); } catch(e) {}
- /* normalize data for blob ctor */
- function blobify(data) {
- if(typeof data === "string") return s2ab(data);
- if(Array.isArray(data)) return a2u(data);
- return data;
- }
- /* write or download file */
- function write_dl(fname, payload, enc) {
- /*global IE_SaveFile, Blob, navigator, saveAs, document, File, chrome */
- if(typeof _fs !== 'undefined' && _fs.writeFileSync) return enc ? _fs.writeFileSync(fname, payload, enc) : _fs.writeFileSync(fname, payload);
- var data = (enc == "utf8") ? utf8write(payload) : payload;
- if(typeof IE_SaveFile !== 'undefined') return IE_SaveFile(data, fname);
- if(typeof Blob !== 'undefined') {
- var blob = new Blob([blobify(data)], {type:"application/octet-stream"});
- if(typeof navigator !== 'undefined' && navigator.msSaveBlob) return navigator.msSaveBlob(blob, fname);
- if(typeof saveAs !== 'undefined') return saveAs(blob, fname);
- if(typeof URL !== 'undefined' && typeof document !== 'undefined' && document.createElement && URL.createObjectURL) {
- var url = URL.createObjectURL(blob);
- if(typeof chrome === 'object' && typeof (chrome.downloads||{}).download == "function") {
- if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
- return chrome.downloads.download({ url: url, filename: fname, saveAs: true});
- }
- var a = document.createElement("a");
- if(a.download != null) {
- a.download = fname; a.href = url; document.body.appendChild(a); a.click();
- document.body.removeChild(a);
- if(URL.revokeObjectURL && typeof setTimeout !== 'undefined') setTimeout(function() { URL.revokeObjectURL(url); }, 60000);
- return url;
- }
- }
- }
- // $FlowIgnore
- if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
- // $FlowIgnore
- var out = File(fname); out.open("w"); out.encoding = "binary";
- if(Array.isArray(payload)) payload = a2s(payload);
- out.write(payload); out.close(); return payload;
- } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
- throw new Error("cannot save file " + fname);
- }
- /* read binary data from file */
- function read_binary(path) {
- if(typeof _fs !== 'undefined') return _fs.readFileSync(path);
- // $FlowIgnore
- if(typeof $ !== 'undefined' && typeof File !== 'undefined' && typeof Folder !== 'undefined') try { // extendscript
- // $FlowIgnore
- var infile = File(path); infile.open("r"); infile.encoding = "binary";
- var data = infile.read(); infile.close();
- return data;
- } catch(e) { if(!e.message || !e.message.match(/onstruct/)) throw e; }
- throw new Error("Cannot access file " + path);
- }
- function keys(o) {
- var ks = Object.keys(o), o2 = [];
- for(var i = 0; i < ks.length; ++i) if(Object.prototype.hasOwnProperty.call(o, ks[i])) o2.push(ks[i]);
- return o2;
- }
- function evert_key(obj, key) {
- var o = ([]), K = keys(obj);
- for(var i = 0; i !== K.length; ++i) if(o[obj[K[i]][key]] == null) o[obj[K[i]][key]] = K[i];
- return o;
- }
- function evert(obj) {
- var o = ([]), K = keys(obj);
- for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = K[i];
- return o;
- }
- function evert_num(obj) {
- var o = ([]), K = keys(obj);
- for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = parseInt(K[i],10);
- return o;
- }
- function evert_arr(obj) {
- var o = ([]), K = keys(obj);
- for(var i = 0; i !== K.length; ++i) {
- if(o[obj[K[i]]] == null) o[obj[K[i]]] = [];
- o[obj[K[i]]].push(K[i]);
- }
- return o;
- }
- var basedate = new Date(1899, 11, 30, 0, 0, 0); // 2209161600000
- function datenum(v, date1904) {
- var epoch = v.getTime();
- if(date1904) epoch -= 1462*24*60*60*1000;
- var dnthresh = basedate.getTime() + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
- return (epoch - dnthresh) / (24 * 60 * 60 * 1000);
- }
- var refdate = new Date();
- var dnthresh = basedate.getTime() + (refdate.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
- var refoffset = refdate.getTimezoneOffset();
- function numdate(v) {
- var out = new Date();
- out.setTime(v * 24 * 60 * 60 * 1000 + dnthresh);
- if (out.getTimezoneOffset() !== refoffset) {
- out.setTime(out.getTime() + (out.getTimezoneOffset() - refoffset) * 60000);
- }
- return out;
- }
- /* ISO 8601 Duration */
- function parse_isodur(s) {
- var sec = 0, mt = 0, time = false;
- var m = s.match(/P([0-9\.]+Y)?([0-9\.]+M)?([0-9\.]+D)?T([0-9\.]+H)?([0-9\.]+M)?([0-9\.]+S)?/);
- if(!m) throw new Error("|" + s + "| is not an ISO8601 Duration");
- for(var i = 1; i != m.length; ++i) {
- if(!m[i]) continue;
- mt = 1;
- if(i > 3) time = true;
- switch(m[i].slice(m[i].length-1)) {
- case 'Y':
- throw new Error("Unsupported ISO Duration Field: " + m[i].slice(m[i].length-1));
- case 'D': mt *= 24;
- /* falls through */
- case 'H': mt *= 60;
- /* falls through */
- case 'M':
- if(!time) throw new Error("Unsupported ISO Duration Field: M");
- else mt *= 60;
- /* falls through */
- case 'S': break;
- }
- sec += mt * parseInt(m[i], 10);
- }
- return sec;
- }
- var good_pd_date = new Date('2017-02-19T19:06:09.000Z');
- if(isNaN(good_pd_date.getFullYear())) good_pd_date = new Date('2/19/17');
- var good_pd = good_pd_date.getFullYear() == 2017;
- /* parses a date as a local date */
- function parseDate(str, fixdate) {
- var d = new Date(str);
- if(good_pd) {
- if(fixdate > 0) d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
- else if(fixdate < 0) d.setTime(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
- return d;
- }
- if(str instanceof Date) return str;
- if(good_pd_date.getFullYear() == 1917 && !isNaN(d.getFullYear())) {
- var s = d.getFullYear();
- if(str.indexOf("" + s) > -1) return d;
- d.setFullYear(d.getFullYear() + 100); return d;
- }
- var n = str.match(/\d+/g)||["2017","2","19","0","0","0"];
- var out = new Date(+n[0], +n[1] - 1, +n[2], (+n[3]||0), (+n[4]||0), (+n[5]||0));
- if(str.indexOf("Z") > -1) out = new Date(out.getTime() - out.getTimezoneOffset() * 60 * 1000);
- return out;
- }
- function cc2str(arr) {
- var o = "";
- for(var i = 0; i != arr.length; ++i) o += String.fromCharCode(arr[i]);
- return o;
- }
- function dup(o) {
- if(typeof JSON != 'undefined' && !Array.isArray(o)) return JSON.parse(JSON.stringify(o));
- if(typeof o != 'object' || o == null) return o;
- if(o instanceof Date) return new Date(o.getTime());
- var out = {};
- for(var k in o) if(Object.prototype.hasOwnProperty.call(o, k)) out[k] = dup(o[k]);
- return out;
- }
- function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
- /* TODO: stress test */
- function fuzzynum(s) {
- var v = Number(s);
- if(!isNaN(v)) return v;
- if(!/\d/.test(s)) return v;
- var wt = 1;
- var ss = s.replace(/([\d]),([\d])/g,"$1$2").replace(/[$]/g,"").replace(/[%]/g, function() { wt *= 100; return "";});
- if(!isNaN(v = Number(ss))) return v / wt;
- ss = ss.replace(/[(](.*)[)]/,function($$, $1) { wt = -wt; return $1;});
- if(!isNaN(v = Number(ss))) return v / wt;
- return v;
- }
- function fuzzydate(s) {
- var o = new Date(s), n = new Date(NaN);
- var y = o.getYear(), m = o.getMonth(), d = o.getDate();
- if(isNaN(d)) return n;
- if(y < 0 || y > 8099) return n;
- if((m > 0 || d > 1) && y != 101) return o;
- if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
- if(s.match(/[^-0-9:,\/\\]/)) return n;
- return o;
- }
- var safe_split_regex = "abacaba".split(/(:?b)/i).length == 5;
- function split_regex(str, re, def) {
- if(safe_split_regex || typeof re == "string") return str.split(re);
- var p = str.split(re), o = [p[0]];
- for(var i = 1; i < p.length; ++i) { o.push(def); o.push(p[i]); }
- return o;
- }
- function getdatastr(data) {
- if(!data) return null;
- if(data.data) return debom(data.data);
- if(data.asNodeBuffer && has_buf) return debom(data.asNodeBuffer().toString('binary'));
- if(data.asBinary) return debom(data.asBinary());
- if(data._data && data._data.getContent) return debom(cc2str(Array.prototype.slice.call(data._data.getContent(),0)));
- if(data.content && data.type) return debom(cc2str(data.content));
- return null;
- }
- function getdatabin(data) {
- if(!data) return null;
- if(data.data) return char_codes(data.data);
- if(data.asNodeBuffer && has_buf) return data.asNodeBuffer();
- if(data._data && data._data.getContent) {
- var o = data._data.getContent();
- if(typeof o == "string") return char_codes(o);
- return Array.prototype.slice.call(o);
- }
- if(data.content && data.type) return data.content;
- return null;
- }
- function getdata(data) { return (data && data.name.slice(-4) === ".bin") ? getdatabin(data) : getdatastr(data); }
- /* Part 2 Section 10.1.2 "Mapping Content Types" Names are case-insensitive */
- /* OASIS does not comment on filename case sensitivity */
- function safegetzipfile(zip, file) {
- var k = zip.FullPaths || keys(zip.files);
- var f = file.toLowerCase(), g = f.replace(/\\/g,'\/');
- for(var i=0; i<k.length; ++i) {
- var n = k[i].replace(/^Root Entry[\/]/,"").toLowerCase();
- if(f == n || g == n) return zip.FileIndex[i];
- }
- return null;
- }
- function getzipfile(zip, file) {
- var o = safegetzipfile(zip, file);
- if(o == null) throw new Error("Cannot find file " + file + " in zip");
- return o;
- }
- function getzipdata(zip, file, safe) {
- if(!safe) return getdata(getzipfile(zip, file));
- if(!file) return null;
- try { return getzipdata(zip, file); } catch(e) { return null; }
- }
- function getzipstr(zip, file, safe) {
- if(!safe) return getdatastr(getzipfile(zip, file));
- if(!file) return null;
- try { return getzipstr(zip, file); } catch(e) { return null; }
- }
- function zipentries(zip) {
- var k = zip.FullPaths || keys(zip.files), o = [];
- for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i].replace(/^Root Entry[\/]/, ""));
- return o.sort();
- }
- function zip_add_file(zip, path, content) {
- if(zip.FullPaths) CFB.utils.cfb_add(zip, path, typeof content == "string" ? (has_buf ? Buffer_from(content) : s2a(utf8write(content))) : content);
- else zip.file(path, content);
- }
- function zip_new() {
- return CFB.utils.cfb_new();
- }
- function zip_read(d, o) {
- var zip;
- switch(o.type) {
- case "base64": zip = CFB.read(d, { type: "base64" }); break;
- case "binary": zip = CFB.read(d, { type: "binary" }); break;
- case "buffer": case "array": zip = CFB.read(d, { type: "buffer" }); break;
- default: throw new Error("Unrecognized type " + o.type);
- }
- return zip;
- }
- function resolve_path(path, base) {
- if(path.charAt(0) == "/") return path.slice(1);
- var result = base.split('/');
- if(base.slice(-1) != "/") result.pop(); // folder path
- var target = path.split('/');
- while (target.length !== 0) {
- var step = target.shift();
- if (step === '..') result.pop();
- else if (step !== '.') result.push(step);
- }
- return result.join('/');
- }
- var XML_HEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n';
- var attregexg=/([^"\s?>\/]+)\s*=\s*((?:")([^"]*)(?:")|(?:')([^']*)(?:')|([^'">\s]+))/g;
- var tagregex=/<[\/\?]?[a-zA-Z0-9:_-]+(?:\s+[^"\s?>\/]+\s*=\s*(?:"[^"]*"|'[^']*'|[^'">\s=]+))*\s?[\/\?]?>/mg;
- if(!(XML_HEADER.match(tagregex))) tagregex = /<[^>]*>/g;
- var nsregex=/<\w*:/, nsregex2 = /<(\/?)\w+:/;
- function parsexmltag(tag, skip_root, skip_LC) {
- var z = ({});
- var eq = 0, c = 0;
- for(; eq !== tag.length; ++eq) if((c = tag.charCodeAt(eq)) === 32 || c === 10 || c === 13) break;
- if(!skip_root) z[0] = tag.slice(0, eq);
- if(eq === tag.length) return z;
- var m = tag.match(attregexg), j=0, v="", i=0, q="", cc="", quot = 1;
- if(m) for(i = 0; i != m.length; ++i) {
- cc = m[i];
- for(c=0; c != cc.length; ++c) if(cc.charCodeAt(c) === 61) break;
- q = cc.slice(0,c).trim();
- while(cc.charCodeAt(c+1) == 32) ++c;
- quot = ((eq=cc.charCodeAt(c+1)) == 34 || eq == 39) ? 1 : 0;
- v = cc.slice(c+1+quot, cc.length-quot);
- for(j=0;j!=q.length;++j) if(q.charCodeAt(j) === 58) break;
- if(j===q.length) {
- if(q.indexOf("_") > 0) q = q.slice(0, q.indexOf("_")); // from ods
- z[q] = v;
- if(!skip_LC) z[q.toLowerCase()] = v;
- }
- else {
- var k = (j===5 && q.slice(0,5)==="xmlns"?"xmlns":"")+q.slice(j+1);
- if(z[k] && q.slice(j-3,j) == "ext") continue; // from ods
- z[k] = v;
- if(!skip_LC) z[k.toLowerCase()] = v;
- }
- }
- return z;
- }
- function strip_ns(x) { return x.replace(nsregex2, "<$1"); }
- var encodings = {
- '"': '"',
- ''': "'",
- '>': '>',
- '<': '<',
- '&': '&'
- };
- var rencoding = evert(encodings);
- //var rencstr = "&<>'\"".split("");
- // TODO: CP remap (need to read file version to determine OS)
- var unescapexml = (function() {
- /* 22.4.2.4 bstr (Basic String) */
- var encregex = /&(?:quot|apos|gt|lt|amp|#x?([\da-fA-F]+));/ig, coderegex = /_x([\da-fA-F]{4})_/ig;
- return function unescapexml(text) {
- var s = text + '', i = s.indexOf("<![CDATA[");
- 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));});
- var j = s.indexOf("]]>");
- return unescapexml(s.slice(0, i)) + s.slice(i+9,j) + unescapexml(s.slice(j+3));
- };
- })();
- var decregex=/[&<>'"]/g, charegex = /[\u0000-\u0008\u000b-\u001f]/g;
- function escapexml(text){
- var s = text + '';
- return s.replace(decregex, function(y) { return rencoding[y]; }).replace(charegex,function(s) { return "_x" + ("000"+s.charCodeAt(0).toString(16)).slice(-4) + "_";});
- }
- function escapexmltag(text){ return escapexml(text).replace(/ /g,"_x0020_"); }
- var htmlcharegex = /[\u0000-\u001f]/g;
- function escapehtml(text){
- var s = text + '';
- 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) + ";"; });
- }
- function escapexlml(text){
- var s = text + '';
- return s.replace(decregex, function(y) { return rencoding[y]; }).replace(htmlcharegex,function(s) { return "&#x" + (s.charCodeAt(0).toString(16)).toUpperCase() + ";"; });
- }
- /* TODO: handle codepages */
- var xlml_fixstr = (function() {
- var entregex = /&#(\d+);/g;
- function entrepl($$,$1) { return String.fromCharCode(parseInt($1,10)); }
- return function xlml_fixstr(str) { return str.replace(entregex,entrepl); };
- })();
- var xlml_unfixstr = (function() {
- return function xlml_unfixstr(str) { return str.replace(/(\r\n|[\r\n])/g,"\ "); };
- })();
- function parsexmlbool(value) {
- switch(value) {
- case 1: case true: case '1': case 'true': case 'TRUE': return true;
- /* case '0': case 'false': case 'FALSE':*/
- default: return false;
- }
- }
- var utf8read = function utf8reada(orig) {
- var out = "", i = 0, c = 0, d = 0, e = 0, f = 0, w = 0;
- while (i < orig.length) {
- c = orig.charCodeAt(i++);
- if (c < 128) { out += String.fromCharCode(c); continue; }
- d = orig.charCodeAt(i++);
- if (c>191 && c<224) { f = ((c & 31) << 6); f |= (d & 63); out += String.fromCharCode(f); continue; }
- e = orig.charCodeAt(i++);
- if (c < 240) { out += String.fromCharCode(((c & 15) << 12) | ((d & 63) << 6) | (e & 63)); continue; }
- f = orig.charCodeAt(i++);
- w = (((c & 7) << 18) | ((d & 63) << 12) | ((e & 63) << 6) | (f & 63))-65536;
- out += String.fromCharCode(0xD800 + ((w>>>10)&1023));
- out += String.fromCharCode(0xDC00 + (w&1023));
- }
- return out;
- };
- var utf8write = function(orig) {
- var out = [], i = 0, c = 0, d = 0;
- while(i < orig.length) {
- c = orig.charCodeAt(i++);
- switch(true) {
- case c < 128: out.push(String.fromCharCode(c)); break;
- case c < 2048:
- out.push(String.fromCharCode(192 + (c >> 6)));
- out.push(String.fromCharCode(128 + (c & 63)));
- break;
- case c >= 55296 && c < 57344:
- c -= 55296; d = orig.charCodeAt(i++) - 56320 + (c<<10);
- out.push(String.fromCharCode(240 + ((d >>18) & 7)));
- out.push(String.fromCharCode(144 + ((d >>12) & 63)));
- out.push(String.fromCharCode(128 + ((d >> 6) & 63)));
- out.push(String.fromCharCode(128 + (d & 63)));
- break;
- default:
- out.push(String.fromCharCode(224 + (c >> 12)));
- out.push(String.fromCharCode(128 + ((c >> 6) & 63)));
- out.push(String.fromCharCode(128 + (c & 63)));
- }
- }
- return out.join("");
- };
- if(has_buf) {
- var utf8readb = function utf8readb(data) {
- var out = Buffer.alloc(2*data.length), w, i, j = 1, k = 0, ww=0, c;
- for(i = 0; i < data.length; i+=j) {
- j = 1;
- if((c=data.charCodeAt(i)) < 128) w = c;
- else if(c < 224) { w = (c&31)*64+(data.charCodeAt(i+1)&63); j=2; }
- else if(c < 240) { w=(c&15)*4096+(data.charCodeAt(i+1)&63)*64+(data.charCodeAt(i+2)&63); j=3; }
- else { j = 4;
- w = (c & 7)*262144+(data.charCodeAt(i+1)&63)*4096+(data.charCodeAt(i+2)&63)*64+(data.charCodeAt(i+3)&63);
- w -= 65536; ww = 0xD800 + ((w>>>10)&1023); w = 0xDC00 + (w&1023);
- }
- if(ww !== 0) { out[k++] = ww&255; out[k++] = ww>>>8; ww = 0; }
- out[k++] = w%256; out[k++] = w>>>8;
- }
- return out.slice(0,k).toString('ucs2');
- };
- var corpus = "foo bar baz\u00e2\u0098\u0083\u00f0\u009f\u008d\u00a3";
- if(utf8read(corpus) == utf8readb(corpus)) utf8read = utf8readb;
- var utf8readc = function utf8readc(data) { return Buffer_from(data, 'binary').toString('utf8'); };
- if(utf8read(corpus) == utf8readc(corpus)) utf8read = utf8readc;
- utf8write = function(data) { return Buffer_from(data, 'utf8').toString("binary"); };
- }
- // matches <foo>...</foo> extracts content
- var matchtag = (function() {
- var mtcache = ({});
- return function matchtag(f,g) {
- var t = f+"|"+(g||"");
- if(mtcache[t]) return mtcache[t];
- return (mtcache[t] = new RegExp('<(?:\\w+:)?'+f+'(?: xml:space="preserve")?(?:[^>]*)>([\\s\\S]*?)</(?:\\w+:)?'+f+'>',((g||""))));
- };
- })();
- var htmldecode = (function() {
- var entities = [
- ['nbsp', ' '], ['middot', '·'],
- ['quot', '"'], ['apos', "'"], ['gt', '>'], ['lt', '<'], ['amp', '&']
- ].map(function(x) { return [new RegExp('&' + x[0] + ';', "ig"), x[1]]; });
- return function htmldecode(str) {
- var o = str
- // Remove new lines and spaces from start of content
- .replace(/^[\t\n\r ]+/, "")
- // Remove new lines and spaces from end of content
- .replace(/[\t\n\r ]+$/,"")
- // Added line which removes any white space characters after and before html tags
- .replace(/>\s+/g,">").replace(/\s+</g,"<")
- // Replace remaining new lines and spaces with space
- .replace(/[\t\n\r ]+/g, " ")
- // Replace <br> tags with new lines
- .replace(/<\s*[bB][rR]\s*\/?>/g,"\n")
- // Strip HTML elements
- .replace(/<[^>]*>/g,"");
- for(var i = 0; i < entities.length; ++i) o = o.replace(entities[i][0], entities[i][1]);
- return o;
- };
- })();
- var vtregex = (function(){ var vt_cache = {};
- return function vt_regex(bt) {
- if(vt_cache[bt] !== undefined) return vt_cache[bt];
- return (vt_cache[bt] = new RegExp("<(?:vt:)?" + bt + ">([\\s\\S]*?)</(?:vt:)?" + bt + ">", 'g') );
- };})();
- var vtvregex = /<\/?(?:vt:)?variant>/g, vtmregex = /<(?:vt:)([^>]*)>([\s\S]*)</;
- function parseVector(data, opts) {
- var h = parsexmltag(data);
- var matches = data.match(vtregex(h.baseType))||[];
- var res = [];
- if(matches.length != h.size) {
- if(opts.WTF) throw new Error("unexpected vector length " + matches.length + " != " + h.size);
- return res;
- }
- matches.forEach(function(x) {
- var v = x.replace(vtvregex,"").match(vtmregex);
- if(v) res.push({v:utf8read(v[2]), t:v[1]});
- });
- return res;
- }
- var wtregex = /(^\s|\s$|\n)/;
- function writetag(f,g) { return '<' + f + (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f + '>'; }
- function wxt_helper(h) { return keys(h).map(function(k) { return " " + k + '="' + h[k] + '"';}).join(""); }
- function writextag(f,g,h) { return '<' + f + ((h != null) ? wxt_helper(h) : "") + ((g != null) ? (g.match(wtregex)?' xml:space="preserve"' : "") + '>' + g + '</' + f : "/") + '>';}
- function write_w3cdtf(d, t) { try { return d.toISOString().replace(/\.\d*/,""); } catch(e) { if(t) throw e; } return ""; }
- function write_vt(s, xlsx) {
- switch(typeof s) {
- case 'string':
- var o = writextag('vt:lpwstr', escapexml(s));
- if(xlsx) o = o.replace(/"/g, "_x0022_");
- return o;
- case 'number': return writextag((s|0)==s?'vt:i4':'vt:r8', escapexml(String(s)));
- case 'boolean': return writextag('vt:bool',s?'true':'false');
- }
- if(s instanceof Date) return writextag('vt:filetime', write_w3cdtf(s));
- throw new Error("Unable to serialize " + s);
- }
- var XMLNS = ({
- 'dc': 'http://purl.org/dc/elements/1.1/',
- 'dcterms': 'http://purl.org/dc/terms/',
- 'dcmitype': 'http://purl.org/dc/dcmitype/',
- 'mx': 'http://schemas.microsoft.com/office/mac/excel/2008/main',
- 'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
- 'sjs': 'http://schemas.openxmlformats.org/package/2006/sheetjs/core-properties',
- 'vt': 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes',
- 'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
- 'xsd': 'http://www.w3.org/2001/XMLSchema'
- });
- XMLNS.main = [
- 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
- 'http://purl.oclc.org/ooxml/spreadsheetml/main',
- 'http://schemas.microsoft.com/office/excel/2006/main',
- 'http://schemas.microsoft.com/office/excel/2006/2'
- ];
- var XLMLNS = ({
- 'o': 'urn:schemas-microsoft-com:office:office',
- 'x': 'urn:schemas-microsoft-com:office:excel',
- 'ss': 'urn:schemas-microsoft-com:office:spreadsheet',
- 'dt': 'uuid:C2F41010-65B3-11d1-A29F-00AA00C14882',
- 'mv': 'http://macVmlSchemaUri',
- 'v': 'urn:schemas-microsoft-com:vml',
- 'html': 'http://www.w3.org/TR/REC-html40'
- });
- function read_double_le(b, idx) {
- var s = 1 - 2 * (b[idx + 7] >>> 7);
- var e = ((b[idx + 7] & 0x7f) << 4) + ((b[idx + 6] >>> 4) & 0x0f);
- var m = (b[idx+6]&0x0f);
- for(var i = 5; i >= 0; --i) m = m * 256 + b[idx + i];
- if(e == 0x7ff) return m == 0 ? (s * Infinity) : NaN;
- if(e == 0) e = -1022;
- else { e -= 1023; m += Math.pow(2,52); }
- return s * Math.pow(2, e - 52) * m;
- }
- function write_double_le(b, v, idx) {
- var bs = ((((v < 0) || (1/v == -Infinity)) ? 1 : 0) << 7), e = 0, m = 0;
- var av = bs ? (-v) : v;
- if(!isFinite(av)) { e = 0x7ff; m = isNaN(v) ? 0x6969 : 0; }
- else if(av == 0) e = m = 0;
- else {
- e = Math.floor(Math.log(av) / Math.LN2);
- m = av * Math.pow(2, 52 - e);
- if((e <= -1023) && (!isFinite(m) || (m < Math.pow(2,52)))) { e = -1022; }
- else { m -= Math.pow(2,52); e+=1023; }
- }
- for(var i = 0; i <= 5; ++i, m/=256) b[idx + i] = m & 0xff;
- b[idx + 6] = ((e & 0x0f) << 4) | (m & 0xf);
- b[idx + 7] = (e >> 4) | bs;
- }
- 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; };
- var ___toBuffer = __toBuffer;
- 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,''); };
- var ___utf16le = __utf16le;
- 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(""); };
- var ___hexlify = __hexlify;
- 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(""); };
- var ___utf8 = __utf8;
- var __lpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
- var ___lpstr = __lpstr;
- var __cpstr = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
- var ___cpstr = __cpstr;
- var __lpwstr = function(b,i) { var len = 2*__readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len-1) : "";};
- var ___lpwstr = __lpwstr;
- var __lpp4, ___lpp4;
- __lpp4 = ___lpp4 = function lpp4_(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf16le(b, i+4,i+4+len) : "";};
- var __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? __utf8(b, i+4,i+4+len) : "";};
- var ___8lpp4 = __8lpp4;
- var __double, ___double;
- __double = ___double = function(b, idx) { return read_double_le(b, idx);};
- var is_buf = function is_buf_a(a) { return Array.isArray(a); };
- if(has_buf) {
- __utf16le = function(b,s,e) { if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e); return b.toString('utf16le',s,e).replace(chr0,'')/*.replace(chr1,'!')*/; };
- __hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
- __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) : "";};
- __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) : "";};
- __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);};
- __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);};
- __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);};
- __utf8 = function utf8_b(b, s, e) { return (Buffer.isBuffer(b)) ? b.toString('utf8',s,e) : ___utf8(b,s,e); };
- __toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
- bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
- __double = function double_(b, i) { if(Buffer.isBuffer(b)) return b.readDoubleLE(i); return ___double(b,i); };
- is_buf = function is_buf_b(a) { return Buffer.isBuffer(a) || Array.isArray(a); };
- }
- /* from js-xls */
- if(typeof cptable !== 'undefined') {
- __utf16le = function(b,s,e) { return cptable.utils.decode(1200, b.slice(s,e)).replace(chr0, ''); };
- __utf8 = function(b,s,e) { return cptable.utils.decode(65001, b.slice(s,e)); };
- __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)) : "";};
- __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)) : "";};
- __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)) : "";};
- __lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(1200, b.slice(i+4,i+4+len)) : "";};
- __8lpp4 = function(b,i) { var len = __readUInt32LE(b,i); return len > 0 ? cptable.utils.decode(65001, b.slice(i+4,i+4+len)) : "";};
- }
- var __readUInt8 = function(b, idx) { return b[idx]; };
- var __readUInt16LE = function(b, idx) { return (b[idx+1]*(1<<8))+b[idx]; };
- var __readInt16LE = function(b, idx) { var u = (b[idx+1]*(1<<8))+b[idx]; return (u < 0x8000) ? u : ((0xffff - u + 1) * -1); };
- var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
- var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)|(b[idx+2]<<16)|(b[idx+1]<<8)|b[idx]; };
- var __readInt32BE = function(b, idx) { return (b[idx]<<24)|(b[idx+1]<<16)|(b[idx+2]<<8)|b[idx+3]; };
- function ReadShift(size, t) {
- var o="", oI, oR, oo=[], w, vv, i, loc;
- switch(t) {
- case 'dbcs':
- loc = this.l;
- if(has_buf && Buffer.isBuffer(this)) o = this.slice(this.l, this.l+2*size).toString("utf16le");
- else for(i = 0; i < size; ++i) { o+=String.fromCharCode(__readUInt16LE(this, loc)); loc+=2; }
- size *= 2;
- break;
- case 'utf8': o = __utf8(this, this.l, this.l + size); break;
- case 'utf16le': size *= 2; o = __utf16le(this, this.l, this.l + size); break;
- case 'wstr':
- if(typeof cptable !== 'undefined') o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l+2*size));
- else return ReadShift.call(this, size, 'dbcs');
- size = 2 * size; break;
- /* [MS-OLEDS] 2.1.4 LengthPrefixedAnsiString */
- case 'lpstr-ansi': o = __lpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
- case 'lpstr-cp': o = __cpstr(this, this.l); size = 4 + __readUInt32LE(this, this.l); break;
- /* [MS-OLEDS] 2.1.5 LengthPrefixedUnicodeString */
- case 'lpwstr': o = __lpwstr(this, this.l); size = 4 + 2 * __readUInt32LE(this, this.l); break;
- /* [MS-OFFCRYPTO] 2.1.2 Length-Prefixed Padded Unicode String (UNICODE-LP-P4) */
- case 'lpp4': size = 4 + __readUInt32LE(this, this.l); o = __lpp4(this, this.l); if(size & 0x02) size += 2; break;
- /* [MS-OFFCRYPTO] 2.1.3 Length-Prefixed UTF-8 String (UTF-8-LP-P4) */
- case '8lpp4': size = 4 + __readUInt32LE(this, this.l); o = __8lpp4(this, this.l); if(size & 0x03) size += 4 - (size & 0x03); break;
- case 'cstr': size = 0; o = "";
- while((w=__readUInt8(this, this.l + size++))!==0) oo.push(_getchar(w));
- o = oo.join(""); break;
- case '_wstr': size = 0; o = "";
- while((w=__readUInt16LE(this,this.l +size))!==0){oo.push(_getchar(w));size+=2;}
- size+=2; o = oo.join(""); break;
- /* sbcs and dbcs support continue records in the SST way TODO codepages */
- case 'dbcs-cont': o = ""; loc = this.l;
- for(i = 0; i < size; ++i) {
- if(this.lens && this.lens.indexOf(loc) !== -1) {
- w = __readUInt8(this, loc);
- this.l = loc + 1;
- vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
- return oo.join("") + vv;
- }
- oo.push(_getchar(__readUInt16LE(this, loc)));
- loc+=2;
- } o = oo.join(""); size *= 2; break;
- case 'cpstr':
- if(typeof cptable !== 'undefined') {
- o = cptable.utils.decode(current_codepage, this.slice(this.l, this.l + size));
- break;
- }
- /* falls through */
- case 'sbcs-cont': o = ""; loc = this.l;
- for(i = 0; i != size; ++i) {
- if(this.lens && this.lens.indexOf(loc) !== -1) {
- w = __readUInt8(this, loc);
- this.l = loc + 1;
- vv = ReadShift.call(this, size-i, w ? 'dbcs-cont' : 'sbcs-cont');
- return oo.join("") + vv;
- }
- oo.push(_getchar(__readUInt8(this, loc)));
- loc+=1;
- } o = oo.join(""); break;
- default:
- switch(size) {
- case 1: oI = __readUInt8(this, this.l); this.l++; return oI;
- case 2: oI = (t === 'i' ? __readInt16LE : __readUInt16LE)(this, this.l); this.l += 2; return oI;
- case 4: case -4:
- if(t === 'i' || ((this[this.l+3] & 0x80)===0)) { oI = ((size > 0) ? __readInt32LE : __readInt32BE)(this, this.l); this.l += 4; return oI; }
- else { oR = __readUInt32LE(this, this.l); this.l += 4; } return oR;
- case 8: case -8:
- if(t === 'f') {
- if(size == 8) oR = __double(this, this.l);
- 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);
- this.l += 8; return oR;
- } else size = 8;
- /* falls through */
- case 16: o = __hexlify(this, this.l, size); break;
- }}
- this.l+=size; return o;
- }
- 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); };
- 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); };
- var __writeUInt16LE = function(b, val, idx) { b[idx] = (val & 0xFF); b[idx+1] = ((val >>> 8) & 0xFF); };
- function WriteShift(t, val, f) {
- var size = 0, i = 0;
- if(f === 'dbcs') {
- for(i = 0; i != val.length; ++i) __writeUInt16LE(this, val.charCodeAt(i), this.l + 2 * i);
- size = 2 * val.length;
- } else if(f === 'sbcs') {
- if(typeof cptable !== 'undefined' && current_ansi == 874) {
- /* TODO: use tables directly, don't encode */
- for(i = 0; i != val.length; ++i) {
- var cppayload = cptable.utils.encode(current_ansi, val.charAt(i));
- this[this.l + i] = cppayload[0];
- }
- } else {
- val = val.replace(/[^\x00-\x7F]/g, "_");
- for(i = 0; i != val.length; ++i) this[this.l + i] = (val.charCodeAt(i) & 0xFF);
- }
- size = val.length;
- } else if(f === 'hex') {
- for(; i < t; ++i) {
- this[this.l++] = (parseInt(val.slice(2*i, 2*i+2), 16)||0);
- } return this;
- } else if(f === 'utf16le') {
- var end = Math.min(this.l + t, this.length);
- for(i = 0; i < Math.min(val.length, t); ++i) {
- var cc = val.charCodeAt(i);
- this[this.l++] = (cc & 0xff);
- this[this.l++] = (cc >> 8);
- }
- while(this.l < end) this[this.l++] = 0;
- return this;
- } else switch(t) {
- case 1: size = 1; this[this.l] = val&0xFF; break;
- case 2: size = 2; this[this.l] = val&0xFF; val >>>= 8; this[this.l+1] = val&0xFF; break;
- 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;
- case 4: size = 4; __writeUInt32LE(this, val, this.l); break;
- case 8: size = 8; if(f === 'f') { write_double_le(this, val, this.l); break; }
- /* falls through */
- case 16: break;
- case -4: size = 4; __writeInt32LE(this, val, this.l); break;
- }
- this.l += size; return this;
- }
- function CheckField(hexstr, fld) {
- var m = __hexlify(this,this.l,hexstr.length>>1);
- if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
- this.l += hexstr.length>>1;
- }
- function prep_blob(blob, pos) {
- blob.l = pos;
- blob.read_shift = ReadShift;
- blob.chk = CheckField;
- blob.write_shift = WriteShift;
- }
- function parsenoop(blob, length) { blob.l += length; }
- function new_buf(sz) {
- var o = new_raw_buf(sz);
- prep_blob(o, 0);
- return o;
- }
- /* [MS-XLSB] 2.1.4 Record */
- function recordhopper(data, cb, opts) {
- if(!data) return;
- var tmpbyte, cntbyte, length;
- prep_blob(data, data.l || 0);
- var L = data.length, RT = 0, tgt = 0;
- while(data.l < L) {
- RT = data.read_shift(1);
- if(RT & 0x80) RT = (RT & 0x7F) + ((data.read_shift(1) & 0x7F)<<7);
- var R = XLSBRecordEnum[RT] || XLSBRecordEnum[0xFFFF];
- tmpbyte = data.read_shift(1);
- length = tmpbyte & 0x7F;
- for(cntbyte = 1; cntbyte <4 && (tmpbyte & 0x80); ++cntbyte) length += ((tmpbyte = data.read_shift(1)) & 0x7F)<<(7*cntbyte);
- tgt = data.l + length;
- var d = (R.f||parsenoop)(data, length, opts);
- data.l = tgt;
- if(cb(d, R.n, RT)) return;
- }
- }
- /* control buffer usage for fixed-length buffers */
- function buf_array() {
- var bufs = [], blksz = has_buf ? 256 : 2048;
- var newblk = function ba_newblk(sz) {
- var o = (new_buf(sz));
- prep_blob(o, 0);
- return o;
- };
- var curbuf = newblk(blksz);
- var endbuf = function ba_endbuf() {
- if(!curbuf) return;
- if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
- if(curbuf.length > 0) bufs.push(curbuf);
- curbuf = null;
- };
- var next = function ba_next(sz) {
- if(curbuf && (sz < (curbuf.length - curbuf.l))) return curbuf;
- endbuf();
- return (curbuf = newblk(Math.max(sz+1, blksz)));
- };
- var end = function ba_end() {
- endbuf();
- return __toBuffer([bufs]);
- };
- var push = function ba_push(buf) { endbuf(); curbuf = buf; if(curbuf.l == null) curbuf.l = curbuf.length; next(blksz); };
- return ({ next:next, push:push, end:end, _bufs:bufs });
- }
- function write_record(ba, type, payload, length) {
- var t = +XLSBRE[type], l;
- if(isNaN(t)) return; // TODO: throw something here?
- if(!length) length = XLSBRecordEnum[t].p || (payload||[]).length || 0;
- l = 1 + (t >= 0x80 ? 1 : 0) + 1/* + length*/;
- if(length >= 0x80) ++l; if(length >= 0x4000) ++l; if(length >= 0x200000) ++l;
- var o = ba.next(l);
- if(t <= 0x7F) o.write_shift(1, t);
- else {
- o.write_shift(1, (t & 0x7F) + 0x80);
- o.write_shift(1, (t >> 7));
- }
- for(var i = 0; i != 4; ++i) {
- if(length >= 0x80) { o.write_shift(1, (length & 0x7F)+0x80); length >>= 7; }
- else { o.write_shift(1, length); break; }
- }
- if(length > 0 && is_buf(payload)) ba.push(payload);
- }
- /* XLS ranges enforced */
- function shift_cell_xls(cell, tgt, opts) {
- var out = dup(cell);
- if(tgt.s) {
- if(out.cRel) out.c += tgt.s.c;
- if(out.rRel) out.r += tgt.s.r;
- } else {
- if(out.cRel) out.c += tgt.c;
- if(out.rRel) out.r += tgt.r;
- }
- if(!opts || opts.biff < 12) {
- while(out.c >= 0x100) out.c -= 0x100;
- while(out.r >= 0x10000) out.r -= 0x10000;
- }
- return out;
- }
- function shift_range_xls(cell, range, opts) {
- var out = dup(cell);
- out.s = shift_cell_xls(out.s, range.s, opts);
- out.e = shift_cell_xls(out.e, range.s, opts);
- return out;
- }
- function encode_cell_xls(c, biff) {
- if(c.cRel && c.c < 0) { c = dup(c); while(c.c < 0) c.c += (biff > 8) ? 0x4000 : 0x100; }
- if(c.rRel && c.r < 0) { c = dup(c); while(c.r < 0) c.r += (biff > 8) ? 0x100000 : ((biff > 5) ? 0x10000 : 0x4000); }
- var s = encode_cell(c);
- if(!c.cRel && c.cRel != null) s = fix_col(s);
- if(!c.rRel && c.rRel != null) s = fix_row(s);
- return s;
- }
- function encode_range_xls(r, opts) {
- if(r.s.r == 0 && !r.s.rRel) {
- if(r.e.r == (opts.biff >= 12 ? 0xFFFFF : (opts.biff >= 8 ? 0x10000 : 0x4000)) && !r.e.rRel) {
- return (r.s.cRel ? "" : "$") + encode_col(r.s.c) + ":" + (r.e.cRel ? "" : "$") + encode_col(r.e.c);
- }
- }
- if(r.s.c == 0 && !r.s.cRel) {
- if(r.e.c == (opts.biff >= 12 ? 0x3FFF : 0xFF) && !r.e.cRel) {
- return (r.s.rRel ? "" : "$") + encode_row(r.s.r) + ":" + (r.e.rRel ? "" : "$") + encode_row(r.e.r);
- }
- }
- return encode_cell_xls(r.s, opts.biff) + ":" + encode_cell_xls(r.e, opts.biff);
- }
- function decode_row(rowstr) { return parseInt(unfix_row(rowstr),10) - 1; }
- function encode_row(row) { return "" + (row + 1); }
- function fix_row(cstr) { return cstr.replace(/([A-Z]|^)(\d+)$/,"$1$$$2"); }
- function unfix_row(cstr) { return cstr.replace(/\$(\d+)$/,"$1"); }
- 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; }
- 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; }
- function fix_col(cstr) { return cstr.replace(/^([A-Z])/,"$$$1"); }
- function unfix_col(cstr) { return cstr.replace(/^\$([A-Z])/,"$1"); }
- function split_cell(cstr) { return cstr.replace(/(\$?[A-Z]*)(\$?\d*)/,"$1,$2").split(","); }
- //function decode_cell(cstr) { var splt = split_cell(cstr); return { c:decode_col(splt[0]), r:decode_row(splt[1]) }; }
- function decode_cell(cstr) {
- var R = 0, C = 0;
- for(var i = 0; i < cstr.length; ++i) {
- var cc = cstr.charCodeAt(i);
- if(cc >= 48 && cc <= 57) R = 10 * R + (cc - 48);
- else if(cc >= 65 && cc <= 90) C = 26 * C + (cc - 64);
- }
- return { c: C - 1, r:R - 1 };
- }
- //function encode_cell(cell) { return encode_col(cell.c) + encode_row(cell.r); }
- function encode_cell(cell) {
- var col = cell.c + 1;
- var s="";
- for(; col; col=((col-1)/26)|0) s = String.fromCharCode(((col-1)%26) + 65) + s;
- return s + (cell.r + 1);
- }
- function decode_range(range) {
- var idx = range.indexOf(":");
- if(idx == -1) return { s: decode_cell(range), e: decode_cell(range) };
- return { s: decode_cell(range.slice(0, idx)), e: decode_cell(range.slice(idx + 1)) };
- }
- function encode_range(cs,ce) {
- if(typeof ce === 'undefined' || typeof ce === 'number') {
- return encode_range(cs.s, cs.e);
- }
- if(typeof cs !== 'string') cs = encode_cell((cs));
- if(typeof ce !== 'string') ce = encode_cell((ce));
- return cs == ce ? cs : cs + ":" + ce;
- }
- function safe_decode_range(range) {
- var o = {s:{c:0,r:0},e:{c:0,r:0}};
- var idx = 0, i = 0, cc = 0;
- var len = range.length;
- for(idx = 0; i < len; ++i) {
- if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
- idx = 26*idx + cc;
- }
- o.s.c = --idx;
- for(idx = 0; i < len; ++i) {
- if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
- idx = 10*idx + cc;
- }
- o.s.r = --idx;
- if(i === len || range.charCodeAt(++i) === 58) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }
- for(idx = 0; i != len; ++i) {
- if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
- idx = 26*idx + cc;
- }
- o.e.c = --idx;
- for(idx = 0; i != len; ++i) {
- if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
- idx = 10*idx + cc;
- }
- o.e.r = --idx;
- return o;
- }
- function safe_format_cell(cell, v) {
- var q = (cell.t == 'd' && v instanceof Date);
- if(cell.z != null) try { return (cell.w = SSF.format(cell.z, q ? datenum(v) : v)); } catch(e) { }
- try { return (cell.w = SSF.format((cell.XF||{}).numFmtId||(q ? 14 : 0), q ? datenum(v) : v)); } catch(e) { return ''+v; }
- }
- function format_cell(cell, v, o) {
- if(cell == null || cell.t == null || cell.t == 'z') return "";
- if(cell.w !== undefined) return cell.w;
- if(cell.t == 'd' && !cell.z && o && o.dateNF) cell.z = o.dateNF;
- if(v == undefined) return safe_format_cell(cell, cell.v);
- return safe_format_cell(cell, v);
- }
- function sheet_to_workbook(sheet, opts) {
- var n = opts && opts.sheet ? opts.sheet : "Sheet1";
- var sheets = {}; sheets[n] = sheet;
- return { SheetNames: [n], Sheets: sheets };
- }
- function sheet_add_aoa(_ws, data, opts) {
- var o = opts || {};
- var dense = _ws ? Array.isArray(_ws) : o.dense;
- if(DENSE != null && dense == null) dense = DENSE;
- var ws = _ws || (dense ? ([]) : ({}));
- var _R = 0, _C = 0;
- if(ws && o.origin != null) {
- if(typeof o.origin == 'number') _R = o.origin;
- else {
- var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
- _R = _origin.r; _C = _origin.c;
- }
- if(!ws["!ref"]) ws["!ref"] = "A1:A1";
- }
- var range = ({s: {c:10000000, r:10000000}, e: {c:0, r:0}});
- if(ws['!ref']) {
- var _range = safe_decode_range(ws['!ref']);
- range.s.c = _range.s.c;
- range.s.r = _range.s.r;
- range.e.c = Math.max(range.e.c, _range.e.c);
- range.e.r = Math.max(range.e.r, _range.e.r);
- if(_R == -1) range.e.r = _R = _range.e.r + 1;
- }
- for(var R = 0; R != data.length; ++R) {
- if(!data[R]) continue;
- if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays");
- for(var C = 0; C != data[R].length; ++C) {
- if(typeof data[R][C] === 'undefined') continue;
- var cell = ({v: data[R][C] });
- var __R = _R + R, __C = _C + C;
- if(range.s.r > __R) range.s.r = __R;
- if(range.s.c > __C) range.s.c = __C;
- if(range.e.r < __R) range.e.r = __R;
- if(range.e.c < __C) range.e.c = __C;
- if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C];
- else {
- if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; }
- if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.sheetStubs) continue; else cell.t = 'z'; }
- else if(typeof cell.v === 'number') cell.t = 'n';
- else if(typeof cell.v === 'boolean') cell.t = 'b';
- else if(cell.v instanceof Date) {
- cell.z = o.dateNF || SSF._table[14];
- if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); }
- else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); }
- }
- else cell.t = 's';
- }
- if(dense) {
- if(!ws[__R]) ws[__R] = [];
- if(ws[__R][__C] && ws[__R][__C].z) cell.z = ws[__R][__C].z;
- ws[__R][__C] = cell;
- } else {
- var cell_ref = encode_cell(({c:__C,r:__R}));
- if(ws[cell_ref] && ws[cell_ref].z) cell.z = ws[cell_ref].z;
- ws[cell_ref] = cell;
- }
- }
- }
- if(range.s.c < 10000000) ws['!ref'] = encode_range(range);
- return ws;
- }
- function aoa_to_sheet(data, opts) { return sheet_add_aoa(null, data, opts); }
- /* [MS-OLEPS] 2.2 PropertyType */
- //var VT_EMPTY = 0x0000;
- //var VT_NULL = 0x0001;
- var VT_I2 = 0x0002;
- var VT_I4 = 0x0003;
- //var VT_R4 = 0x0004;
- //var VT_R8 = 0x0005;
- //var VT_CY = 0x0006;
- //var VT_DATE = 0x0007;
- //var VT_BSTR = 0x0008;
- //var VT_ERROR = 0x000A;
- var VT_BOOL = 0x000B;
- var VT_VARIANT = 0x000C;
- //var VT_DECIMAL = 0x000E;
- //var VT_I1 = 0x0010;
- //var VT_UI1 = 0x0011;
- //var VT_UI2 = 0x0012;
- var VT_UI4 = 0x0013;
- //var VT_I8 = 0x0014;
- //var VT_UI8 = 0x0015;
- //var VT_INT = 0x0016;
- //var VT_UINT = 0x0017;
- var VT_LPSTR = 0x001E;
- //var VT_LPWSTR = 0x001F;
- var VT_FILETIME = 0x0040;
- var VT_BLOB = 0x0041;
- //var VT_STREAM = 0x0042;
- //var VT_STORAGE = 0x0043;
- //var VT_STREAMED_Object = 0x0044;
- //var VT_STORED_Object = 0x0045;
- //var VT_BLOB_Object = 0x0046;
- var VT_CF = 0x0047;
- //var VT_CLSID = 0x0048;
- //var VT_VERSIONED_STREAM = 0x0049;
- var VT_VECTOR = 0x1000;
- //var VT_ARRAY = 0x2000;
- var VT_STRING = 0x0050; // 2.3.3.1.11 VtString
- var VT_USTR = 0x0051; // 2.3.3.1.12 VtUnalignedString
- var VT_CUSTOM = [VT_STRING, VT_USTR];
- /* [MS-OSHARED] 2.3.3.2.2.1 Document Summary Information PIDDSI */
- var DocSummaryPIDDSI = {
- 0x01: { n: 'CodePage', t: VT_I2 },
- 0x02: { n: 'Category', t: VT_STRING },
- 0x03: { n: 'PresentationFormat', t: VT_STRING },
- 0x04: { n: 'ByteCount', t: VT_I4 },
- 0x05: { n: 'LineCount', t: VT_I4 },
- 0x06: { n: 'ParagraphCount', t: VT_I4 },
- 0x07: { n: 'SlideCount', t: VT_I4 },
- 0x08: { n: 'NoteCount', t: VT_I4 },
- 0x09: { n: 'HiddenCount', t: VT_I4 },
- 0x0a: { n: 'MultimediaClipCount', t: VT_I4 },
- 0x0b: { n: 'ScaleCrop', t: VT_BOOL },
- 0x0c: { n: 'HeadingPairs', t: VT_VECTOR | VT_VARIANT },
- 0x0d: { n: 'TitlesOfParts', t: VT_VECTOR | VT_LPSTR },
- 0x0e: { n: 'Manager', t: VT_STRING },
- 0x0f: { n: 'Company', t: VT_STRING },
- 0x10: { n: 'LinksUpToDate', t: VT_BOOL },
- 0x11: { n: 'CharacterCount', t: VT_I4 },
- 0x13: { n: 'SharedDoc', t: VT_BOOL },
- 0x16: { n: 'HyperlinksChanged', t: VT_BOOL },
- 0x17: { n: 'AppVersion', t: VT_I4, p: 'version' },
- 0x18: { n: 'DigSig', t: VT_BLOB },
- 0x1A: { n: 'ContentType', t: VT_STRING },
- 0x1B: { n: 'ContentStatus', t: VT_STRING },
- 0x1C: { n: 'Language', t: VT_STRING },
- 0x1D: { n: 'Version', t: VT_STRING },
- 0xFF: {}
- };
- /* [MS-OSHARED] 2.3.3.2.1.1 Summary Information Property Set PIDSI */
- var SummaryPIDSI = {
- 0x01: { n: 'CodePage', t: VT_I2 },
- 0x02: { n: 'Title', t: VT_STRING },
- 0x03: { n: 'Subject', t: VT_STRING },
- 0x04: { n: 'Author', t: VT_STRING },
- 0x05: { n: 'Keywords', t: VT_STRING },
- 0x06: { n: 'Comments', t: VT_STRING },
- 0x07: { n: 'Template', t: VT_STRING },
- 0x08: { n: 'LastAuthor', t: VT_STRING },
- 0x09: { n: 'RevNumber', t: VT_STRING },
- 0x0A: { n: 'EditTime', t: VT_FILETIME },
- 0x0B: { n: 'LastPrinted', t: VT_FILETIME },
- 0x0C: { n: 'CreatedDate', t: VT_FILETIME },
- 0x0D: { n: 'ModifiedDate', t: VT_FILETIME },
- 0x0E: { n: 'PageCount', t: VT_I4 },
- 0x0F: { n: 'WordCount', t: VT_I4 },
- 0x10: { n: 'CharCount', t: VT_I4 },
- 0x11: { n: 'Thumbnail', t: VT_CF },
- 0x12: { n: 'Application', t: VT_STRING },
- 0x13: { n: 'DocSecurity', t: VT_I4 },
- 0xFF: {}
- };
- /* [MS-OLEPS] 2.18 */
- var SpecialProperties = {
- 0x80000000: { n: 'Locale', t: VT_UI4 },
- 0x80000003: { n: 'Behavior', t: VT_UI4 },
- 0x72627262: {}
- };
- (function() {
- for(var y in SpecialProperties) if(Object.prototype.hasOwnProperty.call(SpecialProperties, y))
- DocSummaryPIDDSI[y] = SummaryPIDSI[y] = SpecialProperties[y];
- })();
- var DocSummaryRE = evert_key(DocSummaryPIDDSI, "n");
- var SummaryRE = evert_key(SummaryPIDSI, "n");
- /* [MS-XLS] 2.4.63 Country/Region codes */
- var CountryEnum = {
- 0x0001: "US", // United States
- 0x0002: "CA", // Canada
- 0x0003: "", // Latin America (except Brazil)
- 0x0007: "RU", // Russia
- 0x0014: "EG", // Egypt
- 0x001E: "GR", // Greece
- 0x001F: "NL", // Netherlands
- 0x0020: "BE", // Belgium
- 0x0021: "FR", // France
- 0x0022: "ES", // Spain
- 0x0024: "HU", // Hungary
- 0x0027: "IT", // Italy
- 0x0029: "CH", // Switzerland
- 0x002B: "AT", // Austria
- 0x002C: "GB", // United Kingdom
- 0x002D: "DK", // Denmark
- 0x002E: "SE", // Sweden
- 0x002F: "NO", // Norway
- 0x0030: "PL", // Poland
- 0x0031: "DE", // Germany
- 0x0034: "MX", // Mexico
- 0x0037: "BR", // Brazil
- 0x003d: "AU", // Australia
- 0x0040: "NZ", // New Zealand
- 0x0042: "TH", // Thailand
- 0x0051: "JP", // Japan
- 0x0052: "KR", // Korea
- 0x0054: "VN", // Viet Nam
- 0x0056: "CN", // China
- 0x005A: "TR", // Turkey
- 0x0069: "JS", // Ramastan
- 0x00D5: "DZ", // Algeria
- 0x00D8: "MA", // Morocco
- 0x00DA: "LY", // Libya
- 0x015F: "PT", // Portugal
- 0x0162: "IS", // Iceland
- 0x0166: "FI", // Finland
- 0x01A4: "CZ", // Czech Republic
- 0x0376: "TW", // Taiwan
- 0x03C1: "LB", // Lebanon
- 0x03C2: "JO", // Jordan
- 0x03C3: "SY", // Syria
- 0x03C4: "IQ", // Iraq
- 0x03C5: "KW", // Kuwait
- 0x03C6: "SA", // Saudi Arabia
- 0x03CB: "AE", // United Arab Emirates
- 0x03CC: "IL", // Israel
- 0x03CE: "QA", // Qatar
- 0x03D5: "IR", // Iran
- 0xFFFF: "US" // United States
- };
- /* [MS-XLS] 2.5.127 */
- var XLSFillPattern = [
- null,
- 'solid',
- 'mediumGray',
- 'darkGray',
- 'lightGray',
- 'darkHorizontal',
- 'darkVertical',
- 'darkDown',
- 'darkUp',
- 'darkGrid',
- 'darkTrellis',
- 'lightHorizontal',
- 'lightVertical',
- 'lightDown',
- 'lightUp',
- 'lightGrid',
- 'lightTrellis',
- 'gray125',
- 'gray0625'
- ];
- function rgbify(arr) { return arr.map(function(x) { return [(x>>16)&255,(x>>8)&255,x&255]; }); }
- /* [MS-XLS] 2.5.161 */
- /* [MS-XLSB] 2.5.75 Icv */
- var _XLSIcv = rgbify([
- /* Color Constants */
- 0x000000,
- 0xFFFFFF,
- 0xFF0000,
- 0x00FF00,
- 0x0000FF,
- 0xFFFF00,
- 0xFF00FF,
- 0x00FFFF,
- /* Overridable Defaults */
- 0x000000,
- 0xFFFFFF,
- 0xFF0000,
- 0x00FF00,
- 0x0000FF,
- 0xFFFF00,
- 0xFF00FF,
- 0x00FFFF,
- 0x800000,
- 0x008000,
- 0x000080,
- 0x808000,
- 0x800080,
- 0x008080,
- 0xC0C0C0,
- 0x808080,
- 0x9999FF,
- 0x993366,
- 0xFFFFCC,
- 0xCCFFFF,
- 0x660066,
- 0xFF8080,
- 0x0066CC,
- 0xCCCCFF,
- 0x000080,
- 0xFF00FF,
- 0xFFFF00,
- 0x00FFFF,
- 0x800080,
- 0x800000,
- 0x008080,
- 0x0000FF,
- 0x00CCFF,
- 0xCCFFFF,
- 0xCCFFCC,
- 0xFFFF99,
- 0x99CCFF,
- 0xFF99CC,
- 0xCC99FF,
- 0xFFCC99,
- 0x3366FF,
- 0x33CCCC,
- 0x99CC00,
- 0xFFCC00,
- 0xFF9900,
- 0xFF6600,
- 0x666699,
- 0x969696,
- 0x003366,
- 0x339966,
- 0x003300,
- 0x333300,
- 0x993300,
- 0x993366,
- 0x333399,
- 0x333333,
- /* Other entries to appease BIFF8/12 */
- 0xFFFFFF, /* 0x40 icvForeground ?? */
- 0x000000, /* 0x41 icvBackground ?? */
- 0x000000, /* 0x42 icvFrame ?? */
- 0x000000, /* 0x43 icv3D ?? */
- 0x000000, /* 0x44 icv3DText ?? */
- 0x000000, /* 0x45 icv3DHilite ?? */
- 0x000000, /* 0x46 icv3DShadow ?? */
- 0x000000, /* 0x47 icvHilite ?? */
- 0x000000, /* 0x48 icvCtlText ?? */
- 0x000000, /* 0x49 icvCtlScrl ?? */
- 0x000000, /* 0x4A icvCtlInv ?? */
- 0x000000, /* 0x4B icvCtlBody ?? */
- 0x000000, /* 0x4C icvCtlFrame ?? */
- 0x000000, /* 0x4D icvCtlFore ?? */
- 0x000000, /* 0x4E icvCtlBack ?? */
- 0x000000, /* 0x4F icvCtlNeutral */
- 0x000000, /* 0x50 icvInfoBk ?? */
- 0x000000 /* 0x51 icvInfoText ?? */
- ]);
- var XLSIcv = dup(_XLSIcv);
- /* [MS-XLSB] 2.5.97.2 */
- var BErr = {
- 0x00: "#NULL!",
- 0x07: "#DIV/0!",
- 0x0F: "#VALUE!",
- 0x17: "#REF!",
- 0x1D: "#NAME?",
- 0x24: "#NUM!",
- 0x2A: "#N/A",
- 0x2B: "#GETTING_DATA",
- 0xFF: "#WTF?"
- };
- var RBErr = evert_num(BErr);
- /* Parts enumerated in OPC spec, MS-XLSB and MS-XLSX */
- /* 12.3 Part Summary <SpreadsheetML> */
- /* 14.2 Part Summary <DrawingML> */
- /* [MS-XLSX] 2.1 Part Enumerations ; [MS-XLSB] 2.1.7 Part Enumeration */
- var ct2type/*{[string]:string}*/ = ({
- /* Workbook */
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml": "workbooks",
- /* Worksheet */
- "application/vnd.ms-excel.binIndexWs": "TODO", /* Binary Index */
- /* Macrosheet */
- "application/vnd.ms-excel.intlmacrosheet": "TODO",
- "application/vnd.ms-excel.binIndexMs": "TODO", /* Binary Index */
- /* File Properties */
- "application/vnd.openxmlformats-package.core-properties+xml": "coreprops",
- "application/vnd.openxmlformats-officedocument.custom-properties+xml": "custprops",
- "application/vnd.openxmlformats-officedocument.extended-properties+xml": "extprops",
- /* Custom Data Properties */
- "application/vnd.openxmlformats-officedocument.customXmlProperties+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.customProperty": "TODO",
- /* PivotTable */
- "application/vnd.ms-excel.pivotTable": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml": "TODO",
- /* Chart Objects */
- "application/vnd.openxmlformats-officedocument.drawingml.chart+xml": "TODO",
- /* Chart Colors */
- "application/vnd.ms-office.chartcolorstyle+xml": "TODO",
- /* Chart Style */
- "application/vnd.ms-office.chartstyle+xml": "TODO",
- /* Chart Advanced */
- "application/vnd.ms-office.chartex+xml": "TODO",
- /* Calculation Chain */
- "application/vnd.ms-excel.calcChain": "calcchains",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml": "calcchains",
- /* Printer Settings */
- "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings": "TODO",
- /* ActiveX */
- "application/vnd.ms-office.activeX": "TODO",
- "application/vnd.ms-office.activeX+xml": "TODO",
- /* Custom Toolbars */
- "application/vnd.ms-excel.attachedToolbars": "TODO",
- /* External Data Connections */
- "application/vnd.ms-excel.connections": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml": "TODO",
- /* External Links */
- "application/vnd.ms-excel.externalLink": "links",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml": "links",
- /* Metadata */
- "application/vnd.ms-excel.sheetMetadata": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml": "TODO",
- /* PivotCache */
- "application/vnd.ms-excel.pivotCacheDefinition": "TODO",
- "application/vnd.ms-excel.pivotCacheRecords": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml": "TODO",
- /* Query Table */
- "application/vnd.ms-excel.queryTable": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml": "TODO",
- /* Shared Workbook */
- "application/vnd.ms-excel.userNames": "TODO",
- "application/vnd.ms-excel.revisionHeaders": "TODO",
- "application/vnd.ms-excel.revisionLog": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml": "TODO",
- /* Single Cell Table */
- "application/vnd.ms-excel.tableSingleCells": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml": "TODO",
- /* Slicer */
- "application/vnd.ms-excel.slicer": "TODO",
- "application/vnd.ms-excel.slicerCache": "TODO",
- "application/vnd.ms-excel.slicer+xml": "TODO",
- "application/vnd.ms-excel.slicerCache+xml": "TODO",
- /* Sort Map */
- "application/vnd.ms-excel.wsSortMap": "TODO",
- /* Table */
- "application/vnd.ms-excel.table": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml": "TODO",
- /* Themes */
- "application/vnd.openxmlformats-officedocument.theme+xml": "themes",
- /* Theme Override */
- "application/vnd.openxmlformats-officedocument.themeOverride+xml": "TODO",
- /* Timeline */
- "application/vnd.ms-excel.Timeline+xml": "TODO", /* verify */
- "application/vnd.ms-excel.TimelineCache+xml": "TODO", /* verify */
- /* VBA */
- "application/vnd.ms-office.vbaProject": "vba",
- "application/vnd.ms-office.vbaProjectSignature": "vba",
- /* Volatile Dependencies */
- "application/vnd.ms-office.volatileDependencies": "TODO",
- "application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml": "TODO",
- /* Control Properties */
- "application/vnd.ms-excel.controlproperties+xml": "TODO",
- /* Data Model */
- "application/vnd.openxmlformats-officedocument.model+data": "TODO",
- /* Survey */
- "application/vnd.ms-excel.Survey+xml": "TODO",
- /* Drawing */
- "application/vnd.openxmlformats-officedocument.drawing+xml": "drawings",
- "application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml": "TODO",
- "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml": "TODO",
- /* VML */
- "application/vnd.openxmlformats-officedocument.vmlDrawing": "TODO",
- "application/vnd.openxmlformats-package.relationships+xml": "rels",
- "application/vnd.openxmlformats-officedocument.oleObject": "TODO",
- /* Image */
- "image/png": "TODO",
- "sheet": "js"
- });
- var CT_LIST = (function(){
- var o = {
- workbooks: {
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
- xlsm: "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
- xlsb: "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
- xlam: "application/vnd.ms-excel.addin.macroEnabled.main+xml",
- xltx: "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"
- },
- strs: { /* Shared Strings */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
- xlsb: "application/vnd.ms-excel.sharedStrings"
- },
- comments: { /* Comments */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
- xlsb: "application/vnd.ms-excel.comments"
- },
- sheets: { /* Worksheet */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
- xlsb: "application/vnd.ms-excel.worksheet"
- },
- charts: { /* Chartsheet */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml",
- xlsb: "application/vnd.ms-excel.chartsheet"
- },
- dialogs: { /* Dialogsheet */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml",
- xlsb: "application/vnd.ms-excel.dialogsheet"
- },
- macros: { /* Macrosheet (Excel 4.0 Macros) */
- xlsx: "application/vnd.ms-excel.macrosheet+xml",
- xlsb: "application/vnd.ms-excel.macrosheet"
- },
- styles: { /* Styles */
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
- xlsb: "application/vnd.ms-excel.styles"
- }
- };
- keys(o).forEach(function(k) { ["xlsm", "xlam"].forEach(function(v) { if(!o[k][v]) o[k][v] = o[k].xlsx; }); });
- keys(o).forEach(function(k){ keys(o[k]).forEach(function(v) { ct2type[o[k][v]] = k; }); });
- return o;
- })();
- var type2ct/*{[string]:Array<string>}*/ = evert_arr(ct2type);
- XMLNS.CT = 'http://schemas.openxmlformats.org/package/2006/content-types';
- function new_ct() {
- return ({
- workbooks:[], sheets:[], charts:[], dialogs:[], macros:[],
- rels:[], strs:[], comments:[], links:[],
- coreprops:[], extprops:[], custprops:[], themes:[], styles:[],
- calcchains:[], vba: [], drawings: [],
- TODO:[], xmlns: "" });
- }
- function parse_ct(data) {
- var ct = new_ct();
- if(!data || !data.match) return ct;
- var ctext = {};
- (data.match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- switch(y[0].replace(nsregex,"<")) {
- case '<?xml': break;
- case '<Types': ct.xmlns = y['xmlns' + (y[0].match(/<(\w+):/)||["",""])[1] ]; break;
- case '<Default': ctext[y.Extension] = y.ContentType; break;
- case '<Override':
- if(ct[ct2type[y.ContentType]] !== undefined) ct[ct2type[y.ContentType]].push(y.PartName);
- break;
- }
- });
- if(ct.xmlns !== XMLNS.CT) throw new Error("Unknown Namespace: " + ct.xmlns);
- ct.calcchain = ct.calcchains.length > 0 ? ct.calcchains[0] : "";
- ct.sst = ct.strs.length > 0 ? ct.strs[0] : "";
- ct.style = ct.styles.length > 0 ? ct.styles[0] : "";
- ct.defaults = ctext;
- delete ct.calcchains;
- return ct;
- }
- var CTYPE_XML_ROOT = writextag('Types', null, {
- 'xmlns': XMLNS.CT,
- 'xmlns:xsd': XMLNS.xsd,
- 'xmlns:xsi': XMLNS.xsi
- });
- var CTYPE_DEFAULTS = [
- ['xml', 'application/xml'],
- ['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
- ['vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'],
- ['data', 'application/vnd.openxmlformats-officedocument.model+data'],
- /* from test files */
- ['bmp', 'image/bmp'],
- ['png', 'image/png'],
- ['gif', 'image/gif'],
- ['emf', 'image/x-emf'],
- ['wmf', 'image/x-wmf'],
- ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
- ['tif', 'image/tiff'], ['tiff', 'image/tiff'],
- ['pdf', 'application/pdf'],
- ['rels', type2ct.rels[0]]
- ].map(function(x) {
- return writextag('Default', null, {'Extension':x[0], 'ContentType': x[1]});
- });
- function write_ct(ct, opts) {
- var o = [], v;
- o[o.length] = (XML_HEADER);
- o[o.length] = (CTYPE_XML_ROOT);
- o = o.concat(CTYPE_DEFAULTS);
- /* only write first instance */
- var f1 = function(w) {
- if(ct[w] && ct[w].length > 0) {
- v = ct[w][0];
- o[o.length] = (writextag('Override', null, {
- 'PartName': (v[0] == '/' ? "":"/") + v,
- 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
- }));
- }
- };
- /* book type-specific */
- var f2 = function(w) {
- (ct[w]||[]).forEach(function(v) {
- o[o.length] = (writextag('Override', null, {
- 'PartName': (v[0] == '/' ? "":"/") + v,
- 'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
- }));
- });
- };
- /* standard type */
- var f3 = function(t) {
- (ct[t]||[]).forEach(function(v) {
- o[o.length] = (writextag('Override', null, {
- 'PartName': (v[0] == '/' ? "":"/") + v,
- 'ContentType': type2ct[t][0]
- }));
- });
- };
- f1('workbooks');
- f2('sheets');
- f2('charts');
- f3('themes');
- ['strs', 'styles'].forEach(f1);
- ['coreprops', 'extprops', 'custprops'].forEach(f3);
- f3('vba');
- f3('comments');
- f3('drawings');
- if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- /* 9.3 Relationships */
- var RELS = ({
- WB: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
- SHEET: "http://sheetjs.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
- HLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
- VML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
- XPATH: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath",
- XMISS: "http://schemas.microsoft.com/office/2006/relationships/xlExternalLinkPath/xlPathMissing",
- XLINK: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
- CXML: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml",
- CXMLP: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps",
- VBA: "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
- });
- /* 9.3.3 Representing Relationships */
- function get_rels_path(file) {
- var n = file.lastIndexOf("/");
- return file.slice(0,n+1) + '_rels/' + file.slice(n+1) + ".rels";
- }
- function parse_rels(data, currentFilePath) {
- var rels = {"!id":{}};
- if (!data) return rels;
- if (currentFilePath.charAt(0) !== '/') {
- currentFilePath = '/'+currentFilePath;
- }
- var hash = {};
- (data.match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- /* 9.3.2.2 OPC_Relationships */
- if (y[0] === '<Relationship') {
- var rel = {}; rel.Type = y.Type; rel.Target = y.Target; rel.Id = y.Id; rel.TargetMode = y.TargetMode;
- var canonictarget = y.TargetMode === 'External' ? y.Target : resolve_path(y.Target, currentFilePath);
- rels[canonictarget] = rel;
- hash[y.Id] = rel;
- }
- });
- rels["!id"] = hash;
- return rels;
- }
- XMLNS.RELS = 'http://schemas.openxmlformats.org/package/2006/relationships';
- var RELS_ROOT = writextag('Relationships', null, {
- //'xmlns:ns0': XMLNS.RELS,
- 'xmlns': XMLNS.RELS
- });
- /* TODO */
- function write_rels(rels) {
- var o = [XML_HEADER, RELS_ROOT];
- keys(rels['!id']).forEach(function(rid) {
- o[o.length] = (writextag('Relationship', null, rels['!id'][rid]));
- });
- if(o.length>2){ o[o.length] = ('</Relationships>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- var RELS_EXTERN = [RELS.HLINK, RELS.XPATH, RELS.XMISS];
- function add_rels(rels, rId, f, type, relobj, targetmode) {
- if(!relobj) relobj = {};
- if(!rels['!id']) rels['!id'] = {};
- if(rId < 0) for(rId = 1; rels['!id']['rId' + rId]; ++rId){/* empty */}
- relobj.Id = 'rId' + rId;
- relobj.Type = type;
- relobj.Target = f;
- if(targetmode) relobj.TargetMode = targetmode;
- else if(RELS_EXTERN.indexOf(relobj.Type) > -1) relobj.TargetMode = "External";
- if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
- rels['!id'][relobj.Id] = relobj;
- rels[('/' + relobj.Target).replace("//","/")] = relobj;
- return rId;
- }
- /* ECMA-376 Part II 11.1 Core Properties Part */
- /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
- var CORE_PROPS = [
- ["cp:category", "Category"],
- ["cp:contentStatus", "ContentStatus"],
- ["cp:keywords", "Keywords"],
- ["cp:lastModifiedBy", "LastAuthor"],
- ["cp:lastPrinted", "LastPrinted"],
- ["cp:revision", "RevNumber"],
- ["cp:version", "Version"],
- ["dc:creator", "Author"],
- ["dc:description", "Comments"],
- ["dc:identifier", "Identifier"],
- ["dc:language", "Language"],
- ["dc:subject", "Subject"],
- ["dc:title", "Title"],
- ["dcterms:created", "CreatedDate", 'date'],
- ["dcterms:modified", "ModifiedDate", 'date']
- ];
- XMLNS.CORE_PROPS = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
- RELS.CORE_PROPS = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
- var CORE_PROPS_REGEX = (function() {
- var r = new Array(CORE_PROPS.length);
- for(var i = 0; i < CORE_PROPS.length; ++i) {
- var f = CORE_PROPS[i];
- var g = "(?:"+ f[0].slice(0,f[0].indexOf(":")) +":)"+ f[0].slice(f[0].indexOf(":")+1);
- r[i] = new RegExp("<" + g + "[^>]*>([\\s\\S]*?)<\/" + g + ">");
- }
- return r;
- })();
- function parse_core_props(data) {
- var p = {};
- data = utf8read(data);
- for(var i = 0; i < CORE_PROPS.length; ++i) {
- var f = CORE_PROPS[i], cur = data.match(CORE_PROPS_REGEX[i]);
- if(cur != null && cur.length > 0) p[f[1]] = unescapexml(cur[1]);
- if(f[2] === 'date' && p[f[1]]) p[f[1]] = parseDate(p[f[1]]);
- }
- return p;
- }
- var CORE_PROPS_XML_ROOT = writextag('cp:coreProperties', null, {
- //'xmlns': XMLNS.CORE_PROPS,
- 'xmlns:cp': XMLNS.CORE_PROPS,
- 'xmlns:dc': XMLNS.dc,
- 'xmlns:dcterms': XMLNS.dcterms,
- 'xmlns:dcmitype': XMLNS.dcmitype,
- 'xmlns:xsi': XMLNS.xsi
- });
- function cp_doit(f, g, h, o, p) {
- if(p[f] != null || g == null || g === "") return;
- p[f] = g;
- g = escapexml(g);
- o[o.length] = (h ? writextag(f,g,h) : writetag(f,g));
- }
- function write_core_props(cp, _opts) {
- var opts = _opts || {};
- var o = [XML_HEADER, CORE_PROPS_XML_ROOT], p = {};
- if(!cp && !opts.Props) return o.join("");
- if(cp) {
- 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);
- 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);
- }
- for(var i = 0; i != CORE_PROPS.length; ++i) {
- var f = CORE_PROPS[i];
- var v = opts.Props && opts.Props[f[1]] != null ? opts.Props[f[1]] : cp ? cp[f[1]] : null;
- if(v === true) v = "1";
- else if(v === false) v = "0";
- else if(typeof v == "number") v = String(v);
- if(v != null) cp_doit(f[0], v, null, o, p);
- }
- if(o.length>2){ o[o.length] = ('</cp:coreProperties>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- /* 15.2.12.3 Extended File Properties Part */
- /* [MS-OSHARED] 2.3.3.2.[1-2].1 (PIDSI/PIDDSI) */
- var EXT_PROPS = [
- ["Application", "Application", "string"],
- ["AppVersion", "AppVersion", "string"],
- ["Company", "Company", "string"],
- ["DocSecurity", "DocSecurity", "string"],
- ["Manager", "Manager", "string"],
- ["HyperlinksChanged", "HyperlinksChanged", "bool"],
- ["SharedDoc", "SharedDoc", "bool"],
- ["LinksUpToDate", "LinksUpToDate", "bool"],
- ["ScaleCrop", "ScaleCrop", "bool"],
- ["HeadingPairs", "HeadingPairs", "raw"],
- ["TitlesOfParts", "TitlesOfParts", "raw"]
- ];
- XMLNS.EXT_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
- RELS.EXT_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties';
- var PseudoPropsPairs = [
- "Worksheets", "SheetNames",
- "NamedRanges", "DefinedNames",
- "Chartsheets", "ChartNames"
- ];
- function load_props_pairs(HP, TOP, props, opts) {
- var v = [];
- if(typeof HP == "string") v = parseVector(HP, opts);
- else for(var j = 0; j < HP.length; ++j) v = v.concat(HP[j].map(function(hp) { return {v:hp}; }));
- var parts = (typeof TOP == "string") ? parseVector(TOP, opts).map(function (x) { return x.v; }) : TOP;
- var idx = 0, len = 0;
- if(parts.length > 0) for(var i = 0; i !== v.length; i += 2) {
- len = +(v[i+1].v);
- switch(v[i].v) {
- case "Worksheets":
- case "工作表":
- case "Листы":
- case "أوراق العمل":
- case "ワークシート":
- case "גליונות עבודה":
- case "Arbeitsblätter":
- case "Çalışma Sayfaları":
- case "Feuilles de calcul":
- case "Fogli di lavoro":
- case "Folhas de cálculo":
- case "Planilhas":
- case "Regneark":
- case "Hojas de cálculo":
- case "Werkbladen":
- props.Worksheets = len;
- props.SheetNames = parts.slice(idx, idx + len);
- break;
- case "Named Ranges":
- case "Rangos con nombre":
- case "名前付き一覧":
- case "Benannte Bereiche":
- case "Navngivne områder":
- props.NamedRanges = len;
- props.DefinedNames = parts.slice(idx, idx + len);
- break;
- case "Charts":
- case "Diagramme":
- props.Chartsheets = len;
- props.ChartNames = parts.slice(idx, idx + len);
- break;
- }
- idx += len;
- }
- }
- function parse_ext_props(data, p, opts) {
- var q = {}; if(!p) p = {};
- data = utf8read(data);
- EXT_PROPS.forEach(function(f) {
- var xml = (data.match(matchtag(f[0]))||[])[1];
- switch(f[2]) {
- case "string": if(xml) p[f[1]] = unescapexml(xml); break;
- case "bool": p[f[1]] = xml === "true"; break;
- case "raw":
- var cur = data.match(new RegExp("<" + f[0] + "[^>]*>([\\s\\S]*?)<\/" + f[0] + ">"));
- if(cur && cur.length > 0) q[f[1]] = cur[1];
- break;
- }
- });
- if(q.HeadingPairs && q.TitlesOfParts) load_props_pairs(q.HeadingPairs, q.TitlesOfParts, p, opts);
- return p;
- }
- var EXT_PROPS_XML_ROOT = writextag('Properties', null, {
- 'xmlns': XMLNS.EXT_PROPS,
- 'xmlns:vt': XMLNS.vt
- });
- function write_ext_props(cp) {
- var o = [], W = writextag;
- if(!cp) cp = {};
- cp.Application = "SheetJS";
- o[o.length] = (XML_HEADER);
- o[o.length] = (EXT_PROPS_XML_ROOT);
- EXT_PROPS.forEach(function(f) {
- if(cp[f[1]] === undefined) return;
- var v;
- switch(f[2]) {
- case 'string': v = escapexml(String(cp[f[1]])); break;
- case 'bool': v = cp[f[1]] ? 'true' : 'false'; break;
- }
- if(v !== undefined) o[o.length] = (W(f[0], v));
- });
- /* TODO: HeadingPairs, TitlesOfParts */
- 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"})));
- 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"})));
- if(o.length>2){ o[o.length] = ('</Properties>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- /* 15.2.12.2 Custom File Properties Part */
- XMLNS.CUST_PROPS = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
- RELS.CUST_PROPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties';
- var custregex = /<[^>]+>[^<]*/g;
- function parse_cust_props(data, opts) {
- var p = {}, name = "";
- var m = data.match(custregex);
- if(m) for(var i = 0; i != m.length; ++i) {
- var x = m[i], y = parsexmltag(x);
- switch(y[0]) {
- case '<?xml': break;
- case '<Properties': break;
- case '<property': name = unescapexml(y.name); break;
- case '</property>': name = null; break;
- default: if (x.indexOf('<vt:') === 0) {
- var toks = x.split('>');
- var type = toks[0].slice(4), text = toks[1];
- /* 22.4.2.32 (CT_Variant). Omit the binary types from 22.4 (Variant Types) */
- switch(type) {
- case 'lpstr': case 'bstr': case 'lpwstr':
- p[name] = unescapexml(text);
- break;
- case 'bool':
- p[name] = parsexmlbool(text);
- break;
- case 'i1': case 'i2': case 'i4': case 'i8': case 'int': case 'uint':
- p[name] = parseInt(text, 10);
- break;
- case 'r4': case 'r8': case 'decimal':
- p[name] = parseFloat(text);
- break;
- case 'filetime': case 'date':
- p[name] = parseDate(text);
- break;
- case 'cy': case 'error':
- p[name] = unescapexml(text);
- break;
- default:
- if(type.slice(-1) == '/') break;
- if(opts.WTF && typeof console !== 'undefined') console.warn('Unexpected', x, type, toks);
- }
- } else if(x.slice(0,2) === "</") {/* empty */
- } else if(opts.WTF) throw new Error(x);
- }
- }
- return p;
- }
- var CUST_PROPS_XML_ROOT = writextag('Properties', null, {
- 'xmlns': XMLNS.CUST_PROPS,
- 'xmlns:vt': XMLNS.vt
- });
- function write_cust_props(cp) {
- var o = [XML_HEADER, CUST_PROPS_XML_ROOT];
- if(!cp) return o.join("");
- var pid = 1;
- keys(cp).forEach(function custprop(k) { ++pid;
- o[o.length] = (writextag('property', write_vt(cp[k], true), {
- 'fmtid': '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}',
- 'pid': pid,
- 'name': escapexml(k)
- }));
- });
- if(o.length>2){ o[o.length] = '</Properties>'; o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- /* from js-harb (C) 2014-present SheetJS */
- var DBF = (function() {
- var dbf_codepage_map = {
- /* Code Pages Supported by Visual FoxPro */
- 0x01: 437, 0x02: 850,
- 0x03: 1252, 0x04: 10000,
- 0x64: 852, 0x65: 866,
- 0x66: 865, 0x67: 861,
- 0x68: 895, 0x69: 620,
- 0x6A: 737, 0x6B: 857,
- 0x78: 950, 0x79: 949,
- 0x7A: 936, 0x7B: 932,
- 0x7C: 874, 0x7D: 1255,
- 0x7E: 1256, 0x96: 10007,
- 0x97: 10029, 0x98: 10006,
- 0xC8: 1250, 0xC9: 1251,
- 0xCA: 1254, 0xCB: 1253,
- /* shapefile DBF extension */
- 0x00: 20127, 0x08: 865,
- 0x09: 437, 0x0A: 850,
- 0x0B: 437, 0x0D: 437,
- 0x0E: 850, 0x0F: 437,
- 0x10: 850, 0x11: 437,
- 0x12: 850, 0x13: 932,
- 0x14: 850, 0x15: 437,
- 0x16: 850, 0x17: 865,
- 0x18: 437, 0x19: 437,
- 0x1A: 850, 0x1B: 437,
- 0x1C: 863, 0x1D: 850,
- 0x1F: 852, 0x22: 852,
- 0x23: 852, 0x24: 860,
- 0x25: 850, 0x26: 866,
- 0x37: 850, 0x40: 852,
- 0x4D: 936, 0x4E: 949,
- 0x4F: 950, 0x50: 874,
- 0x57: 1252, 0x58: 1252,
- 0x59: 1252,
- 0xFF: 16969
- };
- var dbf_reverse_map = evert({
- 0x01: 437, 0x02: 850,
- 0x03: 1252, 0x04: 10000,
- 0x64: 852, 0x65: 866,
- 0x66: 865, 0x67: 861,
- 0x68: 895, 0x69: 620,
- 0x6A: 737, 0x6B: 857,
- 0x78: 950, 0x79: 949,
- 0x7A: 936, 0x7B: 932,
- 0x7C: 874, 0x7D: 1255,
- 0x7E: 1256, 0x96: 10007,
- 0x97: 10029, 0x98: 10006,
- 0xC8: 1250, 0xC9: 1251,
- 0xCA: 1254, 0xCB: 1253,
- 0x00: 20127
- });
- var DBF_SUPPORTED_VERSIONS = [0x02, 0x03, 0x30, 0x31, 0x83, 0x8B, 0x8C, 0xF5];
- /* TODO: find an actual specification */
- function dbf_to_aoa(buf, opts) {
- var out = [];
- /* TODO: browser based */
- var d = (new_raw_buf(1));
- switch(opts.type) {
- case 'base64': d = s2a(Base64.decode(buf)); break;
- case 'binary': d = s2a(buf); break;
- case 'buffer':
- case 'array': d = buf; break;
- }
- prep_blob(d, 0);
- /* header */
- var ft = d.read_shift(1);
- var memo = false;
- var vfp = false, l7 = false;
- switch(ft) {
- case 0x02: case 0x03: break;
- case 0x30: vfp = true; memo = true; break;
- case 0x31: vfp = true; break;
- case 0x83: memo = true; break;
- case 0x8B: memo = true; break;
- case 0x8C: memo = true; l7 = true; break;
- case 0xF5: memo = true; break;
- default: throw new Error("DBF Unsupported Version: " + ft.toString(16));
- }
- var /*filedate = new Date(),*/ nrow = 0, fpos = 0;
- if(ft == 0x02) nrow = d.read_shift(2);
- /*filedate = new Date(d.read_shift(1) + 1900, d.read_shift(1) - 1, d.read_shift(1));*/d.l += 3;
- if(ft != 0x02) nrow = d.read_shift(4);
- if(ft != 0x02) fpos = d.read_shift(2);
- var rlen = d.read_shift(2);
- var /*flags = 0,*/ current_cp = 1252;
- if(ft != 0x02) {
- d.l+=16;
- /*flags = */d.read_shift(1);
- //if(memo && ((flags & 0x02) === 0)) throw new Error("DBF Flags " + flags.toString(16) + " ft " + ft.toString(16));
- /* codepage present in FoxPro */
- if(d[d.l] !== 0) current_cp = dbf_codepage_map[d[d.l]];
- d.l+=1;
- d.l+=2;
- }
- if(l7) d.l += 36;
- var fields = [], field = ({});
- var hend = fpos - 10 - (vfp ? 264 : 0), ww = l7 ? 32 : 11;
- while(ft == 0x02 ? d.l < d.length && d[d.l] != 0x0d: d.l < hend) {
- field = ({});
- field.name = cptable.utils.decode(current_cp, d.slice(d.l, d.l+ww)).replace(/[\u0000\r\n].*$/g,"");
- d.l += ww;
- field.type = String.fromCharCode(d.read_shift(1));
- if(ft != 0x02 && !l7) field.offset = d.read_shift(4);
- field.len = d.read_shift(1);
- if(ft == 0x02) field.offset = d.read_shift(2);
- field.dec = d.read_shift(1);
- if(field.name.length) fields.push(field);
- if(ft != 0x02) d.l += l7 ? 13 : 14;
- switch(field.type) {
- case 'B': // VFP Double
- if((!vfp || field.len != 8) && opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
- break;
- case 'G': // General
- case 'P': // Picture
- if(opts.WTF) console.log('Skipping ' + field.name + ':' + field.type);
- break;
- case 'C': // character
- case 'D': // date
- case 'F': // floating point
- case 'I': // long
- case 'L': // boolean
- case 'M': // memo
- case 'N': // number
- case 'O': // double
- case 'T': // datetime
- case 'Y': // currency
- case '0': // VFP _NullFlags
- case '@': // timestamp
- case '+': // autoincrement
- break;
- default: throw new Error('Unknown Field Type: ' + field.type);
- }
- }
- if(d[d.l] !== 0x0D) d.l = fpos-1;
- else if(ft == 0x02) d.l = 0x209;
- if(ft != 0x02) {
- if(d.read_shift(1) !== 0x0D) throw new Error("DBF Terminator not found " + d.l + " " + d[d.l]);
- d.l = fpos;
- }
- /* data */
- var R = 0, C = 0;
- out[0] = [];
- for(C = 0; C != fields.length; ++C) out[0][C] = fields[C].name;
- while(nrow-- > 0) {
- if(d[d.l] === 0x2A) { d.l+=rlen; continue; }
- ++d.l;
- out[++R] = []; C = 0;
- for(C = 0; C != fields.length; ++C) {
- var dd = d.slice(d.l, d.l+fields[C].len); d.l+=fields[C].len;
- prep_blob(dd, 0);
- var s = cptable.utils.decode(current_cp, dd);
- switch(fields[C].type) {
- case 'C':
- out[R][C] = cptable.utils.decode(current_cp, dd);
- out[R][C] = out[R][C].trim();
- break;
- case 'D':
- if(s.length === 8) out[R][C] = new Date(+s.slice(0,4), +s.slice(4,6)-1, +s.slice(6,8));
- else out[R][C] = s;
- break;
- case 'F': out[R][C] = parseFloat(s.trim()); break;
- case '+': case 'I': out[R][C] = l7 ? dd.read_shift(-4, 'i') ^ 0x80000000 : dd.read_shift(4, 'i'); break;
- case 'L': switch(s.toUpperCase()) {
- case 'Y': case 'T': out[R][C] = true; break;
- case 'N': case 'F': out[R][C] = false; break;
- case ' ': case '?': out[R][C] = false; break; /* NOTE: technically uninitialized */
- default: throw new Error("DBF Unrecognized L:|" + s + "|");
- } break;
- case 'M': /* TODO: handle memo files */
- if(!memo) throw new Error("DBF Unexpected MEMO for type " + ft.toString(16));
- out[R][C] = "##MEMO##" + (l7 ? parseInt(s.trim(), 10): dd.read_shift(4));
- break;
- case 'N': out[R][C] = +s.replace(/\u0000/g,"").trim(); break;
- case '@': out[R][C] = new Date(dd.read_shift(-8, 'f') - 0x388317533400); break;
- case 'T': out[R][C] = new Date((dd.read_shift(4) - 0x253D8C) * 0x5265C00 + dd.read_shift(4)); break;
- case 'Y': out[R][C] = dd.read_shift(4,'i')/1e4; break;
- case 'O': out[R][C] = -dd.read_shift(-8, 'f'); break;
- case 'B': if(vfp && fields[C].len == 8) { out[R][C] = dd.read_shift(8,'f'); break; }
- /* falls through */
- case 'G': case 'P': dd.l += fields[C].len; break;
- case '0':
- if(fields[C].name === '_NullFlags') break;
- /* falls through */
- default: throw new Error("DBF Unsupported data type " + fields[C].type);
- }
- }
- }
- 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));
- if(opts && opts.sheetRows) out = out.slice(0, opts.sheetRows);
- return out;
- }
- function dbf_to_sheet(buf, opts) {
- var o = opts || {};
- if(!o.dateNF) o.dateNF = "yyyymmdd";
- return aoa_to_sheet(dbf_to_aoa(buf, o), o);
- }
- function dbf_to_workbook(buf, opts) {
- try { return sheet_to_workbook(dbf_to_sheet(buf, opts), opts); }
- catch(e) { if(opts && opts.WTF) throw e; }
- return ({SheetNames:[],Sheets:{}});
- }
- var _RLEN = { 'B': 8, 'C': 250, 'L': 1, 'D': 8, '?': 0, '': 0 };
- function sheet_to_dbf(ws, opts) {
- var o = opts || {};
- if(+o.codepage >= 0) set_cp(+o.codepage);
- if(o.type == "string") throw new Error("Cannot write DBF to JS string");
- var ba = buf_array();
- var aoa = sheet_to_json(ws, {header:1, raw:true, cellDates:true});
- var headers = aoa[0], data = aoa.slice(1);
- var i = 0, j = 0, hcnt = 0, rlen = 1;
- for(i = 0; i < headers.length; ++i) {
- if(i == null) continue;
- ++hcnt;
- if(typeof headers[i] === 'number') headers[i] = headers[i].toString(10);
- if(typeof headers[i] !== 'string') throw new Error("DBF Invalid column name " + headers[i] + " |" + (typeof headers[i]) + "|");
- if(headers.indexOf(headers[i]) !== i) for(j=0; j<1024;++j)
- if(headers.indexOf(headers[i] + "_" + j) == -1) { headers[i] += "_" + j; break; }
- }
- var range = safe_decode_range(ws['!ref']);
- var coltypes = [];
- for(i = 0; i <= range.e.c - range.s.c; ++i) {
- var col = [];
- for(j=0; j < data.length; ++j) {
- if(data[j][i] != null) col.push(data[j][i]);
- }
- if(col.length == 0 || headers[i] == null) { coltypes[i] = '?'; continue; }
- var guess = '', _guess = '';
- for(j = 0; j < col.length; ++j) {
- switch(typeof col[j]) {
- /* TODO: check if L2 compat is desired */
- case 'number': _guess = 'B'; break;
- case 'string': _guess = 'C'; break;
- case 'boolean': _guess = 'L'; break;
- case 'object': _guess = col[j] instanceof Date ? 'D' : 'C'; break;
- default: _guess = 'C';
- }
- guess = guess && guess != _guess ? 'C' : _guess;
- if(guess == 'C') break;
- }
- rlen += _RLEN[guess] || 0;
- coltypes[i] = guess;
- }
- var h = ba.next(32);
- h.write_shift(4, 0x13021130);
- h.write_shift(4, data.length);
- h.write_shift(2, 296 + 32 * hcnt);
- h.write_shift(2, rlen);
- for(i=0; i < 4; ++i) h.write_shift(4, 0);
- h.write_shift(4, 0x00000000 | ((+dbf_reverse_map[current_ansi] || 0x03)<<8));
- for(i = 0, j = 0; i < headers.length; ++i) {
- if(headers[i] == null) continue;
- var hf = ba.next(32);
- var _f = (headers[i].slice(-10) + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00").slice(0, 11);
- hf.write_shift(1, _f, "sbcs");
- hf.write_shift(1, coltypes[i] == '?' ? 'C' : coltypes[i], "sbcs");
- hf.write_shift(4, j);
- hf.write_shift(1, _RLEN[coltypes[i]] || 0);
- hf.write_shift(1, 0);
- hf.write_shift(1, 0x02);
- hf.write_shift(4, 0);
- hf.write_shift(1, 0);
- hf.write_shift(4, 0);
- hf.write_shift(4, 0);
- j += _RLEN[coltypes[i]] || 0;
- }
- var hb = ba.next(264);
- hb.write_shift(4, 0x0000000D);
- for(i=0; i < 65;++i) hb.write_shift(4, 0x00000000);
- for(i=0; i < data.length; ++i) {
- var rout = ba.next(rlen);
- rout.write_shift(1, 0);
- for(j=0; j<headers.length; ++j) {
- if(headers[j] == null) continue;
- switch(coltypes[j]) {
- case 'L': rout.write_shift(1, data[i][j] == null ? 0x3F : data[i][j] ? 0x54 : 0x46); break;
- case 'B': rout.write_shift(8, data[i][j]||0, 'f'); break;
- case 'D':
- if(!data[i][j]) rout.write_shift(8, "00000000", "sbcs");
- else {
- rout.write_shift(4, ("0000"+data[i][j].getFullYear()).slice(-4), "sbcs");
- rout.write_shift(2, ("00"+(data[i][j].getMonth()+1)).slice(-2), "sbcs");
- rout.write_shift(2, ("00"+data[i][j].getDate()).slice(-2), "sbcs");
- } break;
- case 'C':
- var _s = String(data[i][j]||"");
- rout.write_shift(1, _s, "sbcs");
- for(hcnt=0; hcnt < 250-_s.length; ++hcnt) rout.write_shift(1, 0x20); break;
- }
- }
- // data
- }
- ba.next(1).write_shift(1, 0x1A);
- return ba.end();
- }
- return {
- versions: DBF_SUPPORTED_VERSIONS,
- to_workbook: dbf_to_workbook,
- to_sheet: dbf_to_sheet,
- from_sheet: sheet_to_dbf
- };
- })();
- var SYLK = (function() {
- /* TODO: stress test sequences */
- var sylk_escapes = ({
- AA:'À', BA:'Á', CA:'Â', DA:195, HA:'Ä', JA:197,
- AE:'È', BE:'É', CE:'Ê', HE:'Ë',
- AI:'Ì', BI:'Í', CI:'Î', HI:'Ï',
- AO:'Ò', BO:'Ó', CO:'Ô', DO:213, HO:'Ö',
- AU:'Ù', BU:'Ú', CU:'Û', HU:'Ü',
- Aa:'à', Ba:'á', Ca:'â', Da:227, Ha:'ä', Ja:229,
- Ae:'è', Be:'é', Ce:'ê', He:'ë',
- Ai:'ì', Bi:'í', Ci:'î', Hi:'ï',
- Ao:'ò', Bo:'ó', Co:'ô', Do:245, Ho:'ö',
- Au:'ù', Bu:'ú', Cu:'û', Hu:'ü',
- KC:'Ç', Kc:'ç', q:'æ', z:'œ', a:'Æ', j:'Œ',
- DN:209, Dn:241, Hy:255,
- S:169, c:170, R:174, B:180,
- 0:176, 1:177, 2:178,
- 3:179, 5:181, 6:182,
- 7:183, Q:185, k:186, b:208, i:216, l:222, s:240, y:248,
- "!":161, '"':162, "#":163, "(":164, "%":165, "'":167, "H ":168,
- "+":171, ";":187, "<":188, "=":189, ">":190, "?":191, "{":223
- });
- var sylk_char_regex = new RegExp("\u001BN(" + keys(sylk_escapes).join("|").replace(/\|\|\|/, "|\\||").replace(/([?()+])/g,"\\$1") + "|\\|)", "gm");
- var sylk_char_fn = function(_, $1){ var o = sylk_escapes[$1]; return typeof o == "number" ? _getansi(o) : o; };
- var decode_sylk_char = function($$, $1, $2) { var newcc = (($1.charCodeAt(0) - 0x20)<<4) | ($2.charCodeAt(0) - 0x30); return newcc == 59 ? $$ : _getansi(newcc); };
- sylk_escapes["|"] = 254;
- /* TODO: find an actual specification */
- function sylk_to_aoa(d, opts) {
- switch(opts.type) {
- case 'base64': return sylk_to_aoa_str(Base64.decode(d), opts);
- case 'binary': return sylk_to_aoa_str(d, opts);
- case 'buffer': return sylk_to_aoa_str(d.toString('binary'), opts);
- case 'array': return sylk_to_aoa_str(cc2str(d), opts);
- }
- throw new Error("Unrecognized type " + opts.type);
- }
- function sylk_to_aoa_str(str, opts) {
- var records = str.split(/[\n\r]+/), R = -1, C = -1, ri = 0, rj = 0, arr = [];
- var formats = [];
- var next_cell_format = null;
- var sht = {}, rowinfo = [], colinfo = [], cw = [];
- var Mval = 0, j;
- if(+opts.codepage >= 0) set_cp(+opts.codepage);
- for (; ri !== records.length; ++ri) {
- Mval = 0;
- var rstr=records[ri].trim().replace(/\x1B([\x20-\x2F])([\x30-\x3F])/g, decode_sylk_char).replace(sylk_char_regex, sylk_char_fn);
- var record=rstr.replace(/;;/g, "\u0000").split(";").map(function(x) { return x.replace(/\u0000/g, ";"); });
- var RT=record[0], val;
- if(rstr.length > 0) switch(RT) {
- case 'ID': break; /* header */
- case 'E': break; /* EOF */
- case 'B': break; /* dimensions */
- case 'O': break; /* options? */
- case 'P':
- if(record[1].charAt(0) == 'P')
- formats.push(rstr.slice(3).replace(/;;/g, ";"));
- break;
- case 'C':
- var C_seen_K = false, C_seen_X = false;
- for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
- case 'X': C = parseInt(record[rj].slice(1))-1; C_seen_X = true; break;
- case 'Y':
- R = parseInt(record[rj].slice(1))-1; if(!C_seen_X) C = 0;
- for(j = arr.length; j <= R; ++j) arr[j] = [];
- break;
- case 'K':
- val = record[rj].slice(1);
- if(val.charAt(0) === '"') val = val.slice(1,val.length - 1);
- else if(val === 'TRUE') val = true;
- else if(val === 'FALSE') val = false;
- else if(!isNaN(fuzzynum(val))) {
- val = fuzzynum(val);
- if(next_cell_format !== null && SSF.is_date(next_cell_format)) val = numdate(val);
- } else if(!isNaN(fuzzydate(val).getDate())) {
- val = parseDate(val);
- }
- if(typeof cptable !== 'undefined' && typeof val == "string" && ((opts||{}).type != "string") && (opts||{}).codepage) val = cptable.utils.decode(opts.codepage, val);
- C_seen_K = true;
- break;
- case 'E':
- var formula = rc_to_a1(record[rj].slice(1), {r:R,c:C});
- arr[R][C] = [arr[R][C], formula];
- break;
- default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
- }
- if(C_seen_K) { arr[R][C] = val; next_cell_format = null; }
- break;
- case 'F':
- var F_seen = 0;
- for(rj=1; rj<record.length; ++rj) switch(record[rj].charAt(0)) {
- case 'X': C = parseInt(record[rj].slice(1))-1; ++F_seen; break;
- case 'Y':
- R = parseInt(record[rj].slice(1))-1; /*C = 0;*/
- for(j = arr.length; j <= R; ++j) arr[j] = [];
- break;
- case 'M': Mval = parseInt(record[rj].slice(1)) / 20; break;
- case 'F': break; /* ??? */
- case 'G': break; /* hide grid */
- case 'P':
- next_cell_format = formats[parseInt(record[rj].slice(1))];
- break;
- case 'S': break; /* cell style */
- case 'D': break; /* column */
- case 'N': break; /* font */
- case 'W':
- cw = record[rj].slice(1).split(" ");
- for(j = parseInt(cw[0], 10); j <= parseInt(cw[1], 10); ++j) {
- Mval = parseInt(cw[2], 10);
- colinfo[j-1] = Mval === 0 ? {hidden:true}: {wch:Mval}; process_col(colinfo[j-1]);
- } break;
- case 'C': /* default column format */
- C = parseInt(record[rj].slice(1))-1;
- if(!colinfo[C]) colinfo[C] = {};
- break;
- case 'R': /* row properties */
- R = parseInt(record[rj].slice(1))-1;
- if(!rowinfo[R]) rowinfo[R] = {};
- if(Mval > 0) { rowinfo[R].hpt = Mval; rowinfo[R].hpx = pt2px(Mval); }
- else if(Mval === 0) rowinfo[R].hidden = true;
- break;
- default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
- }
- if(F_seen < 1) next_cell_format = null; break;
- default: if(opts && opts.WTF) throw new Error("SYLK bad record " + rstr);
- }
- }
- if(rowinfo.length > 0) sht['!rows'] = rowinfo;
- if(colinfo.length > 0) sht['!cols'] = colinfo;
- if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
- return [arr, sht];
- }
- function sylk_to_sheet(d, opts) {
- var aoasht = sylk_to_aoa(d, opts);
- var aoa = aoasht[0], ws = aoasht[1];
- var o = aoa_to_sheet(aoa, opts);
- keys(ws).forEach(function(k) { o[k] = ws[k]; });
- return o;
- }
- function sylk_to_workbook(d, opts) { return sheet_to_workbook(sylk_to_sheet(d, opts), opts); }
- function write_ws_cell_sylk(cell, ws, R, C) {
- var o = "C;Y" + (R+1) + ";X" + (C+1) + ";K";
- switch(cell.t) {
- case 'n':
- o += (cell.v||0);
- if(cell.f && !cell.F) o += ";E" + a1_to_rc(cell.f, {r:R, c:C}); break;
- case 'b': o += cell.v ? "TRUE" : "FALSE"; break;
- case 'e': o += cell.w || cell.v; break;
- case 'd': o += '"' + (cell.w || cell.v) + '"'; break;
- case 's': o += '"' + cell.v.replace(/"/g,"") + '"'; break;
- }
- return o;
- }
- function write_ws_cols_sylk(out, cols) {
- cols.forEach(function(col, i) {
- var rec = "F;W" + (i+1) + " " + (i+1) + " ";
- if(col.hidden) rec += "0";
- else {
- if(typeof col.width == 'number') col.wpx = width2px(col.width);
- if(typeof col.wpx == 'number') col.wch = px2char(col.wpx);
- if(typeof col.wch == 'number') rec += Math.round(col.wch);
- }
- if(rec.charAt(rec.length - 1) != " ") out.push(rec);
- });
- }
- function write_ws_rows_sylk(out, rows) {
- rows.forEach(function(row, i) {
- var rec = "F;";
- if(row.hidden) rec += "M0;";
- else if(row.hpt) rec += "M" + 20 * row.hpt + ";";
- else if(row.hpx) rec += "M" + 20 * px2pt(row.hpx) + ";";
- if(rec.length > 2) out.push(rec + "R" + (i+1));
- });
- }
- function sheet_to_sylk(ws, opts) {
- var preamble = ["ID;PWXL;N;E"], o = [];
- var r = safe_decode_range(ws['!ref']), cell;
- var dense = Array.isArray(ws);
- var RS = "\r\n";
- preamble.push("P;PGeneral");
- preamble.push("F;P0;DG0G8;M255");
- if(ws['!cols']) write_ws_cols_sylk(preamble, ws['!cols']);
- if(ws['!rows']) write_ws_rows_sylk(preamble, ws['!rows']);
- 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(" "));
- for(var R = r.s.r; R <= r.e.r; ++R) {
- for(var C = r.s.c; C <= r.e.c; ++C) {
- var coord = encode_cell({r:R,c:C});
- cell = dense ? (ws[R]||[])[C]: ws[coord];
- if(!cell || (cell.v == null && (!cell.f || cell.F))) continue;
- o.push(write_ws_cell_sylk(cell, ws, R, C, opts));
- }
- }
- return preamble.join(RS) + RS + o.join(RS) + RS + "E" + RS;
- }
- return {
- to_workbook: sylk_to_workbook,
- to_sheet: sylk_to_sheet,
- from_sheet: sheet_to_sylk
- };
- })();
- var DIF = (function() {
- function dif_to_aoa(d, opts) {
- switch(opts.type) {
- case 'base64': return dif_to_aoa_str(Base64.decode(d), opts);
- case 'binary': return dif_to_aoa_str(d, opts);
- case 'buffer': return dif_to_aoa_str(d.toString('binary'), opts);
- case 'array': return dif_to_aoa_str(cc2str(d), opts);
- }
- throw new Error("Unrecognized type " + opts.type);
- }
- function dif_to_aoa_str(str, opts) {
- var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
- for (; ri !== records.length; ++ri) {
- if (records[ri].trim() === 'BOT') { arr[++R] = []; C = 0; continue; }
- if (R < 0) continue;
- var metadata = records[ri].trim().split(",");
- var type = metadata[0], value = metadata[1];
- ++ri;
- var data = records[ri].trim();
- switch (+type) {
- case -1:
- if (data === 'BOT') { arr[++R] = []; C = 0; continue; }
- else if (data !== 'EOD') throw new Error("Unrecognized DIF special command " + data);
- break;
- case 0:
- if(data === 'TRUE') arr[R][C] = true;
- else if(data === 'FALSE') arr[R][C] = false;
- else if(!isNaN(fuzzynum(value))) arr[R][C] = fuzzynum(value);
- else if(!isNaN(fuzzydate(value).getDate())) arr[R][C] = parseDate(value);
- else arr[R][C] = value;
- ++C; break;
- case 1:
- data = data.slice(1,data.length-1);
- arr[R][C++] = data !== '' ? data : null;
- break;
- }
- if (data === 'EOD') break;
- }
- if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
- return arr;
- }
- function dif_to_sheet(str, opts) { return aoa_to_sheet(dif_to_aoa(str, opts), opts); }
- function dif_to_workbook(str, opts) { return sheet_to_workbook(dif_to_sheet(str, opts), opts); }
- var sheet_to_dif = (function() {
- var push_field = function pf(o, topic, v, n, s) {
- o.push(topic);
- o.push(v + "," + n);
- o.push('"' + s.replace(/"/g,'""') + '"');
- };
- var push_value = function po(o, type, v, s) {
- o.push(type + "," + v);
- o.push(type == 1 ? '"' + s.replace(/"/g,'""') + '"' : s);
- };
- return function sheet_to_dif(ws) {
- var o = [];
- var r = safe_decode_range(ws['!ref']), cell;
- var dense = Array.isArray(ws);
- push_field(o, "TABLE", 0, 1, "sheetjs");
- push_field(o, "VECTORS", 0, r.e.r - r.s.r + 1,"");
- push_field(o, "TUPLES", 0, r.e.c - r.s.c + 1,"");
- push_field(o, "DATA", 0, 0,"");
- for(var R = r.s.r; R <= r.e.r; ++R) {
- push_value(o, -1, 0, "BOT");
- for(var C = r.s.c; C <= r.e.c; ++C) {
- var coord = encode_cell({r:R,c:C});
- cell = dense ? (ws[R]||[])[C] : ws[coord];
- if(!cell) { push_value(o, 1, 0, ""); continue;}
- switch(cell.t) {
- case 'n':
- var val = DIF_XL ? cell.w : cell.v;
- if(!val && cell.v != null) val = cell.v;
- if(val == null) {
- if(DIF_XL && cell.f && !cell.F) push_value(o, 1, 0, "=" + cell.f);
- else push_value(o, 1, 0, "");
- }
- else push_value(o, 0, val, "V");
- break;
- case 'b':
- push_value(o, 0, cell.v ? 1 : 0, cell.v ? "TRUE" : "FALSE");
- break;
- case 's':
- push_value(o, 1, 0, (!DIF_XL || isNaN(cell.v)) ? cell.v : '="' + cell.v + '"');
- break;
- case 'd':
- if(!cell.w) cell.w = SSF.format(cell.z || SSF._table[14], datenum(parseDate(cell.v)));
- if(DIF_XL) push_value(o, 0, cell.w, "V");
- else push_value(o, 1, 0, cell.w);
- break;
- default: push_value(o, 1, 0, "");
- }
- }
- }
- push_value(o, -1, 0, "EOD");
- var RS = "\r\n";
- var oo = o.join(RS);
- //while((oo.length & 0x7F) != 0) oo += "\0";
- return oo;
- };
- })();
- return {
- to_workbook: dif_to_workbook,
- to_sheet: dif_to_sheet,
- from_sheet: sheet_to_dif
- };
- })();
- var ETH = (function() {
- function decode(s) { return s.replace(/\\b/g,"\\").replace(/\\c/g,":").replace(/\\n/g,"\n"); }
- function encode(s) { return s.replace(/\\/g, "\\b").replace(/:/g, "\\c").replace(/\n/g,"\\n"); }
- function eth_to_aoa(str, opts) {
- var records = str.split('\n'), R = -1, C = -1, ri = 0, arr = [];
- for (; ri !== records.length; ++ri) {
- var record = records[ri].trim().split(":");
- if(record[0] !== 'cell') continue;
- var addr = decode_cell(record[1]);
- if(arr.length <= addr.r) for(R = arr.length; R <= addr.r; ++R) if(!arr[R]) arr[R] = [];
- R = addr.r; C = addr.c;
- switch(record[2]) {
- case 't': arr[R][C] = decode(record[3]); break;
- case 'v': arr[R][C] = +record[3]; break;
- case 'vtf': var _f = record[record.length - 1];
- /* falls through */
- case 'vtc':
- switch(record[3]) {
- case 'nl': arr[R][C] = +record[4] ? true : false; break;
- default: arr[R][C] = +record[4]; break;
- }
- if(record[2] == 'vtf') arr[R][C] = [arr[R][C], _f];
- }
- }
- if(opts && opts.sheetRows) arr = arr.slice(0, opts.sheetRows);
- return arr;
- }
- function eth_to_sheet(d, opts) { return aoa_to_sheet(eth_to_aoa(d, opts), opts); }
- function eth_to_workbook(d, opts) { return sheet_to_workbook(eth_to_sheet(d, opts), opts); }
- var header = [
- "socialcalc:version:1.5",
- "MIME-Version: 1.0",
- "Content-Type: multipart/mixed; boundary=SocialCalcSpreadsheetControlSave"
- ].join("\n");
- var sep = [
- "--SocialCalcSpreadsheetControlSave",
- "Content-type: text/plain; charset=UTF-8"
- ].join("\n") + "\n";
- /* TODO: the other parts */
- var meta = [
- "# SocialCalc Spreadsheet Control Save",
- "part:sheet"
- ].join("\n");
- var end = "--SocialCalcSpreadsheetControlSave--";
- function sheet_to_eth_data(ws) {
- if(!ws || !ws['!ref']) return "";
- var o = [], oo = [], cell, coord = "";
- var r = decode_range(ws['!ref']);
- var dense = Array.isArray(ws);
- for(var R = r.s.r; R <= r.e.r; ++R) {
- for(var C = r.s.c; C <= r.e.c; ++C) {
- coord = encode_cell({r:R,c:C});
- cell = dense ? (ws[R]||[])[C] : ws[coord];
- if(!cell || cell.v == null || cell.t === 'z') continue;
- oo = ["cell", coord, 't'];
- switch(cell.t) {
- case 's': case 'str': oo.push(encode(cell.v)); break;
- case 'n':
- if(!cell.f) { oo[2]='v'; oo[3]=cell.v; }
- else { oo[2]='vtf'; oo[3]='n'; oo[4]=cell.v; oo[5]=encode(cell.f); }
- break;
- case 'b':
- oo[2] = 'vt'+(cell.f?'f':'c'); oo[3]='nl'; oo[4]=cell.v?"1":"0";
- oo[5] = encode(cell.f||(cell.v?'TRUE':'FALSE'));
- break;
- case 'd':
- var t = datenum(parseDate(cell.v));
- oo[2] = 'vtc'; oo[3] = 'nd'; oo[4] = ""+t;
- oo[5] = cell.w || SSF.format(cell.z || SSF._table[14], t);
- break;
- case 'e': continue;
- }
- o.push(oo.join(":"));
- }
- }
- o.push("sheet:c:" + (r.e.c-r.s.c+1) + ":r:" + (r.e.r-r.s.r+1) + ":tvf:1");
- o.push("valueformat:1:text-wiki");
- //o.push("copiedfrom:" + ws['!ref']); // clipboard only
- return o.join("\n");
- }
- function sheet_to_eth(ws) {
- return [header, sep, meta, sep, sheet_to_eth_data(ws), end].join("\n");
- // return ["version:1.5", sheet_to_eth_data(ws)].join("\n"); // clipboard form
- }
- return {
- to_workbook: eth_to_workbook,
- to_sheet: eth_to_sheet,
- from_sheet: sheet_to_eth
- };
- })();
- var PRN = (function() {
- function set_text_arr(data, arr, R, C, o) {
- if(o.raw) arr[R][C] = data;
- else if(data === 'TRUE') arr[R][C] = true;
- else if(data === 'FALSE') arr[R][C] = false;
- else if(data === ""){/* empty */}
- else if(!isNaN(fuzzynum(data))) arr[R][C] = fuzzynum(data);
- else if(!isNaN(fuzzydate(data).getDate())) arr[R][C] = parseDate(data);
- else arr[R][C] = data;
- }
- function prn_to_aoa_str(f, opts) {
- var o = opts || {};
- var arr = ([]);
- if(!f || f.length === 0) return arr;
- var lines = f.split(/[\r\n]/);
- var L = lines.length - 1;
- while(L >= 0 && lines[L].length === 0) --L;
- var start = 10, idx = 0;
- var R = 0;
- for(; R <= L; ++R) {
- idx = lines[R].indexOf(" ");
- if(idx == -1) idx = lines[R].length; else idx++;
- start = Math.max(start, idx);
- }
- for(R = 0; R <= L; ++R) {
- arr[R] = [];
- /* TODO: confirm that widths are always 10 */
- var C = 0;
- set_text_arr(lines[R].slice(0, start).trim(), arr, R, C, o);
- for(C = 1; C <= (lines[R].length - start)/10 + 1; ++C)
- set_text_arr(lines[R].slice(start+(C-1)*10,start+C*10).trim(),arr,R,C,o);
- }
- if(o.sheetRows) arr = arr.slice(0, o.sheetRows);
- return arr;
- }
- // List of accepted CSV separators
- var guess_seps = {
- 0x2C: ',',
- 0x09: "\t",
- 0x3B: ';'
- };
- // CSV separator weights to be used in case of equal numbers
- var guess_sep_weights = {
- 0x2C: 3,
- 0x09: 2,
- 0x3B: 1
- };
- function guess_sep(str) {
- var cnt = {}, instr = false, end = 0, cc = 0;
- for(;end < str.length;++end) {
- if((cc=str.charCodeAt(end)) == 0x22) instr = !instr;
- else if(!instr && cc in guess_seps) cnt[cc] = (cnt[cc]||0)+1;
- }
- cc = [];
- for(end in cnt) if ( Object.prototype.hasOwnProperty.call(cnt, end) ) {
- cc.push([ cnt[end], end ]);
- }
- if ( !cc.length ) {
- cnt = guess_sep_weights;
- for(end in cnt) if ( Object.prototype.hasOwnProperty.call(cnt, end) ) {
- cc.push([ cnt[end], end ]);
- }
- }
- cc.sort(function(a, b) { return a[0] - b[0] || guess_sep_weights[a[1]] - guess_sep_weights[b[1]]; });
- return guess_seps[cc.pop()[1]];
- }
- function dsv_to_sheet_str(str, opts) {
- var o = opts || {};
- var sep = "";
- if(DENSE != null && o.dense == null) o.dense = DENSE;
- var ws = o.dense ? ([]) : ({});
- var range = ({s: {c:0, r:0}, e: {c:0, r:0}});
- if(str.slice(0,4) == "sep=") {
- // If the line ends in \r\n
- if(str.charCodeAt(5) == 13 && str.charCodeAt(6) == 10 ) {
- sep = str.charAt(4); str = str.slice(7);
- }
- // If line ends in \r OR \n
- else if(str.charCodeAt(5) == 13 || str.charCodeAt(5) == 10 ) {
- //
- sep = str.charAt(4); str = str.slice(6);
- }
- }
- else sep = guess_sep(str.slice(0,1024));
- var R = 0, C = 0, v = 0;
- var start = 0, end = 0, sepcc = sep.charCodeAt(0), instr = false, cc=0;
- str = str.replace(/\r\n/mg, "\n");
- var _re = o.dateNF != null ? dateNF_regex(o.dateNF) : null;
- function finish_cell() {
- var s = str.slice(start, end);
- var cell = ({});
- if(s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') s = s.slice(1,-1).replace(/""/g,'"');
- if(s.length === 0) cell.t = 'z';
- else if(o.raw) { cell.t = 's'; cell.v = s; }
- else if(s.trim().length === 0) { cell.t = 's'; cell.v = s; }
- else if(s.charCodeAt(0) == 0x3D) {
- if(s.charCodeAt(1) == 0x22 && s.charCodeAt(s.length - 1) == 0x22) { cell.t = 's'; cell.v = s.slice(2,-1).replace(/""/g,'"'); }
- else if(fuzzyfmla(s)) { cell.t = 'n'; cell.f = s.slice(1); }
- else { cell.t = 's'; cell.v = s; } }
- else if(s == "TRUE") { cell.t = 'b'; cell.v = true; }
- else if(s == "FALSE") { cell.t = 'b'; cell.v = false; }
- else if(!isNaN(v = fuzzynum(s))) { cell.t = 'n'; if(o.cellText !== false) cell.w = s; cell.v = v; }
- else if(!isNaN(fuzzydate(s).getDate()) || _re && s.match(_re)) {
- cell.z = o.dateNF || SSF._table[14];
- var k = 0;
- if(_re && s.match(_re)){ s=dateNF_fix(s, o.dateNF, (s.match(_re)||[])); k=1; }
- if(o.cellDates) { cell.t = 'd'; cell.v = parseDate(s, k); }
- else { cell.t = 'n'; cell.v = datenum(parseDate(s, k)); }
- if(o.cellText !== false) cell.w = SSF.format(cell.z, cell.v instanceof Date ? datenum(cell.v):cell.v);
- if(!o.cellNF) delete cell.z;
- } else {
- cell.t = 's';
- cell.v = s;
- }
- if(cell.t == 'z'){}
- else if(o.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = cell; }
- else ws[encode_cell({c:C,r:R})] = cell;
- start = end+1;
- if(range.e.c < C) range.e.c = C;
- if(range.e.r < R) range.e.r = R;
- if(cc == sepcc) ++C; else { C = 0; ++R; if(o.sheetRows && o.sheetRows <= R) return true; }
- }
- outer: for(;end < str.length;++end) switch((cc=str.charCodeAt(end))) {
- case 0x22: instr = !instr; break;
- case sepcc: case 0x0a: case 0x0d: if(!instr && finish_cell()) break outer; break;
- default: break;
- }
- if(end - start > 0) finish_cell();
- ws['!ref'] = encode_range(range);
- return ws;
- }
- function prn_to_sheet_str(str, opts) {
- if(!(opts && opts.PRN)) return dsv_to_sheet_str(str, opts);
- if(str.slice(0,4) == "sep=") return dsv_to_sheet_str(str, opts);
- if(str.indexOf("\t") >= 0 || str.indexOf(",") >= 0 || str.indexOf(";") >= 0) return dsv_to_sheet_str(str, opts);
- return aoa_to_sheet(prn_to_aoa_str(str, opts), opts);
- }
- function prn_to_sheet(d, opts) {
- var str = "", bytes = opts.type == 'string' ? [0,0,0,0] : firstbyte(d, opts);
- switch(opts.type) {
- case 'base64': str = Base64.decode(d); break;
- case 'binary': str = d; break;
- case 'buffer':
- if(opts.codepage == 65001) str = d.toString('utf8');
- else if(opts.codepage && typeof cptable !== 'undefined') str = cptable.utils.decode(opts.codepage, d);
- else str = d.toString('binary');
- break;
- case 'array': str = cc2str(d); break;
- case 'string': str = d; break;
- default: throw new Error("Unrecognized type " + opts.type);
- }
- if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str.slice(3));
- else if((opts.type == 'binary') && typeof cptable !== 'undefined' && opts.codepage) str = cptable.utils.decode(opts.codepage, cptable.utils.encode(1252,str));
- if(str.slice(0,19) == "socialcalc:version:") return ETH.to_sheet(opts.type == 'string' ? str : utf8read(str), opts);
- return prn_to_sheet_str(str, opts);
- }
- function prn_to_workbook(d, opts) { return sheet_to_workbook(prn_to_sheet(d, opts), opts); }
- function sheet_to_prn(ws) {
- var o = [];
- var r = safe_decode_range(ws['!ref']), cell;
- var dense = Array.isArray(ws);
- for(var R = r.s.r; R <= r.e.r; ++R) {
- var oo = [];
- for(var C = r.s.c; C <= r.e.c; ++C) {
- var coord = encode_cell({r:R,c:C});
- cell = dense ? (ws[R]||[])[C] : ws[coord];
- if(!cell || cell.v == null) { oo.push(" "); continue; }
- var w = (cell.w || (format_cell(cell), cell.w) || "").slice(0,10);
- while(w.length < 10) w += " ";
- oo.push(w + (C === 0 ? " " : ""));
- }
- o.push(oo.join(""));
- }
- return o.join("\n");
- }
- return {
- to_workbook: prn_to_workbook,
- to_sheet: prn_to_sheet,
- from_sheet: sheet_to_prn
- };
- })();
- /* Excel defaults to SYLK but warns if data is not valid */
- function read_wb_ID(d, opts) {
- var o = opts || {}, OLD_WTF = !!o.WTF; o.WTF = true;
- try {
- var out = SYLK.to_workbook(d, o);
- o.WTF = OLD_WTF;
- return out;
- } catch(e) {
- o.WTF = OLD_WTF;
- if(!e.message.match(/SYLK bad record ID/) && OLD_WTF) throw e;
- return PRN.to_workbook(d, opts);
- }
- }
- /* 18.4.7 rPr CT_RPrElt */
- function parse_rpr(rpr) {
- var font = {}, m = rpr.match(tagregex), i = 0;
- var pass = false;
- if(m) for(;i!=m.length; ++i) {
- var y = parsexmltag(m[i]);
- switch(y[0].replace(/\w*:/g,"")) {
- /* 18.8.12 condense CT_BooleanProperty */
- /* ** not required . */
- case '<condense': break;
- /* 18.8.17 extend CT_BooleanProperty */
- /* ** not required . */
- case '<extend': break;
- /* 18.8.36 shadow CT_BooleanProperty */
- /* ** not required . */
- case '<shadow':
- if(!y.val) break;
- /* falls through */
- case '<shadow>':
- case '<shadow/>': font.shadow = 1; break;
- case '</shadow>': break;
- /* 18.4.1 charset CT_IntProperty TODO */
- case '<charset':
- if(y.val == '1') break;
- font.cp = CS2CP[parseInt(y.val, 10)];
- break;
- /* 18.4.2 outline CT_BooleanProperty TODO */
- case '<outline':
- if(!y.val) break;
- /* falls through */
- case '<outline>':
- case '<outline/>': font.outline = 1; break;
- case '</outline>': break;
- /* 18.4.5 rFont CT_FontName */
- case '<rFont': font.name = y.val; break;
- /* 18.4.11 sz CT_FontSize */
- case '<sz': font.sz = y.val; break;
- /* 18.4.10 strike CT_BooleanProperty */
- case '<strike':
- if(!y.val) break;
- /* falls through */
- case '<strike>':
- case '<strike/>': font.strike = 1; break;
- case '</strike>': break;
- /* 18.4.13 u CT_UnderlineProperty */
- case '<u':
- if(!y.val) break;
- switch(y.val) {
- case 'double': font.uval = "double"; break;
- case 'singleAccounting': font.uval = "single-accounting"; break;
- case 'doubleAccounting': font.uval = "double-accounting"; break;
- }
- /* falls through */
- case '<u>':
- case '<u/>': font.u = 1; break;
- case '</u>': break;
- /* 18.8.2 b */
- case '<b':
- if(y.val == '0') break;
- /* falls through */
- case '<b>':
- case '<b/>': font.b = 1; break;
- case '</b>': break;
- /* 18.8.26 i */
- case '<i':
- if(y.val == '0') break;
- /* falls through */
- case '<i>':
- case '<i/>': font.i = 1; break;
- case '</i>': break;
- /* 18.3.1.15 color CT_Color TODO: tint, theme, auto, indexed */
- case '<color':
- if(y.rgb) font.color = y.rgb.slice(2,8);
- break;
- /* 18.8.18 family ST_FontFamily */
- case '<family': font.family = y.val; break;
- /* 18.4.14 vertAlign CT_VerticalAlignFontProperty TODO */
- case '<vertAlign': font.valign = y.val; break;
- /* 18.8.35 scheme CT_FontScheme TODO */
- case '<scheme': break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': break;
- case '<ext': pass = true; break;
- case '</ext>': pass = false; break;
- default:
- if(y[0].charCodeAt(1) !== 47 && !pass) throw new Error('Unrecognized rich format ' + y[0]);
- }
- }
- return font;
- }
- var parse_rs = (function() {
- var tregex = matchtag("t"), rpregex = matchtag("rPr");
- /* 18.4.4 r CT_RElt */
- function parse_r(r) {
- /* 18.4.12 t ST_Xstring */
- var t = r.match(tregex)/*, cp = 65001*/;
- if(!t) return {t:"s", v:""};
- var o = ({t:'s', v:unescapexml(t[1])});
- var rpr = r.match(rpregex);
- if(rpr) o.s = parse_rpr(rpr[1]);
- return o;
- }
- var rregex = /<(?:\w+:)?r>/g, rend = /<\/(?:\w+:)?r>/;
- return function parse_rs(rs) {
- return rs.replace(rregex,"").split(rend).map(parse_r).filter(function(r) { return r.v; });
- };
- })();
- /* Parse a list of <r> tags */
- var rs_to_html = (function parse_rs_factory() {
- var nlregex = /(\r\n|\n)/g;
- function parse_rpr2(font, intro, outro) {
- var style = [];
- if(font.u) style.push("text-decoration: underline;");
- if(font.uval) style.push("text-underline-style:" + font.uval + ";");
- if(font.sz) style.push("font-size:" + font.sz + "pt;");
- if(font.outline) style.push("text-effect: outline;");
- if(font.shadow) style.push("text-shadow: auto;");
- intro.push('<span style="' + style.join("") + '">');
- if(font.b) { intro.push("<b>"); outro.push("</b>"); }
- if(font.i) { intro.push("<i>"); outro.push("</i>"); }
- if(font.strike) { intro.push("<s>"); outro.push("</s>"); }
- var align = font.valign || "";
- if(align == "superscript" || align == "super") align = "sup";
- else if(align == "subscript") align = "sub";
- if(align != "") { intro.push("<" + align + ">"); outro.push("</" + align + ">"); }
- outro.push("</span>");
- return font;
- }
- /* 18.4.4 r CT_RElt */
- function r_to_html(r) {
- var terms = [[],r.v,[]];
- if(!r.v) return "";
- if(r.s) parse_rpr2(r.s, terms[0], terms[2]);
- return terms[0].join("") + terms[1].replace(nlregex,'<br/>') + terms[2].join("");
- }
- return function parse_rs(rs) {
- return rs.map(r_to_html).join("");
- };
- })();
- /* 18.4.8 si CT_Rst */
- var sitregex = /<(?:\w+:)?t[^>]*>([^<]*)<\/(?:\w+:)?t>/g, sirregex = /<(?:\w+:)?r>/;
- var sirphregex = /<(?:\w+:)?rPh.*?>([\s\S]*?)<\/(?:\w+:)?rPh>/g;
- function parse_si(x, opts) {
- var html = opts ? opts.cellHTML : true;
- var z = {};
- if(!x) return { t: "" };
- //var y;
- /* 18.4.12 t ST_Xstring (Plaintext String) */
- // TODO: is whitespace actually valid here?
- if(x.match(/^\s*<(?:\w+:)?t[^>]*>/)) {
- z.t = unescapexml(utf8read(x.slice(x.indexOf(">")+1).split(/<\/(?:\w+:)?t>/)[0]||""));
- z.r = utf8read(x);
- if(html) z.h = escapehtml(z.t);
- }
- /* 18.4.4 r CT_RElt (Rich Text Run) */
- else if((/*y = */x.match(sirregex))) {
- z.r = utf8read(x);
- z.t = unescapexml(utf8read((x.replace(sirphregex, '').match(sitregex)||[]).join("").replace(tagregex,"")));
- if(html) z.h = rs_to_html(parse_rs(z.r));
- }
- /* 18.4.3 phoneticPr CT_PhoneticPr (TODO: needed for Asian support) */
- /* 18.4.6 rPh CT_PhoneticRun (TODO: needed for Asian support) */
- return z;
- }
- /* 18.4 Shared String Table */
- var sstr0 = /<(?:\w+:)?sst([^>]*)>([\s\S]*)<\/(?:\w+:)?sst>/;
- var sstr1 = /<(?:\w+:)?(?:si|sstItem)>/g;
- var sstr2 = /<\/(?:\w+:)?(?:si|sstItem)>/;
- function parse_sst_xml(data, opts) {
- var s = ([]), ss = "";
- if(!data) return s;
- /* 18.4.9 sst CT_Sst */
- var sst = data.match(sstr0);
- if(sst) {
- ss = sst[2].replace(sstr1,"").split(sstr2);
- for(var i = 0; i != ss.length; ++i) {
- var o = parse_si(ss[i].trim(), opts);
- if(o != null) s[s.length] = o;
- }
- sst = parsexmltag(sst[1]); s.Count = sst.count; s.Unique = sst.uniqueCount;
- }
- return s;
- }
- RELS.SST = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings";
- var straywsregex = /^\s|\s$|[\t\n\r]/;
- function write_sst_xml(sst, opts) {
- if(!opts.bookSST) return "";
- var o = [XML_HEADER];
- o[o.length] = (writextag('sst', null, {
- xmlns: XMLNS.main[0],
- count: sst.Count,
- uniqueCount: sst.Unique
- }));
- for(var i = 0; i != sst.length; ++i) { if(sst[i] == null) continue;
- var s = sst[i];
- var sitag = "<si>";
- if(s.r) sitag += s.r;
- else {
- sitag += "<t";
- if(!s.t) s.t = "";
- if(s.t.match(straywsregex)) sitag += ' xml:space="preserve"';
- sitag += ">" + escapexml(s.t) + "</t>";
- }
- sitag += "</si>";
- o[o.length] = (sitag);
- }
- if(o.length>2){ o[o.length] = ('</sst>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- function hex2RGB(h) {
- var o = h.slice(h[0]==="#"?1:0).slice(0,6);
- return [parseInt(o.slice(0,2),16),parseInt(o.slice(2,4),16),parseInt(o.slice(4,6),16)];
- }
- function rgb2Hex(rgb) {
- for(var i=0,o=1; i!=3; ++i) o = o*256 + (rgb[i]>255?255:rgb[i]<0?0:rgb[i]);
- return o.toString(16).toUpperCase().slice(1);
- }
- function rgb2HSL(rgb) {
- var R = rgb[0]/255, G = rgb[1]/255, B=rgb[2]/255;
- var M = Math.max(R, G, B), m = Math.min(R, G, B), C = M - m;
- if(C === 0) return [0, 0, R];
- var H6 = 0, S = 0, L2 = (M + m);
- S = C / (L2 > 1 ? 2 - L2 : L2);
- switch(M){
- case R: H6 = ((G - B) / C + 6)%6; break;
- case G: H6 = ((B - R) / C + 2); break;
- case B: H6 = ((R - G) / C + 4); break;
- }
- return [H6 / 6, S, L2 / 2];
- }
- function hsl2RGB(hsl){
- var H = hsl[0], S = hsl[1], L = hsl[2];
- var C = S * 2 * (L < 0.5 ? L : 1 - L), m = L - C/2;
- var rgb = [m,m,m], h6 = 6*H;
- var X;
- if(S !== 0) switch(h6|0) {
- case 0: case 6: X = C * h6; rgb[0] += C; rgb[1] += X; break;
- case 1: X = C * (2 - h6); rgb[0] += X; rgb[1] += C; break;
- case 2: X = C * (h6 - 2); rgb[1] += C; rgb[2] += X; break;
- case 3: X = C * (4 - h6); rgb[1] += X; rgb[2] += C; break;
- case 4: X = C * (h6 - 4); rgb[2] += C; rgb[0] += X; break;
- case 5: X = C * (6 - h6); rgb[2] += X; rgb[0] += C; break;
- }
- for(var i = 0; i != 3; ++i) rgb[i] = Math.round(rgb[i]*255);
- return rgb;
- }
- /* 18.8.3 bgColor tint algorithm */
- function rgb_tint(hex, tint) {
- if(tint === 0) return hex;
- var hsl = rgb2HSL(hex2RGB(hex));
- if (tint < 0) hsl[2] = hsl[2] * (1 + tint);
- else hsl[2] = 1 - (1 - hsl[2]) * (1 - tint);
- return rgb2Hex(hsl2RGB(hsl));
- }
- /* 18.3.1.13 width calculations */
- /* [MS-OI29500] 2.1.595 Column Width & Formatting */
- var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
- function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
- function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
- function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }
- //function px2char_(px) { return (((px - 5)/MDW * 100 + 0.5))/100; }
- //function char2width_(chr) { return (((chr * MDW + 5)/MDW*256))/256; }
- function cycle_width(collw) { return char2width(px2char(width2px(collw))); }
- /* XLSX/XLSB/XLS specify width in units of MDW */
- function find_mdw_colw(collw) {
- var delta = Math.abs(collw - cycle_width(collw)), _MDW = MDW;
- 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; }
- MDW = _MDW;
- }
- /* XLML specifies width in terms of pixels */
- /*function find_mdw_wpx(wpx) {
- var delta = Infinity, guess = 0, _MDW = MIN_MDW;
- for(MDW=MIN_MDW; MDW<MAX_MDW; ++MDW) {
- guess = char2width_(px2char_(wpx))*256;
- guess = (guess) % 1;
- if(guess > 0.5) guess--;
- if(Math.abs(guess) < delta) { delta = Math.abs(guess); _MDW = MDW; }
- }
- MDW = _MDW;
- }*/
- function process_col(coll) {
- if(coll.width) {
- coll.wpx = width2px(coll.width);
- coll.wch = px2char(coll.wpx);
- coll.MDW = MDW;
- } else if(coll.wpx) {
- coll.wch = px2char(coll.wpx);
- coll.width = char2width(coll.wch);
- coll.MDW = MDW;
- } else if(typeof coll.wch == 'number') {
- coll.width = char2width(coll.wch);
- coll.wpx = width2px(coll.width);
- coll.MDW = MDW;
- }
- if(coll.customWidth) delete coll.customWidth;
- }
- var DEF_PPI = 96, PPI = DEF_PPI;
- function px2pt(px) { return px * 96 / PPI; }
- function pt2px(pt) { return pt * PPI / 96; }
- /* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
- var XLMLPatternTypeMap = {
- "None": "none",
- "Solid": "solid",
- "Gray50": "mediumGray",
- "Gray75": "darkGray",
- "Gray25": "lightGray",
- "HorzStripe": "darkHorizontal",
- "VertStripe": "darkVertical",
- "ReverseDiagStripe": "darkDown",
- "DiagStripe": "darkUp",
- "DiagCross": "darkGrid",
- "ThickDiagCross": "darkTrellis",
- "ThinHorzStripe": "lightHorizontal",
- "ThinVertStripe": "lightVertical",
- "ThinReverseDiagStripe": "lightDown",
- "ThinHorzCross": "lightGrid"
- };
- /* 18.8.5 borders CT_Borders */
- function parse_borders(t, styles, themes, opts) {
- styles.Borders = [];
- var border = {};
- var pass = false;
- (t[0].match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- switch(strip_ns(y[0])) {
- case '<borders': case '<borders>': case '</borders>': break;
- /* 18.8.4 border CT_Border */
- case '<border': case '<border>': case '<border/>':
- border = {};
- if(y.diagonalUp) border.diagonalUp = parsexmlbool(y.diagonalUp);
- if(y.diagonalDown) border.diagonalDown = parsexmlbool(y.diagonalDown);
- styles.Borders.push(border);
- break;
- case '</border>': break;
- /* note: not in spec, appears to be CT_BorderPr */
- case '<left/>': break;
- case '<left': case '<left>': break;
- case '</left>': break;
- /* note: not in spec, appears to be CT_BorderPr */
- case '<right/>': break;
- case '<right': case '<right>': break;
- case '</right>': break;
- /* 18.8.43 top CT_BorderPr */
- case '<top/>': break;
- case '<top': case '<top>': break;
- case '</top>': break;
- /* 18.8.6 bottom CT_BorderPr */
- case '<bottom/>': break;
- case '<bottom': case '<bottom>': break;
- case '</bottom>': break;
- /* 18.8.13 diagonal CT_BorderPr */
- case '<diagonal': case '<diagonal>': case '<diagonal/>': break;
- case '</diagonal>': break;
- /* 18.8.25 horizontal CT_BorderPr */
- case '<horizontal': case '<horizontal>': case '<horizontal/>': break;
- case '</horizontal>': break;
- /* 18.8.44 vertical CT_BorderPr */
- case '<vertical': case '<vertical>': case '<vertical/>': break;
- case '</vertical>': break;
- /* 18.8.37 start CT_BorderPr */
- case '<start': case '<start>': case '<start/>': break;
- case '</start>': break;
- /* 18.8.16 end CT_BorderPr */
- case '<end': case '<end>': case '<end/>': break;
- case '</end>': break;
- /* 18.8.? color CT_Color */
- case '<color': case '<color>':
- break;
- case '<color/>': case '</color>': break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': break;
- case '<ext': pass = true; break;
- case '</ext>': pass = false; break;
- default: if(opts && opts.WTF) {
- if(!pass) throw new Error('unrecognized ' + y[0] + ' in borders');
- }
- }
- });
- }
- /* 18.8.21 fills CT_Fills */
- function parse_fills(t, styles, themes, opts) {
- styles.Fills = [];
- var fill = {};
- var pass = false;
- (t[0].match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- switch(strip_ns(y[0])) {
- case '<fills': case '<fills>': case '</fills>': break;
- /* 18.8.20 fill CT_Fill */
- case '<fill>': case '<fill': case '<fill/>':
- fill = {}; styles.Fills.push(fill); break;
- case '</fill>': break;
- /* 18.8.24 gradientFill CT_GradientFill */
- case '<gradientFill>': break;
- case '<gradientFill':
- case '</gradientFill>': styles.Fills.push(fill); fill = {}; break;
- /* 18.8.32 patternFill CT_PatternFill */
- case '<patternFill': case '<patternFill>':
- if(y.patternType) fill.patternType = y.patternType;
- break;
- case '<patternFill/>': case '</patternFill>': break;
- /* 18.8.3 bgColor CT_Color */
- case '<bgColor':
- if(!fill.bgColor) fill.bgColor = {};
- if(y.indexed) fill.bgColor.indexed = parseInt(y.indexed, 10);
- if(y.theme) fill.bgColor.theme = parseInt(y.theme, 10);
- if(y.tint) fill.bgColor.tint = parseFloat(y.tint);
- /* Excel uses ARGB strings */
- if(y.rgb) fill.bgColor.rgb = y.rgb.slice(-6);
- break;
- case '<bgColor/>': case '</bgColor>': break;
- /* 18.8.19 fgColor CT_Color */
- case '<fgColor':
- if(!fill.fgColor) fill.fgColor = {};
- if(y.theme) fill.fgColor.theme = parseInt(y.theme, 10);
- if(y.tint) fill.fgColor.tint = parseFloat(y.tint);
- /* Excel uses ARGB strings */
- if(y.rgb != null) fill.fgColor.rgb = y.rgb.slice(-6);
- break;
- case '<fgColor/>': case '</fgColor>': break;
- /* 18.8.38 stop CT_GradientStop */
- case '<stop': case '<stop/>': break;
- case '</stop>': break;
- /* 18.8.? color CT_Color */
- case '<color': case '<color/>': break;
- case '</color>': break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': break;
- case '<ext': pass = true; break;
- case '</ext>': pass = false; break;
- default: if(opts && opts.WTF) {
- if(!pass) throw new Error('unrecognized ' + y[0] + ' in fills');
- }
- }
- });
- }
- /* 18.8.23 fonts CT_Fonts */
- function parse_fonts(t, styles, themes, opts) {
- styles.Fonts = [];
- var font = {};
- var pass = false;
- (t[0].match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- switch(strip_ns(y[0])) {
- case '<fonts': case '<fonts>': case '</fonts>': break;
- /* 18.8.22 font CT_Font */
- case '<font': case '<font>': break;
- case '</font>': case '<font/>':
- styles.Fonts.push(font);
- font = {};
- break;
- /* 18.8.29 name CT_FontName */
- case '<name': if(y.val) font.name = utf8read(y.val); break;
- case '<name/>': case '</name>': break;
- /* 18.8.2 b CT_BooleanProperty */
- case '<b': font.bold = y.val ? parsexmlbool(y.val) : 1; break;
- case '<b/>': font.bold = 1; break;
- /* 18.8.26 i CT_BooleanProperty */
- case '<i': font.italic = y.val ? parsexmlbool(y.val) : 1; break;
- case '<i/>': font.italic = 1; break;
- /* 18.4.13 u CT_UnderlineProperty */
- case '<u':
- switch(y.val) {
- case "none": font.underline = 0x00; break;
- case "single": font.underline = 0x01; break;
- case "double": font.underline = 0x02; break;
- case "singleAccounting": font.underline = 0x21; break;
- case "doubleAccounting": font.underline = 0x22; break;
- } break;
- case '<u/>': font.underline = 1; break;
- /* 18.4.10 strike CT_BooleanProperty */
- case '<strike': font.strike = y.val ? parsexmlbool(y.val) : 1; break;
- case '<strike/>': font.strike = 1; break;
- /* 18.4.2 outline CT_BooleanProperty */
- case '<outline': font.outline = y.val ? parsexmlbool(y.val) : 1; break;
- case '<outline/>': font.outline = 1; break;
- /* 18.8.36 shadow CT_BooleanProperty */
- case '<shadow': font.shadow = y.val ? parsexmlbool(y.val) : 1; break;
- case '<shadow/>': font.shadow = 1; break;
- /* 18.8.12 condense CT_BooleanProperty */
- case '<condense': font.condense = y.val ? parsexmlbool(y.val) : 1; break;
- case '<condense/>': font.condense = 1; break;
- /* 18.8.17 extend CT_BooleanProperty */
- case '<extend': font.extend = y.val ? parsexmlbool(y.val) : 1; break;
- case '<extend/>': font.extend = 1; break;
- /* 18.4.11 sz CT_FontSize */
- case '<sz': if(y.val) font.sz = +y.val; break;
- case '<sz/>': case '</sz>': break;
- /* 18.4.14 vertAlign CT_VerticalAlignFontProperty */
- case '<vertAlign': if(y.val) font.vertAlign = y.val; break;
- case '<vertAlign/>': case '</vertAlign>': break;
- /* 18.8.18 family CT_FontFamily */
- case '<family': if(y.val) font.family = parseInt(y.val,10); break;
- case '<family/>': case '</family>': break;
- /* 18.8.35 scheme CT_FontScheme */
- case '<scheme': if(y.val) font.scheme = y.val; break;
- case '<scheme/>': case '</scheme>': break;
- /* 18.4.1 charset CT_IntProperty */
- case '<charset':
- if(y.val == '1') break;
- y.codepage = CS2CP[parseInt(y.val, 10)];
- break;
- /* 18.?.? color CT_Color */
- case '<color':
- if(!font.color) font.color = {};
- if(y.auto) font.color.auto = parsexmlbool(y.auto);
- if(y.rgb) font.color.rgb = y.rgb.slice(-6);
- else if(y.indexed) {
- font.color.index = parseInt(y.indexed, 10);
- var icv = XLSIcv[font.color.index];
- if(font.color.index == 81) icv = XLSIcv[1];
- if(!icv) throw new Error(x);
- font.color.rgb = icv[0].toString(16) + icv[1].toString(16) + icv[2].toString(16);
- } else if(y.theme) {
- font.color.theme = parseInt(y.theme, 10);
- if(y.tint) font.color.tint = parseFloat(y.tint);
- if(y.theme && themes.themeElements && themes.themeElements.clrScheme) {
- font.color.rgb = rgb_tint(themes.themeElements.clrScheme[font.color.theme].rgb, font.color.tint || 0);
- }
- }
- break;
- case '<color/>': case '</color>': break;
- /* note: sometimes mc:AlternateContent appears bare */
- case '<AlternateContent': pass = true; break;
- case '</AlternateContent>': pass = false; break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': break;
- case '<ext': pass = true; break;
- case '</ext>': pass = false; break;
- default: if(opts && opts.WTF) {
- if(!pass) throw new Error('unrecognized ' + y[0] + ' in fonts');
- }
- }
- });
- }
- /* 18.8.31 numFmts CT_NumFmts */
- function parse_numFmts(t, styles, opts) {
- styles.NumberFmt = [];
- var k/*Array<number>*/ = (keys(SSF._table));
- for(var i=0; i < k.length; ++i) styles.NumberFmt[k[i]] = SSF._table[k[i]];
- var m = t[0].match(tagregex);
- if(!m) return;
- for(i=0; i < m.length; ++i) {
- var y = parsexmltag(m[i]);
- switch(strip_ns(y[0])) {
- case '<numFmts': case '</numFmts>': case '<numFmts/>': case '<numFmts>': break;
- case '<numFmt': {
- var f=unescapexml(utf8read(y.formatCode)), j=parseInt(y.numFmtId,10);
- styles.NumberFmt[j] = f;
- if(j>0) {
- if(j > 0x188) {
- for(j = 0x188; j > 0x3c; --j) if(styles.NumberFmt[j] == null) break;
- styles.NumberFmt[j] = f;
- }
- SSF.load(f,j);
- }
- } break;
- case '</numFmt>': break;
- default: if(opts.WTF) throw new Error('unrecognized ' + y[0] + ' in numFmts');
- }
- }
- }
- function write_numFmts(NF) {
- var o = ["<numFmts>"];
- [[5,8],[23,26],[41,44],[/*63*/50,/*66],[164,*/392]].forEach(function(r) {
- 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])}));
- });
- if(o.length === 1) return "";
- o[o.length] = ("</numFmts>");
- o[0] = writextag('numFmts', null, { count:o.length-2 }).replace("/>", ">");
- return o.join("");
- }
- /* 18.8.10 cellXfs CT_CellXfs */
- var cellXF_uint = [ "numFmtId", "fillId", "fontId", "borderId", "xfId" ];
- var cellXF_bool = [ "applyAlignment", "applyBorder", "applyFill", "applyFont", "applyNumberFormat", "applyProtection", "pivotButton", "quotePrefix" ];
- function parse_cellXfs(t, styles, opts) {
- styles.CellXf = [];
- var xf;
- var pass = false;
- (t[0].match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x), i = 0;
- switch(strip_ns(y[0])) {
- case '<cellXfs': case '<cellXfs>': case '<cellXfs/>': case '</cellXfs>': break;
- /* 18.8.45 xf CT_Xf */
- case '<xf': case '<xf/>':
- xf = y;
- delete xf[0];
- for(i = 0; i < cellXF_uint.length; ++i) if(xf[cellXF_uint[i]])
- xf[cellXF_uint[i]] = parseInt(xf[cellXF_uint[i]], 10);
- for(i = 0; i < cellXF_bool.length; ++i) if(xf[cellXF_bool[i]])
- xf[cellXF_bool[i]] = parsexmlbool(xf[cellXF_bool[i]]);
- if(xf.numFmtId > 0x188) {
- for(i = 0x188; i > 0x3c; --i) if(styles.NumberFmt[xf.numFmtId] == styles.NumberFmt[i]) { xf.numFmtId = i; break; }
- }
- styles.CellXf.push(xf); break;
- case '</xf>': break;
- /* 18.8.1 alignment CT_CellAlignment */
- case '<alignment': case '<alignment/>':
- var alignment = {};
- if(y.vertical) alignment.vertical = y.vertical;
- if(y.horizontal) alignment.horizontal = y.horizontal;
- if(y.textRotation != null) alignment.textRotation = y.textRotation;
- if(y.indent) alignment.indent = y.indent;
- if(y.wrapText) alignment.wrapText = parsexmlbool(y.wrapText);
- xf.alignment = alignment;
- break;
- case '</alignment>': break;
- /* 18.8.33 protection CT_CellProtection */
- case '<protection':
- break;
- case '</protection>': case '<protection/>': break;
- /* note: sometimes mc:AlternateContent appears bare */
- case '<AlternateContent': pass = true; break;
- case '</AlternateContent>': pass = false; break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': break;
- case '<ext': pass = true; break;
- case '</ext>': pass = false; break;
- default: if(opts && opts.WTF) {
- if(!pass) throw new Error('unrecognized ' + y[0] + ' in cellXfs');
- }
- }
- });
- }
- function write_cellXfs(cellXfs) {
- var o = [];
- o[o.length] = (writextag('cellXfs',null));
- cellXfs.forEach(function(c) {
- o[o.length] = (writextag('xf', null, c));
- });
- o[o.length] = ("</cellXfs>");
- if(o.length === 2) return "";
- o[0] = writextag('cellXfs',null, {count:o.length-2}).replace("/>",">");
- return o.join("");
- }
- /* 18.8 Styles CT_Stylesheet*/
- var parse_sty_xml= (function make_pstyx() {
- var numFmtRegex = /<(?:\w+:)?numFmts([^>]*)>[\S\s]*?<\/(?:\w+:)?numFmts>/;
- var cellXfRegex = /<(?:\w+:)?cellXfs([^>]*)>[\S\s]*?<\/(?:\w+:)?cellXfs>/;
- var fillsRegex = /<(?:\w+:)?fills([^>]*)>[\S\s]*?<\/(?:\w+:)?fills>/;
- var fontsRegex = /<(?:\w+:)?fonts([^>]*)>[\S\s]*?<\/(?:\w+:)?fonts>/;
- var bordersRegex = /<(?:\w+:)?borders([^>]*)>[\S\s]*?<\/(?:\w+:)?borders>/;
- return function parse_sty_xml(data, themes, opts) {
- var styles = {};
- if(!data) return styles;
- data = data.replace(/<!--([\s\S]*?)-->/mg,"").replace(/<!DOCTYPE[^\[]*\[[^\]]*\]>/gm,"");
- /* 18.8.39 styleSheet CT_Stylesheet */
- var t;
- /* 18.8.31 numFmts CT_NumFmts ? */
- if((t=data.match(numFmtRegex))) parse_numFmts(t, styles, opts);
- /* 18.8.23 fonts CT_Fonts ? */
- if((t=data.match(fontsRegex))) parse_fonts(t, styles, themes, opts);
- /* 18.8.21 fills CT_Fills ? */
- if((t=data.match(fillsRegex))) parse_fills(t, styles, themes, opts);
- /* 18.8.5 borders CT_Borders ? */
- if((t=data.match(bordersRegex))) parse_borders(t, styles, themes, opts);
- /* 18.8.9 cellStyleXfs CT_CellStyleXfs ? */
- /* 18.8.8 cellStyles CT_CellStyles ? */
- /* 18.8.10 cellXfs CT_CellXfs ? */
- if((t=data.match(cellXfRegex))) parse_cellXfs(t, styles, opts);
- /* 18.8.15 dxfs CT_Dxfs ? */
- /* 18.8.42 tableStyles CT_TableStyles ? */
- /* 18.8.11 colors CT_Colors ? */
- /* 18.2.10 extLst CT_ExtensionList ? */
- return styles;
- };
- })();
- var STYLES_XML_ROOT = writextag('styleSheet', null, {
- 'xmlns': XMLNS.main[0],
- 'xmlns:vt': XMLNS.vt
- });
- RELS.STY = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
- function write_sty_xml(wb, opts) {
- var o = [XML_HEADER, STYLES_XML_ROOT], w;
- if(wb.SSF && (w = write_numFmts(wb.SSF)) != null) o[o.length] = w;
- o[o.length] = ('<fonts count="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts>');
- o[o.length] = ('<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>');
- o[o.length] = ('<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>');
- o[o.length] = ('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>');
- if((w = write_cellXfs(opts.cellXfs))) o[o.length] = (w);
- o[o.length] = ('<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>');
- o[o.length] = ('<dxfs count="0"/>');
- o[o.length] = ('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4"/>');
- if(o.length>2){ o[o.length] = ('</styleSheet>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- RELS.THEME = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
- /* Even though theme layout is dk1 lt1 dk2 lt2, true order is lt1 dk1 lt2 dk2 */
- var XLSXThemeClrScheme = [
- '</a:lt1>', '</a:dk1>', '</a:lt2>', '</a:dk2>',
- '</a:accent1>', '</a:accent2>', '</a:accent3>',
- '</a:accent4>', '</a:accent5>', '</a:accent6>',
- '</a:hlink>', '</a:folHlink>'
- ];
- /* 20.1.6.2 clrScheme CT_ColorScheme */
- function parse_clrScheme(t, themes, opts) {
- themes.themeElements.clrScheme = [];
- var color = {};
- (t[0].match(tagregex)||[]).forEach(function(x) {
- var y = parsexmltag(x);
- switch(y[0]) {
- /* 20.1.6.2 clrScheme (Color Scheme) CT_ColorScheme */
- case '<a:clrScheme': case '</a:clrScheme>': break;
- /* 20.1.2.3.32 srgbClr CT_SRgbColor */
- case '<a:srgbClr':
- color.rgb = y.val; break;
- /* 20.1.2.3.33 sysClr CT_SystemColor */
- case '<a:sysClr':
- color.rgb = y.lastClr; break;
- /* 20.1.4.1.1 accent1 (Accent 1) */
- /* 20.1.4.1.2 accent2 (Accent 2) */
- /* 20.1.4.1.3 accent3 (Accent 3) */
- /* 20.1.4.1.4 accent4 (Accent 4) */
- /* 20.1.4.1.5 accent5 (Accent 5) */
- /* 20.1.4.1.6 accent6 (Accent 6) */
- /* 20.1.4.1.9 dk1 (Dark 1) */
- /* 20.1.4.1.10 dk2 (Dark 2) */
- /* 20.1.4.1.15 folHlink (Followed Hyperlink) */
- /* 20.1.4.1.19 hlink (Hyperlink) */
- /* 20.1.4.1.22 lt1 (Light 1) */
- /* 20.1.4.1.23 lt2 (Light 2) */
- case '<a:dk1>': case '</a:dk1>':
- case '<a:lt1>': case '</a:lt1>':
- case '<a:dk2>': case '</a:dk2>':
- case '<a:lt2>': case '</a:lt2>':
- case '<a:accent1>': case '</a:accent1>':
- case '<a:accent2>': case '</a:accent2>':
- case '<a:accent3>': case '</a:accent3>':
- case '<a:accent4>': case '</a:accent4>':
- case '<a:accent5>': case '</a:accent5>':
- case '<a:accent6>': case '</a:accent6>':
- case '<a:hlink>': case '</a:hlink>':
- case '<a:folHlink>': case '</a:folHlink>':
- if (y[0].charAt(1) === '/') {
- themes.themeElements.clrScheme[XLSXThemeClrScheme.indexOf(y[0])] = color;
- color = {};
- } else {
- color.name = y[0].slice(3, y[0].length - 1);
- }
- break;
- default: if(opts && opts.WTF) throw new Error('Unrecognized ' + y[0] + ' in clrScheme');
- }
- });
- }
- /* 20.1.4.1.18 fontScheme CT_FontScheme */
- function parse_fontScheme() { }
- /* 20.1.4.1.15 fmtScheme CT_StyleMatrix */
- function parse_fmtScheme() { }
- var clrsregex = /<a:clrScheme([^>]*)>[\s\S]*<\/a:clrScheme>/;
- var fntsregex = /<a:fontScheme([^>]*)>[\s\S]*<\/a:fontScheme>/;
- var fmtsregex = /<a:fmtScheme([^>]*)>[\s\S]*<\/a:fmtScheme>/;
- /* 20.1.6.10 themeElements CT_BaseStyles */
- function parse_themeElements(data, themes, opts) {
- themes.themeElements = {};
- var t;
- [
- /* clrScheme CT_ColorScheme */
- ['clrScheme', clrsregex, parse_clrScheme],
- /* fontScheme CT_FontScheme */
- ['fontScheme', fntsregex, parse_fontScheme],
- /* fmtScheme CT_StyleMatrix */
- ['fmtScheme', fmtsregex, parse_fmtScheme]
- ].forEach(function(m) {
- if(!(t=data.match(m[1]))) throw new Error(m[0] + ' not found in themeElements');
- m[2](t, themes, opts);
- });
- }
- var themeltregex = /<a:themeElements([^>]*)>[\s\S]*<\/a:themeElements>/;
- /* 14.2.7 Theme Part */
- function parse_theme_xml(data, opts) {
- /* 20.1.6.9 theme CT_OfficeStyleSheet */
- if(!data || data.length === 0) return parse_theme_xml(write_theme());
- var t;
- var themes = {};
- /* themeElements CT_BaseStyles */
- if(!(t=data.match(themeltregex))) throw new Error('themeElements not found in theme');
- parse_themeElements(t[0], themes, opts);
- themes.raw = data;
- return themes;
- }
- function write_theme(Themes, opts) {
- if(opts && opts.themeXLSX) return opts.themeXLSX;
- if(Themes && typeof Themes.raw == "string") return Themes.raw;
- var o = [XML_HEADER];
- o[o.length] = '<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="Office Theme">';
- o[o.length] = '<a:themeElements>';
- o[o.length] = '<a:clrScheme name="Office">';
- o[o.length] = '<a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1>';
- o[o.length] = '<a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1>';
- o[o.length] = '<a:dk2><a:srgbClr val="1F497D"/></a:dk2>';
- o[o.length] = '<a:lt2><a:srgbClr val="EEECE1"/></a:lt2>';
- o[o.length] = '<a:accent1><a:srgbClr val="4F81BD"/></a:accent1>';
- o[o.length] = '<a:accent2><a:srgbClr val="C0504D"/></a:accent2>';
- o[o.length] = '<a:accent3><a:srgbClr val="9BBB59"/></a:accent3>';
- o[o.length] = '<a:accent4><a:srgbClr val="8064A2"/></a:accent4>';
- o[o.length] = '<a:accent5><a:srgbClr val="4BACC6"/></a:accent5>';
- o[o.length] = '<a:accent6><a:srgbClr val="F79646"/></a:accent6>';
- o[o.length] = '<a:hlink><a:srgbClr val="0000FF"/></a:hlink>';
- o[o.length] = '<a:folHlink><a:srgbClr val="800080"/></a:folHlink>';
- o[o.length] = '</a:clrScheme>';
- o[o.length] = '<a:fontScheme name="Office">';
- o[o.length] = '<a:majorFont>';
- o[o.length] = '<a:latin typeface="Cambria"/>';
- o[o.length] = '<a:ea typeface=""/>';
- o[o.length] = '<a:cs typeface=""/>';
- o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
- o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
- o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
- o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
- o[o.length] = '<a:font script="Arab" typeface="Times New Roman"/>';
- o[o.length] = '<a:font script="Hebr" typeface="Times New Roman"/>';
- o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
- o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
- o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
- o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
- o[o.length] = '<a:font script="Khmr" typeface="MoolBoran"/>';
- o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
- o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
- o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
- o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
- o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
- o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
- o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
- o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
- o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
- o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
- o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
- o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
- o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
- o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
- o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
- o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
- o[o.length] = '<a:font script="Viet" typeface="Times New Roman"/>';
- o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
- o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
- o[o.length] = '</a:majorFont>';
- o[o.length] = '<a:minorFont>';
- o[o.length] = '<a:latin typeface="Calibri"/>';
- o[o.length] = '<a:ea typeface=""/>';
- o[o.length] = '<a:cs typeface=""/>';
- o[o.length] = '<a:font script="Jpan" typeface="MS Pゴシック"/>';
- o[o.length] = '<a:font script="Hang" typeface="맑은 고딕"/>';
- o[o.length] = '<a:font script="Hans" typeface="宋体"/>';
- o[o.length] = '<a:font script="Hant" typeface="新細明體"/>';
- o[o.length] = '<a:font script="Arab" typeface="Arial"/>';
- o[o.length] = '<a:font script="Hebr" typeface="Arial"/>';
- o[o.length] = '<a:font script="Thai" typeface="Tahoma"/>';
- o[o.length] = '<a:font script="Ethi" typeface="Nyala"/>';
- o[o.length] = '<a:font script="Beng" typeface="Vrinda"/>';
- o[o.length] = '<a:font script="Gujr" typeface="Shruti"/>';
- o[o.length] = '<a:font script="Khmr" typeface="DaunPenh"/>';
- o[o.length] = '<a:font script="Knda" typeface="Tunga"/>';
- o[o.length] = '<a:font script="Guru" typeface="Raavi"/>';
- o[o.length] = '<a:font script="Cans" typeface="Euphemia"/>';
- o[o.length] = '<a:font script="Cher" typeface="Plantagenet Cherokee"/>';
- o[o.length] = '<a:font script="Yiii" typeface="Microsoft Yi Baiti"/>';
- o[o.length] = '<a:font script="Tibt" typeface="Microsoft Himalaya"/>';
- o[o.length] = '<a:font script="Thaa" typeface="MV Boli"/>';
- o[o.length] = '<a:font script="Deva" typeface="Mangal"/>';
- o[o.length] = '<a:font script="Telu" typeface="Gautami"/>';
- o[o.length] = '<a:font script="Taml" typeface="Latha"/>';
- o[o.length] = '<a:font script="Syrc" typeface="Estrangelo Edessa"/>';
- o[o.length] = '<a:font script="Orya" typeface="Kalinga"/>';
- o[o.length] = '<a:font script="Mlym" typeface="Kartika"/>';
- o[o.length] = '<a:font script="Laoo" typeface="DokChampa"/>';
- o[o.length] = '<a:font script="Sinh" typeface="Iskoola Pota"/>';
- o[o.length] = '<a:font script="Mong" typeface="Mongolian Baiti"/>';
- o[o.length] = '<a:font script="Viet" typeface="Arial"/>';
- o[o.length] = '<a:font script="Uigh" typeface="Microsoft Uighur"/>';
- o[o.length] = '<a:font script="Geor" typeface="Sylfaen"/>';
- o[o.length] = '</a:minorFont>';
- o[o.length] = '</a:fontScheme>';
- o[o.length] = '<a:fmtScheme name="Office">';
- o[o.length] = '<a:fillStyleLst>';
- o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
- o[o.length] = '<a:gradFill rotWithShape="1">';
- o[o.length] = '<a:gsLst>';
- o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="50000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
- o[o.length] = '<a:gs pos="35000"><a:schemeClr val="phClr"><a:tint val="37000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
- o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="15000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
- o[o.length] = '</a:gsLst>';
- o[o.length] = '<a:lin ang="16200000" scaled="1"/>';
- o[o.length] = '</a:gradFill>';
- o[o.length] = '<a:gradFill rotWithShape="1">';
- o[o.length] = '<a:gsLst>';
- 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>';
- 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>';
- o[o.length] = '</a:gsLst>';
- o[o.length] = '<a:lin ang="16200000" scaled="0"/>';
- o[o.length] = '</a:gradFill>';
- o[o.length] = '</a:fillStyleLst>';
- o[o.length] = '<a:lnStyleLst>';
- 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>';
- 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>';
- 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>';
- o[o.length] = '</a:lnStyleLst>';
- o[o.length] = '<a:effectStyleLst>';
- o[o.length] = '<a:effectStyle>';
- o[o.length] = '<a:effectLst>';
- 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>';
- o[o.length] = '</a:effectLst>';
- o[o.length] = '</a:effectStyle>';
- o[o.length] = '<a:effectStyle>';
- o[o.length] = '<a:effectLst>';
- 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>';
- o[o.length] = '</a:effectLst>';
- o[o.length] = '</a:effectStyle>';
- o[o.length] = '<a:effectStyle>';
- o[o.length] = '<a:effectLst>';
- 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>';
- o[o.length] = '</a:effectLst>';
- 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>';
- o[o.length] = '<a:sp3d><a:bevelT w="63500" h="25400"/></a:sp3d>';
- o[o.length] = '</a:effectStyle>';
- o[o.length] = '</a:effectStyleLst>';
- o[o.length] = '<a:bgFillStyleLst>';
- o[o.length] = '<a:solidFill><a:schemeClr val="phClr"/></a:solidFill>';
- o[o.length] = '<a:gradFill rotWithShape="1">';
- o[o.length] = '<a:gsLst>';
- o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="40000"/><a:satMod val="350000"/></a:schemeClr></a:gs>';
- 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>';
- o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="20000"/><a:satMod val="255000"/></a:schemeClr></a:gs>';
- o[o.length] = '</a:gsLst>';
- o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="-80000" r="50000" b="180000"/></a:path>';
- o[o.length] = '</a:gradFill>';
- o[o.length] = '<a:gradFill rotWithShape="1">';
- o[o.length] = '<a:gsLst>';
- o[o.length] = '<a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="80000"/><a:satMod val="300000"/></a:schemeClr></a:gs>';
- o[o.length] = '<a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="30000"/><a:satMod val="200000"/></a:schemeClr></a:gs>';
- o[o.length] = '</a:gsLst>';
- o[o.length] = '<a:path path="circle"><a:fillToRect l="50000" t="50000" r="50000" b="50000"/></a:path>';
- o[o.length] = '</a:gradFill>';
- o[o.length] = '</a:bgFillStyleLst>';
- o[o.length] = '</a:fmtScheme>';
- o[o.length] = '</a:themeElements>';
- o[o.length] = '<a:objectDefaults>';
- o[o.length] = '<a:spDef>';
- 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>';
- o[o.length] = '</a:spDef>';
- o[o.length] = '<a:lnDef>';
- 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>';
- o[o.length] = '</a:lnDef>';
- o[o.length] = '</a:objectDefaults>';
- o[o.length] = '<a:extraClrSchemeLst/>';
- o[o.length] = '</a:theme>';
- return o.join("");
- }
- /* 18.14 Supplementary Workbook Data */
- function parse_xlink_xml() {
- //var opts = _opts || {};
- //if(opts.WTF) throw "XLSX External Link";
- }
- /* [MS-XLSB] 2.1.7.25 External Link */
- function parse_xlink_bin(data, rel, name, _opts) {
- if(!data) return data;
- var opts = _opts || {};
- var pass = false, end = false;
- recordhopper(data, function xlink_parse(val, R_n, RT) {
- if(end) return;
- switch(RT) {
- case 0x0167: /* 'BrtSupTabs' */
- case 0x016B: /* 'BrtExternTableStart' */
- case 0x016C: /* 'BrtExternTableEnd' */
- case 0x016E: /* 'BrtExternRowHdr' */
- case 0x016F: /* 'BrtExternCellBlank' */
- case 0x0170: /* 'BrtExternCellReal' */
- case 0x0171: /* 'BrtExternCellBool' */
- case 0x0172: /* 'BrtExternCellError' */
- case 0x0173: /* 'BrtExternCellString' */
- case 0x01D8: /* 'BrtExternValueMeta' */
- case 0x0241: /* 'BrtSupNameStart' */
- case 0x0242: /* 'BrtSupNameValueStart' */
- case 0x0243: /* 'BrtSupNameValueEnd' */
- case 0x0244: /* 'BrtSupNameNum' */
- case 0x0245: /* 'BrtSupNameErr' */
- case 0x0246: /* 'BrtSupNameSt' */
- case 0x0247: /* 'BrtSupNameNil' */
- case 0x0248: /* 'BrtSupNameBool' */
- case 0x0249: /* 'BrtSupNameFmla' */
- case 0x024A: /* 'BrtSupNameBits' */
- case 0x024B: /* 'BrtSupNameEnd' */
- break;
- case 0x0023: /* 'BrtFRTBegin' */
- pass = true; break;
- case 0x0024: /* 'BrtFRTEnd' */
- pass = false; break;
- default:
- if((R_n||"").indexOf("Begin") > 0){/* empty */}
- else if((R_n||"").indexOf("End") > 0){/* empty */}
- else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT.toString(16) + " " + R_n);
- }
- }, opts);
- }
- /* 20.5 DrawingML - SpreadsheetML Drawing */
- RELS.IMG = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
- RELS.DRAW = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
- /* 20.5.2.35 wsDr CT_Drawing */
- function parse_drawing(data, rels) {
- if(!data) return "??";
- /*
- Chartsheet Drawing:
- - 20.5.2.35 wsDr CT_Drawing
- - 20.5.2.1 absoluteAnchor CT_AbsoluteAnchor
- - 20.5.2.16 graphicFrame CT_GraphicalObjectFrame
- - 20.1.2.2.16 graphic CT_GraphicalObject
- - 20.1.2.2.17 graphicData CT_GraphicalObjectData
- - chart reference
- the actual type is based on the URI of the graphicData
- TODO: handle embedded charts and other types of graphics
- */
- var id = (data.match(/<c:chart [^>]*r:id="([^"]*)"/)||["",""])[1];
- return rels['!id'][id].Target;
- }
- /* L.5.5.2 SpreadsheetML Comments + VML Schema */
- var _shapeid = 1024;
- function write_comments_vml(rId, comments) {
- var csize = [21600, 21600];
- /* L.5.2.1.2 Path Attribute */
- var bbox = ["m0,0l0",csize[1],csize[0],csize[1],csize[0],"0xe"].join(",");
- var o = [
- writextag("xml", null, { 'xmlns:v': XLMLNS.v, 'xmlns:o': XLMLNS.o, 'xmlns:x': XLMLNS.x, 'xmlns:mv': XLMLNS.mv }).replace(/\/>/,">"),
- writextag("o:shapelayout", writextag("o:idmap", null, {'v:ext':"edit", 'data':rId}), {'v:ext':"edit"}),
- writextag("v:shapetype", [
- writextag("v:stroke", null, {joinstyle:"miter"}),
- writextag("v:path", null, {gradientshapeok:"t", 'o:connecttype':"rect"})
- ].join(""), {id:"_x0000_t202", 'o:spt':202, coordsize:csize.join(","),path:bbox})
- ];
- while(_shapeid < rId * 1000) _shapeid += 1000;
- comments.forEach(function(x) {
- var c = decode_cell(x[0]);
- var fillopts = {'color2':"#BEFF82", 'type':"gradient"};
- if(fillopts.type == "gradient") fillopts.angle = "-180";
- var fillparm = fillopts.type == "gradient" ? writextag("o:fill", null, {type:"gradientUnscaled", 'v:ext':"view"}) : null;
- var fillxml = writextag('v:fill', fillparm, fillopts);
- var shadata = ({on:"t", 'obscured':"t"});
- ++_shapeid;
- o = o.concat([
- '<v:shape' + wxt_helper({
- id:'_x0000_s' + _shapeid,
- type:"#_x0000_t202",
- style:"position:absolute; margin-left:80pt;margin-top:5pt;width:104pt;height:64pt;z-index:10" + (x[1].hidden ? ";visibility:hidden" : "") ,
- fillcolor:"#ECFAD4",
- strokecolor:"#edeaa1"
- }) + '>',
- fillxml,
- writextag("v:shadow", null, shadata),
- writextag("v:path", null, {'o:connecttype':"none"}),
- '<v:textbox><div style="text-align:left"></div></v:textbox>',
- '<x:ClientData ObjectType="Note">',
- '<x:MoveWithCells/>',
- '<x:SizeWithCells/>',
- /* Part 4 19.4.2.3 Anchor (Anchor) */
- writetag('x:Anchor', [c.c+1, 0, c.r+1, 0, c.c+3, 20, c.r+5, 20].join(",")),
- writetag('x:AutoFill', "False"),
- writetag('x:Row', String(c.r)),
- writetag('x:Column', String(c.c)),
- x[1].hidden ? '' : '<x:Visible/>',
- '</x:ClientData>',
- '</v:shape>'
- ]); });
- o.push('</xml>');
- return o.join("");
- }
- RELS.CMNT = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
- function sheet_insert_comments(sheet, comments) {
- var dense = Array.isArray(sheet);
- var cell;
- comments.forEach(function(comment) {
- var r = decode_cell(comment.ref);
- if(dense) {
- if(!sheet[r.r]) sheet[r.r] = [];
- cell = sheet[r.r][r.c];
- } else cell = sheet[comment.ref];
- if (!cell) {
- cell = ({t:"z"});
- if(dense) sheet[r.r][r.c] = cell;
- else sheet[comment.ref] = cell;
- var range = safe_decode_range(sheet["!ref"]||"BDWGO1000001:A1");
- if(range.s.r > r.r) range.s.r = r.r;
- if(range.e.r < r.r) range.e.r = r.r;
- if(range.s.c > r.c) range.s.c = r.c;
- if(range.e.c < r.c) range.e.c = r.c;
- var encoded = encode_range(range);
- if (encoded !== sheet["!ref"]) sheet["!ref"] = encoded;
- }
- if (!cell.c) cell.c = [];
- var o = ({a: comment.author, t: comment.t, r: comment.r});
- if(comment.h) o.h = comment.h;
- cell.c.push(o);
- });
- }
- /* 18.7 Comments */
- function parse_comments_xml(data, opts) {
- /* 18.7.6 CT_Comments */
- if(data.match(/<(?:\w+:)?comments *\/>/)) return [];
- var authors = [];
- var commentList = [];
- var authtag = data.match(/<(?:\w+:)?authors>([\s\S]*)<\/(?:\w+:)?authors>/);
- if(authtag && authtag[1]) authtag[1].split(/<\/\w*:?author>/).forEach(function(x) {
- if(x === "" || x.trim() === "") return;
- var a = x.match(/<(?:\w+:)?author[^>]*>(.*)/);
- if(a) authors.push(a[1]);
- });
- var cmnttag = data.match(/<(?:\w+:)?commentList>([\s\S]*)<\/(?:\w+:)?commentList>/);
- if(cmnttag && cmnttag[1]) cmnttag[1].split(/<\/\w*:?comment>/).forEach(function(x) {
- if(x === "" || x.trim() === "") return;
- var cm = x.match(/<(?:\w+:)?comment[^>]*>/);
- if(!cm) return;
- var y = parsexmltag(cm[0]);
- var comment = ({ author: y.authorId && authors[y.authorId] || "sheetjsghost", ref: y.ref, guid: y.guid });
- var cell = decode_cell(y.ref);
- if(opts.sheetRows && opts.sheetRows <= cell.r) return;
- var textMatch = x.match(/<(?:\w+:)?text>([\s\S]*)<\/(?:\w+:)?text>/);
- var rt = !!textMatch && !!textMatch[1] && parse_si(textMatch[1]) || {r:"",t:"",h:""};
- comment.r = rt.r;
- if(rt.r == "<t></t>") rt.t = rt.h = "";
- comment.t = rt.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
- if(opts.cellHTML) comment.h = rt.h;
- commentList.push(comment);
- });
- return commentList;
- }
- var CMNT_XML_ROOT = writextag('comments', null, { 'xmlns': XMLNS.main[0] });
- function write_comments_xml(data) {
- var o = [XML_HEADER, CMNT_XML_ROOT];
- var iauthor = [];
- o.push("<authors>");
- data.forEach(function(x) { x[1].forEach(function(w) { var a = escapexml(w.a);
- if(iauthor.indexOf(a) > -1) return;
- iauthor.push(a);
- o.push("<author>" + a + "</author>");
- }); });
- o.push("</authors>");
- o.push("<commentList>");
- data.forEach(function(d) {
- d[1].forEach(function(c) {
- /* 18.7.3 CT_Comment */
- o.push('<comment ref="' + d[0] + '" authorId="' + iauthor.indexOf(escapexml(c.a)) + '"><text>');
- o.push(writetag("t", c.t == null ? "" : escapexml(c.t)));
- o.push('</text></comment>');
- });
- });
- o.push("</commentList>");
- if(o.length>2) { o[o.length] = ('</comments>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- var CT_VBA = "application/vnd.ms-office.vbaProject";
- function make_vba_xls(cfb) {
- var newcfb = CFB.utils.cfb_new({root:"R"});
- cfb.FullPaths.forEach(function(p, i) {
- if(p.slice(-1) === "/" || !p.match(/_VBA_PROJECT_CUR/)) return;
- var newpath = p.replace(/^[^\/]*/,"R").replace(/\/_VBA_PROJECT_CUR\u0000*/, "");
- CFB.utils.cfb_add(newcfb, newpath, cfb.FileIndex[i].content);
- });
- return CFB.write(newcfb);
- }
- function fill_vba_xls(cfb, vba) {
- vba.FullPaths.forEach(function(p, i) {
- if(i == 0) return;
- var newpath = p.replace(/[^\/]*[\/]/, "/_VBA_PROJECT_CUR/");
- if(newpath.slice(-1) !== "/") CFB.utils.cfb_add(cfb, newpath, vba.FileIndex[i].content);
- });
- }
- var VBAFMTS = [ "xlsb", "xlsm", "xlam", "biff8", "xla" ];
- RELS.DS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet";
- RELS.MS = "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet";
- /* macro and dialog sheet stubs */
- function parse_ds_bin() { return {'!type':'dialog'}; }
- function parse_ds_xml() { return {'!type':'dialog'}; }
- function parse_ms_bin() { return {'!type':'macro'}; }
- function parse_ms_xml() { return {'!type':'macro'}; }
- /* TODO: it will be useful to parse the function str */
- var rc_to_a1 = (function(){
- var rcregex = /(^|[^A-Za-z_])R(\[?-?\d+\]|[1-9]\d*|)C(\[?-?\d+\]|[1-9]\d*|)(?![A-Za-z0-9_])/g;
- var rcbase = ({r:0,c:0});
- function rcfunc($$,$1,$2,$3) {
- var cRel = false, rRel = false;
- if($2.length == 0) rRel = true;
- else if($2.charAt(0) == "[") { rRel = true; $2 = $2.slice(1, -1); }
- if($3.length == 0) cRel = true;
- else if($3.charAt(0) == "[") { cRel = true; $3 = $3.slice(1, -1); }
- var R = $2.length>0?parseInt($2,10)|0:0, C = $3.length>0?parseInt($3,10)|0:0;
- if(cRel) C += rcbase.c; else --C;
- if(rRel) R += rcbase.r; else --R;
- return $1 + (cRel ? "" : "$") + encode_col(C) + (rRel ? "" : "$") + encode_row(R);
- }
- return function rc_to_a1(fstr, base) {
- rcbase = base;
- return fstr.replace(rcregex, rcfunc);
- };
- })();
- 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;
- var a1_to_rc =(function(){
- return function a1_to_rc(fstr, base) {
- return fstr.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
- var c = decode_col($3) - ($2 ? 0 : base.c);
- var r = decode_row($5) - ($4 ? 0 : base.r);
- var R = (r == 0 ? "" : !$4 ? "[" + r + "]" : (r+1));
- var C = (c == 0 ? "" : !$2 ? "[" + c + "]" : (c+1));
- return $1 + "R" + R + "C" + C;
- });
- };
- })();
- /* no defined name can collide with a valid cell address A1:XFD1048576 ... except LOG10! */
- function shift_formula_str(f, delta) {
- return f.replace(crefregex, function($0, $1, $2, $3, $4, $5) {
- return $1+($2=="$" ? $2+$3 : encode_col(decode_col($3)+delta.c))+($4=="$" ? $4+$5 : encode_row(decode_row($5) + delta.r));
- });
- }
- function shift_formula_xlsx(f, range, cell) {
- var r = decode_range(range), s = r.s, c = decode_cell(cell);
- var delta = {r:c.r - s.r, c:c.c - s.c};
- return shift_formula_str(f, delta);
- }
- /* TODO: parse formula */
- function fuzzyfmla(f) {
- if(f.length == 1) return false;
- return true;
- }
- function _xlfn(f) {
- return f.replace(/_xlfn\./g,"");
- }
- var strs = {}; // shared strings
- var _ssfopts = {}; // spreadsheet formatting options
- RELS.WS = [
- "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
- "http://purl.oclc.org/ooxml/officeDocument/relationships/worksheet"
- ];
- /*global Map */
- var browser_has_Map = typeof Map !== 'undefined';
- function get_sst_id(sst, str, rev) {
- var i = 0, len = sst.length;
- if(rev) {
- if(browser_has_Map ? rev.has(str) : Object.prototype.hasOwnProperty.call(rev, str)) {
- var revarr = browser_has_Map ? rev.get(str) : rev[str];
- for(; i < revarr.length; ++i) {
- if(sst[revarr[i]].t === str) { sst.Count ++; return revarr[i]; }
- }
- }
- } else for(; i < len; ++i) {
- if(sst[i].t === str) { sst.Count ++; return i; }
- }
- sst[len] = ({t:str}); sst.Count ++; sst.Unique ++;
- if(rev) {
- if(browser_has_Map) {
- if(!rev.has(str)) rev.set(str, []);
- rev.get(str).push(len);
- } else {
- if(!Object.prototype.hasOwnProperty.call(rev, str)) rev[str] = [];
- rev[str].push(len);
- }
- }
- return len;
- }
- function col_obj_w(C, col) {
- var p = ({min:C+1,max:C+1});
- /* wch (chars), wpx (pixels) */
- var wch = -1;
- if(col.MDW) MDW = col.MDW;
- if(col.width != null) p.customWidth = 1;
- else if(col.wpx != null) wch = px2char(col.wpx);
- else if(col.wch != null) wch = col.wch;
- if(wch > -1) { p.width = char2width(wch); p.customWidth = 1; }
- else if(col.width != null) p.width = col.width;
- if(col.hidden) p.hidden = true;
- return p;
- }
- function default_margins(margins, mode) {
- if(!margins) return;
- var defs = [0.7, 0.7, 0.75, 0.75, 0.3, 0.3];
- if(mode == 'xlml') defs = [1, 1, 1, 1, 0.5, 0.5];
- if(margins.left == null) margins.left = defs[0];
- if(margins.right == null) margins.right = defs[1];
- if(margins.top == null) margins.top = defs[2];
- if(margins.bottom == null) margins.bottom = defs[3];
- if(margins.header == null) margins.header = defs[4];
- if(margins.footer == null) margins.footer = defs[5];
- }
- function get_cell_style(styles, cell, opts) {
- var z = opts.revssf[cell.z != null ? cell.z : "General"];
- var i = 0x3c, len = styles.length;
- if(z == null && opts.ssf) {
- for(; i < 0x188; ++i) if(opts.ssf[i] == null) {
- SSF.load(cell.z, i);
- // $FlowIgnore
- opts.ssf[i] = cell.z;
- opts.revssf[cell.z] = z = i;
- break;
- }
- }
- for(i = 0; i != len; ++i) if(styles[i].numFmtId === z) return i;
- styles[len] = {
- numFmtId:z,
- fontId:0,
- fillId:0,
- borderId:0,
- xfId:0,
- applyNumberFormat:1
- };
- return len;
- }
- function safe_format(p, fmtid, fillid, opts, themes, styles) {
- try {
- if(opts.cellNF) p.z = SSF._table[fmtid];
- } catch(e) { if(opts.WTF) throw e; }
- if(p.t === 'z') return;
- if(p.t === 'd' && typeof p.v === 'string') p.v = parseDate(p.v);
- if(!opts || opts.cellText !== false) try {
- if(SSF._table[fmtid] == null) SSF.load(SSFImplicit[fmtid] || "General", fmtid);
- if(p.t === 'e') p.w = p.w || BErr[p.v];
- else if(fmtid === 0) {
- if(p.t === 'n') {
- if((p.v|0) === p.v) p.w = SSF._general_int(p.v);
- else p.w = SSF._general_num(p.v);
- }
- else if(p.t === 'd') {
- var dd = datenum(p.v);
- if((dd|0) === dd) p.w = SSF._general_int(dd);
- else p.w = SSF._general_num(dd);
- }
- else if(p.v === undefined) return "";
- else p.w = SSF._general(p.v,_ssfopts);
- }
- else if(p.t === 'd') p.w = SSF.format(fmtid,datenum(p.v),_ssfopts);
- else p.w = SSF.format(fmtid,p.v,_ssfopts);
- } catch(e) { if(opts.WTF) throw e; }
- if(!opts.cellStyles) return;
- if(fillid != null) try {
- p.s = styles.Fills[fillid];
- if (p.s.fgColor && p.s.fgColor.theme && !p.s.fgColor.rgb) {
- p.s.fgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.fgColor.theme].rgb, p.s.fgColor.tint || 0);
- if(opts.WTF) p.s.fgColor.raw_rgb = themes.themeElements.clrScheme[p.s.fgColor.theme].rgb;
- }
- if (p.s.bgColor && p.s.bgColor.theme) {
- p.s.bgColor.rgb = rgb_tint(themes.themeElements.clrScheme[p.s.bgColor.theme].rgb, p.s.bgColor.tint || 0);
- if(opts.WTF) p.s.bgColor.raw_rgb = themes.themeElements.clrScheme[p.s.bgColor.theme].rgb;
- }
- } catch(e) { if(opts.WTF && styles.Fills) throw e; }
- }
- function check_ws(ws, sname, i) {
- if(ws && ws['!ref']) {
- var range = safe_decode_range(ws['!ref']);
- if(range.e.c < range.s.c || range.e.r < range.s.r) throw new Error("Bad range (" + i + "): " + ws['!ref']);
- }
- }
- function parse_ws_xml_dim(ws, s) {
- var d = safe_decode_range(s);
- if(d.s.r<=d.e.r && d.s.c<=d.e.c && d.s.r>=0 && d.s.c>=0) ws["!ref"] = encode_range(d);
- }
- var mergecregex = /<(?:\w:)?mergeCell ref="[A-Z0-9:]+"\s*[\/]?>/g;
- var sheetdataregex = /<(?:\w+:)?sheetData[^>]*>([\s\S]*)<\/(?:\w+:)?sheetData>/;
- var hlinkregex = /<(?:\w:)?hyperlink [^>]*>/mg;
- var dimregex = /"(\w*:\w*)"/;
- var colregex = /<(?:\w:)?col\b[^>]*[\/]?>/g;
- var afregex = /<(?:\w:)?autoFilter[^>]*([\/]|>([\s\S]*)<\/(?:\w:)?autoFilter)>/g;
- var marginregex= /<(?:\w:)?pageMargins[^>]*\/>/g;
- var sheetprregex = /<(?:\w:)?sheetPr\b(?:[^>a-z][^>]*)?\/>/;
- var svsregex = /<(?:\w:)?sheetViews[^>]*(?:[\/]|>([\s\S]*)<\/(?:\w:)?sheetViews)>/;
- /* 18.3 Worksheets */
- function parse_ws_xml(data, opts, idx, rels, wb, themes, styles) {
- if(!data) return data;
- if(!rels) rels = {'!id':{}};
- if(DENSE != null && opts.dense == null) opts.dense = DENSE;
- /* 18.3.1.99 worksheet CT_Worksheet */
- var s = opts.dense ? ([]) : ({});
- var refguess = ({s: {r:2000000, c:2000000}, e: {r:0, c:0} });
- var data1 = "", data2 = "";
- var mtch = data.match(sheetdataregex);
- if(mtch) {
- data1 = data.slice(0, mtch.index);
- data2 = data.slice(mtch.index + mtch[0].length);
- } else data1 = data2 = data;
- /* 18.3.1.82 sheetPr CT_SheetPr */
- var sheetPr = data1.match(sheetprregex);
- if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
- /* 18.3.1.35 dimension CT_SheetDimension */
- var ridx = (data1.match(/<(?:\w*:)?dimension/)||{index:-1}).index;
- if(ridx > 0) {
- var ref = data1.slice(ridx,ridx+50).match(dimregex);
- if(ref) parse_ws_xml_dim(s, ref[1]);
- }
- /* 18.3.1.88 sheetViews CT_SheetViews */
- var svs = data1.match(svsregex);
- if(svs && svs[1]) parse_ws_xml_sheetviews(svs[1], wb);
- /* 18.3.1.17 cols CT_Cols */
- var columns = [];
- if(opts.cellStyles) {
- /* 18.3.1.13 col CT_Col */
- var cols = data1.match(colregex);
- if(cols) parse_ws_xml_cols(columns, cols);
- }
- /* 18.3.1.80 sheetData CT_SheetData ? */
- if(mtch) parse_ws_xml_data(mtch[1], s, opts, refguess, themes, styles);
- /* 18.3.1.2 autoFilter CT_AutoFilter */
- var afilter = data2.match(afregex);
- if(afilter) s['!autofilter'] = parse_ws_xml_autofilter(afilter[0]);
- /* 18.3.1.55 mergeCells CT_MergeCells */
- var merges = [];
- var _merge = data2.match(mergecregex);
- if(_merge) for(ridx = 0; ridx != _merge.length; ++ridx)
- merges[ridx] = safe_decode_range(_merge[ridx].slice(_merge[ridx].indexOf("\"")+1));
- /* 18.3.1.48 hyperlinks CT_Hyperlinks */
- var hlink = data2.match(hlinkregex);
- if(hlink) parse_ws_xml_hlinks(s, hlink, rels);
- /* 18.3.1.62 pageMargins CT_PageMargins */
- var margins = data2.match(marginregex);
- if(margins) s['!margins'] = parse_ws_xml_margins(parsexmltag(margins[0]));
- if(!s["!ref"] && refguess.e.c >= refguess.s.c && refguess.e.r >= refguess.s.r) s["!ref"] = encode_range(refguess);
- if(opts.sheetRows > 0 && s["!ref"]) {
- var tmpref = safe_decode_range(s["!ref"]);
- if(opts.sheetRows <= +tmpref.e.r) {
- tmpref.e.r = opts.sheetRows - 1;
- if(tmpref.e.r > refguess.e.r) tmpref.e.r = refguess.e.r;
- if(tmpref.e.r < tmpref.s.r) tmpref.s.r = tmpref.e.r;
- if(tmpref.e.c > refguess.e.c) tmpref.e.c = refguess.e.c;
- if(tmpref.e.c < tmpref.s.c) tmpref.s.c = tmpref.e.c;
- s["!fullref"] = s["!ref"];
- s["!ref"] = encode_range(tmpref);
- }
- }
- if(columns.length > 0) s["!cols"] = columns;
- if(merges.length > 0) s["!merges"] = merges;
- return s;
- }
- function write_ws_xml_merges(merges) {
- if(merges.length === 0) return "";
- var o = '<mergeCells count="' + merges.length + '">';
- for(var i = 0; i != merges.length; ++i) o += '<mergeCell ref="' + encode_range(merges[i]) + '"/>';
- return o + '</mergeCells>';
- }
- /* 18.3.1.82-3 sheetPr CT_ChartsheetPr / CT_SheetPr */
- function parse_ws_xml_sheetpr(sheetPr, s, wb, idx) {
- var data = parsexmltag(sheetPr);
- if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
- if(data.codeName) wb.Sheets[idx].CodeName = unescapexml(utf8read(data.codeName));
- }
- function write_ws_xml_sheetpr(ws, wb, idx, opts, o) {
- var needed = false;
- var props = {}, payload = null;
- if(opts.bookType !== 'xlsx' && wb.vbaraw) {
- var cname = wb.SheetNames[idx];
- try { if(wb.Workbook) cname = wb.Workbook.Sheets[idx].CodeName || cname; } catch(e) {}
- needed = true;
- props.codeName = utf8write(escapexml(cname));
- }
- if(ws && ws["!outline"]) {
- var outlineprops = {summaryBelow:1, summaryRight:1};
- if(ws["!outline"].above) outlineprops.summaryBelow = 0;
- if(ws["!outline"].left) outlineprops.summaryRight = 0;
- payload = (payload||"") + writextag('outlinePr', null, outlineprops);
- }
- if(!needed && !payload) return;
- o[o.length] = (writextag('sheetPr', payload, props));
- }
- /* 18.3.1.85 sheetProtection CT_SheetProtection */
- var sheetprot_deffalse = ["objects", "scenarios", "selectLockedCells", "selectUnlockedCells"];
- var sheetprot_deftrue = [
- "formatColumns", "formatRows", "formatCells",
- "insertColumns", "insertRows", "insertHyperlinks",
- "deleteColumns", "deleteRows",
- "sort", "autoFilter", "pivotTables"
- ];
- function write_ws_xml_protection(sp) {
- // algorithmName, hashValue, saltValue, spinCount
- var o = ({sheet:1});
- sheetprot_deffalse.forEach(function(n) { if(sp[n] != null && sp[n]) o[n] = "1"; });
- sheetprot_deftrue.forEach(function(n) { if(sp[n] != null && !sp[n]) o[n] = "0"; });
- /* TODO: algorithm */
- if(sp.password) o.password = crypto_CreatePasswordVerifier_Method1(sp.password).toString(16).toUpperCase();
- return writextag('sheetProtection', null, o);
- }
- function parse_ws_xml_hlinks(s, data, rels) {
- var dense = Array.isArray(s);
- for(var i = 0; i != data.length; ++i) {
- var val = parsexmltag(utf8read(data[i]), true);
- if(!val.ref) return;
- var rel = ((rels || {})['!id']||[])[val.id];
- if(rel) {
- val.Target = rel.Target;
- if(val.location) val.Target += "#"+val.location;
- } else {
- val.Target = "#" + val.location;
- rel = {Target: val.Target, TargetMode: 'Internal'};
- }
- val.Rel = rel;
- if(val.tooltip) { val.Tooltip = val.tooltip; delete val.tooltip; }
- var rng = safe_decode_range(val.ref);
- for(var R=rng.s.r;R<=rng.e.r;++R) for(var C=rng.s.c;C<=rng.e.c;++C) {
- var addr = encode_cell({c:C,r:R});
- if(dense) {
- if(!s[R]) s[R] = [];
- if(!s[R][C]) s[R][C] = {t:"z",v:undefined};
- s[R][C].l = val;
- } else {
- if(!s[addr]) s[addr] = {t:"z",v:undefined};
- s[addr].l = val;
- }
- }
- }
- }
- function parse_ws_xml_margins(margin) {
- var o = {};
- ["left", "right", "top", "bottom", "header", "footer"].forEach(function(k) {
- if(margin[k]) o[k] = parseFloat(margin[k]);
- });
- return o;
- }
- function write_ws_xml_margins(margin) {
- default_margins(margin);
- return writextag('pageMargins', null, margin);
- }
- function parse_ws_xml_cols(columns, cols) {
- var seencol = false;
- for(var coli = 0; coli != cols.length; ++coli) {
- var coll = parsexmltag(cols[coli], true);
- if(coll.hidden) coll.hidden = parsexmlbool(coll.hidden);
- var colm=parseInt(coll.min, 10)-1, colM=parseInt(coll.max,10)-1;
- delete coll.min; delete coll.max; coll.width = +coll.width;
- if(!seencol && coll.width) { seencol = true; find_mdw_colw(coll.width); }
- process_col(coll);
- while(colm <= colM) columns[colm++] = dup(coll);
- }
- }
- function write_ws_xml_cols(ws, cols) {
- var o = ["<cols>"], col;
- for(var i = 0; i != cols.length; ++i) {
- if(!(col = cols[i])) continue;
- o[o.length] = (writextag('col', null, col_obj_w(i, col)));
- }
- o[o.length] = "</cols>";
- return o.join("");
- }
- function parse_ws_xml_autofilter(data) {
- var o = { ref: (data.match(/ref="([^"]*)"/)||[])[1]};
- return o;
- }
- function write_ws_xml_autofilter(data, ws, wb, idx) {
- var ref = typeof data.ref == "string" ? data.ref : encode_range(data.ref);
- if(!wb.Workbook) wb.Workbook = ({Sheets:[]});
- if(!wb.Workbook.Names) wb.Workbook.Names = [];
- var names = wb.Workbook.Names;
- var range = decode_range(ref);
- if(range.s.r == range.e.r) { range.e.r = decode_range(ws["!ref"]).e.r; ref = encode_range(range); }
- for(var i = 0; i < names.length; ++i) {
- var name = names[i];
- if(name.Name != '_xlnm._FilterDatabase') continue;
- if(name.Sheet != idx) continue;
- name.Ref = "'" + wb.SheetNames[idx] + "'!" + ref; break;
- }
- if(i == names.length) names.push({ Name: '_xlnm._FilterDatabase', Sheet: idx, Ref: "'" + wb.SheetNames[idx] + "'!" + ref });
- return writextag("autoFilter", null, {ref:ref});
- }
- /* 18.3.1.88 sheetViews CT_SheetViews */
- /* 18.3.1.87 sheetView CT_SheetView */
- var sviewregex = /<(?:\w:)?sheetView(?:[^>a-z][^>]*)?\/?>/;
- function parse_ws_xml_sheetviews(data, wb) {
- if(!wb.Views) wb.Views = [{}];
- (data.match(sviewregex)||[]).forEach(function(r, i) {
- var tag = parsexmltag(r);
- // $FlowIgnore
- if(!wb.Views[i]) wb.Views[i] = {};
- // $FlowIgnore
- if(parsexmlbool(tag.rightToLeft)) wb.Views[i].RTL = true;
- });
- }
- function write_ws_xml_sheetviews(ws, opts, idx, wb) {
- var sview = ({workbookViewId:"0"});
- // $FlowIgnore
- if((((wb||{}).Workbook||{}).Views||[])[0]) sview.rightToLeft = wb.Workbook.Views[0].RTL ? "1" : "0";
- return writextag("sheetViews", writextag("sheetView", null, sview), {});
- }
- function write_ws_xml_cell(cell, ref, ws, opts) {
- if(cell.v === undefined && typeof cell.f !== "string" || cell.t === 'z') return "";
- var vv = "";
- var oldt = cell.t, oldv = cell.v;
- if(cell.t !== "z") switch(cell.t) {
- case 'b': vv = cell.v ? "1" : "0"; break;
- case 'n': vv = ''+cell.v; break;
- case 'e': vv = BErr[cell.v]; break;
- case 'd':
- if(opts && opts.cellDates) vv = parseDate(cell.v, -1).toISOString();
- else {
- cell = dup(cell);
- cell.t = 'n';
- vv = ''+(cell.v = datenum(parseDate(cell.v)));
- }
- if(typeof cell.z === 'undefined') cell.z = SSF._table[14];
- break;
- default: vv = cell.v; break;
- }
- var v = writetag('v', escapexml(vv)), o = ({r:ref});
- /* TODO: cell style */
- var os = get_cell_style(opts.cellXfs, cell, opts);
- if(os !== 0) o.s = os;
- switch(cell.t) {
- case 'n': break;
- case 'd': o.t = "d"; break;
- case 'b': o.t = "b"; break;
- case 'e': o.t = "e"; break;
- case 'z': break;
- default: if(cell.v == null) { delete cell.t; break; }
- if(opts && opts.bookSST) {
- v = writetag('v', ''+get_sst_id(opts.Strings, cell.v, opts.revStrings));
- o.t = "s"; break;
- }
- o.t = "str"; break;
- }
- if(cell.t != oldt) { cell.t = oldt; cell.v = oldv; }
- if(typeof cell.f == "string" && cell.f) {
- var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null;
- v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : "");
- }
- if(cell.l) ws['!links'].push([ref, cell.l]);
- if(cell.c) ws['!comments'].push([ref, cell.c]);
- return writextag('c', v, o);
- }
- var parse_ws_xml_data = (function() {
- var cellregex = /<(?:\w+:)?c[ \/>]/, rowregex = /<\/(?:\w+:)?row>/;
- var rregex = /r=["']([^"']*)["']/, isregex = /<(?:\w+:)?is>([\S\s]*?)<\/(?:\w+:)?is>/;
- var refregex = /ref=["']([^"']*)["']/;
- var match_v = matchtag("v"), match_f = matchtag("f");
- return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
- var ri = 0, x = "", cells = [], cref = [], idx=0, i=0, cc=0, d="", p;
- var tag, tagr = 0, tagc = 0;
- var sstr, ftag;
- var fmtid = 0, fillid = 0;
- var do_format = Array.isArray(styles.CellXf), cf;
- var arrayf = [];
- var sharedf = [];
- var dense = Array.isArray(s);
- var rows = [], rowobj = {}, rowrite = false;
- var sheetStubs = !!opts.sheetStubs;
- for(var marr = sdata.split(rowregex), mt = 0, marrlen = marr.length; mt != marrlen; ++mt) {
- x = marr[mt].trim();
- var xlen = x.length;
- if(xlen === 0) continue;
- /* 18.3.1.73 row CT_Row */
- for(ri = 0; ri < xlen; ++ri) if(x.charCodeAt(ri) === 62) break; ++ri;
- tag = parsexmltag(x.slice(0,ri), true);
- tagr = tag.r != null ? parseInt(tag.r, 10) : tagr+1; tagc = -1;
- if(opts.sheetRows && opts.sheetRows < tagr) continue;
- if(guess.s.r > tagr - 1) guess.s.r = tagr - 1;
- if(guess.e.r < tagr - 1) guess.e.r = tagr - 1;
- if(opts && opts.cellStyles) {
- rowobj = {}; rowrite = false;
- if(tag.ht) { rowrite = true; rowobj.hpt = parseFloat(tag.ht); rowobj.hpx = pt2px(rowobj.hpt); }
- if(tag.hidden == "1") { rowrite = true; rowobj.hidden = true; }
- if(tag.outlineLevel != null) { rowrite = true; rowobj.level = +tag.outlineLevel; }
- if(rowrite) rows[tagr-1] = rowobj;
- }
- /* 18.3.1.4 c CT_Cell */
- cells = x.slice(ri).split(cellregex);
- for(var rslice = 0; rslice != cells.length; ++rslice) if(cells[rslice].trim().charAt(0) != "<") break;
- cells = cells.slice(rslice);
- for(ri = 0; ri != cells.length; ++ri) {
- x = cells[ri].trim();
- if(x.length === 0) continue;
- cref = x.match(rregex); idx = ri; i=0; cc=0;
- x = "<c " + (x.slice(0,1)=="<"?">":"") + x;
- if(cref != null && cref.length === 2) {
- idx = 0; d=cref[1];
- for(i=0; i != d.length; ++i) {
- if((cc=d.charCodeAt(i)-64) < 1 || cc > 26) break;
- idx = 26*idx + cc;
- }
- --idx;
- tagc = idx;
- } else ++tagc;
- for(i = 0; i != x.length; ++i) if(x.charCodeAt(i) === 62) break; ++i;
- tag = parsexmltag(x.slice(0,i), true);
- if(!tag.r) tag.r = encode_cell({r:tagr-1, c:tagc});
- d = x.slice(i);
- p = ({t:""});
- if((cref=d.match(match_v))!= null && cref[1] !== '') p.v=unescapexml(cref[1]);
- if(opts.cellFormula) {
- if((cref=d.match(match_f))!= null && cref[1] !== '') {
- /* TODO: match against XLSXFutureFunctions */
- p.f=unescapexml(utf8read(cref[1])).replace(/\r\n/g, "\n");
- if(!opts.xlfn) p.f = _xlfn(p.f);
- if(cref[0].indexOf('t="array"') > -1) {
- p.F = (d.match(refregex)||[])[1];
- if(p.F.indexOf(":") > -1) arrayf.push([safe_decode_range(p.F), p.F]);
- } else if(cref[0].indexOf('t="shared"') > -1) {
- // TODO: parse formula
- ftag = parsexmltag(cref[0]);
- var ___f = unescapexml(utf8read(cref[1]));
- if(!opts.xlfn) ___f = _xlfn(___f);
- sharedf[parseInt(ftag.si, 10)] = [ftag, ___f, tag.r];
- }
- } else if((cref=d.match(/<f[^>]*\/>/))) {
- ftag = parsexmltag(cref[0]);
- if(sharedf[ftag.si]) p.f = shift_formula_xlsx(sharedf[ftag.si][1], sharedf[ftag.si][2]/*[0].ref*/, tag.r);
- }
- /* TODO: factor out contains logic */
- var _tag = decode_cell(tag.r);
- for(i = 0; i < arrayf.length; ++i)
- if(_tag.r >= arrayf[i][0].s.r && _tag.r <= arrayf[i][0].e.r)
- if(_tag.c >= arrayf[i][0].s.c && _tag.c <= arrayf[i][0].e.c)
- p.F = arrayf[i][1];
- }
- if(tag.t == null && p.v === undefined) {
- if(p.f || p.F) {
- p.v = 0; p.t = "n";
- } else if(!sheetStubs) continue;
- else p.t = "z";
- }
- else p.t = tag.t || "n";
- if(guess.s.c > tagc) guess.s.c = tagc;
- if(guess.e.c < tagc) guess.e.c = tagc;
- /* 18.18.11 t ST_CellType */
- switch(p.t) {
- case 'n':
- if(p.v == "" || p.v == null) {
- if(!sheetStubs) continue;
- p.t = 'z';
- } else p.v = parseFloat(p.v);
- break;
- case 's':
- if(typeof p.v == 'undefined') {
- if(!sheetStubs) continue;
- p.t = 'z';
- } else {
- sstr = strs[parseInt(p.v, 10)];
- p.v = sstr.t;
- p.r = sstr.r;
- if(opts.cellHTML) p.h = sstr.h;
- }
- break;
- case 'str':
- p.t = "s";
- p.v = (p.v!=null) ? utf8read(p.v) : '';
- if(opts.cellHTML) p.h = escapehtml(p.v);
- break;
- case 'inlineStr':
- cref = d.match(isregex);
- p.t = 's';
- if(cref != null && (sstr = parse_si(cref[1]))) {
- p.v = sstr.t;
- if(opts.cellHTML) p.h = sstr.h;
- } else p.v = "";
- break;
- case 'b': p.v = parsexmlbool(p.v); break;
- case 'd':
- if(opts.cellDates) p.v = parseDate(p.v, 1);
- else { p.v = datenum(parseDate(p.v, 1)); p.t = 'n'; }
- break;
- /* error string in .w, number in .v */
- case 'e':
- if(!opts || opts.cellText !== false) p.w = p.v;
- p.v = RBErr[p.v]; break;
- }
- /* formatting */
- fmtid = fillid = 0;
- cf = null;
- if(do_format && tag.s !== undefined) {
- cf = styles.CellXf[tag.s];
- if(cf != null) {
- if(cf.numFmtId != null) fmtid = cf.numFmtId;
- if(opts.cellStyles) {
- if(cf.fillId != null) fillid = cf.fillId;
- }
- }
- }
- safe_format(p, fmtid, fillid, opts, themes, styles);
- if(opts.cellDates && do_format && p.t == 'n' && SSF.is_date(SSF._table[fmtid])) { p.t = 'd'; p.v = numdate(p.v); }
- if(dense) {
- var _r = decode_cell(tag.r);
- if(!s[_r.r]) s[_r.r] = [];
- s[_r.r][_r.c] = p;
- } else s[tag.r] = p;
- }
- }
- if(rows.length > 0) s['!rows'] = rows;
- }; })();
- function write_ws_xml_data(ws, opts, idx, wb) {
- var o = [], r = [], range = safe_decode_range(ws['!ref']), cell="", ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
- var dense = Array.isArray(ws);
- var params = ({r:rr}), row, height = -1;
- for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
- for(R = range.s.r; R <= range.e.r; ++R) {
- r = [];
- rr = encode_row(R);
- for(C = range.s.c; C <= range.e.c; ++C) {
- ref = cols[C] + rr;
- var _cell = dense ? (ws[R]||[])[C]: ws[ref];
- if(_cell === undefined) continue;
- if((cell = write_ws_xml_cell(_cell, ref, ws, opts, idx, wb)) != null) r.push(cell);
- }
- if(r.length > 0 || (rows && rows[R])) {
- params = ({r:rr});
- if(rows && rows[R]) {
- row = rows[R];
- if(row.hidden) params.hidden = 1;
- height = -1;
- if(row.hpx) height = px2pt(row.hpx);
- else if(row.hpt) height = row.hpt;
- if(height > -1) { params.ht = height; params.customHeight = 1; }
- if(row.level) { params.outlineLevel = row.level; }
- }
- o[o.length] = (writextag('row', r.join(""), params));
- }
- }
- if(rows) for(; R < rows.length; ++R) {
- if(rows && rows[R]) {
- params = ({r:R+1});
- row = rows[R];
- if(row.hidden) params.hidden = 1;
- height = -1;
- if (row.hpx) height = px2pt(row.hpx);
- else if (row.hpt) height = row.hpt;
- if (height > -1) { params.ht = height; params.customHeight = 1; }
- if (row.level) { params.outlineLevel = row.level; }
- o[o.length] = (writextag('row', "", params));
- }
- }
- return o.join("");
- }
- var WS_XML_ROOT = writextag('worksheet', null, {
- 'xmlns': XMLNS.main[0],
- 'xmlns:r': XMLNS.r
- });
- function write_ws_xml(idx, opts, wb, rels) {
- var o = [XML_HEADER, WS_XML_ROOT];
- var s = wb.SheetNames[idx], sidx = 0, rdata = "";
- var ws = wb.Sheets[s];
- if(ws == null) ws = {};
- var ref = ws['!ref'] || 'A1';
- var range = safe_decode_range(ref);
- if(range.e.c > 0x3FFF || range.e.r > 0xFFFFF) {
- if(opts.WTF) throw new Error("Range " + ref + " exceeds format limit A1:XFD1048576");
- range.e.c = Math.min(range.e.c, 0x3FFF);
- range.e.r = Math.min(range.e.c, 0xFFFFF);
- ref = encode_range(range);
- }
- if(!rels) rels = {};
- ws['!comments'] = [];
- var _drawing = [];
- write_ws_xml_sheetpr(ws, wb, idx, opts, o);
- o[o.length] = (writextag('dimension', null, {'ref': ref}));
- o[o.length] = write_ws_xml_sheetviews(ws, opts, idx, wb);
- /* TODO: store in WB, process styles */
- if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {
- defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16',
- baseColWidth:opts.sheetFormat.baseColWidth||'10',
- outlineLevelRow:opts.sheetFormat.outlineLevelRow||'7'
- }));
- if(ws['!cols'] != null && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
- o[sidx = o.length] = '<sheetData/>';
- ws['!links'] = [];
- if(ws['!ref'] != null) {
- rdata = write_ws_xml_data(ws, opts, idx, wb, rels);
- if(rdata.length > 0) o[o.length] = (rdata);
- }
- if(o.length>sidx+1) { o[o.length] = ('</sheetData>'); o[sidx]=o[sidx].replace("/>",">"); }
- /* sheetCalcPr */
- if(ws['!protect'] != null) o[o.length] = write_ws_xml_protection(ws['!protect']);
- /* protectedRanges */
- /* scenarios */
- if(ws['!autofilter'] != null) o[o.length] = write_ws_xml_autofilter(ws['!autofilter'], ws, wb, idx);
- /* sortState */
- /* dataConsolidate */
- /* customSheetViews */
- if(ws['!merges'] != null && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));
- /* phoneticPr */
- /* conditionalFormatting */
- /* dataValidations */
- var relc = -1, rel, rId = -1;
- if(ws['!links'].length > 0) {
- o[o.length] = "<hyperlinks>";
- ws['!links'].forEach(function(l) {
- if(!l[1].Target) return;
- rel = ({"ref":l[0]});
- if(l[1].Target.charAt(0) != "#") {
- rId = add_rels(rels, -1, escapexml(l[1].Target).replace(/#.*$/, ""), RELS.HLINK);
- rel["r:id"] = "rId"+rId;
- }
- if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1));
- if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip);
- o[o.length] = writextag("hyperlink",null,rel);
- });
- o[o.length] = "</hyperlinks>";
- }
- delete ws['!links'];
- /* printOptions */
- if(ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']);
- /* pageSetup */
- /* headerFooter */
- /* rowBreaks */
- /* colBreaks */
- /* customProperties */
- /* cellWatches */
- if(!opts || opts.ignoreEC || (opts.ignoreEC == (void 0))) o[o.length] = writetag("ignoredErrors", writextag("ignoredError", null, {numberStoredAsText:1, sqref:ref}));
- /* smartTags */
- if(_drawing.length > 0) {
- rId = add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
- o[o.length] = writextag("drawing", null, {"r:id":"rId" + rId});
- ws['!drawing'] = _drawing;
- }
- if(ws['!comments'].length > 0) {
- rId = add_rels(rels, -1, "../drawings/vmlDrawing" + (idx+1) + ".vml", RELS.VML);
- o[o.length] = writextag("legacyDrawing", null, {"r:id":"rId" + rId});
- ws['!legacy'] = rId;
- }
- /* legacyDrawingHF */
- /* picture */
- /* oleObjects */
- /* controls */
- /* webPublishItems */
- /* tableParts */
- /* extLst */
- if(o.length>1) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- RELS.CHART = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
- RELS.CHARTEX = "http://schemas.microsoft.com/office/2014/relationships/chartEx";
- function parse_Cache(data) {
- var col = [];
- var num = data.match(/^<c:numCache>/);
- var f;
- /* 21.2.2.150 pt CT_NumVal */
- (data.match(/<c:pt idx="(\d*)">(.*?)<\/c:pt>/mg)||[]).forEach(function(pt) {
- var q = pt.match(/<c:pt idx="(\d*?)"><c:v>(.*)<\/c:v><\/c:pt>/);
- if(!q) return;
- col[+q[1]] = num ? +q[2] : q[2];
- });
- /* 21.2.2.71 formatCode CT_Xstring */
- var nf = unescapexml((data.match(/<c:formatCode>([\s\S]*?)<\/c:formatCode>/) || ["","General"])[1]);
- (data.match(/<c:f>(.*?)<\/c:f>/mg)||[]).forEach(function(F) { f = F.replace(/<.*?>/g,""); });
- return [col, nf, f];
- }
- /* 21.2 DrawingML - Charts */
- function parse_chart(data, name, opts, rels, wb, csheet) {
- var cs = ((csheet || {"!type":"chart"}));
- if(!data) return csheet;
- /* 21.2.2.27 chart CT_Chart */
- var C = 0, R = 0, col = "A";
- var refguess = {s: {r:2000000, c:2000000}, e: {r:0, c:0} };
- /* 21.2.2.120 numCache CT_NumData */
- (data.match(/<c:numCache>[\s\S]*?<\/c:numCache>/gm)||[]).forEach(function(nc) {
- var cache = parse_Cache(nc);
- refguess.s.r = refguess.s.c = 0;
- refguess.e.c = C;
- col = encode_col(C);
- cache[0].forEach(function(n,i) {
- cs[col + encode_row(i)] = {t:'n', v:n, z:cache[1] };
- R = i;
- });
- if(refguess.e.r < R) refguess.e.r = R;
- ++C;
- });
- if(C > 0) cs["!ref"] = encode_range(refguess);
- return cs;
- }
- RELS.CS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet";
- var CS_XML_ROOT = writextag('chartsheet', null, {
- 'xmlns': XMLNS.main[0],
- 'xmlns:r': XMLNS.r
- });
- /* 18.3 Worksheets also covers Chartsheets */
- function parse_cs_xml(data, opts, idx, rels, wb) {
- if(!data) return data;
- /* 18.3.1.12 chartsheet CT_ChartSheet */
- if(!rels) rels = {'!id':{}};
- var s = ({'!type':"chart", '!drawel':null, '!rel':""});
- var m;
- /* 18.3.1.83 sheetPr CT_ChartsheetPr */
- var sheetPr = data.match(sheetprregex);
- if(sheetPr) parse_ws_xml_sheetpr(sheetPr[0], s, wb, idx);
- /* 18.3.1.36 drawing CT_Drawing */
- if((m = data.match(/drawing r:id="(.*?)"/))) s['!rel'] = m[1];
- if(rels['!id'][s['!rel']]) s['!drawel'] = rels['!id'][s['!rel']];
- return s;
- }
- function write_cs_xml(idx, opts, wb, rels) {
- var o = [XML_HEADER, CS_XML_ROOT];
- o[o.length] = writextag("drawing", null, {"r:id": "rId1"});
- add_rels(rels, -1, "../drawings/drawing" + (idx+1) + ".xml", RELS.DRAW);
- if(o.length>2) { o[o.length] = ('</chartsheet>'); o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- /* [MS-XLSB] 2.4.331 BrtCsProp */
- function parse_BrtCsProp(data, length) {
- data.l += 10;
- var name = parse_XLWideString(data, length - 10);
- return { name: name };
- }
- /* [MS-XLSB] 2.1.7.7 Chart Sheet */
- function parse_cs_bin(data, opts, idx, rels, wb) {
- if(!data) return data;
- if(!rels) rels = {'!id':{}};
- var s = {'!type':"chart", '!drawel':null, '!rel':""};
- var state = [];
- var pass = false;
- recordhopper(data, function cs_parse(val, R_n, RT) {
- switch(RT) {
- case 0x0226: /* 'BrtDrawing' */
- s['!rel'] = val; break;
- case 0x028B: /* 'BrtCsProp' */
- if(!wb.Sheets[idx]) wb.Sheets[idx] = {};
- if(val.name) wb.Sheets[idx].CodeName = val.name;
- break;
- case 0x0232: /* 'BrtBkHim' */
- case 0x028C: /* 'BrtCsPageSetup' */
- case 0x029D: /* 'BrtCsProtection' */
- case 0x02A7: /* 'BrtCsProtectionIso' */
- case 0x0227: /* 'BrtLegacyDrawing' */
- case 0x0228: /* 'BrtLegacyDrawingHF' */
- case 0x01DC: /* 'BrtMargins' */
- case 0x0C00: /* 'BrtUid' */
- break;
- case 0x0023: /* 'BrtFRTBegin' */
- pass = true; break;
- case 0x0024: /* 'BrtFRTEnd' */
- pass = false; break;
- case 0x0025: /* 'BrtACBegin' */
- state.push(R_n); break;
- case 0x0026: /* 'BrtACEnd' */
- state.pop(); break;
- default:
- if((R_n||"").indexOf("Begin") > 0) state.push(R_n);
- else if((R_n||"").indexOf("End") > 0) state.pop();
- else if(!pass || opts.WTF) throw new Error("Unexpected record " + RT + " " + R_n);
- }
- }, opts);
- if(rels['!id'][s['!rel']]) s['!drawel'] = rels['!id'][s['!rel']];
- return s;
- }
- function write_cs_bin() {
- var ba = buf_array();
- write_record(ba, "BrtBeginSheet");
- /* [BrtCsProp] */
- /* CSVIEWS */
- /* [[BrtCsProtectionIso] BrtCsProtection] */
- /* [USERCSVIEWS] */
- /* [BrtMargins] */
- /* [BrtCsPageSetup] */
- /* [HEADERFOOTER] */
- /* BrtDrawing */
- /* [BrtLegacyDrawing] */
- /* [BrtLegacyDrawingHF] */
- /* [BrtBkHim] */
- /* [WEBPUBITEMS] */
- /* FRTCHARTSHEET */
- write_record(ba, "BrtEndSheet");
- return ba.end();
- }
- /* 18.2.28 (CT_WorkbookProtection) Defaults */
- var WBPropsDef = [
- ['allowRefreshQuery', false, "bool"],
- ['autoCompressPictures', true, "bool"],
- ['backupFile', false, "bool"],
- ['checkCompatibility', false, "bool"],
- ['CodeName', ''],
- ['date1904', false, "bool"],
- ['defaultThemeVersion', 0, "int"],
- ['filterPrivacy', false, "bool"],
- ['hidePivotFieldList', false, "bool"],
- ['promptedSolutions', false, "bool"],
- ['publishItems', false, "bool"],
- ['refreshAllConnections', false, "bool"],
- ['saveExternalLinkValues', true, "bool"],
- ['showBorderUnselectedTables', true, "bool"],
- ['showInkAnnotation', true, "bool"],
- ['showObjects', 'all'],
- ['showPivotChartFilter', false, "bool"],
- ['updateLinks', 'userSet']
- ];
- /* 18.2.30 (CT_BookView) Defaults */
- var WBViewDef = [
- ['activeTab', 0, "int"],
- ['autoFilterDateGrouping', true, "bool"],
- ['firstSheet', 0, "int"],
- ['minimized', false, "bool"],
- ['showHorizontalScroll', true, "bool"],
- ['showSheetTabs', true, "bool"],
- ['showVerticalScroll', true, "bool"],
- ['tabRatio', 600, "int"],
- ['visibility', 'visible']
- //window{Height,Width}, {x,y}Window
- ];
- /* 18.2.19 (CT_Sheet) Defaults */
- var SheetDef = [
- //['state', 'visible']
- ];
- /* 18.2.2 (CT_CalcPr) Defaults */
- var CalcPrDef = [
- ['calcCompleted', 'true'],
- ['calcMode', 'auto'],
- ['calcOnSave', 'true'],
- ['concurrentCalc', 'true'],
- ['fullCalcOnLoad', 'false'],
- ['fullPrecision', 'true'],
- ['iterate', 'false'],
- ['iterateCount', '100'],
- ['iterateDelta', '0.001'],
- ['refMode', 'A1']
- ];
- /* 18.2.3 (CT_CustomWorkbookView) Defaults */
- /*var CustomWBViewDef = [
- ['autoUpdate', 'false'],
- ['changesSavedWin', 'false'],
- ['includeHiddenRowCol', 'true'],
- ['includePrintSettings', 'true'],
- ['maximized', 'false'],
- ['minimized', 'false'],
- ['onlySync', 'false'],
- ['personalView', 'false'],
- ['showComments', 'commIndicator'],
- ['showFormulaBar', 'true'],
- ['showHorizontalScroll', 'true'],
- ['showObjects', 'all'],
- ['showSheetTabs', 'true'],
- ['showStatusbar', 'true'],
- ['showVerticalScroll', 'true'],
- ['tabRatio', '600'],
- ['xWindow', '0'],
- ['yWindow', '0']
- ];*/
- function push_defaults_array(target, defaults) {
- for(var j = 0; j != target.length; ++j) { var w = target[j];
- for(var i=0; i != defaults.length; ++i) { var z = defaults[i];
- if(w[z[0]] == null) w[z[0]] = z[1];
- else switch(z[2]) {
- case "bool": if(typeof w[z[0]] == "string") w[z[0]] = parsexmlbool(w[z[0]]); break;
- case "int": if(typeof w[z[0]] == "string") w[z[0]] = parseInt(w[z[0]], 10); break;
- }
- }
- }
- }
- function push_defaults(target, defaults) {
- for(var i = 0; i != defaults.length; ++i) { var z = defaults[i];
- if(target[z[0]] == null) target[z[0]] = z[1];
- else switch(z[2]) {
- case "bool": if(typeof target[z[0]] == "string") target[z[0]] = parsexmlbool(target[z[0]]); break;
- case "int": if(typeof target[z[0]] == "string") target[z[0]] = parseInt(target[z[0]], 10); break;
- }
- }
- }
- function parse_wb_defaults(wb) {
- push_defaults(wb.WBProps, WBPropsDef);
- push_defaults(wb.CalcPr, CalcPrDef);
- push_defaults_array(wb.WBView, WBViewDef);
- push_defaults_array(wb.Sheets, SheetDef);
- _ssfopts.date1904 = parsexmlbool(wb.WBProps.date1904);
- }
- function safe1904(wb) {
- /* TODO: store date1904 somewhere else */
- if(!wb.Workbook) return "false";
- if(!wb.Workbook.WBProps) return "false";
- return parsexmlbool(wb.Workbook.WBProps.date1904) ? "true" : "false";
- }
- var badchars = "][*?\/\\".split("");
- function check_ws_name(n, safe) {
- if(n.length > 31) { if(safe) return false; throw new Error("Sheet names cannot exceed 31 chars"); }
- var _good = true;
- badchars.forEach(function(c) {
- if(n.indexOf(c) == -1) return;
- if(!safe) throw new Error("Sheet name cannot contain : \\ / ? * [ ]");
- _good = false;
- });
- return _good;
- }
- function check_wb_names(N, S, codes) {
- N.forEach(function(n,i) {
- check_ws_name(n);
- for(var j = 0; j < i; ++j) if(n == N[j]) throw new Error("Duplicate Sheet Name: " + n);
- if(codes) {
- var cn = (S && S[i] && S[i].CodeName) || n;
- if(cn.charCodeAt(0) == 95 && cn.length > 22) throw new Error("Bad Code Name: Worksheet" + cn);
- }
- });
- }
- function check_wb(wb) {
- if(!wb || !wb.SheetNames || !wb.Sheets) throw new Error("Invalid Workbook");
- if(!wb.SheetNames.length) throw new Error("Workbook is empty");
- var Sheets = (wb.Workbook && wb.Workbook.Sheets) || [];
- check_wb_names(wb.SheetNames, Sheets, !!wb.vbaraw);
- for(var i = 0; i < wb.SheetNames.length; ++i) check_ws(wb.Sheets[wb.SheetNames[i]], wb.SheetNames[i], i);
- /* TODO: validate workbook */
- }
- /* 18.2 Workbook */
- var wbnsregex = /<\w+:workbook/;
- function parse_wb_xml(data, opts) {
- if(!data) throw new Error("Could not find file");
- var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, Names:[], xmlns: "" };
- var pass = false, xmlns = "xmlns";
- var dname = {}, dnstart = 0;
- data.replace(tagregex, function xml_wb(x, idx) {
- var y = parsexmltag(x);
- switch(strip_ns(y[0])) {
- case '<?xml': break;
- /* 18.2.27 workbook CT_Workbook 1 */
- case '<workbook':
- if(x.match(wbnsregex)) xmlns = "xmlns" + x.match(/<(\w+):/)[1];
- wb.xmlns = y[xmlns];
- break;
- case '</workbook>': break;
- /* 18.2.13 fileVersion CT_FileVersion ? */
- case '<fileVersion': delete y[0]; wb.AppVersion = y; break;
- case '<fileVersion/>': case '</fileVersion>': break;
- /* 18.2.12 fileSharing CT_FileSharing ? */
- case '<fileSharing':
- break;
- case '<fileSharing/>': break;
- /* 18.2.28 workbookPr CT_WorkbookPr ? */
- case '<workbookPr':
- case '<workbookPr/>':
- WBPropsDef.forEach(function(w) {
- if(y[w[0]] == null) return;
- switch(w[2]) {
- case "bool": wb.WBProps[w[0]] = parsexmlbool(y[w[0]]); break;
- case "int": wb.WBProps[w[0]] = parseInt(y[w[0]], 10); break;
- default: wb.WBProps[w[0]] = y[w[0]];
- }
- });
- if(y.codeName) wb.WBProps.CodeName = utf8read(y.codeName);
- break;
- case '</workbookPr>': break;
- /* 18.2.29 workbookProtection CT_WorkbookProtection ? */
- case '<workbookProtection':
- break;
- case '<workbookProtection/>': break;
- /* 18.2.1 bookViews CT_BookViews ? */
- case '<bookViews': case '<bookViews>': case '</bookViews>': break;
- /* 18.2.30 workbookView CT_BookView + */
- case '<workbookView': case '<workbookView/>': delete y[0]; wb.WBView.push(y); break;
- case '</workbookView>': break;
- /* 18.2.20 sheets CT_Sheets 1 */
- case '<sheets': case '<sheets>': case '</sheets>': break; // aggregate sheet
- /* 18.2.19 sheet CT_Sheet + */
- case '<sheet':
- switch(y.state) {
- case "hidden": y.Hidden = 1; break;
- case "veryHidden": y.Hidden = 2; break;
- default: y.Hidden = 0;
- }
- delete y.state;
- y.name = unescapexml(utf8read(y.name));
- delete y[0]; wb.Sheets.push(y); break;
- case '</sheet>': break;
- /* 18.2.15 functionGroups CT_FunctionGroups ? */
- case '<functionGroups': case '<functionGroups/>': break;
- /* 18.2.14 functionGroup CT_FunctionGroup + */
- case '<functionGroup': break;
- /* 18.2.9 externalReferences CT_ExternalReferences ? */
- case '<externalReferences': case '</externalReferences>': case '<externalReferences>': break;
- /* 18.2.8 externalReference CT_ExternalReference + */
- case '<externalReference': break;
- /* 18.2.6 definedNames CT_DefinedNames ? */
- case '<definedNames/>': break;
- case '<definedNames>': case '<definedNames': pass=true; break;
- case '</definedNames>': pass=false; break;
- /* 18.2.5 definedName CT_DefinedName + */
- case '<definedName': {
- dname = {};
- dname.Name = utf8read(y.name);
- if(y.comment) dname.Comment = y.comment;
- if(y.localSheetId) dname.Sheet = +y.localSheetId;
- if(parsexmlbool(y.hidden||"0")) dname.Hidden = true;
- dnstart = idx + x.length;
- } break;
- case '</definedName>': {
- dname.Ref = unescapexml(utf8read(data.slice(dnstart, idx)));
- wb.Names.push(dname);
- } break;
- case '<definedName/>': break;
- /* 18.2.2 calcPr CT_CalcPr ? */
- case '<calcPr': delete y[0]; wb.CalcPr = y; break;
- case '<calcPr/>': delete y[0]; wb.CalcPr = y; break;
- case '</calcPr>': break;
- /* 18.2.16 oleSize CT_OleSize ? (ref required) */
- case '<oleSize': break;
- /* 18.2.4 customWorkbookViews CT_CustomWorkbookViews ? */
- case '<customWorkbookViews>': case '</customWorkbookViews>': case '<customWorkbookViews': break;
- /* 18.2.3 customWorkbookView CT_CustomWorkbookView + */
- case '<customWorkbookView': case '</customWorkbookView>': break;
- /* 18.2.18 pivotCaches CT_PivotCaches ? */
- case '<pivotCaches>': case '</pivotCaches>': case '<pivotCaches': break;
- /* 18.2.17 pivotCache CT_PivotCache ? */
- case '<pivotCache': break;
- /* 18.2.21 smartTagPr CT_SmartTagPr ? */
- case '<smartTagPr': case '<smartTagPr/>': break;
- /* 18.2.23 smartTagTypes CT_SmartTagTypes ? */
- case '<smartTagTypes': case '<smartTagTypes>': case '</smartTagTypes>': break;
- /* 18.2.22 smartTagType CT_SmartTagType ? */
- case '<smartTagType': break;
- /* 18.2.24 webPublishing CT_WebPublishing ? */
- case '<webPublishing': case '<webPublishing/>': break;
- /* 18.2.11 fileRecoveryPr CT_FileRecoveryPr ? */
- case '<fileRecoveryPr': case '<fileRecoveryPr/>': break;
- /* 18.2.26 webPublishObjects CT_WebPublishObjects ? */
- case '<webPublishObjects>': case '<webPublishObjects': case '</webPublishObjects>': break;
- /* 18.2.25 webPublishObject CT_WebPublishObject ? */
- case '<webPublishObject': break;
- /* 18.2.10 extLst CT_ExtensionList ? */
- case '<extLst': case '<extLst>': case '</extLst>': case '<extLst/>': break;
- /* 18.2.7 ext CT_Extension + */
- case '<ext': pass=true; break; //TODO: check with versions of excel
- case '</ext>': pass=false; break;
- /* Others */
- case '<ArchID': break;
- case '<AlternateContent':
- case '<AlternateContent>': pass=true; break;
- case '</AlternateContent>': pass=false; break;
- /* TODO */
- case '<revisionPtr': break;
- default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in workbook');
- }
- return x;
- });
- if(XMLNS.main.indexOf(wb.xmlns) === -1) throw new Error("Unknown Namespace: " + wb.xmlns);
- parse_wb_defaults(wb);
- return wb;
- }
- var WB_XML_ROOT = writextag('workbook', null, {
- 'xmlns': XMLNS.main[0],
- //'xmlns:mx': XMLNS.mx,
- //'xmlns:s': XMLNS.main[0],
- 'xmlns:r': XMLNS.r
- });
- function write_wb_xml(wb) {
- var o = [XML_HEADER];
- o[o.length] = WB_XML_ROOT;
- var write_names = (wb.Workbook && (wb.Workbook.Names||[]).length > 0);
- /* fileVersion */
- /* fileSharing */
- var workbookPr = ({codeName:"ThisWorkbook"});
- if(wb.Workbook && wb.Workbook.WBProps) {
- WBPropsDef.forEach(function(x) {
- if((wb.Workbook.WBProps[x[0]]) == null) return;
- if((wb.Workbook.WBProps[x[0]]) == x[1]) return;
- workbookPr[x[0]] = (wb.Workbook.WBProps[x[0]]);
- });
- if(wb.Workbook.WBProps.CodeName) { workbookPr.codeName = wb.Workbook.WBProps.CodeName; delete workbookPr.CodeName; }
- }
- o[o.length] = (writextag('workbookPr', null, workbookPr));
- /* workbookProtection */
- var sheets = wb.Workbook && wb.Workbook.Sheets || [];
- var i = 0;
- /* bookViews only written if first worksheet is hidden */
- if(sheets && sheets[0] && !!sheets[0].Hidden) {
- o[o.length] = "<bookViews>";
- for(i = 0; i != wb.SheetNames.length; ++i) {
- if(!sheets[i]) break;
- if(!sheets[i].Hidden) break;
- }
- if(i == wb.SheetNames.length) i = 0;
- o[o.length] = '<workbookView firstSheet="' + i + '" activeTab="' + i + '"/>';
- o[o.length] = "</bookViews>";
- }
- o[o.length] = "<sheets>";
- for(i = 0; i != wb.SheetNames.length; ++i) {
- var sht = ({name:escapexml(wb.SheetNames[i].slice(0,31))});
- sht.sheetId = ""+(i+1);
- sht["r:id"] = "rId"+(i+1);
- if(sheets[i]) switch(sheets[i].Hidden) {
- case 1: sht.state = "hidden"; break;
- case 2: sht.state = "veryHidden"; break;
- }
- o[o.length] = (writextag('sheet',null,sht));
- }
- o[o.length] = "</sheets>";
- /* functionGroups */
- /* externalReferences */
- if(write_names) {
- o[o.length] = "<definedNames>";
- if(wb.Workbook && wb.Workbook.Names) wb.Workbook.Names.forEach(function(n) {
- var d = {name:n.Name};
- if(n.Comment) d.comment = n.Comment;
- if(n.Sheet != null) d.localSheetId = ""+n.Sheet;
- if(n.Hidden) d.hidden = "1";
- if(!n.Ref) return;
- o[o.length] = writextag('definedName', escapexml(n.Ref), d);
- });
- o[o.length] = "</definedNames>";
- }
- /* calcPr */
- /* oleSize */
- /* customWorkbookViews */
- /* pivotCaches */
- /* smartTagPr */
- /* smartTagTypes */
- /* webPublishing */
- /* fileRecoveryPr */
- /* webPublishObjects */
- /* extLst */
- if(o.length>2){ o[o.length] = '</workbook>'; o[1]=o[1].replace("/>",">"); }
- return o.join("");
- }
- function parse_wb(data, name, opts) {
- if(name.slice(-4)===".bin") return parse_wb_bin((data), opts);
- return parse_wb_xml((data), opts);
- }
- function parse_ws(data, name, idx, opts, rels, wb, themes, styles) {
- if(name.slice(-4)===".bin") return parse_ws_bin((data), opts, idx, rels, wb, themes, styles);
- return parse_ws_xml((data), opts, idx, rels, wb, themes, styles);
- }
- function parse_cs(data, name, idx, opts, rels, wb, themes, styles) {
- if(name.slice(-4)===".bin") return parse_cs_bin((data), opts, idx, rels, wb, themes, styles);
- return parse_cs_xml((data), opts, idx, rels, wb, themes, styles);
- }
- function parse_ms(data, name, idx, opts, rels, wb, themes, styles) {
- if(name.slice(-4)===".bin") return parse_ms_bin((data), opts, idx, rels, wb, themes, styles);
- return parse_ms_xml((data), opts, idx, rels, wb, themes, styles);
- }
- function parse_ds(data, name, idx, opts, rels, wb, themes, styles) {
- if(name.slice(-4)===".bin") return parse_ds_bin((data), opts, idx, rels, wb, themes, styles);
- return parse_ds_xml((data), opts, idx, rels, wb, themes, styles);
- }
- function parse_sty(data, name, themes, opts) {
- if(name.slice(-4)===".bin") return parse_sty_bin((data), themes, opts);
- return parse_sty_xml((data), themes, opts);
- }
- function parse_theme(data, name, opts) {
- return parse_theme_xml(data, opts);
- }
- function parse_sst(data, name, opts) {
- if(name.slice(-4)===".bin") return parse_sst_bin((data), opts);
- return parse_sst_xml((data), opts);
- }
- function parse_cmnt(data, name, opts) {
- if(name.slice(-4)===".bin") return parse_comments_bin((data), opts);
- return parse_comments_xml((data), opts);
- }
- function parse_cc(data, name, opts) {
- if(name.slice(-4)===".bin") return parse_cc_bin((data), name, opts);
- return parse_cc_xml((data), name, opts);
- }
- function parse_xlink(data, rel, name, opts) {
- if(name.slice(-4)===".bin") return parse_xlink_bin((data), rel, name, opts);
- return parse_xlink_xml((data), rel, name, opts);
- }
- function write_wb(wb, name, opts) {
- return (name.slice(-4)===".bin" ? write_wb_bin : write_wb_xml)(wb, opts);
- }
- function write_ws(data, name, opts, wb, rels) {
- return (name.slice(-4)===".bin" ? write_ws_bin : write_ws_xml)(data, opts, wb, rels);
- }
- // eslint-disable-next-line no-unused-vars
- function write_cs(data, name, opts, wb, rels) {
- return (name.slice(-4)===".bin" ? write_cs_bin : write_cs_xml)(data, opts, wb, rels);
- }
- function write_sty(data, name, opts) {
- return (name.slice(-4)===".bin" ? write_sty_bin : write_sty_xml)(data, opts);
- }
- function write_sst(data, name, opts) {
- return (name.slice(-4)===".bin" ? write_sst_bin : write_sst_xml)(data, opts);
- }
- function write_cmnt(data, name, opts) {
- return (name.slice(-4)===".bin" ? write_comments_bin : write_comments_xml)(data, opts);
- }
- /*
- function write_cc(data, name:string, opts) {
- return (name.slice(-4)===".bin" ? write_cc_bin : write_cc_xml)(data, opts);
- }
- */
- /* note: browser DOM element cannot see mso- style attrs, must parse */
- var HTML_ = (function() {
- function html_to_sheet(str, _opts) {
- var opts = _opts || {};
- if(DENSE != null && opts.dense == null) opts.dense = DENSE;
- var ws = opts.dense ? ([]) : ({});
- str = str.replace(/<!--.*?-->/g, "");
- var mtch = str.match(/<table/i);
- if(!mtch) throw new Error("Invalid HTML: could not find <table>");
- var mtch2 = str.match(/<\/table/i);
- var i = mtch.index, j = mtch2 && mtch2.index || str.length;
- var rows = split_regex(str.slice(i, j), /(:?<tr[^>]*>)/i, "<tr>");
- var R = -1, C = 0, RS = 0, CS = 0;
- var range = {s:{r:10000000, c:10000000},e:{r:0,c:0}};
- var merges = [];
- for(i = 0; i < rows.length; ++i) {
- var row = rows[i].trim();
- var hd = row.slice(0,3).toLowerCase();
- if(hd == "<tr") { ++R; if(opts.sheetRows && opts.sheetRows <= R) { --R; break; } C = 0; continue; }
- if(hd != "<td" && hd != "<th") continue;
- var cells = row.split(/<\/t[dh]>/i);
- for(j = 0; j < cells.length; ++j) {
- var cell = cells[j].trim();
- if(!cell.match(/<t[dh]/i)) continue;
- var m = cell, cc = 0;
- /* TODO: parse styles etc */
- while(m.charAt(0) == "<" && (cc = m.indexOf(">")) > -1) m = m.slice(cc+1);
- for(var midx = 0; midx < merges.length; ++midx) {
- var _merge = merges[midx];
- if(_merge.s.c == C && _merge.s.r < R && R <= _merge.e.r) { C = _merge.e.c + 1; midx = -1; }
- }
- var tag = parsexmltag(cell.slice(0, cell.indexOf(">")));
- CS = tag.colspan ? +tag.colspan : 1;
- if((RS = +tag.rowspan)>1 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
- var _t = tag.t || "";
- /* TODO: generate stub cells */
- if(!m.length) { C += CS; continue; }
- m = htmldecode(m);
- if(range.s.r > R) range.s.r = R; if(range.e.r < R) range.e.r = R;
- if(range.s.c > C) range.s.c = C; if(range.e.c < C) range.e.c = C;
- if(!m.length) continue;
- var o = {t:'s', v:m};
- if(opts.raw || !m.trim().length || _t == 's'){}
- else if(m === 'TRUE') o = {t:'b', v:true};
- else if(m === 'FALSE') o = {t:'b', v:false};
- else if(!isNaN(fuzzynum(m))) o = {t:'n', v:fuzzynum(m)};
- else if(!isNaN(fuzzydate(m).getDate())) {
- o = ({t:'d', v:parseDate(m)});
- if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
- o.z = opts.dateNF || SSF._table[14];
- }
- if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = o; }
- else ws[encode_cell({r:R, c:C})] = o;
- C += CS;
- }
- }
- ws['!ref'] = encode_range(range);
- if(merges.length) ws["!merges"] = merges;
- return ws;
- }
- function html_to_book(str, opts) {
- return sheet_to_workbook(html_to_sheet(str, opts), opts);
- }
- function make_html_row(ws, r, R, o) {
- var M = (ws['!merges'] ||[]);
- var oo = [];
- for(var C = r.s.c; C <= r.e.c; ++C) {
- var RS = 0, CS = 0;
- for(var j = 0; j < M.length; ++j) {
- if(M[j].s.r > R || M[j].s.c > C) continue;
- if(M[j].e.r < R || M[j].e.c < C) continue;
- if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
- RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
- }
- if(RS < 0) continue;
- var coord = encode_cell({r:R,c:C});
- var cell = o.dense ? (ws[R]||[])[C] : ws[coord];
- /* TODO: html entities */
- var w = (cell && cell.v != null) && (cell.h || escapehtml(cell.w || (format_cell(cell), cell.w) || "")) || "";
- var sp = ({});
- if(RS > 1) sp.rowspan = RS;
- if(CS > 1) sp.colspan = CS;
- sp.t = cell && cell.t || 'z';
- if(o.editable) w = '<span contenteditable="true">' + w + '</span>';
- sp.id = (o.id || "sjs") + "-" + coord;
- if(sp.t != "z") { sp.v = cell.v; if(cell.z != null) sp.z = cell.z; }
- oo.push(writextag('td', w, sp));
- }
- var preamble = "<tr>";
- return preamble + oo.join("") + "</tr>";
- }
- function make_html_preamble(ws, R, o) {
- var out = [];
- return out.join("") + '<table' + (o && o.id ? ' id="' + o.id + '"' : "") + '>';
- }
- var _BEGIN = '<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body>';
- var _END = '</body></html>';
- function sheet_to_html(ws, opts/*, wb:?Workbook*/) {
- var o = opts || {};
- var header = o.header != null ? o.header : _BEGIN;
- var footer = o.footer != null ? o.footer : _END;
- var out = [header];
- var r = decode_range(ws['!ref']);
- o.dense = Array.isArray(ws);
- out.push(make_html_preamble(ws, r, o));
- for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o));
- out.push("</table>" + footer);
- return out.join("");
- }
- return {
- to_workbook: html_to_book,
- to_sheet: html_to_sheet,
- _row: make_html_row,
- BEGIN: _BEGIN,
- END: _END,
- _preamble: make_html_preamble,
- from_sheet: sheet_to_html
- };
- })();
- function sheet_add_dom(ws, table, _opts) {
- var opts = _opts || {};
- if(DENSE != null) opts.dense = DENSE;
- var or_R = 0, or_C = 0;
- if(opts.origin != null) {
- if(typeof opts.origin == 'number') or_R = opts.origin;
- else {
- var _origin = typeof opts.origin == "string" ? decode_cell(opts.origin) : opts.origin;
- or_R = _origin.r; or_C = _origin.c;
- }
- }
- var rows = table.getElementsByTagName('tr');
- var sheetRows = Math.min(opts.sheetRows||10000000, rows.length);
- var range = {s:{r:0,c:0},e:{r:or_R,c:or_C}};
- if(ws["!ref"]) {
- var _range = decode_range(ws["!ref"]);
- range.s.r = Math.min(range.s.r, _range.s.r);
- range.s.c = Math.min(range.s.c, _range.s.c);
- range.e.r = Math.max(range.e.r, _range.e.r);
- range.e.c = Math.max(range.e.c, _range.e.c);
- if(or_R == -1) range.e.r = or_R = _range.e.r + 1;
- }
- var merges = [], midx = 0;
- var rowinfo = ws["!rows"] || (ws["!rows"] = []);
- var _R = 0, R = 0, _C = 0, C = 0, RS = 0, CS = 0;
- if(!ws["!cols"]) ws['!cols'] = [];
- for(; _R < rows.length && R < sheetRows; ++_R) {
- var row = rows[_R];
- if (is_dom_element_hidden(row)) {
- if (opts.display) continue;
- rowinfo[R] = {hidden: true};
- }
- var elts = (row.children);
- for(_C = C = 0; _C < elts.length; ++_C) {
- var elt = elts[_C];
- if (opts.display && is_dom_element_hidden(elt)) continue;
- var v = elt.hasAttribute('v') ? elt.getAttribute('v') : htmldecode(elt.innerHTML);
- var z = elt.getAttribute('z');
- for(midx = 0; midx < merges.length; ++midx) {
- var m = merges[midx];
- 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; }
- }
- /* TODO: figure out how to extract nonstandard mso- style */
- CS = +elt.getAttribute("colspan") || 1;
- 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}});
- var o = {t:'s', v:v};
- var _t = elt.getAttribute("t") || "";
- if(v != null) {
- if(v.length == 0) o.t = _t || 'z';
- else if(opts.raw || v.trim().length == 0 || _t == "s"){}
- else if(v === 'TRUE') o = {t:'b', v:true};
- else if(v === 'FALSE') o = {t:'b', v:false};
- else if(!isNaN(fuzzynum(v))) o = {t:'n', v:fuzzynum(v)};
- else if(!isNaN(fuzzydate(v).getDate())) {
- o = ({t:'d', v:parseDate(v)});
- if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)});
- o.z = opts.dateNF || SSF._table[14];
- }
- }
- if(o.z === undefined && z != null) o.z = z;
- if(opts.dense) { if(!ws[R + or_R]) ws[R + or_R] = []; ws[R + or_R][C + or_C] = o; }
- else ws[encode_cell({c:C + or_C, r:R + or_R})] = o;
- if(range.e.c < C + or_C) range.e.c = C + or_C;
- C += CS;
- }
- ++R;
- }
- if(merges.length) ws['!merges'] = (ws["!merges"] || []).concat(merges);
- range.e.r = Math.max(range.e.r, R - 1 + or_R);
- ws['!ref'] = encode_range(range);
- 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
- return ws;
- }
- function parse_dom_table(table, _opts) {
- var opts = _opts || {};
- var ws = opts.dense ? ([]) : ({});
- return sheet_add_dom(ws, table, _opts);
- }
- function table_to_book(table, opts) {
- return sheet_to_workbook(parse_dom_table(table, opts), opts);
- }
- function is_dom_element_hidden(element) {
- var display = '';
- var get_computed_style = get_get_computed_style_function(element);
- if(get_computed_style) display = get_computed_style(element).getPropertyValue('display');
- 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)
- return display === 'none';
- }
- /* global getComputedStyle */
- function get_get_computed_style_function(element) {
- // The proper getComputedStyle implementation is the one defined in the element window
- if(element.ownerDocument.defaultView && typeof element.ownerDocument.defaultView.getComputedStyle === 'function') return element.ownerDocument.defaultView.getComputedStyle;
- // If it is not available, try to get one from the global namespace
- if(typeof getComputedStyle === 'function') return getComputedStyle;
- return null;
- }
- function write_sheet_index(wb, sheet) {
- if(!sheet) return 0;
- var idx = wb.SheetNames.indexOf(sheet);
- if(idx == -1) throw new Error("Sheet not found: " + sheet);
- return idx;
- }
- function write_obj_str(factory) {
- return function write_str(wb, o) {
- var idx = write_sheet_index(wb, o.sheet);
- return factory.from_sheet(wb.Sheets[wb.SheetNames[idx]], o, wb);
- };
- }
- var write_htm_str = write_obj_str(HTML_);
- var write_csv_str = write_obj_str({from_sheet:sheet_to_csv});
- var write_slk_str = write_obj_str(typeof SYLK !== "undefined" ? SYLK : {});
- var write_dif_str = write_obj_str(typeof DIF !== "undefined" ? DIF : {});
- var write_prn_str = write_obj_str(typeof PRN !== "undefined" ? PRN : {});
- var write_rtf_str = write_obj_str(typeof RTF !== "undefined" ? RTF : {});
- var write_txt_str = write_obj_str({from_sheet:sheet_to_txt});
- var write_dbf_buf = write_obj_str(typeof DBF !== "undefined" ? DBF : {});
- var write_eth_str = write_obj_str(typeof ETH !== "undefined" ? ETH : {});
- function fix_opts_func(defaults) {
- return function fix_opts(opts) {
- for(var i = 0; i != defaults.length; ++i) {
- var d = defaults[i];
- if(opts[d[0]] === undefined) opts[d[0]] = d[1];
- if(d[2] === 'n') opts[d[0]] = Number(opts[d[0]]);
- }
- };
- }
- var fix_read_opts = function(opts) {
- fix_opts_func([
- ['cellNF', false], /* emit cell number format string as .z */
- ['cellHTML', true], /* emit html string as .h */
- ['cellFormula', true], /* emit formulae as .f */
- ['cellStyles', false], /* emits style/theme as .s */
- ['cellText', true], /* emit formatted text as .w */
- ['cellDates', false], /* emit date cells with type `d` */
- ['sheetStubs', false], /* emit empty cells */
- ['sheetRows', 0, 'n'], /* read n rows (0 = read all rows) */
- ['bookDeps', false], /* parse calculation chains */
- ['bookSheets', false], /* only try to get sheet names (no Sheets) */
- ['bookProps', false], /* only try to get properties (no Sheets) */
- ['bookFiles', false], /* include raw file structure (keys, files, cfb) */
- ['bookVBA', false], /* include vba raw data (vbaraw) */
- ['password',''], /* password */
- ['WTF', false] /* WTF mode (throws errors) */
- ])(opts);
- };
- var fix_write_opts = fix_opts_func([
- ['cellDates', false], /* write date cells with type `d` */
- ['bookSST', false], /* Generate Shared String Table */
- ['bookType', 'xlsx'], /* Type of workbook (xlsx/m/b) */
- ['compression', false], /* Use file compression */
- ['WTF', false] /* WTF mode (throws errors) */
- ]);
- function get_sheet_type(n) {
- if(RELS.WS.indexOf(n) > -1) return "sheet";
- if(RELS.CS && n == RELS.CS) return "chart";
- if(RELS.DS && n == RELS.DS) return "dialog";
- if(RELS.MS && n == RELS.MS) return "macro";
- return (n && n.length) ? n : "sheet";
- }
- function safe_parse_wbrels(wbrels, sheets) {
- if(!wbrels) return 0;
- try {
- 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)]; });
- } catch(e) { return null; }
- return !wbrels || wbrels.length === 0 ? null : wbrels;
- }
- function safe_parse_sheet(zip, path, relsPath, sheet, idx, sheetRels, sheets, stype, opts, wb, themes, styles) {
- try {
- sheetRels[sheet]=parse_rels(getzipstr(zip, relsPath, true), path);
- var data = getzipdata(zip, path);
- var _ws;
- switch(stype) {
- case 'sheet': _ws = parse_ws(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
- case 'chart': _ws = parse_cs(data, path, idx, opts, sheetRels[sheet], wb, themes, styles);
- if(!_ws || !_ws['!drawel']) break;
- var dfile = resolve_path(_ws['!drawel'].Target, path);
- var drelsp = get_rels_path(dfile);
- var draw = parse_drawing(getzipstr(zip, dfile, true), parse_rels(getzipstr(zip, drelsp, true), dfile));
- var chartp = resolve_path(draw, dfile);
- var crelsp = get_rels_path(chartp);
- _ws = parse_chart(getzipstr(zip, chartp, true), chartp, opts, parse_rels(getzipstr(zip, crelsp, true), chartp), wb, _ws);
- break;
- case 'macro': _ws = parse_ms(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
- case 'dialog': _ws = parse_ds(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break;
- default: throw new Error("Unrecognized sheet type " + stype);
- }
- sheets[sheet] = _ws;
- /* scan rels for comments */
- var comments = [];
- if(sheetRels && sheetRels[sheet]) keys(sheetRels[sheet]).forEach(function(n) {
- if(sheetRels[sheet][n].Type == RELS.CMNT) {
- var dfile = resolve_path(sheetRels[sheet][n].Target, path);
- comments = parse_cmnt(getzipdata(zip, dfile, true), dfile, opts);
- if(!comments || !comments.length) return;
- sheet_insert_comments(_ws, comments);
- }
- });
- } catch(e) { if(opts.WTF) throw e; }
- }
- function strip_front_slash(x) { return x.charAt(0) == '/' ? x.slice(1) : x; }
- function parse_zip(zip, opts) {
- make_ssf(SSF);
- opts = opts || {};
- fix_read_opts(opts);
- /* OpenDocument Part 3 Section 2.2.1 OpenDocument Package */
- if(safegetzipfile(zip, 'META-INF/manifest.xml')) return parse_ods(zip, opts);
- /* UOC */
- if(safegetzipfile(zip, 'objectdata.xml')) return parse_ods(zip, opts);
- /* Numbers */
- if(safegetzipfile(zip, 'Index/Document.iwa')) throw new Error('Unsupported NUMBERS file');
- var entries = zipentries(zip);
- var dir = parse_ct((getzipstr(zip, '[Content_Types].xml')));
- var xlsb = false;
- var sheets, binname;
- if(dir.workbooks.length === 0) {
- binname = "xl/workbook.xml";
- if(getzipdata(zip,binname, true)) dir.workbooks.push(binname);
- }
- if(dir.workbooks.length === 0) {
- binname = "xl/workbook.bin";
- if(!getzipdata(zip,binname,true)) throw new Error("Could not find workbook");
- dir.workbooks.push(binname);
- xlsb = true;
- }
- if(dir.workbooks[0].slice(-3) == "bin") xlsb = true;
- var themes = ({});
- var styles = ({});
- if(!opts.bookSheets && !opts.bookProps) {
- strs = [];
- if(dir.sst) try { strs=parse_sst(getzipdata(zip, strip_front_slash(dir.sst)), dir.sst, opts); } catch(e) { if(opts.WTF) throw e; }
- if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipstr(zip, dir.themes[0].replace(/^\//,''), true)||"",dir.themes[0], opts);
- if(dir.style) styles = parse_sty(getzipdata(zip, strip_front_slash(dir.style)), dir.style, themes, opts);
- }
- /*var externbooks = */dir.links.map(function(link) {
- try {
- var rels = parse_rels(getzipstr(zip, get_rels_path(strip_front_slash(link))), link);
- return parse_xlink(getzipdata(zip, strip_front_slash(link)), rels, link, opts);
- } catch(e) {}
- });
- var wb = parse_wb(getzipdata(zip, strip_front_slash(dir.workbooks[0])), dir.workbooks[0], opts);
- var props = {}, propdata = "";
- if(dir.coreprops.length) {
- propdata = getzipdata(zip, strip_front_slash(dir.coreprops[0]), true);
- if(propdata) props = parse_core_props(propdata);
- if(dir.extprops.length !== 0) {
- propdata = getzipdata(zip, strip_front_slash(dir.extprops[0]), true);
- if(propdata) parse_ext_props(propdata, props, opts);
- }
- }
- var custprops = {};
- if(!opts.bookSheets || opts.bookProps) {
- if (dir.custprops.length !== 0) {
- propdata = getzipstr(zip, strip_front_slash(dir.custprops[0]), true);
- if(propdata) custprops = parse_cust_props(propdata, opts);
- }
- }
- var out = ({});
- if(opts.bookSheets || opts.bookProps) {
- if(wb.Sheets) sheets = wb.Sheets.map(function pluck(x){ return x.name; });
- else if(props.Worksheets && props.SheetNames.length > 0) sheets=props.SheetNames;
- if(opts.bookProps) { out.Props = props; out.Custprops = custprops; }
- if(opts.bookSheets && typeof sheets !== 'undefined') out.SheetNames = sheets;
- if(opts.bookSheets ? out.SheetNames : opts.bookProps) return out;
- }
- sheets = {};
- var deps = {};
- if(opts.bookDeps && dir.calcchain) deps=parse_cc(getzipdata(zip, strip_front_slash(dir.calcchain)),dir.calcchain,opts);
- var i=0;
- var sheetRels = ({});
- var path, relsPath;
- {
- var wbsheets = wb.Sheets;
- props.Worksheets = wbsheets.length;
- props.SheetNames = [];
- for(var j = 0; j != wbsheets.length; ++j) {
- props.SheetNames[j] = wbsheets[j].name;
- }
- }
- var wbext = xlsb ? "bin" : "xml";
- var wbrelsi = dir.workbooks[0].lastIndexOf("/");
- var wbrelsfile = (dir.workbooks[0].slice(0, wbrelsi+1) + "_rels/" + dir.workbooks[0].slice(wbrelsi+1) + ".rels").replace(/^\//,"");
- if(!safegetzipfile(zip, wbrelsfile)) wbrelsfile = 'xl/_rels/workbook.' + wbext + '.rels';
- var wbrels = parse_rels(getzipstr(zip, wbrelsfile, true), wbrelsfile);
- if(wbrels) wbrels = safe_parse_wbrels(wbrels, wb.Sheets);
- /* Numbers iOS hack */
- var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0;
- wsloop: for(i = 0; i != props.Worksheets; ++i) {
- var stype = "sheet";
- if(wbrels && wbrels[i]) {
- path = 'xl/' + (wbrels[i][1]).replace(/[\/]?xl\//, "");
- if(!safegetzipfile(zip, path)) path = wbrels[i][1];
- if(!safegetzipfile(zip, path)) path = wbrelsfile.replace(/_rels\/.*$/,"") + wbrels[i][1];
- stype = wbrels[i][2];
- } else {
- path = 'xl/worksheets/sheet'+(i+1-nmode)+"." + wbext;
- path = path.replace(/sheet0\./,"sheet.");
- }
- relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
- if(opts && opts.sheets != null) switch(typeof opts.sheets) {
- case "number": if(i != opts.sheets) continue wsloop; break;
- case "string": if(props.SheetNames[i].toLowerCase() != opts.sheets.toLowerCase()) continue wsloop; break;
- default: if(Array.isArray && Array.isArray(opts.sheets)) {
- var snjseen = false;
- for(var snj = 0; snj != opts.sheets.length; ++snj) {
- if(typeof opts.sheets[snj] == "number" && opts.sheets[snj] == i) snjseen=1;
- if(typeof opts.sheets[snj] == "string" && opts.sheets[snj].toLowerCase() == props.SheetNames[i].toLowerCase()) snjseen = 1;
- }
- if(!snjseen) continue wsloop;
- }
- }
- safe_parse_sheet(zip, path, relsPath, props.SheetNames[i], i, sheetRels, sheets, stype, opts, wb, themes, styles);
- }
- out = ({
- Directory: dir,
- Workbook: wb,
- Props: props,
- Custprops: custprops,
- Deps: deps,
- Sheets: sheets,
- SheetNames: props.SheetNames,
- Strings: strs,
- Styles: styles,
- Themes: themes,
- SSF: SSF.get_table()
- });
- if(opts && opts.bookFiles) {
- out.keys = entries;
- out.files = zip.files;
- }
- if(opts && opts.bookVBA) {
- if(dir.vba.length > 0) out.vbaraw = getzipdata(zip,strip_front_slash(dir.vba[0]),true);
- else if(dir.defaults && dir.defaults.bin === CT_VBA) out.vbaraw = getzipdata(zip, 'xl/vbaProject.bin',true);
- }
- return out;
- }
- /* [MS-OFFCRYPTO] 2.1.1 */
- function parse_xlsxcfb(cfb, _opts) {
- var opts = _opts || {};
- var f = 'Workbook', data = CFB.find(cfb, f);
- try {
- f = '/!DataSpaces/Version';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- /*var version = */parse_DataSpaceVersionInfo(data.content);
- /* 2.3.4.1 */
- f = '/!DataSpaces/DataSpaceMap';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- var dsm = parse_DataSpaceMap(data.content);
- 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")
- throw new Error("ECMA-376 Encrypted file bad " + f);
- /* 2.3.4.2 */
- f = '/!DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- var seds = parse_DataSpaceDefinition(data.content);
- if(seds.length != 1 || seds[0] != "StrongEncryptionTransform")
- throw new Error("ECMA-376 Encrypted file bad " + f);
- /* 2.3.4.3 */
- f = '/!DataSpaces/TransformInfo/StrongEncryptionTransform/!Primary';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- /*var hdr = */parse_Primary(data.content);
- } catch(e) {}
- f = '/EncryptionInfo';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- var einfo = parse_EncryptionInfo(data.content);
- /* 2.3.4.4 */
- f = '/EncryptedPackage';
- data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
- /*global decrypt_agile */
- if(einfo[0] == 0x04 && typeof decrypt_agile !== 'undefined') return decrypt_agile(einfo[1], data.content, opts.password || "", opts);
- /*global decrypt_std76 */
- if(einfo[0] == 0x02 && typeof decrypt_std76 !== 'undefined') return decrypt_std76(einfo[1], data.content, opts.password || "", opts);
- throw new Error("File is password-protected");
- }
- function write_zip(wb, opts) {
- _shapeid = 1024;
- if(opts.bookType == "ods") return write_ods(wb, opts);
- if(wb && !wb.SSF) {
- wb.SSF = SSF.get_table();
- }
- if(wb && wb.SSF) {
- make_ssf(SSF); SSF.load_table(wb.SSF);
- // $FlowIgnore
- opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
- opts.ssf = wb.SSF;
- }
- opts.rels = {}; opts.wbrels = {};
- opts.Strings = []; opts.Strings.Count = 0; opts.Strings.Unique = 0;
- if(browser_has_Map) opts.revStrings = new Map();
- else { opts.revStrings = {}; opts.revStrings.foo = []; delete opts.revStrings.foo; }
- var wbext = opts.bookType == "xlsb" ? "bin" : "xml";
- var vbafmt = VBAFMTS.indexOf(opts.bookType) > -1;
- var ct = new_ct();
- fix_write_opts(opts = opts || {});
- var zip = zip_new();
- var f = "", rId = 0;
- opts.cellXfs = [];
- get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
- if(!wb.Props) wb.Props = {};
- f = "docProps/core.xml";
- zip_add_file(zip, f, write_core_props(wb.Props, opts));
- ct.coreprops.push(f);
- add_rels(opts.rels, 2, f, RELS.CORE_PROPS);
- f = "docProps/app.xml";
- if(wb.Props && wb.Props.SheetNames){/* empty */}
- else if(!wb.Workbook || !wb.Workbook.Sheets) wb.Props.SheetNames = wb.SheetNames;
- else {
- var _sn = [];
- for(var _i = 0; _i < wb.SheetNames.length; ++_i)
- if((wb.Workbook.Sheets[_i]||{}).Hidden != 2) _sn.push(wb.SheetNames[_i]);
- wb.Props.SheetNames = _sn;
- }
- wb.Props.Worksheets = wb.Props.SheetNames.length;
- zip_add_file(zip, f, write_ext_props(wb.Props, opts));
- ct.extprops.push(f);
- add_rels(opts.rels, 3, f, RELS.EXT_PROPS);
- if(wb.Custprops !== wb.Props && keys(wb.Custprops||{}).length > 0) {
- f = "docProps/custom.xml";
- zip_add_file(zip, f, write_cust_props(wb.Custprops, opts));
- ct.custprops.push(f);
- add_rels(opts.rels, 4, f, RELS.CUST_PROPS);
- }
- for(rId=1;rId <= wb.SheetNames.length; ++rId) {
- var wsrels = {'!id':{}};
- var ws = wb.Sheets[wb.SheetNames[rId-1]];
- var _type = (ws || {})["!type"] || "sheet";
- switch(_type) {
- case "chart":
- /* falls through */
- default:
- f = "xl/worksheets/sheet" + rId + "." + wbext;
- zip_add_file(zip, f, write_ws(rId-1, f, opts, wb, wsrels));
- ct.sheets.push(f);
- add_rels(opts.wbrels, -1, "worksheets/sheet" + rId + "." + wbext, RELS.WS[0]);
- }
- if(ws) {
- var comments = ws['!comments'];
- var need_vml = false;
- if(comments && comments.length > 0) {
- var cf = "xl/comments" + rId + "." + wbext;
- zip_add_file(zip, cf, write_cmnt(comments, cf, opts));
- ct.comments.push(cf);
- add_rels(wsrels, -1, "../comments" + rId + "." + wbext, RELS.CMNT);
- need_vml = true;
- }
- if(ws['!legacy']) {
- if(need_vml) zip_add_file(zip, "xl/drawings/vmlDrawing" + (rId) + ".vml", write_comments_vml(rId, ws['!comments']));
- }
- delete ws['!comments'];
- delete ws['!legacy'];
- }
- if(wsrels['!id'].rId1) zip_add_file(zip, get_rels_path(f), write_rels(wsrels));
- }
- if(opts.Strings != null && opts.Strings.length > 0) {
- f = "xl/sharedStrings." + wbext;
- zip_add_file(zip, f, write_sst(opts.Strings, f, opts));
- ct.strs.push(f);
- add_rels(opts.wbrels, -1, "sharedStrings." + wbext, RELS.SST);
- }
- f = "xl/workbook." + wbext;
- zip_add_file(zip, f, write_wb(wb, f, opts));
- ct.workbooks.push(f);
- add_rels(opts.rels, 1, f, RELS.WB);
- /* TODO: something more intelligent with themes */
- f = "xl/theme/theme1.xml";
- zip_add_file(zip, f, write_theme(wb.Themes, opts));
- ct.themes.push(f);
- add_rels(opts.wbrels, -1, "theme/theme1.xml", RELS.THEME);
- /* TODO: something more intelligent with styles */
- f = "xl/styles." + wbext;
- zip_add_file(zip, f, write_sty(wb, f, opts));
- ct.styles.push(f);
- add_rels(opts.wbrels, -1, "styles." + wbext, RELS.STY);
- if(wb.vbaraw && vbafmt) {
- f = "xl/vbaProject.bin";
- zip_add_file(zip, f, wb.vbaraw);
- ct.vba.push(f);
- add_rels(opts.wbrels, -1, "vbaProject.bin", RELS.VBA);
- }
- zip_add_file(zip, "[Content_Types].xml", write_ct(ct, opts));
- zip_add_file(zip, '_rels/.rels', write_rels(opts.rels));
- zip_add_file(zip, 'xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
- delete opts.revssf; delete opts.ssf;
- return zip;
- }
- function firstbyte(f,o) {
- var x = "";
- switch((o||{}).type || "base64") {
- case 'buffer': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
- case 'base64': x = Base64.decode(f.slice(0,12)); break;
- case 'binary': x = f; break;
- case 'array': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
- default: throw new Error("Unrecognized type " + (o && o.type || "undefined"));
- }
- 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)];
- }
- function read_cfb(cfb, opts) {
- if(CFB.find(cfb, "EncryptedPackage")) return parse_xlsxcfb(cfb, opts);
- return parse_xlscfb(cfb, opts);
- }
- function read_zip(data, opts) {
- var zip, d = data;
- var o = opts||{};
- if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
- zip = zip_read(d, o);
- return parse_zip(zip, o);
- }
- function read_plaintext(data, o) {
- var i = 0;
- main: while(i < data.length) switch(data.charCodeAt(i)) {
- case 0x0A: case 0x0D: case 0x20: ++i; break;
- case 0x3C: return parse_xlml(data.slice(i),o);
- default: break main;
- }
- return PRN.to_workbook(data, o);
- }
- function read_plaintext_raw(data, o) {
- var str = "", bytes = firstbyte(data, o);
- switch(o.type) {
- case 'base64': str = Base64.decode(data); break;
- case 'binary': str = data; break;
- case 'buffer': str = data.toString('binary'); break;
- case 'array': str = cc2str(data); break;
- default: throw new Error("Unrecognized type " + o.type);
- }
- if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str);
- return read_plaintext(str, o);
- }
- function read_utf16(data, o) {
- var d = data;
- if(o.type == 'base64') d = Base64.decode(d);
- d = cptable.utils.decode(1200, d.slice(2), 'str');
- o.type = "binary";
- return read_plaintext(d, o);
- }
- function bstrify(data) {
- return !data.match(/[^\x00-\x7F]/) ? data : utf8write(data);
- }
- function read_prn(data, d, o, str) {
- if(str) { o.type = "string"; return PRN.to_workbook(data, o); }
- return PRN.to_workbook(d, o);
- }
- function readSync(data, opts) {
- reset_cp();
- if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data), opts);
- var d = data, n = [0,0,0,0], str = false;
- var o = opts||{};
- if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
- _ssfopts = {};
- if(o.dateNF) _ssfopts.dateNF = o.dateNF;
- if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
- if(o.type == "file") { o.type = has_buf ? "buffer" : "binary"; d = read_binary(data); }
- if(o.type == "string") { str = true; o.type = "binary"; o.codepage = 65001; d = bstrify(data); }
- if(o.type == 'array' && typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && typeof ArrayBuffer !== 'undefined') {
- // $FlowIgnore
- var ab=new ArrayBuffer(3), vu=new Uint8Array(ab); vu.foo="bar";
- // $FlowIgnore
- if(!vu.foo) {o=dup(o); o.type='array'; return readSync(ab2a(d), o);}
- }
- switch((n = firstbyte(d, o))[0]) {
- 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;
- case 0x09: if(n[1] <= 0x04) return parse_xlscfb(d, o); break;
- case 0x3C: return parse_xlml(d, o);
- case 0x49: if(n[1] === 0x44) return read_wb_ID(d, o); break;
- case 0x54: if(n[1] === 0x41 && n[2] === 0x42 && n[3] === 0x4C) return DIF.to_workbook(d, o); break;
- case 0x50: return (n[1] === 0x4B && n[2] < 0x09 && n[3] < 0x09) ? read_zip(d, o) : read_prn(data, d, o, str);
- case 0xEF: return n[3] === 0x3C ? parse_xlml(d, o) : read_prn(data, d, o, str);
- case 0xFF: if(n[1] === 0xFE) { return read_utf16(d, o); } break;
- case 0x00: if(n[1] === 0x00 && n[2] >= 0x02 && n[3] === 0x00) return WK_.to_workbook(d, o); break;
- case 0x03: case 0x83: case 0x8B: case 0x8C: return DBF.to_workbook(d, o);
- case 0x7B: if(n[1] === 0x5C && n[2] === 0x72 && n[3] === 0x74) return RTF.to_workbook(d, o); break;
- case 0x0A: case 0x0D: case 0x20: return read_plaintext_raw(d, o);
- }
- if(DBF.versions.indexOf(n[0]) > -1 && n[2] <= 12 && n[3] <= 31) return DBF.to_workbook(d, o);
- return read_prn(data, d, o, str);
- }
- function readFileSync(filename, opts) {
- var o = opts||{}; o.type = 'file';
- return readSync(filename, o);
- }
- function write_cfb_ctr(cfb, o) {
- switch(o.type) {
- case "base64": case "binary": break;
- case "buffer": case "array": o.type = ""; break;
- case "file": return write_dl(o.file, CFB.write(cfb, {type:has_buf ? 'buffer' : ""}));
- case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
- default: throw new Error("Unrecognized type " + o.type);
- }
- return CFB.write(cfb, o);
- }
- function write_zip_type(wb, opts) {
- var o = opts||{};
- var z = write_zip(wb, o);
- var oopts = {};
- if(o.compression) oopts.compression = 'DEFLATE';
- if(o.password) oopts.type = has_buf ? "nodebuffer" : "string";
- else switch(o.type) {
- case "base64": oopts.type = "base64"; break;
- case "binary": oopts.type = "string"; break;
- case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
- case "buffer":
- case "file": oopts.type = has_buf ? "nodebuffer" : "string"; break;
- default: throw new Error("Unrecognized type " + o.type);
- }
- var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: {"nodebuffer": "buffer", "string": "binary"}[oopts.type] || oopts.type}) : z.generate(oopts);
- /*jshint -W083 */
- if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o); // eslint-disable-line no-undef
- /*jshint +W083 */
- if(o.type === "file") return write_dl(o.file, out);
- return o.type == "string" ? utf8read(out) : out;
- }
- function write_cfb_type(wb, opts) {
- var o = opts||{};
- var cfb = write_xlscfb(wb, o);
- return write_cfb_ctr(cfb, o);
- }
- function write_string_type(out, opts, bom) {
- if(!bom) bom = "";
- var o = bom + out;
- switch(opts.type) {
- case "base64": return Base64.encode(utf8write(o));
- case "binary": return utf8write(o);
- case "string": return out;
- case "file": return write_dl(opts.file, o, 'utf8');
- case "buffer": {
- if(has_buf) return Buffer_from(o, 'utf8');
- else return write_string_type(o, {type:'binary'}).split("").map(function(c) { return c.charCodeAt(0); });
- }
- }
- throw new Error("Unrecognized type " + opts.type);
- }
- function write_stxt_type(out, opts) {
- switch(opts.type) {
- case "base64": return Base64.encode(out);
- case "binary": return out;
- case "string": return out; /* override in sheet_to_txt */
- case "file": return write_dl(opts.file, out, 'binary');
- case "buffer": {
- if(has_buf) return Buffer_from(out, 'binary');
- else return out.split("").map(function(c) { return c.charCodeAt(0); });
- }
- }
- throw new Error("Unrecognized type " + opts.type);
- }
- /* TODO: test consistency */
- function write_binary_type(out, opts) {
- switch(opts.type) {
- case "string":
- case "base64":
- case "binary":
- var bstr = "";
- // $FlowIgnore
- for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
- return opts.type == 'base64' ? Base64.encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
- case "file": return write_dl(opts.file, out);
- case "buffer": return out;
- default: throw new Error("Unrecognized type " + opts.type);
- }
- }
- function writeSync(wb, opts) {
- reset_cp();
- check_wb(wb);
- var o = opts||{};
- if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
- if(o.type == "array") { o.type = "binary"; var out = (writeSync(wb, o)); o.type = "array"; return s2ab(out); }
- switch(o.bookType || 'xlsb') {
- case 'xml':
- case 'xlml': return write_string_type(write_xlml(wb, o), o);
- case 'slk':
- case 'sylk': return write_string_type(write_slk_str(wb, o), o);
- case 'htm':
- case 'html': return write_string_type(write_htm_str(wb, o), o);
- case 'txt': return write_stxt_type(write_txt_str(wb, o), o);
- case 'csv': return write_string_type(write_csv_str(wb, o), o, "\ufeff");
- case 'dif': return write_string_type(write_dif_str(wb, o), o);
- case 'dbf': return write_binary_type(write_dbf_buf(wb, o), o);
- case 'prn': return write_string_type(write_prn_str(wb, o), o);
- case 'rtf': return write_string_type(write_rtf_str(wb, o), o);
- case 'eth': return write_string_type(write_eth_str(wb, o), o);
- case 'fods': return write_string_type(write_ods(wb, o), o);
- case 'biff2': if(!o.biff) o.biff = 2; /* falls through */
- case 'biff3': if(!o.biff) o.biff = 3; /* falls through */
- case 'biff4': if(!o.biff) o.biff = 4; return write_binary_type(write_biff_buf(wb, o), o);
- case 'biff5': if(!o.biff) o.biff = 5; /* falls through */
- case 'biff8':
- case 'xla':
- case 'xls': if(!o.biff) o.biff = 8; return write_cfb_type(wb, o);
- case 'xlsx':
- case 'xlsm':
- case 'xlam':
- case 'xlsb':
- case 'ods': return write_zip_type(wb, o);
- default: throw new Error ("Unrecognized bookType |" + o.bookType + "|");
- }
- }
- function resolve_book_type(o) {
- if(o.bookType) return;
- var _BT = {
- "xls": "biff8",
- "htm": "html",
- "slk": "sylk",
- "socialcalc": "eth",
- "Sh33tJS": "WTF"
- };
- var ext = o.file.slice(o.file.lastIndexOf(".")).toLowerCase();
- if(ext.match(/^\.[a-z]+$/)) o.bookType = ext.slice(1);
- o.bookType = _BT[o.bookType] || o.bookType;
- }
- function writeFileSync(wb, filename, opts) {
- var o = opts||{}; o.type = 'file';
- o.file = filename;
- resolve_book_type(o);
- return writeSync(wb, o);
- }
- function writeFileAsync(filename, wb, opts, cb) {
- var o = opts||{}; o.type = 'file';
- o.file = filename;
- resolve_book_type(o);
- o.type = 'buffer';
- var _cb = cb; if(!(_cb instanceof Function)) _cb = (opts);
- return _fs.writeFile(filename, writeSync(wb, o), _cb);
- }
- function make_json_row(sheet, r, R, cols, header, hdr, dense, o) {
- var rr = encode_row(R);
- var defval = o.defval, raw = o.raw || !Object.prototype.hasOwnProperty.call(o, "raw");
- var isempty = true;
- var row = (header === 1) ? [] : {};
- if(header !== 1) {
- if(Object.defineProperty) try { Object.defineProperty(row, '__rowNum__', {value:R, enumerable:false}); } catch(e) { row.__rowNum__ = R; }
- else row.__rowNum__ = R;
- }
- if(!dense || sheet[R]) for (var C = r.s.c; C <= r.e.c; ++C) {
- var val = dense ? sheet[R][C] : sheet[cols[C] + rr];
- if(val === undefined || val.t === undefined) {
- if(defval === undefined) continue;
- if(hdr[C] != null) { row[hdr[C]] = defval; }
- continue;
- }
- var v = val.v;
- switch(val.t){
- case 'z': if(v == null) break; continue;
- case 'e': v = void 0; break;
- case 's': case 'd': case 'b': case 'n': break;
- default: throw new Error('unrecognized type ' + val.t);
- }
- if(hdr[C] != null) {
- if(v == null) {
- if(defval !== undefined) row[hdr[C]] = defval;
- else if(raw && v === null) row[hdr[C]] = null;
- else continue;
- } else {
- row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o);
- }
- if(v != null) isempty = false;
- }
- }
- return { row: row, isempty: isempty };
- }
- function sheet_to_json(sheet, opts) {
- if(sheet == null || sheet["!ref"] == null) return [];
- var val = {t:'n',v:0}, header = 0, offset = 1, hdr = [], v=0, vv="";
- var r = {s:{r:0,c:0},e:{r:0,c:0}};
- var o = opts || {};
- var range = o.range != null ? o.range : sheet["!ref"];
- if(o.header === 1) header = 1;
- else if(o.header === "A") header = 2;
- else if(Array.isArray(o.header)) header = 3;
- else if(o.header == null) header = 0;
- switch(typeof range) {
- case 'string': r = safe_decode_range(range); break;
- case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
- default: r = range;
- }
- if(header > 0) offset = 0;
- var rr = encode_row(r.s.r);
- var cols = [];
- var out = [];
- var outi = 0, counter = 0;
- var dense = Array.isArray(sheet);
- var R = r.s.r, C = 0, CC = 0;
- if(dense && !sheet[R]) sheet[R] = [];
- for(C = r.s.c; C <= r.e.c; ++C) {
- cols[C] = encode_col(C);
- val = dense ? sheet[R][C] : sheet[cols[C] + rr];
- switch(header) {
- case 1: hdr[C] = C - r.s.c; break;
- case 2: hdr[C] = cols[C]; break;
- case 3: hdr[C] = o.header[C - r.s.c]; break;
- default:
- if(val == null) val = {w: "__EMPTY", t: "s"};
- vv = v = format_cell(val, null, o);
- counter = 0;
- for(CC = 0; CC < hdr.length; ++CC) if(hdr[CC] == vv) vv = v + "_" + (++counter);
- hdr[C] = vv;
- }
- }
- for (R = r.s.r + offset; R <= r.e.r; ++R) {
- var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
- if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row.row;
- }
- out.length = outi;
- return out;
- }
- var qreg = /"/g;
- function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) {
- var isempty = true;
- var row = [], txt = "", rr = encode_row(R);
- for(var C = r.s.c; C <= r.e.c; ++C) {
- if (!cols[C]) continue;
- var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
- if(val == null) txt = "";
- else if(val.v != null) {
- isempty = false;
- txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o));
- 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; }
- if(txt == "ID") txt = '"ID"';
- } else if(val.f != null && !val.F) {
- isempty = false;
- txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
- } else txt = "";
- /* NOTE: Excel CSV does not support array formulae */
- row.push(txt);
- }
- if(o.blankrows === false && isempty) return null;
- return row.join(FS);
- }
- function sheet_to_csv(sheet, opts) {
- var out = [];
- var o = opts == null ? {} : opts;
- if(sheet == null || sheet["!ref"] == null) return "";
- var r = safe_decode_range(sheet["!ref"]);
- var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
- var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
- var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
- var row = "", cols = [];
- o.dense = Array.isArray(sheet);
- var colinfo = o.skipHidden && sheet["!cols"] || [];
- var rowinfo = o.skipHidden && sheet["!rows"] || [];
- for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
- for(var R = r.s.r; R <= r.e.r; ++R) {
- if ((rowinfo[R]||{}).hidden) continue;
- row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
- if(row == null) { continue; }
- if(o.strip) row = row.replace(endregex,"");
- out.push(row + RS);
- }
- delete o.dense;
- return out.join("");
- }
- function sheet_to_txt(sheet, opts) {
- if(!opts) opts = {}; opts.FS = "\t"; opts.RS = "\n";
- var s = sheet_to_csv(sheet, opts);
- if(typeof cptable == 'undefined' || opts.type == 'string') return s;
- var o = cptable.utils.encode(1200, s, 'str');
- return String.fromCharCode(255) + String.fromCharCode(254) + o;
- }
- function sheet_to_formulae(sheet) {
- var y = "", x, val="";
- if(sheet == null || sheet["!ref"] == null) return [];
- var r = safe_decode_range(sheet['!ref']), rr = "", cols = [], C;
- var cmds = [];
- var dense = Array.isArray(sheet);
- for(C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
- for(var R = r.s.r; R <= r.e.r; ++R) {
- rr = encode_row(R);
- for(C = r.s.c; C <= r.e.c; ++C) {
- y = cols[C] + rr;
- x = dense ? (sheet[R]||[])[C] : sheet[y];
- val = "";
- if(x === undefined) continue;
- else if(x.F != null) {
- y = x.F;
- if(!x.f) continue;
- val = x.f;
- if(y.indexOf(":") == -1) y = y + ":" + y;
- }
- if(x.f != null) val = x.f;
- else if(x.t == 'z') continue;
- else if(x.t == 'n' && x.v != null) val = "" + x.v;
- else if(x.t == 'b') val = x.v ? "TRUE" : "FALSE";
- else if(x.w !== undefined) val = "'" + x.w;
- else if(x.v === undefined) continue;
- else if(x.t == 's') val = "'" + x.v;
- else val = ""+x.v;
- cmds[cmds.length] = y + "=" + val;
- }
- }
- return cmds;
- }
- function sheet_add_json(_ws, js, opts) {
- var o = opts || {};
- var offset = +!o.skipHeader;
- var ws = _ws || ({});
- var _R = 0, _C = 0;
- if(ws && o.origin != null) {
- if(typeof o.origin == 'number') _R = o.origin;
- else {
- var _origin = typeof o.origin == "string" ? decode_cell(o.origin) : o.origin;
- _R = _origin.r; _C = _origin.c;
- }
- }
- var cell;
- var range = ({s: {c:0, r:0}, e: {c:_C, r:_R + js.length - 1 + offset}});
- if(ws['!ref']) {
- var _range = safe_decode_range(ws['!ref']);
- range.e.c = Math.max(range.e.c, _range.e.c);
- range.e.r = Math.max(range.e.r, _range.e.r);
- if(_R == -1) { _R = _range.e.r + 1; range.e.r = _R + js.length - 1 + offset; }
- } else {
- if(_R == -1) { _R = 0; range.e.r = js.length - 1 + offset; }
- }
- var hdr = o.header || [], C = 0;
- js.forEach(function (JS, R) {
- keys(JS).forEach(function(k) {
- if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
- var v = JS[k];
- var t = 'z';
- var z = "";
- var ref = encode_cell({c:_C + C,r:_R + R + offset});
- cell = utils.sheet_get_cell(ws, ref);
- if(v && typeof v === 'object' && !(v instanceof Date)){
- ws[ref] = v;
- } else {
- if(typeof v == 'number') t = 'n';
- else if(typeof v == 'boolean') t = 'b';
- else if(typeof v == 'string') t = 's';
- else if(v instanceof Date) {
- t = 'd';
- if(!o.cellDates) { t = 'n'; v = datenum(v); }
- z = (o.dateNF || SSF._table[14]);
- }
- if(!cell) ws[ref] = cell = ({t:t, v:v});
- else {
- cell.t = t; cell.v = v;
- delete cell.w; delete cell.R;
- if(z) cell.z = z;
- }
- if(z) cell.z = z;
- }
- });
- });
- range.e.c = Math.max(range.e.c, _C + hdr.length - 1);
- var __R = encode_row(_R);
- if(offset) for(C = 0; C < hdr.length; ++C) ws[encode_col(C + _C) + __R] = {t:'s', v:hdr[C]};
- ws['!ref'] = encode_range(range);
- return ws;
- }
- function json_to_sheet(js, opts) { return sheet_add_json(null, js, opts); }
- var utils = {
- encode_col: encode_col,
- encode_row: encode_row,
- encode_cell: encode_cell,
- encode_range: encode_range,
- decode_col: decode_col,
- decode_row: decode_row,
- split_cell: split_cell,
- decode_cell: decode_cell,
- decode_range: decode_range,
- format_cell: format_cell,
- get_formulae: sheet_to_formulae,
- make_csv: sheet_to_csv,
- make_json: sheet_to_json,
- make_formulae: sheet_to_formulae,
- sheet_add_aoa: sheet_add_aoa,
- sheet_add_json: sheet_add_json,
- sheet_add_dom: sheet_add_dom,
- aoa_to_sheet: aoa_to_sheet,
- json_to_sheet: json_to_sheet,
- table_to_sheet: parse_dom_table,
- table_to_book: table_to_book,
- sheet_to_csv: sheet_to_csv,
- sheet_to_txt: sheet_to_txt,
- sheet_to_json: sheet_to_json,
- sheet_to_html: HTML_.from_sheet,
- sheet_to_formulae: sheet_to_formulae,
- sheet_to_row_object_array: sheet_to_json
- };
- (function(utils) {
- utils.consts = utils.consts || {};
- function add_consts(R/*Array<any>*/) { R.forEach(function(a){ utils.consts[a[0]] = a[1]; }); }
- function get_default(x, y, z) { return x[y] != null ? x[y] : (x[y] = z); }
- /* get cell, creating a stub if necessary */
- function ws_get_cell_stub(ws, R, C) {
- /* A1 cell address */
- if(typeof R == "string") {
- /* dense */
- if(Array.isArray(ws)) {
- var RC = decode_cell(R);
- if(!ws[RC.r]) ws[RC.r] = [];
- return ws[RC.r][RC.c] || (ws[RC.r][RC.c] = {t:'z'});
- }
- return ws[R] || (ws[R] = {t:'z'});
- }
- /* cell address object */
- if(typeof R != "number") return ws_get_cell_stub(ws, encode_cell(R));
- /* R and C are 0-based indices */
- return ws_get_cell_stub(ws, encode_cell({r:R,c:C||0}));
- }
- utils.sheet_get_cell = ws_get_cell_stub;
- /* find sheet index for given name / validate index */
- function wb_sheet_idx(wb, sh) {
- if(typeof sh == "number") {
- if(sh >= 0 && wb.SheetNames.length > sh) return sh;
- throw new Error("Cannot find sheet # " + sh);
- } else if(typeof sh == "string") {
- var idx = wb.SheetNames.indexOf(sh);
- if(idx > -1) return idx;
- throw new Error("Cannot find sheet name |" + sh + "|");
- } else throw new Error("Cannot find sheet |" + sh + "|");
- }
- /* simple blank workbook object */
- utils.book_new = function() {
- return { SheetNames: [], Sheets: {} };
- };
- /* add a worksheet to the end of a given workbook */
- utils.book_append_sheet = function(wb, ws, name) {
- if(!name) for(var i = 1; i <= 0xFFFF; ++i, name = undefined) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
- if(!name || wb.SheetNames.length >= 0xFFFF) throw new Error("Too many worksheets");
- check_ws_name(name);
- if(wb.SheetNames.indexOf(name) >= 0) throw new Error("Worksheet with name |" + name + "| already exists!");
- wb.SheetNames.push(name);
- wb.Sheets[name] = ws;
- };
- /* set sheet visibility (visible/hidden/very hidden) */
- utils.book_set_sheet_visibility = function(wb, sh, vis) {
- get_default(wb,"Workbook",{});
- get_default(wb.Workbook,"Sheets",[]);
- var idx = wb_sheet_idx(wb, sh);
- // $FlowIgnore
- get_default(wb.Workbook.Sheets,idx, {});
- switch(vis) {
- case 0: case 1: case 2: break;
- default: throw new Error("Bad sheet visibility setting " + vis);
- }
- // $FlowIgnore
- wb.Workbook.Sheets[idx].Hidden = vis;
- };
- add_consts([
- ["SHEET_VISIBLE", 0],
- ["SHEET_HIDDEN", 1],
- ["SHEET_VERY_HIDDEN", 2]
- ]);
- /* set number format */
- utils.cell_set_number_format = function(cell, fmt) {
- cell.z = fmt;
- return cell;
- };
- /* set cell hyperlink */
- utils.cell_set_hyperlink = function(cell, target, tooltip) {
- if(!target) {
- delete cell.l;
- } else {
- cell.l = ({ Target: target });
- if(tooltip) cell.l.Tooltip = tooltip;
- }
- return cell;
- };
- utils.cell_set_internal_link = function(cell, range, tooltip) { return utils.cell_set_hyperlink(cell, "#" + range, tooltip); };
- /* add to cell comments */
- utils.cell_add_comment = function(cell, text, author) {
- if(!cell.c) cell.c = [];
- cell.c.push({t:text, a:author||"SheetJS"});
- };
- /* set array formula and flush related cells */
- utils.sheet_set_array_formula = function(ws, range, formula) {
- var rng = typeof range != "string" ? range : safe_decode_range(range);
- var rngstr = typeof range == "string" ? range : encode_range(range);
- for(var R = rng.s.r; R <= rng.e.r; ++R) for(var C = rng.s.c; C <= rng.e.c; ++C) {
- var cell = ws_get_cell_stub(ws, R, C);
- cell.t = 'n';
- cell.F = rngstr;
- delete cell.v;
- if(R == rng.s.r && C == rng.s.c) cell.f = formula;
- }
- return ws;
- };
- return utils;
- })(utils);
- if(typeof parse_xlscfb !== "undefined") XLSX.parse_xlscfb = parse_xlscfb;
- XLSX.parse_zip = parse_zip;
- XLSX.read = readSync; //xlsread
- XLSX.readFile = readFileSync; //readFile
- XLSX.readFileSync = readFileSync;
- XLSX.write = writeSync;
- XLSX.writeFile = writeFileSync;
- XLSX.writeFileSync = writeFileSync;
- XLSX.writeFileAsync = writeFileAsync;
- XLSX.utils = utils;
- XLSX.SSF = SSF;
- if(typeof CFB !== "undefined") XLSX.CFB = CFB;
- }
- /*global define */
- if(typeof exports !== 'undefined') make_xlsx_lib(exports);
- else if(typeof module !== 'undefined' && module.exports) make_xlsx_lib(module.exports);
- else if(typeof define === 'function' && define.amd) define(function() { if(!XLSX.version) make_xlsx_lib(XLSX); return XLSX; });
- else make_xlsx_lib(XLSX);
- /*exported XLS, ODS */
- var XLS = XLSX, ODS = XLSX;
|