Resolve #511, allow empty columns in the pivot table

This commit is contained in:
xuri 2019-10-24 23:18:02 +08:00
parent 9fe267ffcf
commit 87390cdd99
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
6 changed files with 51 additions and 17 deletions

View File

@ -56,7 +56,7 @@ type xlsxCalcChainCollection []xlsxCalcChainC
// Filter provides a function to filter calculation chain. // Filter provides a function to filter calculation chain.
func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC { func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC {
results := make([]xlsxCalcChainC, 0) var results []xlsxCalcChainC
for _, v := range c { for _, v := range c {
if fn(v) { if fn(v) {
results = append(results, v) results = append(results, v)

View File

@ -253,7 +253,10 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
}, },
}, },
}, },
ColFields: &xlsxColFields{}, ColItems: &xlsxColItems{
Count: 1,
I: []*xlsxI{{}},
},
DataFields: &xlsxDataFields{}, DataFields: &xlsxDataFields{},
PivotTableStyleInfo: &xlsxPivotTableStyleInfo{ PivotTableStyleInfo: &xlsxPivotTableStyleInfo{
Name: "PivotStyleLight16", Name: "PivotStyleLight16",
@ -286,19 +289,10 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
// count row fields // count row fields
pt.RowFields.Count = len(pt.RowFields.Field) pt.RowFields.Count = len(pt.RowFields.Field)
// col fields err = f.addPivotColFields(&pt, opt)
colFieldsIndex, err := f.getPivotFieldsIndex(opt.Columns, opt)
if err != nil { if err != nil {
return err 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 // data fields
dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt) dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt)
@ -330,6 +324,31 @@ func inStrSlice(a []string, x string) int {
return -1 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 // addPivotFields create pivot fields based on the column order of the first
// row in the data region by given pivot table definition and option. // row in the data region by given pivot table definition and option.
func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error { func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {

View File

@ -54,6 +54,12 @@ func TestAddPivotTable(t *testing.T) {
Columns: []string{"Region", "Year"}, Columns: []string{"Region", "Year"},
Data: []string{"Sales"}, 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") f.NewSheet("Sheet2")
assert.NoError(t, f.AddPivotTable(&PivotTableOption{ assert.NoError(t, f.AddPivotTable(&PivotTableOption{
DataRange: "Sheet1!$A$1:$E$31", DataRange: "Sheet1!$A$1:$E$31",

View File

@ -22,7 +22,7 @@ func TestRows(t *testing.T) {
t.FailNow() t.FailNow()
} }
collectedRows := make([][]string, 0) var collectedRows [][]string
for rows.Next() { for rows.Next() {
columns, err := rows.Columns() columns, err := rows.Columns()
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -66,6 +66,15 @@ func ExampleFile_GetPageLayout() {
// - fit to width: 1 // - 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) { func TestPageLayoutOption(t *testing.T) {
const sheet = "Sheet1" const sheet = "Sheet1"

View File

@ -47,10 +47,10 @@ const (
NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/" NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/"
NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/" NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/"
NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/" NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/"
// The extLst child element ([ISO/IEC29500-1:2016] section 18.2.10) of the // ExtURIConditionalFormattings is the extLst child element
// worksheet element ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by // ([ISO/IEC29500-1:2016] section 18.2.10) of the worksheet element
// the addition of new child ext elements ([ISO/IEC29500-1:2016] section // ([ISO/IEC29500-1:2016] section 18.3.1.99) is extended by the addition of
// 18.2.7) // new child ext elements ([ISO/IEC29500-1:2016] section 18.2.7)
ExtURIConditionalFormattings = "{78C0D931-6437-407D-A8EE-F0AAD7539E65}" ExtURIConditionalFormattings = "{78C0D931-6437-407D-A8EE-F0AAD7539E65}"
ExtURIDataValidations = "{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}" ExtURIDataValidations = "{CCE6A557-97BC-4B89-ADB6-D9C93CAAB3DF}"
ExtURISparklineGroups = "{05C60535-1F16-4fd2-B633-F4F36F0B64E0}" ExtURISparklineGroups = "{05C60535-1F16-4fd2-B633-F4F36F0B64E0}"