summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjinhyuk-kim-ca <71794373+jinhyuk-kim-ca@users.noreply.github.com>2020-09-27 01:34:39 -0400
committerGitHub <noreply@github.com>2020-09-27 13:34:39 +0800
commitc49222023756d6740877f286cb7d3e7a5a0950eb (patch)
tree8521c01dcc2b1876b16cbda2c39fe6f70640c413
parent89465f41b5a29ac2e1debe16b8cb6f59c344f917 (diff)
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>
-rw-r--r--pivotTable.go9
-rw-r--r--pivotTable_test.go4
2 files changed, 11 insertions, 2 deletions
diff --git a/pivotTable.go b/pivotTable.go
index 0c1dd61..a197c1f 100644
--- a/pivotTable.go
+++ b/pivotTable.go
@@ -460,6 +460,15 @@ func inPivotTableField(a []PivotTableField, x string) int {
// definition and option.
func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {
if len(opt.Columns) == 0 {
+ if len(opt.Data) <= 1 {
+ return nil
+ }
+ pt.ColFields = &xlsxColFields{}
+ // in order to create pivot table in case there is no input from Columns
+ pt.ColFields.Count = 1
+ pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
+ X: -2,
+ })
return nil
}
diff --git a/pivotTable_test.go b/pivotTable_test.go
index 6e73640..61bb07b 100644
--- a/pivotTable_test.go
+++ b/pivotTable_test.go
@@ -84,7 +84,7 @@ func TestAddPivotTable(t *testing.T) {
DataRange: "Sheet1!$A$1:$E$31",
PivotTableRange: "Sheet1!$AE$2:$AG$33",
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
- Data: []PivotTableField{{Data: "Sales", Subtotal: "Max", Name: "Summarize by Max"}},
+ Data: []PivotTableField{{Data: "Sales", Subtotal: "Max", Name: "Summarize by Max"}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales"}},
RowGrandTotals: true,
ColGrandTotals: true,
ShowDrill: true,
@@ -98,7 +98,7 @@ func TestAddPivotTable(t *testing.T) {
PivotTableRange: "Sheet1!$AJ$2:$AP1$35",
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
Filter: []PivotTableField{{Data: "Region"}},
- Columns: []PivotTableField{{Data: "Type", DefaultSubtotal: true}},
+ Columns: []PivotTableField{},
Data: []PivotTableField{{Subtotal: "Sum", Name: "Summarize by Sum"}},
RowGrandTotals: true,
ColGrandTotals: true,