summaryrefslogtreecommitdiff
path: root/pivotTable.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2020-02-21 23:07:43 +0800
committerxuri <xuri.me@gmail.com>2020-02-21 23:07:43 +0800
commit6dcb7013eeeb8902be97c564c7a5a05dddcb06b8 (patch)
treef39b5723e0d04304b7665c6f1e5031585d685a8a /pivotTable.go
parentad883caa0f77dfc016ae99bd5fbb606953eb99a0 (diff)
Resolve #582, support to set date field subtotal and names for pivot table
- typo fixed and update do.dev badge in the README.
Diffstat (limited to 'pivotTable.go')
-rw-r--r--pivotTable.go58
1 files changed, 50 insertions, 8 deletions
diff --git a/pivotTable.go b/pivotTable.go
index ee0d94e..696dfe7 100644
--- a/pivotTable.go
+++ b/pivotTable.go
@@ -20,6 +20,8 @@ import (
// PivotTableOption directly maps the format settings of the pivot table.
type PivotTableOption struct {
DataRange string
+ DataSubtotal string
+ DataFieldName string
PivotTableRange string
Rows []string
Columns []string
@@ -28,9 +30,29 @@ type PivotTableOption struct {
}
// AddPivotTable provides the method to add pivot table by given pivot table
-// options. For example, create a pivot table on the Sheet1!$G$2:$M$34 area
-// with the region Sheet1!$A$1:$E$31 as the data source, summarize by sum for
-// sales:
+// options.
+//
+// DataSubtotal specifies the aggregation function that applies to this data
+// field. The default value is sum. The possible values for this attribute
+// are:
+//
+// Average
+// Count
+// CountNums
+// Max
+// Min
+// Product
+// StdDev
+// StdDevp
+// Sum
+// Var
+// Varp
+//
+// DataFieldName specifies the name of the data field. Maximum 255 characters
+// are allowed in data field name, excess characters will be truncated.
+//
+// For example, create a pivot table on the Sheet1!$G$2:$M$34 area with the
+// region Sheet1!$A$1:$E$31 as the data source, summarize by sum for sales:
//
// package main
//
@@ -62,6 +84,8 @@ type PivotTableOption struct {
// Rows: []string{"Month", "Year"},
// Columns: []string{"Type"},
// Data: []string{"Sales"},
+// DataSubtotal: "Sum",
+// DataFieldName: "Summarize as Sum",
// }); err != nil {
// fmt.Println(err)
// }
@@ -278,9 +302,9 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
if err != nil {
return err
}
- for _, filedIdx := range rowFieldsIndex {
+ for _, fieldIdx := range rowFieldsIndex {
pt.RowFields.Field = append(pt.RowFields.Field, &xlsxField{
- X: filedIdx,
+ X: fieldIdx,
})
}
@@ -297,9 +321,15 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, op
if err != nil {
return err
}
+ dataFieldName := opt.DataFieldName
+ if len(dataFieldName) > 255 {
+ dataFieldName = dataFieldName[0:255]
+ }
for _, dataField := range dataFieldsIndex {
pt.DataFields.DataField = append(pt.DataFields.DataField, &xlsxDataField{
- Fld: dataField,
+ Name: dataFieldName,
+ Fld: dataField,
+ Subtotal: f.getFieldsSubtotal(opt),
})
}
@@ -336,9 +366,9 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opt *PivotTableOp
if err != nil {
return err
}
- for _, filedIdx := range colFieldsIndex {
+ for _, fieldIdx := range colFieldsIndex {
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
- X: filedIdx,
+ X: fieldIdx,
})
}
@@ -430,6 +460,18 @@ func (f *File) getPivotFieldsIndex(fields []string, opt *PivotTableOption) ([]in
return pivotFieldsIndex, nil
}
+// getFieldsSubtotal prepare data subtotal by given fields and pivot option.
+func (f *File) getFieldsSubtotal(opt *PivotTableOption) (subtotal string) {
+ subtotal = "sum"
+ for _, enum := range []string{"average", "count", "countNums", "max", "min", "product", "stdDev", "stdDevp", "sum", "var", "varp"} {
+ if strings.ToLower(enum) == strings.ToLower(opt.DataSubtotal) {
+ subtotal = enum
+ return
+ }
+ }
+ return
+}
+
// addWorkbookPivotCache add the association ID of the pivot cache in xl/workbook.xml.
func (f *File) addWorkbookPivotCache(RID int) int {
wb := f.workbookReader()