@@ -51,6 +51,7 @@ type PivotTableOptions struct {
5151 UseAutoFormatting bool
5252 PageOverThenDown bool
5353 MergeItem bool
54+ ClassicLayout bool
5455 CompactData bool
5556 ShowError bool
5657 ShowRowHeaders bool
@@ -220,6 +221,9 @@ func (f *File) parseFormatPivotTableSet(opts *PivotTableOptions) (*xlsxWorksheet
220221 if ! ok {
221222 return dataSheet , pivotTableSheetPath , ErrSheetNotExist {pivotTableSheetName }
222223 }
224+ if opts .CompactData && opts .ClassicLayout {
225+ return nil , "" , ErrPivotTableClassicLayout
226+ }
223227 return dataSheet , pivotTableSheetPath , err
224228}
225229
@@ -352,6 +356,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
352356 MergeItem : & opts .MergeItem ,
353357 CreatedVersion : pivotTableVersion ,
354358 CompactData : & opts .CompactData ,
359+ GridDropZones : opts .ClassicLayout ,
355360 ShowError : & opts .ShowError ,
356361 FieldPrintTitles : opts .FieldPrintTitles ,
357362 ItemPrintTitles : opts .ItemPrintTitles ,
@@ -387,6 +392,12 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
387392 if pt .Name == "" {
388393 pt .Name = fmt .Sprintf ("PivotTable%d" , pivotTableID )
389394 }
395+
396+ // set classic layout
397+ if opts .ClassicLayout {
398+ pt .Compact , pt .CompactData = boolPtr (false ), boolPtr (false )
399+ }
400+
390401 // pivot fields
391402 _ = f .addPivotFields (& pt , opts )
392403
@@ -537,6 +548,14 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableO
537548 return err
538549}
539550
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+
540559// addPivotFields create pivot fields based on the column order of the first
541560// row in the data region by given pivot table definition and option.
542561func (f * File ) addPivotFields (pt * xlsxPivotTableDefinition , opts * PivotTableOptions ) error {
@@ -554,8 +573,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
554573 } else {
555574 items = append (items , & xlsxItem {T : "default" })
556575 }
557-
558- pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , & xlsxPivotField {
576+ fld := & xlsxPivotField {
559577 Name : f .getPivotTableFieldName (name , opts .Rows ),
560578 Axis : "axisRow" ,
561579 DataField : inPivotTableField (opts .Data , name ) != - 1 ,
@@ -568,11 +586,13 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
568586 Count : len (items ),
569587 Item : items ,
570588 },
571- })
589+ }
590+ fld .setClassicLayout (opts .ClassicLayout )
591+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
572592 continue
573593 }
574594 if inPivotTableField (opts .Filter , name ) != - 1 {
575- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
595+ fld := & xlsxPivotField {
576596 Axis : "axisPage" ,
577597 DataField : inPivotTableField (opts .Data , name ) != - 1 ,
578598 Name : f .getPivotTableFieldName (name , opts .Columns ),
@@ -582,7 +602,9 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
582602 {T : "default" },
583603 },
584604 },
585- })
605+ }
606+ fld .setClassicLayout (opts .ClassicLayout )
607+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
586608 continue
587609 }
588610 if inPivotTableField (opts .Columns , name ) != - 1 {
@@ -593,7 +615,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
593615 } else {
594616 items = append (items , & xlsxItem {T : "default" })
595617 }
596- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
618+ fld := & xlsxPivotField {
597619 Name : f .getPivotTableFieldName (name , opts .Columns ),
598620 Axis : "axisCol" ,
599621 DataField : inPivotTableField (opts .Data , name ) != - 1 ,
@@ -606,16 +628,22 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
606628 Count : len (items ),
607629 Item : items ,
608630 },
609- })
631+ }
632+ fld .setClassicLayout (opts .ClassicLayout )
633+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
610634 continue
611635 }
612636 if inPivotTableField (opts .Data , name ) != - 1 {
613- pt . PivotFields . PivotField = append ( pt . PivotFields . PivotField , & xlsxPivotField {
637+ fld := & xlsxPivotField {
614638 DataField : true ,
615- })
639+ }
640+ fld .setClassicLayout (opts .ClassicLayout )
641+ pt .PivotFields .PivotField = append (pt .PivotFields .PivotField , fld )
616642 continue
617643 }
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 )
619647 }
620648 return err
621649}
@@ -847,6 +875,7 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
847875 DataRange : fmt .Sprintf ("%s!%s" , pc .CacheSource .WorksheetSource .Sheet , pc .CacheSource .WorksheetSource .Ref ),
848876 PivotTableRange : fmt .Sprintf ("%s!%s" , sheet , pt .Location .Ref ),
849877 Name : pt .Name ,
878+ ClassicLayout : pt .GridDropZones ,
850879 FieldPrintTitles : pt .FieldPrintTitles ,
851880 ItemPrintTitles : pt .ItemPrintTitles ,
852881 }
0 commit comments