Skip to content

Commit aa8f6f0

Browse files
committed
This closes qax-os#1029, support specify compact and outline for the pivot table
1 parent 28841af commit aa8f6f0

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

pivotTable.go

+31-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ import (
1919
)
2020

2121
// PivotTableOption directly maps the format settings of the pivot table.
22+
//
23+
// PivotTableStyleName: The built-in pivot table style names
24+
//
25+
// PivotStyleLight1 - PivotStyleLight28
26+
// PivotStyleMedium1 - PivotStyleMedium28
27+
// PivotStyleDark1 - PivotStyleDark28
28+
//
2229
type PivotTableOption struct {
2330
pivotTableSheetName string
2431
DataRange string
@@ -63,8 +70,10 @@ type PivotTableOption struct {
6370
// Name specifies the name of the data field. Maximum 255 characters
6471
// are allowed in data field name, excess characters will be truncated.
6572
type PivotTableField struct {
73+
Compact bool
6674
Data string
6775
Name string
76+
Outline bool
6877
Subtotal string
6978
DefaultSubtotal bool
7079
}
@@ -277,13 +286,13 @@ func (f *File) addPivotCache(pivotCacheID int, pivotCacheXML string, opt *PivotT
277286
pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opt.DataRange}
278287
}
279288
for _, name := range order {
280-
defaultRowsSubtotal, rowOk := f.getPivotTableFieldNameDefaultSubtotal(name, opt.Rows)
281-
defaultColumnsSubtotal, colOk := f.getPivotTableFieldNameDefaultSubtotal(name, opt.Columns)
289+
rowOptions, rowOk := f.getPivotTableFieldOptions(name, opt.Rows)
290+
columnOptions, colOk := f.getPivotTableFieldOptions(name, opt.Columns)
282291
sharedItems := xlsxSharedItems{
283292
Count: 0,
284293
}
285294
s := xlsxString{}
286-
if (rowOk && !defaultRowsSubtotal) || (colOk && !defaultColumnsSubtotal) {
295+
if (rowOk && !rowOptions.DefaultSubtotal) || (colOk && !columnOptions.DefaultSubtotal) {
287296
s = xlsxString{
288297
V: "",
289298
}
@@ -522,22 +531,24 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio
522531
x := 0
523532
for _, name := range order {
524533
if inPivotTableField(opt.Rows, name) != -1 {
525-
defaultSubtotal, ok := f.getPivotTableFieldNameDefaultSubtotal(name, opt.Rows)
534+
rowOptions, ok := f.getPivotTableFieldOptions(name, opt.Rows)
526535
var items []*xlsxItem
527-
if !ok || !defaultSubtotal {
536+
if !ok || !rowOptions.DefaultSubtotal {
528537
items = append(items, &xlsxItem{X: &x})
529538
} else {
530539
items = append(items, &xlsxItem{T: "default"})
531540
}
532541

533542
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
534-
Axis: "axisRow",
535-
Name: f.getPivotTableFieldName(name, opt.Rows),
543+
Name: f.getPivotTableFieldName(name, opt.Rows),
544+
Axis: "axisRow",
545+
Compact: &rowOptions.Compact,
546+
Outline: &rowOptions.Outline,
547+
DefaultSubtotal: &rowOptions.DefaultSubtotal,
536548
Items: &xlsxItems{
537549
Count: len(items),
538550
Item: items,
539551
},
540-
DefaultSubtotal: &defaultSubtotal,
541552
})
542553
continue
543554
}
@@ -555,21 +566,23 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio
555566
continue
556567
}
557568
if inPivotTableField(opt.Columns, name) != -1 {
558-
defaultSubtotal, ok := f.getPivotTableFieldNameDefaultSubtotal(name, opt.Columns)
569+
columnOptions, ok := f.getPivotTableFieldOptions(name, opt.Columns)
559570
var items []*xlsxItem
560-
if !ok || !defaultSubtotal {
571+
if !ok || !columnOptions.DefaultSubtotal {
561572
items = append(items, &xlsxItem{X: &x})
562573
} else {
563574
items = append(items, &xlsxItem{T: "default"})
564575
}
565576
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
566-
Axis: "axisCol",
567-
Name: f.getPivotTableFieldName(name, opt.Columns),
577+
Name: f.getPivotTableFieldName(name, opt.Columns),
578+
Axis: "axisCol",
579+
Compact: &columnOptions.Compact,
580+
Outline: &columnOptions.Outline,
581+
DefaultSubtotal: &columnOptions.DefaultSubtotal,
568582
Items: &xlsxItems{
569583
Count: len(items),
570584
Item: items,
571585
},
572-
DefaultSubtotal: &defaultSubtotal,
573586
})
574587
continue
575588
}
@@ -669,13 +682,15 @@ func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) str
669682
return ""
670683
}
671684

672-
func (f *File) getPivotTableFieldNameDefaultSubtotal(name string, fields []PivotTableField) (bool, bool) {
685+
// getPivotTableFieldOptions return options for specific field by given field name.
686+
func (f *File) getPivotTableFieldOptions(name string, fields []PivotTableField) (options PivotTableField, ok bool) {
673687
for _, field := range fields {
674688
if field.Data == name {
675-
return field.DefaultSubtotal, true
689+
options, ok = field, true
690+
return
676691
}
677692
}
678-
return false, false
693+
return
679694
}
680695

681696
// addWorkbookPivotCache add the association ID of the pivot cache in workbook.xml.

xmlPivotTable.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ type xlsxPivotTableDefinition struct {
7171
ShowEmptyRow bool `xml:"showEmptyRow,attr,omitempty"`
7272
ShowEmptyCol bool `xml:"showEmptyCol,attr,omitempty"`
7373
ShowHeaders bool `xml:"showHeaders,attr,omitempty"`
74-
Compact bool `xml:"compact,attr"`
75-
Outline bool `xml:"outline,attr"`
74+
Compact *bool `xml:"compact,attr"`
75+
Outline *bool `xml:"outline,attr"`
7676
OutlineData bool `xml:"outlineData,attr,omitempty"`
7777
CompactData *bool `xml:"compactData,attr,omitempty"`
7878
Published bool `xml:"published,attr,omitempty"`
@@ -125,10 +125,10 @@ type xlsxPivotField struct {
125125
ShowDropDowns bool `xml:"showDropDowns,attr,omitempty"`
126126
HiddenLevel bool `xml:"hiddenLevel,attr,omitempty"`
127127
UniqueMemberProperty string `xml:"uniqueMemberProperty,attr,omitempty"`
128-
Compact bool `xml:"compact,attr"`
128+
Compact *bool `xml:"compact,attr"`
129129
AllDrilled bool `xml:"allDrilled,attr,omitempty"`
130130
NumFmtID string `xml:"numFmtId,attr,omitempty"`
131-
Outline bool `xml:"outline,attr"`
131+
Outline *bool `xml:"outline,attr"`
132132
SubtotalTop bool `xml:"subtotalTop,attr,omitempty"`
133133
DragToRow bool `xml:"dragToRow,attr,omitempty"`
134134
DragToCol bool `xml:"dragToCol,attr,omitempty"`

0 commit comments

Comments
 (0)