Skip to content

Commit c492220

Browse files
jinhyuk-kim-caJin Kimxuri
authored
Pivot table generation fails when no Columns and multiple Data are provided. (#708)
fix to create pivot table in case there is no input from Columns Co-authored-by: Jin Kim <jinhyuk.kim@cerence.com> Co-authored-by: xuri <xuri.me@gmail.com>
1 parent 89465f4 commit c492220

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

pivotTable.go

+9
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ func inPivotTableField(a []PivotTableField, x string) int {
460460
// definition and option.
461461
func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {
462462
if len(opt.Columns) == 0 {
463+
if len(opt.Data) <= 1 {
464+
return nil
465+
}
466+
pt.ColFields = &xlsxColFields{}
467+
// in order to create pivot table in case there is no input from Columns
468+
pt.ColFields.Count = 1
469+
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
470+
X: -2,
471+
})
463472
return nil
464473
}
465474

pivotTable_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestAddPivotTable(t *testing.T) {
8484
DataRange: "Sheet1!$A$1:$E$31",
8585
PivotTableRange: "Sheet1!$AE$2:$AG$33",
8686
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
87-
Data: []PivotTableField{{Data: "Sales", Subtotal: "Max", Name: "Summarize by Max"}},
87+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Max", Name: "Summarize by Max"}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales"}},
8888
RowGrandTotals: true,
8989
ColGrandTotals: true,
9090
ShowDrill: true,
@@ -98,7 +98,7 @@ func TestAddPivotTable(t *testing.T) {
9898
PivotTableRange: "Sheet1!$AJ$2:$AP1$35",
9999
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
100100
Filter: []PivotTableField{{Data: "Region"}},
101-
Columns: []PivotTableField{{Data: "Type", DefaultSubtotal: true}},
101+
Columns: []PivotTableField{},
102102
Data: []PivotTableField{{Subtotal: "Sum", Name: "Summarize by Sum"}},
103103
RowGrandTotals: true,
104104
ColGrandTotals: true,

0 commit comments

Comments
 (0)