@@ -51,6 +51,7 @@ type PivotTableOptions struct {
51
51
UseAutoFormatting bool
52
52
PageOverThenDown bool
53
53
MergeItem bool
54
+ ClassicLayout bool
54
55
CompactData bool
55
56
ShowError bool
56
57
ShowRowHeaders bool
@@ -220,6 +221,9 @@ func (f *File) parseFormatPivotTableSet(opts *PivotTableOptions) (*xlsxWorksheet
220
221
if ! ok {
221
222
return dataSheet , pivotTableSheetPath , ErrSheetNotExist {pivotTableSheetName }
222
223
}
224
+ if opts .CompactData && opts .ClassicLayout {
225
+ return nil , "" , ErrPivotTableClassicLayout
226
+ }
223
227
return dataSheet , pivotTableSheetPath , err
224
228
}
225
229
@@ -352,6 +356,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
352
356
MergeItem : & opts .MergeItem ,
353
357
CreatedVersion : pivotTableVersion ,
354
358
CompactData : & opts .CompactData ,
359
+ GridDropZones : opts .ClassicLayout ,
355
360
ShowError : & opts .ShowError ,
356
361
FieldPrintTitles : opts .FieldPrintTitles ,
357
362
ItemPrintTitles : opts .ItemPrintTitles ,
@@ -387,6 +392,12 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
387
392
if pt .Name == "" {
388
393
pt .Name = fmt .Sprintf ("PivotTable%d" , pivotTableID )
389
394
}
395
+
396
+ // set classic layout
397
+ if opts .ClassicLayout {
398
+ pt .Compact , pt .CompactData = boolPtr (false ), boolPtr (false )
399
+ }
400
+
390
401
// pivot fields
391
402
_ = f .addPivotFields (& pt , opts )
392
403
@@ -537,6 +548,14 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableO
537
548
return err
538
549
}
539
550
551
+ // setClassicLayout provides a method to set classic layout for pivot table by
552
+ // setting Compact and Outline to false.
553
+ func (fld * xlsxPivotField ) setClassicLayout (classicLayout bool ) {
554
+ if classicLayout {
555
+ fld .Compact , fld .Outline = boolPtr (false ), boolPtr (false )
556
+ }
557
+ }
558
+
540
559
// addPivotFields create pivot fields based on the column order of the first
541
560
// row in the data region by given pivot table definition and option.
542
561
func (f * File ) addPivotFields (pt * xlsxPivotTableDefinition , opts * PivotTableOptions ) error {
@@ -554,8 +573,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
554
573
} else {
555
574
items = append (items , & xlsxItem {T : "default" })
556
575
}
557
-
558
- pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , & xlsxPivotField {
576
+ fld := & xlsxPivotField {
559
577
Name : f .getPivotTableFieldName (name , opts .Rows ),
560
578
Axis : "axisRow" ,
561
579
DataField : inPivotTableField (opts .Data , name ) != - 1 ,
@@ -568,11 +586,13 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
568
586
Count : len (items ),
569
587
Item : items ,
570
588
},
571
- })
589
+ }
590
+ fld .setClassicLayout (opts .ClassicLayout )
591
+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
572
592
continue
573
593
}
574
594
if inPivotTableField (opts .Filter , name ) != - 1 {
575
- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
595
+ fld := & xlsxPivotField {
576
596
Axis : "axisPage" ,
577
597
DataField : inPivotTableField (opts .Data , name ) != - 1 ,
578
598
Name : f .getPivotTableFieldName (name , opts .Columns ),
@@ -582,7 +602,9 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
582
602
{T : "default" },
583
603
},
584
604
},
585
- })
605
+ }
606
+ fld .setClassicLayout (opts .ClassicLayout )
607
+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
586
608
continue
587
609
}
588
610
if inPivotTableField (opts .Columns , name ) != - 1 {
@@ -593,7 +615,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
593
615
} else {
594
616
items = append (items , & xlsxItem {T : "default" })
595
617
}
596
- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
618
+ fld := & xlsxPivotField {
597
619
Name : f .getPivotTableFieldName (name , opts .Columns ),
598
620
Axis : "axisCol" ,
599
621
DataField : inPivotTableField (opts .Data , name ) != - 1 ,
@@ -606,16 +628,22 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
606
628
Count : len (items ),
607
629
Item : items ,
608
630
},
609
- })
631
+ }
632
+ fld .setClassicLayout (opts .ClassicLayout )
633
+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
610
634
continue
611
635
}
612
636
if inPivotTableField (opts .Data , name ) != - 1 {
613
- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
637
+ fld := & xlsxPivotField {
614
638
DataField : true ,
615
- })
639
+ }
640
+ fld .setClassicLayout (opts .ClassicLayout )
641
+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
616
642
continue
617
643
}
618
- pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , & xlsxPivotField {})
644
+ fld := & xlsxPivotField {}
645
+ fld .setClassicLayout (opts .ClassicLayout )
646
+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
619
647
}
620
648
return err
621
649
}
@@ -847,6 +875,7 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
847
875
DataRange : fmt .Sprintf ("%s!%s" , pc .CacheSource .WorksheetSource .Sheet , pc .CacheSource .WorksheetSource .Ref ),
848
876
PivotTableRange : fmt .Sprintf ("%s!%s" , sheet , pt .Location .Ref ),
849
877
Name : pt .Name ,
878
+ ClassicLayout : pt .GridDropZones ,
850
879
FieldPrintTitles : pt .FieldPrintTitles ,
851
880
ItemPrintTitles : pt .ItemPrintTitles ,
852
881
}
0 commit comments