@@ -62,6 +62,10 @@ type PivotTableOptions struct {
62
62
}
63
63
64
64
// PivotTableField directly maps the field settings of the pivot table.
65
+ //
66
+ // Name specifies the name of the data field. Maximum 255 characters
67
+ // are allowed in data field name, excess characters will be truncated.
68
+ //
65
69
// Subtotal specifies the aggregation function that applies to this data
66
70
// field. The default value is sum. The possible values for this attribute
67
71
// are:
@@ -78,15 +82,17 @@ type PivotTableOptions struct {
78
82
// Var
79
83
// Varp
80
84
//
81
- // Name specifies the name of the data field. Maximum 255 characters
82
- // are allowed in data field name, excess characters will be truncated.
85
+ // NumFmt specifies the number format ID of the data field, this filed only
86
+ // accepts built-in number format ID and does not support custom number format
87
+ // expression currently.
83
88
type PivotTableField struct {
84
89
Compact bool
85
90
Data string
86
91
Name string
87
92
Outline bool
88
93
Subtotal string
89
94
DefaultSubtotal bool
95
+ NumFmt int
90
96
}
91
97
92
98
// AddPivotTable provides the method to add pivot table by given pivot table
@@ -452,6 +458,7 @@ func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opts *PivotTable
452
458
}
453
459
dataFieldsSubtotals := f .getPivotTableFieldsSubtotal (opts .Data )
454
460
dataFieldsName := f .getPivotTableFieldsName (opts .Data )
461
+ dataFieldsNumFmtID := f .getPivotTableFieldsNumFmtID (opts .Data )
455
462
for idx , dataField := range dataFieldsIndex {
456
463
if pt .DataFields == nil {
457
464
pt .DataFields = & xlsxDataFields {}
@@ -460,6 +467,7 @@ func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opts *PivotTable
460
467
Name : dataFieldsName [idx ],
461
468
Fld : dataField ,
462
469
Subtotal : dataFieldsSubtotals [idx ],
470
+ NumFmtID : dataFieldsNumFmtID [idx ],
463
471
})
464
472
}
465
473
@@ -687,6 +695,22 @@ func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) str
687
695
return ""
688
696
}
689
697
698
+ // getPivotTableFieldsNumFmtID prepare fields number format ID by given pivot
699
+ // table fields.
700
+ func (f * File ) getPivotTableFieldsNumFmtID (fields []PivotTableField ) []int {
701
+ field := make ([]int , len (fields ))
702
+ for idx , fld := range fields {
703
+ if _ , ok := builtInNumFmt [fld .NumFmt ]; ok {
704
+ field [idx ] = fld .NumFmt
705
+ continue
706
+ }
707
+ if (27 <= fld .NumFmt && fld .NumFmt <= 36 ) || (50 <= fld .NumFmt && fld .NumFmt <= 81 ) {
708
+ field [idx ] = fld .NumFmt
709
+ }
710
+ }
711
+ return field
712
+ }
713
+
690
714
// getPivotTableFieldOptions return options for specific field by given field name.
691
715
func (f * File ) getPivotTableFieldOptions (name string , fields []PivotTableField ) (options PivotTableField , ok bool ) {
692
716
for _ , field := range fields {
@@ -761,12 +785,11 @@ func (f *File) getPivotTableDataRange(opts *PivotTableOptions) error {
761
785
opts .pivotDataRange = opts .DataRange
762
786
return nil
763
787
}
764
- for _ , sheetName := range f .GetSheetList () {
765
- tables , err := f .GetTables (sheetName )
766
- e := ErrSheetNotExist {sheetName }
767
- if err != nil && err .Error () != newNotWorksheetError (sheetName ).Error () && err .Error () != e .Error () {
768
- return err
769
- }
788
+ tbls , err := f .getTables ()
789
+ if err != nil {
790
+ return err
791
+ }
792
+ for sheetName , tables := range tbls {
770
793
for _ , table := range tables {
771
794
if table .Name == opts .DataRange {
772
795
opts .pivotDataRange , opts .namedDataRange = fmt .Sprintf ("%s!%s" , sheetName , table .Range ), true
@@ -891,6 +914,7 @@ func (f *File) extractPivotTableFields(order []string, pt *xlsxPivotTableDefinit
891
914
Data : order [field .Fld ],
892
915
Name : field .Name ,
893
916
Subtotal : cases .Title (language .English ).String (field .Subtotal ),
917
+ NumFmt : field .NumFmtID ,
894
918
})
895
919
}
896
920
}
@@ -991,8 +1015,8 @@ func (f *File) DeletePivotTable(sheet, name string) error {
991
1015
return err
992
1016
}
993
1017
pivotTableCaches := map [string ]int {}
994
- for _ , sheetName := range f . GetSheetList () {
995
- sheetPivotTables , _ := f . GetPivotTables ( sheetName )
1018
+ pivotTables , _ := f . getPivotTables ()
1019
+ for _ , sheetPivotTables := range pivotTables {
996
1020
for _ , sheetPivotTable := range sheetPivotTables {
997
1021
pivotTableCaches [sheetPivotTable .pivotCacheXML ]++
998
1022
}
@@ -1013,3 +1037,17 @@ func (f *File) DeletePivotTable(sheet, name string) error {
1013
1037
}
1014
1038
return newNoExistTableError (name )
1015
1039
}
1040
+
1041
+ // getPivotTables provides a function to get all pivot tables in a workbook.
1042
+ func (f * File ) getPivotTables () (map [string ][]PivotTableOptions , error ) {
1043
+ pivotTables := map [string ][]PivotTableOptions {}
1044
+ for _ , sheetName := range f .GetSheetList () {
1045
+ pts , err := f .GetPivotTables (sheetName )
1046
+ e := ErrSheetNotExist {sheetName }
1047
+ if err != nil && err .Error () != newNotWorksheetError (sheetName ).Error () && err .Error () != e .Error () {
1048
+ return pivotTables , err
1049
+ }
1050
+ pivotTables [sheetName ] = append (pivotTables [sheetName ], pts ... )
1051
+ }
1052
+ return pivotTables , nil
1053
+ }
0 commit comments