From 87390cdd99b3afbe07daeef9abe96f57d03cb352 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 24 Oct 2019 23:18:02 +0800 Subject: [PATCH] Resolve #511, allow empty columns in the pivot table --- calcchain.go | 2 +- pivotTable.go | 41 ++++++++++++++++++++++++++++++----------- pivotTable_test.go | 6 ++++++ rows_test.go | 2 +- sheet_test.go | 9 +++++++++ xmlDrawing.go | 8 ++++---- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/calcchain.go b/calcchain.go index b4cadef..7cc175c 100644 --- a/calcchain.go +++ b/calcchain.go @@ -56,7 +56,7 @@ type xlsxCalcChainCollection []xlsxCalcChainC // Filter provides a function to filter calculation chain. func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC { - results := make([]xlsxCalcChainC, 0) + var results []xlsxCalcChainC for _, v := range c { if fn(v) { results = append(results, v) diff --git a/pivotTable.go b/pivotTable.go index 881d774..6045e41 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -253,7 +253,10 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op }, }, }, - ColFields: &xlsxColFields{}, + ColItems: &xlsxColItems{ + Count: 1, + I: []*xlsxI{{}}, + }, DataFields: &xlsxDataFields{}, PivotTableStyleInfo: &xlsxPivotTableStyleInfo{ Name: "PivotStyleLight16", @@ -286,19 +289,10 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op // count row fields pt.RowFields.Count = len(pt.RowFields.Field) - // col fields - colFieldsIndex, err := f.getPivotFieldsIndex(opt.Columns, opt) + err = f.addPivotColFields(&pt, opt) if err != nil { return err } - for _, filedIdx := range colFieldsIndex { - pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{ - X: filedIdx, - }) - } - - // count col fields - pt.ColFields.Count = len(pt.ColFields.Field) // data fields dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt) @@ -330,6 +324,31 @@ func inStrSlice(a []string, x string) int { return -1 } +// addPivotColFields create pivot column fields by given pivot table +// definition and option. +func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error { + if len(opt.Columns) == 0 { + return nil + } + + pt.ColFields = &xlsxColFields{} + + // col fields + colFieldsIndex, err := f.getPivotFieldsIndex(opt.Columns, opt) + if err != nil { + return err + } + for _, filedIdx := range colFieldsIndex { + pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{ + X: filedIdx, + }) + } + + // count col fields + pt.ColFields.Count = len(pt.ColFields.Field) + return err +} + // addPivotFields create pivot fields based on the column order of the first // row in the data region by given pivot table definition and option. func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error { diff --git a/pivotTable_test.go b/pivotTable_test.go index 27e5914..9bf08e8 100644 --- a/pivotTable_test.go +++ b/pivotTable_test.go @@ -54,6 +54,12 @@ func TestAddPivotTable(t *testing.T) { Columns: []string{"Region", "Year"}, Data: []string{"Sales"}, })) + assert.NoError(t, f.AddPivotTable(&PivotTableOption{ + DataRange: "Sheet1!$A$1:$E$31", + PivotTableRange: "Sheet1!$AE$2:$AG$33", + Rows: []string{"Month", "Year"}, + Data: []string{"Sales"}, + })) f.NewSheet("Sheet2") assert.NoError(t, f.AddPivotTable(&PivotTableOption{ DataRange: "Sheet1!$A$1:$E$31", diff --git a/rows_test.go b/rows_test.go index f0fbe03..a443e89 100644 --- a/rows_test.go +++ b/rows_test.go @@ -22,7 +22,7 @@ func TestRows(t *testing.T) { t.FailNow() } - collectedRows := make([][]string, 0) + var collectedRows [][]string for rows.Next() { columns, err := rows.Columns() assert.NoError(t, err) diff --git a/sheet_test.go b/sheet_test.go index 5179793..ea345a3 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -66,6 +66,15 @@ func ExampleFile_GetPageLayout() { // - fit to width: 1 } +func TestNewSheet(t *testing.T) { + f := excelize.NewFile() + sheetID := f.NewSheet("Sheet2") + f.SetActiveSheet(sheetID) + // delete original sheet + f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1"))) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestNewSheet.xlsx"))) +} + func TestPageLayoutOption(t *testing.T) { const sheet = "Sheet1" diff --git a/xmlDrawing.go b/xmlDrawing.go index 4338c5e..1c24f08 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -47,10 +47,10 @@ const ( NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/" NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/" NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/" - // The extLst child element ([ISO/IEC29500-1:2016] section 18.2.10) of the - // worksheet element ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by - // the addition of new child ext elements ([ISO/IEC29500-1:2016] section - // 18.2.7) + // ExtURIConditionalFormattings is the extLst child element + // ([ISO/IEC29500-1:2016] section 18.2.10) of the worksheet element + // ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by the addition of + // new child ext elements ([ISO/IEC29500-1:2016] section 18.2.7) ExtURIConditionalFormattings = "{78C0D931-6437-407D-A8EE-F0AAD7539E65}" ExtURIDataValidations = "{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}" ExtURISparklineGroups = "{05C60535-1F16-4fd2-B633-F4F36F0B64E0}"