From 2bd359bd01312bedd1b5f76f68765d1d66fa9a95 Mon Sep 17 00:00:00 2001 From: Ludovic Braconnier Date: Wed, 30 Sep 2020 18:20:11 +0200 Subject: [PATCH] fix pivot fails in case of multi columns and multi data --- pivotTable.go | 7 +++++++ pivotTable_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pivotTable.go b/pivotTable.go index a197c1fa29..b7c80c2496 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -485,6 +485,13 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opt *PivotTableOp }) } + //in order to create pivot in case there is many Columns and Many Datas + if len(opt.Data) > 1 { + pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{ + X: -2, + }) + } + // count col fields pt.ColFields.Count = len(pt.ColFields.Field) return err diff --git a/pivotTable_test.go b/pivotTable_test.go index 61bb07b15f..42103f3ed9 100644 --- a/pivotTable_test.go +++ b/pivotTable_test.go @@ -135,6 +135,20 @@ func TestAddPivotTable(t *testing.T) { ShowColHeaders: true, ShowLastColumn: true, })) + //Test Pivot table with many data, many rows, many cols + assert.NoError(t, f.AddPivotTable(&PivotTableOption{ + DataRange: "Sheet1!$A$1:$E$31", + PivotTableRange: "Sheet2!$A$56:$AG$90", + Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}}, + Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Type"}}, + Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Sum of Sales"}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales"}}, + RowGrandTotals: true, + ColGrandTotals: true, + ShowDrill: true, + ShowRowHeaders: true, + ShowColHeaders: true, + ShowLastColumn: true, + })) // Test empty pivot table options assert.EqualError(t, f.AddPivotTable(nil), "parameter is required")