Skip to content

Commit

Permalink
The AddSlicer function now support create pivot table slicer
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 26, 2023
1 parent 9c079e5 commit c62d23e
Show file tree
Hide file tree
Showing 10 changed files with 610 additions and 188 deletions.
56 changes: 38 additions & 18 deletions pivotTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
// PivotStyleMedium1 - PivotStyleMedium28
// PivotStyleDark1 - PivotStyleDark28
type PivotTableOptions struct {
pivotTableXML string
pivotCacheXML string
pivotTableSheetName string
DataRange string
PivotTableRange string
Expand Down Expand Up @@ -286,7 +288,7 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
SaveData: false,
RefreshOnLoad: true,
CreatedVersion: pivotTableVersion,
RefreshedVersion: pivotTableVersion,
RefreshedVersion: pivotTableRefreshedVersion,
MinRefreshableVersion: pivotTableVersion,
CacheSource: &xlsxCacheSource{
Type: "worksheet",
Expand All @@ -301,23 +303,9 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opts.DataRange}
}
for _, name := range order {
rowOptions, rowOk := f.getPivotTableFieldOptions(name, opts.Rows)
columnOptions, colOk := f.getPivotTableFieldOptions(name, opts.Columns)
sharedItems := xlsxSharedItems{
Count: 0,
}
s := xlsxString{}
if (rowOk && !rowOptions.DefaultSubtotal) || (colOk && !columnOptions.DefaultSubtotal) {
s = xlsxString{
V: "",
}
sharedItems.Count++
sharedItems.S = &s
}

pc.CacheFields.CacheField = append(pc.CacheFields.CacheField, &xlsxCacheField{
Name: name,
SharedItems: &sharedItems,
SharedItems: &xlsxSharedItems{ContainsBlank: true, M: []xlsxMissing{{}}},
})
}
pc.CacheFields.Count = len(pc.CacheFields.CacheField)
Expand Down Expand Up @@ -349,13 +337,13 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
CacheID: cacheID,
RowGrandTotals: &opts.RowGrandTotals,
ColGrandTotals: &opts.ColGrandTotals,
UpdatedVersion: pivotTableVersion,
UpdatedVersion: pivotTableRefreshedVersion,
MinRefreshableVersion: pivotTableVersion,
ShowDrill: &opts.ShowDrill,
UseAutoFormatting: &opts.UseAutoFormatting,
PageOverThenDown: &opts.PageOverThenDown,
MergeItem: &opts.MergeItem,
CreatedVersion: pivotTableVersion,
CreatedVersion: 3,
CompactData: &opts.CompactData,
ShowError: &opts.ShowError,
DataCaption: "Values",
Expand Down Expand Up @@ -788,6 +776,8 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
}
dataRange := fmt.Sprintf("%s!%s", pc.CacheSource.WorksheetSource.Sheet, pc.CacheSource.WorksheetSource.Ref)
opts = PivotTableOptions{
pivotTableXML: pivotTableXML,
pivotCacheXML: pivotCacheXML,
pivotTableSheetName: sheet,
DataRange: dataRange,
PivotTableRange: fmt.Sprintf("%s!%s", sheet, pt.Location.Ref),
Expand Down Expand Up @@ -886,3 +876,33 @@ func extractPivotTableField(data string, fld *xlsxPivotField) PivotTableField {
}
return pivotTableField
}

// genPivotCacheDefinitionID generates a unique pivot table cache definition ID.
func (f *File) genPivotCacheDefinitionID() int {
var (
ID int
decodeExtLst = new(decodeExtLst)
decodeX14PivotCacheDefinition = new(decodeX14PivotCacheDefinition)
)
f.Pkg.Range(func(k, v interface{}) bool {
if strings.Contains(k.(string), "xl/pivotCache/pivotCacheDefinition") {
pc, err := f.pivotCacheReader(k.(string))
if err != nil {
return true
}
if pc.ExtLst != nil {
_ = f.xmlNewDecoder(strings.NewReader("<extLst>" + pc.ExtLst.Ext + "</extLst>")).Decode(decodeExtLst)
for _, ext := range decodeExtLst.Ext {
if ext.URI == ExtURIPivotCacheDefinition {
_ = f.xmlNewDecoder(strings.NewReader(ext.Content)).Decode(decodeX14PivotCacheDefinition)
if ID < decodeX14PivotCacheDefinition.PivotCacheID {
ID = decodeX14PivotCacheDefinition.PivotCacheID
}
}
}
}
}
return true
})
return ID + 1
}
18 changes: 17 additions & 1 deletion pivotTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestPivotTable(t *testing.T) {
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)]))
}
expected := &PivotTableOptions{
pivotTableXML: "xl/pivotTables/pivotTable1.xml",
pivotCacheXML: "xl/pivotCache/pivotCacheDefinition1.xml",
DataRange: "Sheet1!A1:E31",
PivotTableRange: "Sheet1!G2:M34",
Name: "PivotTable1",
Expand Down Expand Up @@ -374,5 +376,19 @@ func TestGetPivotFieldsOrder(t *testing.T) {

func TestGetPivotTableFieldName(t *testing.T) {
f := NewFile()
f.getPivotTableFieldName("-", []PivotTableField{})
assert.Empty(t, f.getPivotTableFieldName("-", []PivotTableField{}))
}

func TestGetPivotTableFieldOptions(t *testing.T) {
f := NewFile()
_, ok := f.getPivotTableFieldOptions("-", []PivotTableField{})
assert.False(t, ok)
}

func TestGenPivotCacheDefinitionID(t *testing.T) {
f := NewFile()
// Test generate pivot table cache definition ID with unsupported charset
f.Pkg.Store("xl/pivotCache/pivotCacheDefinition1.xml", MacintoshCyrillicCharset)
assert.Equal(t, 1, f.genPivotCacheDefinitionID())
assert.NoError(t, f.Close())
}
Loading

0 comments on commit c62d23e

Please sign in to comment.