@@ -14,6 +14,7 @@ package excelize
14
14
import (
15
15
"encoding/xml"
16
16
"io"
17
+ "sort"
17
18
"strings"
18
19
)
19
20
@@ -389,15 +390,14 @@ func (f *File) addSparklineGroupByStyle(ID int) *xlsxX14SparklineGroup {
389
390
// Axis | Show sparkline axis
390
391
func (f * File ) AddSparkline (sheet string , opts * SparklineOptions ) error {
391
392
var (
392
- err error
393
- ws * xlsxWorksheet
394
- sparkType string
395
- sparkTypes map [string ]string
396
- specifiedSparkTypes string
397
- ok bool
398
- group * xlsxX14SparklineGroup
399
- groups * xlsxX14SparklineGroups
400
- sparklineGroupsBytes , extBytes []byte
393
+ err error
394
+ ws * xlsxWorksheet
395
+ sparkType string
396
+ sparkTypes map [string ]string
397
+ specifiedSparkTypes string
398
+ ok bool
399
+ group * xlsxX14SparklineGroup
400
+ groups * xlsxX14SparklineGroups
401
401
)
402
402
403
403
// parameter validation
@@ -434,25 +434,8 @@ func (f *File) AddSparkline(sheet string, opts *SparklineOptions) error {
434
434
group .RightToLeft = opts .Reverse
435
435
}
436
436
f .addSparkline (opts , group )
437
- if ws .ExtLst .Ext != "" { // append mode ext
438
- if err = f .appendSparkline (ws , group , groups ); err != nil {
439
- return err
440
- }
441
- } else {
442
- groups = & xlsxX14SparklineGroups {
443
- XMLNSXM : NameSpaceSpreadSheetExcel2006Main .Value ,
444
- SparklineGroups : []* xlsxX14SparklineGroup {group },
445
- }
446
- if sparklineGroupsBytes , err = xml .Marshal (groups ); err != nil {
447
- return err
448
- }
449
- if extBytes , err = xml .Marshal (& xlsxWorksheetExt {
450
- URI : ExtURISparklineGroups ,
451
- Content : string (sparklineGroupsBytes ),
452
- }); err != nil {
453
- return err
454
- }
455
- ws .ExtLst .Ext = string (extBytes )
437
+ if err = f .appendSparkline (ws , group , groups ); err != nil {
438
+ return err
456
439
}
457
440
f .addSheetNameSpace (sheet , NameSpaceSpreadSheetX14 )
458
441
return err
@@ -504,42 +487,50 @@ func (f *File) appendSparkline(ws *xlsxWorksheet, group *xlsxX14SparklineGroup,
504
487
var (
505
488
err error
506
489
idx int
507
- decodeExtLst * decodeWorksheetExt
490
+ appendMode bool
491
+ decodeExtLst = new (decodeWorksheetExt )
508
492
decodeSparklineGroups * decodeX14SparklineGroups
509
493
ext * xlsxWorksheetExt
510
494
sparklineGroupsBytes , sparklineGroupBytes , extLstBytes []byte
511
495
)
512
- decodeExtLst = new (decodeWorksheetExt )
513
- if err = f .xmlNewDecoder (strings .NewReader ("<extLst>" + ws .ExtLst .Ext + "</extLst>" )).
514
- Decode (decodeExtLst ); err != nil && err != io .EOF {
515
- return err
516
- }
517
- for idx , ext = range decodeExtLst .Ext {
518
- if ext .URI == ExtURISparklineGroups {
519
- decodeSparklineGroups = new (decodeX14SparklineGroups )
520
- if err = f .xmlNewDecoder (strings .NewReader (ext .Content )).
521
- Decode (decodeSparklineGroups ); err != nil && err != io .EOF {
522
- return err
523
- }
524
- if sparklineGroupBytes , err = xml .Marshal (group ); err != nil {
525
- return err
526
- }
527
- if groups == nil {
528
- groups = & xlsxX14SparklineGroups {}
529
- }
530
- groups .XMLNSXM = NameSpaceSpreadSheetExcel2006Main .Value
531
- groups .Content = decodeSparklineGroups .Content + string (sparklineGroupBytes )
532
- if sparklineGroupsBytes , err = xml .Marshal (groups ); err != nil {
533
- return err
496
+ sparklineGroupBytes , _ = xml .Marshal (group )
497
+ if ws .ExtLst != nil { // append mode ext
498
+ if err = f .xmlNewDecoder (strings .NewReader ("<extLst>" + ws .ExtLst .Ext + "</extLst>" )).
499
+ Decode (decodeExtLst ); err != nil && err != io .EOF {
500
+ return err
501
+ }
502
+ for idx , ext = range decodeExtLst .Ext {
503
+ if ext .URI == ExtURISparklineGroups {
504
+ decodeSparklineGroups = new (decodeX14SparklineGroups )
505
+ if err = f .xmlNewDecoder (strings .NewReader (ext .Content )).
506
+ Decode (decodeSparklineGroups ); err != nil && err != io .EOF {
507
+ return err
508
+ }
509
+ if groups == nil {
510
+ groups = & xlsxX14SparklineGroups {}
511
+ }
512
+ groups .XMLNSXM = NameSpaceSpreadSheetExcel2006Main .Value
513
+ groups .Content = decodeSparklineGroups .Content + string (sparklineGroupBytes )
514
+ sparklineGroupsBytes , _ = xml .Marshal (groups )
515
+ decodeExtLst .Ext [idx ].Content = string (sparklineGroupsBytes )
516
+ appendMode = true
534
517
}
535
- decodeExtLst .Ext [idx ].Content = string (sparklineGroupsBytes )
536
518
}
537
519
}
538
- if extLstBytes , err = xml .Marshal (decodeExtLst ); err != nil {
539
- return err
540
- }
541
- ws .ExtLst = & xlsxExtLst {
542
- Ext : strings .TrimSuffix (strings .TrimPrefix (string (extLstBytes ), "<extLst>" ), "</extLst>" ),
520
+ if ! appendMode {
521
+ sparklineGroupsBytes , _ = xml .Marshal (& xlsxX14SparklineGroups {
522
+ XMLNSXM : NameSpaceSpreadSheetExcel2006Main .Value ,
523
+ SparklineGroups : []* xlsxX14SparklineGroup {group },
524
+ })
525
+ decodeExtLst .Ext = append (decodeExtLst .Ext , & xlsxWorksheetExt {
526
+ URI : ExtURISparklineGroups , Content : string (sparklineGroupsBytes ),
527
+ })
543
528
}
529
+ sort .Slice (decodeExtLst .Ext , func (i , j int ) bool {
530
+ return inStrSlice (extensionURIPriority , decodeExtLst .Ext [i ].URI , false ) <
531
+ inStrSlice (extensionURIPriority , decodeExtLst .Ext [j ].URI , false )
532
+ })
533
+ extLstBytes , err = xml .Marshal (decodeExtLst )
534
+ ws .ExtLst = & xlsxExtLst {Ext : strings .TrimSuffix (strings .TrimPrefix (string (extLstBytes ), "<extLst>" ), "</extLst>" )}
544
535
return err
545
536
}
0 commit comments