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.
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)

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{},
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 {

View File

@ -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",

View File

@ -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)

View File

@ -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"

View File

@ -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}"