diff options
author | xuri <xuri.me@gmail.com> | 2020-04-09 01:00:14 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2020-04-09 01:00:14 +0800 |
commit | e36650f4ffd3e305d2c3834620f97ec382cf6faf (patch) | |
tree | 3d0a20cdacb43d3beead9d19bab327da7499e786 /pivotTable.go | |
parent | a2e1da8d9d90ed71a33523cfe2c5231cd0b5fad9 (diff) |
Resolve #598, filter support for AddPivotTable
Diffstat (limited to 'pivotTable.go')
-rw-r--r-- | pivotTable.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/pivotTable.go b/pivotTable.go index b7dc859..7061bfe 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -25,6 +25,7 @@ type PivotTableOption struct { Rows []PivotTableField Columns []PivotTableField Data []PivotTableField + Filter []PivotTableField } // PivotTableField directly maps the field settings of the pivot table. @@ -86,6 +87,7 @@ type PivotTableField struct { // DataRange: "Sheet1!$A$1:$E$31", // PivotTableRange: "Sheet1!$G$2:$M$34", // Rows: []excelize.PivotTableField{{Data: "Month"}, {Data: "Year"}}, +// Filter: []excelize.PivotTableField{{Data: "Region"}}, // Columns: []excelize.PivotTableField{{Data: "Type"}}, // Data: []excelize.PivotTableField{{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}}, // }); err != nil { @@ -283,6 +285,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op Count: 1, I: []*xlsxI{{}}, }, + PageFields: &xlsxPageFields{}, DataFields: &xlsxDataFields{}, PivotTableStyleInfo: &xlsxPivotTableStyleInfo{ Name: "PivotStyleLight16", @@ -320,6 +323,19 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op return err } + // page fields + pageFieldsIndex, err := f.getPivotFieldsIndex(opt.Filter, opt) + if err != nil { + return err + } + pageFieldsName := f.getPivotTableFieldsName(opt.Filter) + for idx, pageField := range pageFieldsIndex { + pt.PageFields.PageField = append(pt.PageFields.PageField, &xlsxPageField{ + Name: pageFieldsName[idx], + Fld: pageField, + }) + } + // data fields dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt) if err != nil { @@ -412,6 +428,19 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio }) continue } + if inPivotTableField(opt.Filter, name) != -1 { + pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{ + Axis: "axisPage", + Name: f.getPivotTableFieldName(name, opt.Columns), + Items: &xlsxItems{ + Count: 1, + Item: []*xlsxItem{ + {T: "default"}, + }, + }, + }) + continue + } if inPivotTableField(opt.Columns, name) != -1 { pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{ Axis: "axisCol", |