Skip to content

Commit

Permalink
Resolve #511, allow empty columns in the pivot table
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Oct 24, 2019
1 parent 9fe267f commit 87390cd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 30 additions & 11 deletions pivotTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions pivotTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 4 additions & 4 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 87390cd

Please sign in to comment.