Browse Source

Resolve #598, filter support for AddPivotTable

xuri 5 years ago
parent
commit
e36650f4ff
4 changed files with 35 additions and 4 deletions
  1. 2 1
      cell.go
  2. 29 0
      pivotTable.go
  3. 1 0
      pivotTable_test.go
  4. 3 3
      xmlPivotTable.go

+ 2 - 1
cell.go

@@ -458,7 +458,8 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
 }
 }
 
 
 // SetCellRichText provides a function to set cell with rich text by given
 // SetCellRichText provides a function to set cell with rich text by given
-// worksheet. For example:
+// worksheet. For example, set rich text on the A1 cell of the worksheet named
+// Sheet1:
 //
 //
 //    package main
 //    package main
 //
 //

+ 29 - 0
pivotTable.go

@@ -25,6 +25,7 @@ type PivotTableOption struct {
 	Rows            []PivotTableField
 	Rows            []PivotTableField
 	Columns         []PivotTableField
 	Columns         []PivotTableField
 	Data            []PivotTableField
 	Data            []PivotTableField
+	Filter          []PivotTableField
 }
 }
 
 
 // PivotTableField directly maps the field settings of the pivot table.
 // PivotTableField directly maps the field settings of the pivot table.
@@ -86,6 +87,7 @@ type PivotTableField struct {
 //            DataRange:       "Sheet1!$A$1:$E$31",
 //            DataRange:       "Sheet1!$A$1:$E$31",
 //            PivotTableRange: "Sheet1!$G$2:$M$34",
 //            PivotTableRange: "Sheet1!$G$2:$M$34",
 //            Rows:            []excelize.PivotTableField{{Data: "Month"}, {Data: "Year"}},
 //            Rows:            []excelize.PivotTableField{{Data: "Month"}, {Data: "Year"}},
+//            Filter:          []excelize.PivotTableField{{Data: "Region"}},
 //            Columns:         []excelize.PivotTableField{{Data: "Type"}},
 //            Columns:         []excelize.PivotTableField{{Data: "Type"}},
 //            Data:            []excelize.PivotTableField{{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}},
 //            Data:            []excelize.PivotTableField{{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}},
 //        }); err != nil {
 //        }); err != nil {
@@ -283,6 +285,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
 			Count: 1,
 			Count: 1,
 			I:     []*xlsxI{{}},
 			I:     []*xlsxI{{}},
 		},
 		},
+		PageFields: &xlsxPageFields{},
 		DataFields: &xlsxDataFields{},
 		DataFields: &xlsxDataFields{},
 		PivotTableStyleInfo: &xlsxPivotTableStyleInfo{
 		PivotTableStyleInfo: &xlsxPivotTableStyleInfo{
 			Name:           "PivotStyleLight16",
 			Name:           "PivotStyleLight16",
@@ -320,6 +323,19 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
 		return err
 		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
 	// data fields
 	dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt)
 	dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt)
 	if err != nil {
 	if err != nil {
@@ -412,6 +428,19 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opt *PivotTableOptio
 			})
 			})
 			continue
 			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 {
 		if inPivotTableField(opt.Columns, name) != -1 {
 			pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
 			pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
 				Axis: "axisCol",
 				Axis: "axisCol",

+ 1 - 0
pivotTable_test.go

@@ -29,6 +29,7 @@ func TestAddPivotTable(t *testing.T) {
 		DataRange:       "Sheet1!$A$1:$E$31",
 		DataRange:       "Sheet1!$A$1:$E$31",
 		PivotTableRange: "Sheet1!$G$2:$M$34",
 		PivotTableRange: "Sheet1!$G$2:$M$34",
 		Rows:            []PivotTableField{{Data: "Month"}, {Data: "Year"}},
 		Rows:            []PivotTableField{{Data: "Month"}, {Data: "Year"}},
+		Filter:          []PivotTableField{{Data: "Region"}},
 		Columns:         []PivotTableField{{Data: "Type"}},
 		Columns:         []PivotTableField{{Data: "Type"}},
 		Data:            []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Summarize by Sum"}},
 		Data:            []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Summarize by Sum"}},
 	}))
 	}))

+ 3 - 3
xmlPivotTable.go

@@ -251,9 +251,9 @@ type xlsxPageFields struct {
 type xlsxPageField struct {
 type xlsxPageField struct {
 	Fld    int         `xml:"fld,attr"`
 	Fld    int         `xml:"fld,attr"`
 	Item   int         `xml:"item,attr,omitempty"`
 	Item   int         `xml:"item,attr,omitempty"`
-	Hier   int         `xml:"hier,attr"`
-	Name   string      `xml:"name,attr"`
-	Cap    string      `xml:"cap,attr"`
+	Hier   int         `xml:"hier,attr,omitempty"`
+	Name   string      `xml:"name,attr,omitempty"`
+	Cap    string      `xml:"cap,attr,omitempty"`
 	ExtLst *xlsxExtLst `xml:"extLst"`
 	ExtLst *xlsxExtLst `xml:"extLst"`
 }
 }