From 9c278365f2137e0528fec6c4ab98ca53c2e8041b Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 13 Jul 2024 10:41:57 +0800 Subject: [PATCH] This closes #1945, an error will be return if column header cell is empty in pivot table data range - Update unit tests --- pivotTable.go | 9 +++++++-- pivotTable_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pivotTable.go b/pivotTable.go index c3b9ea648c..7ed79ada03 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -260,6 +260,9 @@ func (f *File) getTableFieldsOrder(opts *PivotTableOptions) ([]string, error) { if err != nil { return order, err } + if name == "" { + return order, ErrParameterInvalid + } order = append(order, name) } return order, nil @@ -272,8 +275,10 @@ func (f *File) addPivotCache(opts *PivotTableOptions) error { if err != nil { return newPivotTableDataRangeError(err.Error()) } - // data range has been checked - order, _ := f.getTableFieldsOrder(opts) + order, err := f.getTableFieldsOrder(opts) + if err != nil { + return newPivotTableDataRangeError(err.Error()) + } topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1]) bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3]) pc := xlsxPivotCacheDefinition{ diff --git a/pivotTable_test.go b/pivotTable_test.go index f3bb2bfd73..133301d09e 100644 --- a/pivotTable_test.go +++ b/pivotTable_test.go @@ -300,6 +300,7 @@ func TestPivotTable(t *testing.T) { assert.EqualError(t, err, `parameter 'DataRange' parsing error: parameter is required`) // Test add pivot table with unsupported charset content types. f = NewFile() + assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})) f.ContentTypes = nil f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) assert.EqualError(t, f.AddPivotTable(&PivotTableOptions{ @@ -393,6 +394,25 @@ func TestPivotTableDataRange(t *testing.T) { f.Relationships.Delete("xl/worksheets/_rels/sheet1.xml.rels") f.Pkg.Delete("xl/worksheets/_rels/sheet1.xml.rels") assert.EqualError(t, f.DeletePivotTable("Sheet1", "PivotTable1"), "table PivotTable1 does not exist") + + t.Run("data_range_with_empty_column", func(t *testing.T) { + // Test add pivot table with data range doesn't organized as a list with labeled columns + f := NewFile() + // Create some data in a sheet + month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} + types := []string{"Meat", "Dairy", "Beverages", "Produce"} + assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "", "Type"})) + for row := 2; row < 32; row++ { + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), month[rand.Intn(12)])) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), types[rand.Intn(4)])) + } + assert.Equal(t, newPivotTableDataRangeError("parameter is invalid"), f.AddPivotTable(&PivotTableOptions{ + DataRange: "Sheet1!A1:E31", + PivotTableRange: "Sheet1!G2:M34", + Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}}, + Data: []PivotTableField{{Data: "Type"}}, + })) + }) } func TestParseFormatPivotTableSet(t *testing.T) {