Skip to content

Commit

Permalink
Add options to set classic layout for pivot table
Browse files Browse the repository at this point in the history
  • Loading branch information
Zncl2222 committed Sep 19, 2024
1 parent 02189fb commit 5f7a4ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
35 changes: 24 additions & 11 deletions pivotTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type PivotTableOptions struct {
ShowLastColumn bool
FieldPrintTitles bool
ItemPrintTitles bool
ClassicLayout bool
PivotTableStyleName string
}

Expand Down Expand Up @@ -387,6 +388,14 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
if pt.Name == "" {
pt.Name = fmt.Sprintf("PivotTable%d", pivotTableID)
}

// set classic layout
if opts.ClassicLayout {
pt.CompactData = boolPtr(false)
pt.Compact = boolPtr(false)
pt.GridDropZones = opts.ClassicLayout
}

// pivot fields
_ = f.addPivotFields(&pt, opts)

Expand Down Expand Up @@ -537,6 +546,15 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableO
return err
}

// setClassicLayout provides a method to set classic layout for pivot table by
// setting Compact and Outline to false.
func setClassicLayout(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) {
if opts.ClassicLayout {
pt.PivotFields.PivotField[len(pt.PivotFields.PivotField)-1].Compact = boolPtr(false)
pt.PivotFields.PivotField[len(pt.PivotFields.PivotField)-1].Outline = boolPtr(false)
}
}

// addPivotFields create pivot fields based on the column order of the first
// row in the data region by given pivot table definition and option.
func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
Expand Down Expand Up @@ -569,9 +587,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
Item: items,
},
})
continue
}
if inPivotTableField(opts.Filter, name) != -1 {
} else if inPivotTableField(opts.Filter, name) != -1 {
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
Axis: "axisPage",
DataField: inPivotTableField(opts.Data, name) != -1,
Expand All @@ -583,9 +599,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
},
},
})
continue
}
if inPivotTableField(opts.Columns, name) != -1 {
} else if inPivotTableField(opts.Columns, name) != -1 {
columnOptions, ok := f.getPivotTableFieldOptions(name, opts.Columns)
var items []*xlsxItem
if !ok || !columnOptions.DefaultSubtotal {
Expand All @@ -607,15 +621,14 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
Item: items,
},
})
continue
}
if inPivotTableField(opts.Data, name) != -1 {
} else if inPivotTableField(opts.Data, name) != -1 {
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
DataField: true,
})
continue
} else {
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{})
}
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{})
setClassicLayout(pt, opts)
}
return err
}
Expand Down
16 changes: 16 additions & 0 deletions pivotTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ func TestPivotTable(t *testing.T) {
ShowLastColumn: true,
}))

// Test classic layout pivot table
assert.NoError(t, f.AddPivotTable(&PivotTableOptions{
DataRange: "dataRange",
PivotTableRange: "Sheet2!A65:AJ100",
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Type"}},
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Sum of Sales", NumFmt: -1}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales", NumFmt: 38}},
RowGrandTotals: true,
ColGrandTotals: true,
ShowDrill: true,
ShowRowHeaders: true,
ShowColHeaders: true,
ShowLastColumn: true,
ClassicLayout: true,
}))

// Test empty pivot table options
assert.Equal(t, ErrParameterRequired, f.AddPivotTable(nil))
// Test add pivot table with custom name which exceeds the max characters limit
Expand Down

0 comments on commit 5f7a4ce

Please sign in to comment.