Skip to content

Commit c62d23e

Browse files
committed
The AddSlicer function now support create pivot table slicer
1 parent 9c079e5 commit c62d23e

10 files changed

+610
-188
lines changed

pivotTable.go

+38-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
// PivotStyleMedium1 - PivotStyleMedium28
3434
// PivotStyleDark1 - PivotStyleDark28
3535
type PivotTableOptions struct {
36+
pivotTableXML string
37+
pivotCacheXML string
3638
pivotTableSheetName string
3739
DataRange string
3840
PivotTableRange string
@@ -286,7 +288,7 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
286288
SaveData: false,
287289
RefreshOnLoad: true,
288290
CreatedVersion: pivotTableVersion,
289-
RefreshedVersion: pivotTableVersion,
291+
RefreshedVersion: pivotTableRefreshedVersion,
290292
MinRefreshableVersion: pivotTableVersion,
291293
CacheSource: &xlsxCacheSource{
292294
Type: "worksheet",
@@ -301,23 +303,9 @@ func (f *File) addPivotCache(pivotCacheXML string, opts *PivotTableOptions) erro
301303
pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opts.DataRange}
302304
}
303305
for _, name := range order {
304-
rowOptions, rowOk := f.getPivotTableFieldOptions(name, opts.Rows)
305-
columnOptions, colOk := f.getPivotTableFieldOptions(name, opts.Columns)
306-
sharedItems := xlsxSharedItems{
307-
Count: 0,
308-
}
309-
s := xlsxString{}
310-
if (rowOk && !rowOptions.DefaultSubtotal) || (colOk && !columnOptions.DefaultSubtotal) {
311-
s = xlsxString{
312-
V: "",
313-
}
314-
sharedItems.Count++
315-
sharedItems.S = &s
316-
}
317-
318306
pc.CacheFields.CacheField = append(pc.CacheFields.CacheField, &xlsxCacheField{
319307
Name: name,
320-
SharedItems: &sharedItems,
308+
SharedItems: &xlsxSharedItems{ContainsBlank: true, M: []xlsxMissing{{}}},
321309
})
322310
}
323311
pc.CacheFields.Count = len(pc.CacheFields.CacheField)
@@ -349,13 +337,13 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
349337
CacheID: cacheID,
350338
RowGrandTotals: &opts.RowGrandTotals,
351339
ColGrandTotals: &opts.ColGrandTotals,
352-
UpdatedVersion: pivotTableVersion,
340+
UpdatedVersion: pivotTableRefreshedVersion,
353341
MinRefreshableVersion: pivotTableVersion,
354342
ShowDrill: &opts.ShowDrill,
355343
UseAutoFormatting: &opts.UseAutoFormatting,
356344
PageOverThenDown: &opts.PageOverThenDown,
357345
MergeItem: &opts.MergeItem,
358-
CreatedVersion: pivotTableVersion,
346+
CreatedVersion: 3,
359347
CompactData: &opts.CompactData,
360348
ShowError: &opts.ShowError,
361349
DataCaption: "Values",
@@ -788,6 +776,8 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
788776
}
789777
dataRange := fmt.Sprintf("%s!%s", pc.CacheSource.WorksheetSource.Sheet, pc.CacheSource.WorksheetSource.Ref)
790778
opts = PivotTableOptions{
779+
pivotTableXML: pivotTableXML,
780+
pivotCacheXML: pivotCacheXML,
791781
pivotTableSheetName: sheet,
792782
DataRange: dataRange,
793783
PivotTableRange: fmt.Sprintf("%s!%s", sheet, pt.Location.Ref),
@@ -886,3 +876,33 @@ func extractPivotTableField(data string, fld *xlsxPivotField) PivotTableField {
886876
}
887877
return pivotTableField
888878
}
879+
880+
// genPivotCacheDefinitionID generates a unique pivot table cache definition ID.
881+
func (f *File) genPivotCacheDefinitionID() int {
882+
var (
883+
ID int
884+
decodeExtLst = new(decodeExtLst)
885+
decodeX14PivotCacheDefinition = new(decodeX14PivotCacheDefinition)
886+
)
887+
f.Pkg.Range(func(k, v interface{}) bool {
888+
if strings.Contains(k.(string), "xl/pivotCache/pivotCacheDefinition") {
889+
pc, err := f.pivotCacheReader(k.(string))
890+
if err != nil {
891+
return true
892+
}
893+
if pc.ExtLst != nil {
894+
_ = f.xmlNewDecoder(strings.NewReader("<extLst>" + pc.ExtLst.Ext + "</extLst>")).Decode(decodeExtLst)
895+
for _, ext := range decodeExtLst.Ext {
896+
if ext.URI == ExtURIPivotCacheDefinition {
897+
_ = f.xmlNewDecoder(strings.NewReader(ext.Content)).Decode(decodeX14PivotCacheDefinition)
898+
if ID < decodeX14PivotCacheDefinition.PivotCacheID {
899+
ID = decodeX14PivotCacheDefinition.PivotCacheID
900+
}
901+
}
902+
}
903+
}
904+
}
905+
return true
906+
})
907+
return ID + 1
908+
}

pivotTable_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func TestPivotTable(t *testing.T) {
2626
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)]))
2727
}
2828
expected := &PivotTableOptions{
29+
pivotTableXML: "xl/pivotTables/pivotTable1.xml",
30+
pivotCacheXML: "xl/pivotCache/pivotCacheDefinition1.xml",
2931
DataRange: "Sheet1!A1:E31",
3032
PivotTableRange: "Sheet1!G2:M34",
3133
Name: "PivotTable1",
@@ -374,5 +376,19 @@ func TestGetPivotFieldsOrder(t *testing.T) {
374376

375377
func TestGetPivotTableFieldName(t *testing.T) {
376378
f := NewFile()
377-
f.getPivotTableFieldName("-", []PivotTableField{})
379+
assert.Empty(t, f.getPivotTableFieldName("-", []PivotTableField{}))
380+
}
381+
382+
func TestGetPivotTableFieldOptions(t *testing.T) {
383+
f := NewFile()
384+
_, ok := f.getPivotTableFieldOptions("-", []PivotTableField{})
385+
assert.False(t, ok)
386+
}
387+
388+
func TestGenPivotCacheDefinitionID(t *testing.T) {
389+
f := NewFile()
390+
// Test generate pivot table cache definition ID with unsupported charset
391+
f.Pkg.Store("xl/pivotCache/pivotCacheDefinition1.xml", MacintoshCyrillicCharset)
392+
assert.Equal(t, 1, f.genPivotCacheDefinitionID())
393+
assert.NoError(t, f.Close())
378394
}

0 commit comments

Comments
 (0)