@@ -155,6 +155,7 @@ function write_ws_xml_cell(cell, ref, ws, opts, idx, wb) {
155155 var ff = cell . F && cell . F . substr ( 0 , ref . length ) == ref ? { t :"array" , ref :cell . F } : null ;
156156 v = writextag ( 'f' , escapexml ( cell . f ) , ff ) + ( cell . v != null ? v : "" ) ;
157157 }
158+ if ( cell . l ) ws [ '!links' ] . push ( [ ref , cell . l ] ) ;
158159 return writextag ( 'c' , v , o ) ;
159160}
160161
@@ -290,7 +291,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
290291 }
291292} ; } ) ( ) ;
292293
293- function write_ws_xml_data ( ws /*:Worksheet*/ , opts , idx /*:number*/ , wb /*:Workbook*/ ) /*:string*/ {
294+ function write_ws_xml_data ( ws /*:Worksheet*/ , opts , idx /*:number*/ , wb /*:Workbook*/ , rels ) /*:string*/ {
294295 var o = [ ] , r = [ ] , range = safe_decode_range ( ws [ '!ref' ] ) , cell , ref , rr = "" , cols = [ ] , R = 0 , C = 0 ;
295296 for ( C = range . s . c ; C <= range . e . c ; ++ C ) cols [ C ] = encode_col ( C ) ;
296297 for ( R = range . s . r ; R <= range . e . r ; ++ R ) {
@@ -311,111 +312,42 @@ var WS_XML_ROOT = writextag('worksheet', null, {
311312 'xmlns:r' : XMLNS . r
312313} ) ;
313314
314- function write_ws_xml ( idx , opts , wb ) {
315- var o = [ XML_HEADER , WS_XML_ROOT ] ;
316- var s = wb . SheetNames [ idx ] , sidx = 0 , rdata = "" ;
317- var ws = wb . Sheets [ s ] ;
318- if ( ws === undefined ) ws = { } ;
319- var ref = ws [ '!ref' ] ;
320- if ( ref === undefined ) ref = 'A1' ;
321- o [ o . length ] = ( writextag ( 'dimension' , null , { 'ref' : ref } ) ) ;
322-
323- var kids = [ ] ;
324- if ( ws [ '!freeze' ] ) {
325- var pane = '' ;
326- pane = writextag ( 'pane' , null , ws [ '!freeze' ] )
327- kids . push ( pane )
328-
329- var selection = writextag ( 'selection' , null , {
330- pane : "topLeft"
331- } )
332- kids . push ( selection )
333-
334- var selection = writextag ( 'selection' , null , {
335- pane : "bottomLeft"
336- } )
337- kids . push ( selection )
338-
339- var selection = writextag ( 'selection' , null , {
340- pane : "bottomRight" ,
341- activeCell : ws [ '!freeze' ] ,
342- sqref : ws [ '!freeze' ]
343- } )
344- kids . push ( selection )
345- }
346-
347-
348- //<selection pane="bottomRight" activeCell="A4" sqref="A4"/>
349-
350- var sheetView = writextag ( 'sheetView' , kids . join ( '' ) || undefined , {
351- showGridLines : opts . showGridLines == false ? '0' : '1' ,
352- tabSelected : opts . tabSelected === undefined ? '0' : opts . tabSelected , // see issue #26, need to set WorkbookViews if this is set
353- workbookViewId : opts . workbookViewId === undefined ? '0' : opts . workbookViewId
354- } ) ;
355- o [ o . length ] = writextag ( 'sheetViews' , sheetView ) ;
356-
357- if ( ws [ '!cols' ] !== undefined && ws [ '!cols' ] . length > 0 ) o [ o . length ] = ( write_ws_xml_cols ( ws , ws [ '!cols' ] ) ) ;
358- o [ sidx = o . length ] = '<sheetData/>' ;
359- if ( ws [ '!ref' ] !== undefined ) {
360- rdata = write_ws_xml_data ( ws , opts , idx , wb ) ;
361- if ( rdata . length > 0 ) o [ o . length ] = ( rdata ) ;
362- }
363- if ( o . length > sidx + 1 ) {
364- o [ o . length ] = ( '</sheetData>' ) ;
365- o [ sidx ] = o [ sidx ] . replace ( "/>" , ">" ) ;
366- }
367-
368- if ( ws [ '!merges' ] !== undefined && ws [ '!merges' ] . length > 0 ) o [ o . length ] = ( write_ws_xml_merges ( ws [ '!merges' ] ) ) ;
369-
370- if ( ws [ '!pageSetup' ] !== undefined ) o [ o . length ] = write_ws_xml_pagesetup ( ws [ '!pageSetup' ] ) ;
371- if ( ws [ '!rowBreaks' ] !== undefined ) o [ o . length ] = write_ws_xml_row_breaks ( ws [ '!rowBreaks' ] ) ;
372- if ( ws [ '!colBreaks' ] !== undefined ) o [ o . length ] = write_ws_xml_col_breaks ( ws [ '!colBreaks' ] ) ;
373-
374- if ( o . length > 2 ) {
375- o [ o . length ] = ( '</worksheet>' ) ;
376- o [ 1 ] = o [ 1 ] . replace ( "/>" , ">" ) ;
377- }
378- return o . join ( "" ) ;
379- }
380-
381- function write_ws_xml_row_breaks ( breaks ) {
382- var brk = [ ] ;
383- for ( var i = 0 ; i < breaks . length ; i ++ ) {
384- var thisBreak = '' + ( breaks [ i ] ) ;
385- var nextBreak = '' + ( breaks [ i + 1 ] || '16383' ) ;
386- brk . push ( writextag ( 'brk' , null , { id : thisBreak , max : nextBreak , man : '1' } ) )
387- }
388- return writextag ( 'rowBreaks' , brk . join ( ' ' ) , { count : brk . length , manualBreakCount : brk . length } )
389- }
390- function write_ws_xml_col_breaks ( breaks ) {
391- var brk = [ ] ;
392- for ( var i = 0 ; i < breaks . length ; i ++ ) {
393- var thisBreak = '' + ( breaks [ i ] ) ;
394- var nextBreak = '' + ( breaks [ i + 1 ] || '1048575' ) ;
395- brk . push ( writextag ( 'brk' , null , { id : thisBreak , max : nextBreak , man : '1' } ) )
396- }
397- return writextag ( 'colBreaks' , brk . join ( ' ' ) , { count : brk . length , manualBreakCount : brk . length } )
398-
399- function write_ws_xml ( idx /*:number*/ , opts , wb /*:Workbook*/ ) /*:string*/ {
315+ function write_ws_xml ( idx /*:number*/ , opts , wb /*:Workbook*/ , rels ) /*:string*/ {
400316 var o = [ XML_HEADER , WS_XML_ROOT ] ;
401317 var s = wb . SheetNames [ idx ] , sidx = 0 , rdata = "" ;
402318 var ws = wb . Sheets [ s ] ;
403319 if ( ws === undefined ) ws = { } ;
404320 var ref = ws [ '!ref' ] ; if ( ref === undefined ) ref = 'A1' ;
321+ if ( ! rels ) rels = { } ;
322+
323+ o [ o . length ] = ( writextag ( 'sheetPr' , null , { 'codeName' : escapexml ( wb . SheetNames [ idx ] ) } ) ) ;
405324 o [ o . length ] = ( writextag ( 'dimension' , null , { 'ref' : ref } ) ) ;
406325
407326 if ( ws [ '!cols' ] !== undefined && ws [ '!cols' ] . length > 0 ) o [ o . length ] = ( write_ws_xml_cols ( ws , ws [ '!cols' ] ) ) ;
408327 o [ sidx = o . length ] = '<sheetData/>' ;
328+ ws [ '!links' ] = [ ] ;
409329 if ( ws [ '!ref' ] != null ) {
410- rdata = write_ws_xml_data ( ws , opts , idx , wb ) ;
330+ rdata = write_ws_xml_data ( ws , opts , idx , wb , rels ) ;
411331 if ( rdata . length > 0 ) o [ o . length ] = ( rdata ) ;
412332 }
413333 if ( o . length > sidx + 1 ) { o [ o . length ] = ( '</sheetData>' ) ; o [ sidx ] = o [ sidx ] . replace ( "/>" , ">" ) ; }
414334
415335 if ( ws [ '!merges' ] != null && ws [ '!merges' ] . length > 0 ) o [ o . length ] = ( write_ws_xml_merges ( ws [ '!merges' ] ) ) ;
416336
417- if ( o . length > 2 ) { o [ o . length ] = ( '</worksheet>' ) ; o [ 1 ] = o [ 1 ] . replace ( "/>" , ">" ) ; }
418-
337+ var relc = - 1 , rel ;
338+ if ( ws [ '!links' ] . length > 0 ) {
339+ o [ o . length ] = "<hyperlinks>" ;
340+ ws [ '!links' ] . forEach ( function ( l ) {
341+ if ( ! l [ 1 ] . Target ) return ;
342+ var rId = add_rels ( rels , - 1 , escapexml ( l [ 1 ] . Target ) . replace ( / # .* $ / , "" ) , RELS . HLINK ) ;
343+ rel = ( { "ref" :l [ 0 ] , "r:id" :"rId" + rId } /*:any*/ ) ;
344+ if ( ( relc = l [ 1 ] . Target . indexOf ( "#" ) ) > - 1 ) rel . location = escapexml ( l [ 1 ] . Target . substr ( relc + 1 ) ) ;
345+ if ( l [ 1 ] . Tooltip ) rel . tooltip = escapexml ( l [ 1 ] . Tooltip ) ;
346+ o [ o . length ] = writextag ( "hyperlink" , null , rel ) ;
347+ } ) ;
348+ o [ o . length ] = "</hyperlinks>" ;
349+ }
419350 delete ws [ '!links' ] ;
351+ if ( o . length > 2 ) { o [ o . length ] = ( '</worksheet>' ) ; o [ 1 ] = o [ 1 ] . replace ( "/>" , ">" ) ; }
420352 return o . join ( "" ) ;
421- } }
353+ }
0 commit comments