@@ -21,12 +21,25 @@ import (
21
21
22
22
// PivotTableOption directly maps the format settings of the pivot table.
23
23
type PivotTableOption struct {
24
- DataRange string
25
- PivotTableRange string
26
- Rows []PivotTableField
27
- Columns []PivotTableField
28
- Data []PivotTableField
29
- Filter []PivotTableField
24
+ DataRange string
25
+ PivotTableRange string
26
+ Rows []PivotTableField
27
+ Columns []PivotTableField
28
+ Data []PivotTableField
29
+ Filter []PivotTableField
30
+ RowGrandTotals bool
31
+ ColGrandTotals bool
32
+ ShowDrill bool
33
+ UseAutoFormatting bool
34
+ PageOverThenDown bool
35
+ MergeItem bool
36
+ CompactData bool
37
+ ShowRowHeaders bool
38
+ ShowColHeaders bool
39
+ ShowRowStripes bool
40
+ ShowColStripes bool
41
+ ShowLastColumn bool
42
+ PivotTableStyleName string
30
43
}
31
44
32
45
// PivotTableField directly maps the field settings of the pivot table.
@@ -49,9 +62,10 @@ type PivotTableOption struct {
49
62
// Name specifies the name of the data field. Maximum 255 characters
50
63
// are allowed in data field name, excess characters will be truncated.
51
64
type PivotTableField struct {
52
- Data string
53
- Name string
54
- Subtotal string
65
+ Data string
66
+ Name string
67
+ Subtotal string
68
+ DefaultSubtotal bool
55
69
}
56
70
57
71
// AddPivotTable provides the method to add pivot table by given pivot table
@@ -233,12 +247,25 @@ func (f *File) addPivotCache(pivotCacheID int, pivotCacheXML string, opt *PivotT
233
247
},
234
248
CacheFields : & xlsxCacheFields {},
235
249
}
250
+
236
251
for _ , name := range order {
252
+ defaultRowsSubtotal , rowOk := f .getPivotTableFieldNameDefaultSubtotal (name , opt .Rows )
253
+ defaultColumnsSubtotal , colOk := f .getPivotTableFieldNameDefaultSubtotal (name , opt .Columns )
254
+ sharedItems := xlsxSharedItems {
255
+ Count : 0 ,
256
+ }
257
+ s := xlsxString {}
258
+ if (rowOk && ! defaultRowsSubtotal ) || (colOk && ! defaultColumnsSubtotal ) {
259
+ s = xlsxString {
260
+ V : "" ,
261
+ }
262
+ sharedItems .Count ++
263
+ sharedItems .S = & s
264
+ }
265
+
237
266
pc .CacheFields .CacheField = append (pc .CacheFields .CacheField , & xlsxCacheField {
238
- Name : name ,
239
- SharedItems : & xlsxSharedItems {
240
- Count : 0 ,
241
- },
267
+ Name : name ,
268
+ SharedItems : & sharedItems ,
242
269
})
243
270
}
244
271
pc .CacheFields .Count = len (pc .CacheFields .CacheField )
@@ -259,10 +286,24 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
259
286
hcell , _ := CoordinatesToCellName (coordinates [0 ], coordinates [1 ])
260
287
vcell , _ := CoordinatesToCellName (coordinates [2 ], coordinates [3 ])
261
288
289
+ pivotTableStyle := func () string {
290
+ if opt .PivotTableStyleName == "" {
291
+ return "PivotStyleLight16"
292
+ } else {
293
+ return opt .PivotTableStyleName
294
+ }
295
+ }
262
296
pt := xlsxPivotTableDefinition {
263
- Name : fmt .Sprintf ("Pivot Table%d" , pivotTableID ),
264
- CacheID : cacheID ,
265
- DataCaption : "Values" ,
297
+ Name : fmt .Sprintf ("Pivot Table%d" , pivotTableID ),
298
+ CacheID : cacheID ,
299
+ RowGrandTotals : & opt .RowGrandTotals ,
300
+ ColGrandTotals : & opt .ColGrandTotals ,
301
+ ShowDrill : & opt .ShowDrill ,
302
+ UseAutoFormatting : & opt .UseAutoFormatting ,
303
+ PageOverThenDown : & opt .PageOverThenDown ,
304
+ MergeItem : & opt .MergeItem ,
305
+ CompactData : & opt .CompactData ,
306
+ DataCaption : "Values" ,
266
307
Location : & xlsxLocation {
267
308
Ref : hcell + ":" + vcell ,
268
309
FirstDataCol : 1 ,
@@ -283,10 +324,12 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
283
324
I : []* xlsxI {{}},
284
325
},
285
326
PivotTableStyleInfo : & xlsxPivotTableStyleInfo {
286
- Name : "PivotStyleLight16" ,
287
- ShowRowHeaders : true ,
288
- ShowColHeaders : true ,
289
- ShowLastColumn : true ,
327
+ Name : pivotTableStyle (),
328
+ ShowRowHeaders : opt .ShowRowHeaders ,
329
+ ShowColHeaders : opt .ShowColHeaders ,
330
+ ShowRowStripes : opt .ShowRowStripes ,
331
+ ShowColStripes : opt .ShowColStripes ,
332
+ ShowLastColumn : opt .ShowLastColumn ,
290
333
},
291
334
}
292
335
@@ -440,17 +483,25 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio
440
483
if err != nil {
441
484
return err
442
485
}
486
+ x := 0
443
487
for _ , name := range order {
444
488
if inPivotTableField (opt .Rows , name ) != - 1 {
489
+ defaultSubtotal , ok := f .getPivotTableFieldNameDefaultSubtotal (name , opt .Rows )
490
+ var items []* xlsxItem
491
+ if ! ok || ! defaultSubtotal {
492
+ items = append (items , & xlsxItem {X : & x })
493
+ } else {
494
+ items = append (items , & xlsxItem {T : "default" })
495
+ }
496
+
445
497
pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , & xlsxPivotField {
446
498
Axis : "axisRow" ,
447
499
Name : f .getPivotTableFieldName (name , opt .Rows ),
448
500
Items : & xlsxItems {
449
- Count : 1 ,
450
- Item : []* xlsxItem {
451
- {T : "default" },
452
- },
501
+ Count : len (items ),
502
+ Item : items ,
453
503
},
504
+ DefaultSubtotal : & defaultSubtotal ,
454
505
})
455
506
continue
456
507
}
@@ -468,15 +519,21 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio
468
519
continue
469
520
}
470
521
if inPivotTableField (opt .Columns , name ) != - 1 {
522
+ defaultSubtotal , ok := f .getPivotTableFieldNameDefaultSubtotal (name , opt .Columns )
523
+ var items []* xlsxItem
524
+ if ! ok || ! defaultSubtotal {
525
+ items = append (items , & xlsxItem {X : & x })
526
+ } else {
527
+ items = append (items , & xlsxItem {T : "default" })
528
+ }
471
529
pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , & xlsxPivotField {
472
530
Axis : "axisCol" ,
473
531
Name : f .getPivotTableFieldName (name , opt .Columns ),
474
532
Items : & xlsxItems {
475
- Count : 1 ,
476
- Item : []* xlsxItem {
477
- {T : "default" },
478
- },
533
+ Count : len (items ),
534
+ Item : items ,
479
535
},
536
+ DefaultSubtotal : & defaultSubtotal ,
480
537
})
481
538
continue
482
539
}
@@ -574,6 +631,15 @@ func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) str
574
631
return ""
575
632
}
576
633
634
+ func (f * File ) getPivotTableFieldNameDefaultSubtotal (name string , fields []PivotTableField ) (bool , bool ) {
635
+ for _ , field := range fields {
636
+ if field .Data == name {
637
+ return field .DefaultSubtotal , true
638
+ }
639
+ }
640
+ return false , false
641
+ }
642
+
577
643
// addWorkbookPivotCache add the association ID of the pivot cache in xl/workbook.xml.
578
644
func (f * File ) addWorkbookPivotCache (RID int ) int {
579
645
wb := f .workbookReader ()
0 commit comments