@@ -33,6 +33,8 @@ import (
33
33
// PivotStyleMedium1 - PivotStyleMedium28
34
34
// PivotStyleDark1 - PivotStyleDark28
35
35
type PivotTableOptions struct {
36
+ pivotTableXML string
37
+ pivotCacheXML string
36
38
pivotTableSheetName string
37
39
DataRange string
38
40
PivotTableRange string
@@ -286,7 +288,7 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
286
288
SaveData : false ,
287
289
RefreshOnLoad : true ,
288
290
CreatedVersion : pivotTableVersion ,
289
- RefreshedVersion : pivotTableVersion ,
291
+ RefreshedVersion : pivotTableRefreshedVersion ,
290
292
MinRefreshableVersion : pivotTableVersion ,
291
293
CacheSource : & xlsxCacheSource {
292
294
Type : "worksheet" ,
@@ -301,23 +303,9 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
301
303
pc .CacheSource .WorksheetSource = & xlsxWorksheetSource {Name : opts .DataRange }
302
304
}
303
305
for _ , name := range order {
304
- rowOptions , rowOk := f .getPivotTableFieldOptions (name , opts .Rows )
305
- columnOptions , colOk := f .getPivotTableFieldOptions (name , opts .Columns )
306
- sharedItems := xlsxSharedItems {
307
- Count : 0 ,
308
- }
309
- s := xlsxString {}
310
- if (rowOk && ! rowOptions .DefaultSubtotal ) || (colOk && ! columnOptions .DefaultSubtotal ) {
311
- s = xlsxString {
312
- V : "" ,
313
- }
314
- sharedItems .Count ++
315
- sharedItems .S = & s
316
- }
317
-
318
306
pc .CacheFields .CacheField = append (pc .CacheFields .CacheField , & xlsxCacheField {
319
307
Name : name ,
320
- SharedItems : & sharedItems ,
308
+ SharedItems : & xlsxSharedItems { ContainsBlank : true , M : [] xlsxMissing {{}}} ,
321
309
})
322
310
}
323
311
pc .CacheFields .Count = len (pc .CacheFields .CacheField )
@@ -349,13 +337,13 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
349
337
CacheID : cacheID ,
350
338
RowGrandTotals : & opts .RowGrandTotals ,
351
339
ColGrandTotals : & opts .ColGrandTotals ,
352
- UpdatedVersion : pivotTableVersion ,
340
+ UpdatedVersion : pivotTableRefreshedVersion ,
353
341
MinRefreshableVersion : pivotTableVersion ,
354
342
ShowDrill : & opts .ShowDrill ,
355
343
UseAutoFormatting : & opts .UseAutoFormatting ,
356
344
PageOverThenDown : & opts .PageOverThenDown ,
357
345
MergeItem : & opts .MergeItem ,
358
- CreatedVersion : pivotTableVersion ,
346
+ CreatedVersion : 3 ,
359
347
CompactData : & opts .CompactData ,
360
348
ShowError : & opts .ShowError ,
361
349
DataCaption : "Values" ,
@@ -788,6 +776,8 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
788
776
}
789
777
dataRange := fmt .Sprintf ("%s!%s" , pc .CacheSource .WorksheetSource .Sheet , pc .CacheSource .WorksheetSource .Ref )
790
778
opts = PivotTableOptions {
779
+ pivotTableXML : pivotTableXML ,
780
+ pivotCacheXML : pivotCacheXML ,
791
781
pivotTableSheetName : sheet ,
792
782
DataRange : dataRange ,
793
783
PivotTableRange : fmt .Sprintf ("%s!%s" , sheet , pt .Location .Ref ),
@@ -886,3 +876,33 @@ func extractPivotTableField(data string, fld *xlsxPivotField) PivotTableField {
886
876
}
887
877
return pivotTableField
888
878
}
879
+
880
+ // genPivotCacheDefinitionID generates a unique pivot table cache definition ID.
881
+ func (f * File ) genPivotCacheDefinitionID () int {
882
+ var (
883
+ ID int
884
+ decodeExtLst = new (decodeExtLst )
885
+ decodeX14PivotCacheDefinition = new (decodeX14PivotCacheDefinition )
886
+ )
887
+ f .Pkg .Range (func (k , v interface {}) bool {
888
+ if strings .Contains (k .(string ), "xl/pivotCache/pivotCacheDefinition" ) {
889
+ pc , err := f .pivotCacheReader (k .(string ))
890
+ if err != nil {
891
+ return true
892
+ }
893
+ if pc .ExtLst != nil {
894
+ _ = f .xmlNewDecoder (strings .NewReader ("<extLst>" + pc .ExtLst .Ext + "</extLst>" )).Decode (decodeExtLst )
895
+ for _ , ext := range decodeExtLst .Ext {
896
+ if ext .URI == ExtURIPivotCacheDefinition {
897
+ _ = f .xmlNewDecoder (strings .NewReader (ext .Content )).Decode (decodeX14PivotCacheDefinition )
898
+ if ID < decodeX14PivotCacheDefinition .PivotCacheID {
899
+ ID = decodeX14PivotCacheDefinition .PivotCacheID
900
+ }
901
+ }
902
+ }
903
+ }
904
+ }
905
+ return true
906
+ })
907
+ return ID + 1
908
+ }
0 commit comments