2022-01-09 00:20:42 +08:00
|
|
|
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
|
2019-09-20 00:20:30 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
|
|
|
// data. This library needs Go version 1.15 or later.
|
2019-09-20 00:20:30 +08:00
|
|
|
|
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PivotTableOption directly maps the format settings of the pivot table.
|
2021-10-11 00:08:45 +08:00
|
|
|
//
|
|
|
|
// PivotTableStyleName: The built-in pivot table style names
|
|
|
|
//
|
|
|
|
// PivotStyleLight1 - PivotStyleLight28
|
|
|
|
// PivotStyleMedium1 - PivotStyleMedium28
|
|
|
|
// PivotStyleDark1 - PivotStyleDark28
|
|
|
|
//
|
2019-09-20 00:20:30 +08:00
|
|
|
type PivotTableOption struct {
|
2021-06-29 22:26:55 +08:00
|
|
|
pivotTableSheetName string
|
2020-09-11 00:45:52 +08:00
|
|
|
DataRange string
|
|
|
|
PivotTableRange string
|
|
|
|
Rows []PivotTableField
|
|
|
|
Columns []PivotTableField
|
|
|
|
Data []PivotTableField
|
|
|
|
Filter []PivotTableField
|
|
|
|
RowGrandTotals bool
|
|
|
|
ColGrandTotals bool
|
|
|
|
ShowDrill bool
|
|
|
|
UseAutoFormatting bool
|
|
|
|
PageOverThenDown bool
|
|
|
|
MergeItem bool
|
|
|
|
CompactData bool
|
2021-03-13 13:22:28 +08:00
|
|
|
ShowError bool
|
2020-09-11 00:45:52 +08:00
|
|
|
ShowRowHeaders bool
|
|
|
|
ShowColHeaders bool
|
|
|
|
ShowRowStripes bool
|
|
|
|
ShowColStripes bool
|
|
|
|
ShowLastColumn bool
|
|
|
|
PivotTableStyleName string
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// PivotTableField directly maps the field settings of the pivot table.
|
|
|
|
// Subtotal specifies the aggregation function that applies to this data
|
2020-02-21 23:07:43 +08:00
|
|
|
// field. The default value is sum. The possible values for this attribute
|
|
|
|
// are:
|
|
|
|
//
|
|
|
|
// Average
|
|
|
|
// Count
|
|
|
|
// CountNums
|
|
|
|
// Max
|
|
|
|
// Min
|
|
|
|
// Product
|
|
|
|
// StdDev
|
|
|
|
// StdDevp
|
|
|
|
// Sum
|
|
|
|
// Var
|
|
|
|
// Varp
|
|
|
|
//
|
2020-02-26 18:53:50 +08:00
|
|
|
// Name specifies the name of the data field. Maximum 255 characters
|
2020-02-21 23:07:43 +08:00
|
|
|
// are allowed in data field name, excess characters will be truncated.
|
2020-02-26 18:53:50 +08:00
|
|
|
type PivotTableField struct {
|
2021-10-11 00:08:45 +08:00
|
|
|
Compact bool
|
2020-09-11 00:45:52 +08:00
|
|
|
Data string
|
|
|
|
Name string
|
2021-10-11 00:08:45 +08:00
|
|
|
Outline bool
|
2020-09-11 00:45:52 +08:00
|
|
|
Subtotal string
|
|
|
|
DefaultSubtotal bool
|
2020-02-26 18:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPivotTable provides the method to add pivot table by given pivot table
|
2021-03-15 23:56:36 +08:00
|
|
|
// options. Note that the same fields can not in Columns, Rows and Filter
|
|
|
|
// fields at the same time.
|
2020-02-21 23:07:43 +08:00
|
|
|
//
|
|
|
|
// 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:
|
2019-09-20 00:20:30 +08:00
|
|
|
//
|
|
|
|
// package main
|
|
|
|
//
|
|
|
|
// import (
|
|
|
|
// "fmt"
|
|
|
|
// "math/rand"
|
|
|
|
//
|
2021-07-28 00:38:09 +08:00
|
|
|
// "github.com/xuri/excelize/v2"
|
2019-09-20 00:20:30 +08:00
|
|
|
// )
|
|
|
|
//
|
|
|
|
// func main() {
|
|
|
|
// f := excelize.NewFile()
|
|
|
|
// // Create some data in a sheet
|
|
|
|
// month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
|
|
|
|
// year := []int{2017, 2018, 2019}
|
|
|
|
// types := []string{"Meat", "Dairy", "Beverages", "Produce"}
|
|
|
|
// region := []string{"East", "West", "North", "South"}
|
|
|
|
// f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})
|
|
|
|
// for i := 0; i < 30; i++ {
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), month[rand.Intn(12)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), year[rand.Intn(3)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), types[rand.Intn(4)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), rand.Intn(5000))
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), region[rand.Intn(4)])
|
|
|
|
// }
|
2019-12-31 01:01:16 +08:00
|
|
|
// if err := f.AddPivotTable(&excelize.PivotTableOption{
|
2019-09-20 00:20:30 +08:00
|
|
|
// DataRange: "Sheet1!$A$1:$E$31",
|
|
|
|
// PivotTableRange: "Sheet1!$G$2:$M$34",
|
2020-09-15 23:31:24 +08:00
|
|
|
// Rows: []excelize.PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
|
2020-04-09 01:00:14 +08:00
|
|
|
// Filter: []excelize.PivotTableField{{Data: "Region"}},
|
2020-09-15 23:31:24 +08:00
|
|
|
// Columns: []excelize.PivotTableField{{Data: "Type", DefaultSubtotal: true}},
|
2020-02-26 18:53:50 +08:00
|
|
|
// Data: []excelize.PivotTableField{{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}},
|
2020-09-15 23:31:24 +08:00
|
|
|
// RowGrandTotals: true,
|
|
|
|
// ColGrandTotals: true,
|
|
|
|
// ShowDrill: true,
|
|
|
|
// ShowRowHeaders: true,
|
|
|
|
// ShowColHeaders: true,
|
|
|
|
// ShowLastColumn: true,
|
2019-12-31 01:01:16 +08:00
|
|
|
// }); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
// fmt.Println(err)
|
2019-09-20 00:20:30 +08:00
|
|
|
// }
|
2019-12-31 01:01:16 +08:00
|
|
|
// if err := f.SaveAs("Book1.xlsx"); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
// fmt.Println(err)
|
2019-09-20 00:20:30 +08:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (f *File) AddPivotTable(opt *PivotTableOption) error {
|
|
|
|
// parameter validation
|
2022-03-24 00:19:30 +08:00
|
|
|
_, pivotTableSheetPath, err := f.parseFormatPivotTableSet(opt)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pivotTableID := f.countPivotTables() + 1
|
|
|
|
pivotCacheID := f.countPivotCache() + 1
|
|
|
|
|
|
|
|
sheetRelationshipsPivotTableXML := "../pivotTables/pivotTable" + strconv.Itoa(pivotTableID) + ".xml"
|
|
|
|
pivotTableXML := strings.Replace(sheetRelationshipsPivotTableXML, "..", "xl", -1)
|
|
|
|
pivotCacheXML := "xl/pivotCache/pivotCacheDefinition" + strconv.Itoa(pivotCacheID) + ".xml"
|
2022-03-24 00:19:30 +08:00
|
|
|
err = f.addPivotCache(pivotCacheXML, opt)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// workbook pivot cache
|
2020-11-06 20:03:13 +08:00
|
|
|
workBookPivotCacheRID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipPivotCache, fmt.Sprintf("/xl/pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "")
|
2019-09-20 00:20:30 +08:00
|
|
|
cacheID := f.addWorkbookPivotCache(workBookPivotCacheRID)
|
|
|
|
|
|
|
|
pivotCacheRels := "xl/pivotTables/_rels/pivotTable" + strconv.Itoa(pivotTableID) + ".xml.rels"
|
|
|
|
// rId not used
|
|
|
|
_ = f.addRels(pivotCacheRels, SourceRelationshipPivotCache, fmt.Sprintf("../pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "")
|
|
|
|
err = f.addPivotTable(cacheID, pivotTableID, pivotTableXML, opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pivotTableSheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(pivotTableSheetPath, "xl/worksheets/") + ".rels"
|
|
|
|
f.addRels(pivotTableSheetRels, SourceRelationshipPivotTable, sheetRelationshipsPivotTableXML, "")
|
|
|
|
f.addContentTypePart(pivotTableID, "pivotTable")
|
|
|
|
f.addContentTypePart(pivotCacheID, "pivotCache")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseFormatPivotTableSet provides a function to validate pivot table
|
|
|
|
// properties.
|
|
|
|
func (f *File) parseFormatPivotTableSet(opt *PivotTableOption) (*xlsxWorksheet, string, error) {
|
|
|
|
if opt == nil {
|
2021-07-05 00:03:56 +08:00
|
|
|
return nil, "", ErrParameterRequired
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
pivotTableSheetName, _, err := f.adjustRange(opt.PivotTableRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("parameter 'PivotTableRange' parsing error: %s", err.Error())
|
|
|
|
}
|
2021-06-29 22:26:55 +08:00
|
|
|
opt.pivotTableSheetName = pivotTableSheetName
|
|
|
|
dataRange := f.getDefinedNameRefTo(opt.DataRange, pivotTableSheetName)
|
|
|
|
if dataRange == "" {
|
|
|
|
dataRange = opt.DataRange
|
|
|
|
}
|
|
|
|
dataSheetName, _, err := f.adjustRange(dataRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("parameter 'DataRange' parsing error: %s", err.Error())
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
dataSheet, err := f.workSheetReader(dataSheetName)
|
|
|
|
if err != nil {
|
|
|
|
return dataSheet, "", err
|
|
|
|
}
|
|
|
|
pivotTableSheetPath, ok := f.sheetMap[trimSheetName(pivotTableSheetName)]
|
|
|
|
if !ok {
|
|
|
|
return dataSheet, pivotTableSheetPath, fmt.Errorf("sheet %s is not exist", pivotTableSheetName)
|
|
|
|
}
|
|
|
|
return dataSheet, pivotTableSheetPath, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjustRange adjust range, for example: adjust Sheet1!$E$31:$A$1 to Sheet1!$A$1:$E$31
|
|
|
|
func (f *File) adjustRange(rangeStr string) (string, []int, error) {
|
|
|
|
if len(rangeStr) < 1 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return "", []int{}, ErrParameterRequired
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
rng := strings.Split(rangeStr, "!")
|
|
|
|
if len(rng) != 2 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return "", []int{}, ErrParameterInvalid
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
trimRng := strings.Replace(rng[1], "$", "", -1)
|
2021-08-13 01:32:44 +08:00
|
|
|
coordinates, err := areaRefToCoordinates(trimRng)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return rng[0], []int{}, err
|
|
|
|
}
|
|
|
|
x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
|
|
|
|
if x1 == x2 && y1 == y2 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return rng[0], []int{}, ErrParameterInvalid
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Correct the coordinate area, such correct C1:B3 to B1:C3.
|
|
|
|
if x2 < x1 {
|
|
|
|
x1, x2 = x2, x1
|
|
|
|
}
|
|
|
|
|
|
|
|
if y2 < y1 {
|
|
|
|
y1, y2 = y2, y1
|
|
|
|
}
|
|
|
|
return rng[0], []int{x1, y1, x2, y2}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// getPivotFieldsOrder provides a function to get order list of pivot table
|
|
|
|
// fields.
|
2021-06-29 22:26:55 +08:00
|
|
|
func (f *File) getPivotFieldsOrder(opt *PivotTableOption) ([]string, error) {
|
2022-03-24 00:19:30 +08:00
|
|
|
var order []string
|
2021-06-29 22:26:55 +08:00
|
|
|
dataRange := f.getDefinedNameRefTo(opt.DataRange, opt.pivotTableSheetName)
|
|
|
|
if dataRange == "" {
|
|
|
|
dataRange = opt.DataRange
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
dataSheet, coordinates, err := f.adjustRange(dataRange)
|
|
|
|
if err != nil {
|
|
|
|
return order, fmt.Errorf("parameter 'DataRange' parsing error: %s", err.Error())
|
|
|
|
}
|
|
|
|
for col := coordinates[0]; col <= coordinates[2]; col++ {
|
|
|
|
coordinate, _ := CoordinatesToCellName(col, coordinates[1])
|
|
|
|
name, err := f.GetCellValue(dataSheet, coordinate)
|
|
|
|
if err != nil {
|
|
|
|
return order, err
|
|
|
|
}
|
|
|
|
order = append(order, name)
|
|
|
|
}
|
|
|
|
return order, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotCache provides a function to create a pivot cache by given properties.
|
2022-03-24 00:19:30 +08:00
|
|
|
func (f *File) addPivotCache(pivotCacheXML string, opt *PivotTableOption) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// validate data range
|
2021-06-29 22:26:55 +08:00
|
|
|
definedNameRef := true
|
|
|
|
dataRange := f.getDefinedNameRefTo(opt.DataRange, opt.pivotTableSheetName)
|
|
|
|
if dataRange == "" {
|
|
|
|
definedNameRef = false
|
|
|
|
dataRange = opt.DataRange
|
|
|
|
}
|
|
|
|
dataSheet, coordinates, err := f.adjustRange(dataRange)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parameter 'DataRange' parsing error: %s", err.Error())
|
|
|
|
}
|
2020-04-10 00:04:23 +08:00
|
|
|
// data range has been checked
|
2021-06-29 22:26:55 +08:00
|
|
|
order, _ := f.getPivotFieldsOrder(opt)
|
2022-01-09 00:20:42 +08:00
|
|
|
hCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
|
|
|
|
vCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
|
2019-09-20 00:20:30 +08:00
|
|
|
pc := xlsxPivotCacheDefinition{
|
2021-03-15 23:56:36 +08:00
|
|
|
SaveData: false,
|
|
|
|
RefreshOnLoad: true,
|
|
|
|
CreatedVersion: pivotTableVersion,
|
|
|
|
RefreshedVersion: pivotTableVersion,
|
|
|
|
MinRefreshableVersion: pivotTableVersion,
|
2019-09-20 00:20:30 +08:00
|
|
|
CacheSource: &xlsxCacheSource{
|
|
|
|
Type: "worksheet",
|
|
|
|
WorksheetSource: &xlsxWorksheetSource{
|
2022-01-09 00:20:42 +08:00
|
|
|
Ref: hCell + ":" + vCell,
|
2019-09-20 00:20:30 +08:00
|
|
|
Sheet: dataSheet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CacheFields: &xlsxCacheFields{},
|
|
|
|
}
|
2021-06-29 22:26:55 +08:00
|
|
|
if definedNameRef {
|
|
|
|
pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opt.DataRange}
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
for _, name := range order {
|
2021-10-11 00:08:45 +08:00
|
|
|
rowOptions, rowOk := f.getPivotTableFieldOptions(name, opt.Rows)
|
|
|
|
columnOptions, colOk := f.getPivotTableFieldOptions(name, opt.Columns)
|
2020-09-11 00:45:52 +08:00
|
|
|
sharedItems := xlsxSharedItems{
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
s := xlsxString{}
|
2021-10-11 00:08:45 +08:00
|
|
|
if (rowOk && !rowOptions.DefaultSubtotal) || (colOk && !columnOptions.DefaultSubtotal) {
|
2020-09-11 00:45:52 +08:00
|
|
|
s = xlsxString{
|
|
|
|
V: "",
|
|
|
|
}
|
|
|
|
sharedItems.Count++
|
|
|
|
sharedItems.S = &s
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:20:30 +08:00
|
|
|
pc.CacheFields.CacheField = append(pc.CacheFields.CacheField, &xlsxCacheField{
|
2020-09-11 00:45:52 +08:00
|
|
|
Name: name,
|
|
|
|
SharedItems: &sharedItems,
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
pc.CacheFields.Count = len(pc.CacheFields.CacheField)
|
|
|
|
pivotCache, err := xml.Marshal(pc)
|
|
|
|
f.saveFileList(pivotCacheXML, pivotCache)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotTable provides a function to create a pivot table by given pivot
|
|
|
|
// table ID and properties.
|
|
|
|
func (f *File) addPivotTable(cacheID, pivotTableID int, pivotTableXML string, opt *PivotTableOption) error {
|
|
|
|
// validate pivot table range
|
|
|
|
_, coordinates, err := f.adjustRange(opt.PivotTableRange)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parameter 'PivotTableRange' parsing error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
hCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
|
|
|
|
vCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
|
2019-09-20 00:20:30 +08:00
|
|
|
|
2020-09-11 00:45:52 +08:00
|
|
|
pivotTableStyle := func() string {
|
|
|
|
if opt.PivotTableStyleName == "" {
|
|
|
|
return "PivotStyleLight16"
|
|
|
|
}
|
2020-09-15 23:31:24 +08:00
|
|
|
return opt.PivotTableStyleName
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt := xlsxPivotTableDefinition{
|
2021-03-15 23:56:36 +08:00
|
|
|
Name: fmt.Sprintf("Pivot Table%d", pivotTableID),
|
|
|
|
CacheID: cacheID,
|
|
|
|
RowGrandTotals: &opt.RowGrandTotals,
|
|
|
|
ColGrandTotals: &opt.ColGrandTotals,
|
|
|
|
UpdatedVersion: pivotTableVersion,
|
|
|
|
MinRefreshableVersion: pivotTableVersion,
|
|
|
|
ShowDrill: &opt.ShowDrill,
|
|
|
|
UseAutoFormatting: &opt.UseAutoFormatting,
|
|
|
|
PageOverThenDown: &opt.PageOverThenDown,
|
|
|
|
MergeItem: &opt.MergeItem,
|
|
|
|
CreatedVersion: pivotTableVersion,
|
|
|
|
CompactData: &opt.CompactData,
|
|
|
|
ShowError: &opt.ShowError,
|
|
|
|
DataCaption: "Values",
|
2019-09-20 00:20:30 +08:00
|
|
|
Location: &xlsxLocation{
|
2022-01-09 00:20:42 +08:00
|
|
|
Ref: hCell + ":" + vCell,
|
2019-09-20 00:20:30 +08:00
|
|
|
FirstDataCol: 1,
|
|
|
|
FirstDataRow: 1,
|
|
|
|
FirstHeaderRow: 1,
|
|
|
|
},
|
|
|
|
PivotFields: &xlsxPivotFields{},
|
|
|
|
RowItems: &xlsxRowItems{
|
|
|
|
Count: 1,
|
|
|
|
I: []*xlsxI{
|
|
|
|
{
|
|
|
|
[]*xlsxX{{}, {}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-24 23:18:02 +08:00
|
|
|
ColItems: &xlsxColItems{
|
|
|
|
Count: 1,
|
|
|
|
I: []*xlsxI{{}},
|
|
|
|
},
|
2019-09-20 00:20:30 +08:00
|
|
|
PivotTableStyleInfo: &xlsxPivotTableStyleInfo{
|
2020-09-11 00:45:52 +08:00
|
|
|
Name: pivotTableStyle(),
|
|
|
|
ShowRowHeaders: opt.ShowRowHeaders,
|
|
|
|
ShowColHeaders: opt.ShowColHeaders,
|
|
|
|
ShowRowStripes: opt.ShowRowStripes,
|
|
|
|
ShowColStripes: opt.ShowColStripes,
|
|
|
|
ShowLastColumn: opt.ShowLastColumn,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// pivot fields
|
2020-04-10 00:04:23 +08:00
|
|
|
_ = f.addPivotFields(&pt, opt)
|
2019-09-20 00:20:30 +08:00
|
|
|
|
|
|
|
// count pivot fields
|
|
|
|
pt.PivotFields.Count = len(pt.PivotFields.PivotField)
|
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// data range has been checked
|
|
|
|
_ = f.addPivotRowFields(&pt, opt)
|
|
|
|
_ = f.addPivotColFields(&pt, opt)
|
|
|
|
_ = f.addPivotPageFields(&pt, opt)
|
|
|
|
_ = f.addPivotDataFields(&pt, opt)
|
|
|
|
|
|
|
|
pivotTable, err := xml.Marshal(pt)
|
|
|
|
f.saveFileList(pivotTableXML, pivotTable)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotRowFields provides a method to add row fields for pivot table by
|
|
|
|
// given pivot table options.
|
|
|
|
func (f *File) addPivotRowFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// row fields
|
|
|
|
rowFieldsIndex, err := f.getPivotFieldsIndex(opt.Rows, opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 23:07:43 +08:00
|
|
|
for _, fieldIdx := range rowFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.RowFields == nil {
|
|
|
|
pt.RowFields = &xlsxRowFields{}
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.RowFields.Field = append(pt.RowFields.Field, &xlsxField{
|
2020-02-21 23:07:43 +08:00
|
|
|
X: fieldIdx,
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// count row fields
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.RowFields != nil {
|
|
|
|
pt.RowFields.Count = len(pt.RowFields.Field)
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2020-04-10 00:04:23 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// addPivotPageFields provides a method to add page fields for pivot table by
|
|
|
|
// given pivot table options.
|
|
|
|
func (f *File) addPivotPageFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {
|
2020-04-09 01:00:14 +08:00
|
|
|
// page fields
|
|
|
|
pageFieldsIndex, err := f.getPivotFieldsIndex(opt.Filter, opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pageFieldsName := f.getPivotTableFieldsName(opt.Filter)
|
|
|
|
for idx, pageField := range pageFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.PageFields == nil {
|
|
|
|
pt.PageFields = &xlsxPageFields{}
|
|
|
|
}
|
2020-04-09 01:00:14 +08:00
|
|
|
pt.PageFields.PageField = append(pt.PageFields.PageField, &xlsxPageField{
|
|
|
|
Name: pageFieldsName[idx],
|
|
|
|
Fld: pageField,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// count page fields
|
|
|
|
if pt.PageFields != nil {
|
|
|
|
pt.PageFields.Count = len(pt.PageFields.PageField)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotDataFields provides a method to add data fields for pivot table by
|
|
|
|
// given pivot table options.
|
|
|
|
func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opt *PivotTableOption) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// data fields
|
|
|
|
dataFieldsIndex, err := f.getPivotFieldsIndex(opt.Data, opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-26 18:53:50 +08:00
|
|
|
dataFieldsSubtotals := f.getPivotTableFieldsSubtotal(opt.Data)
|
|
|
|
dataFieldsName := f.getPivotTableFieldsName(opt.Data)
|
|
|
|
for idx, dataField := range dataFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.DataFields == nil {
|
|
|
|
pt.DataFields = &xlsxDataFields{}
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.DataFields.DataField = append(pt.DataFields.DataField, &xlsxDataField{
|
2020-02-26 18:53:50 +08:00
|
|
|
Name: dataFieldsName[idx],
|
2020-02-21 23:07:43 +08:00
|
|
|
Fld: dataField,
|
2020-02-26 18:53:50 +08:00
|
|
|
Subtotal: dataFieldsSubtotals[idx],
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// count data fields
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.DataFields != nil {
|
|
|
|
pt.DataFields.Count = len(pt.DataFields.DataField)
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// inPivotTableField provides a method to check if an element is present in
|
|
|
|
// pivot table fields list, and return the index of its location, otherwise
|
|
|
|
// return -1.
|
|
|
|
func inPivotTableField(a []PivotTableField, x string) int {
|
|
|
|
for idx, n := range a {
|
|
|
|
if x == n.Data {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
// 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 {
|
2020-09-27 13:34:39 +08:00
|
|
|
if len(opt.Data) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pt.ColFields = &xlsxColFields{}
|
|
|
|
// in order to create pivot table in case there is no input from Columns
|
|
|
|
pt.ColFields.Count = 1
|
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
|
|
|
X: -2,
|
|
|
|
})
|
2019-10-24 23:18:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pt.ColFields = &xlsxColFields{}
|
|
|
|
|
|
|
|
// col fields
|
|
|
|
colFieldsIndex, err := f.getPivotFieldsIndex(opt.Columns, opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 23:07:43 +08:00
|
|
|
for _, fieldIdx := range colFieldsIndex {
|
2019-10-24 23:18:02 +08:00
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
2020-02-21 23:07:43 +08:00
|
|
|
X: fieldIdx,
|
2019-10-24 23:18:02 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
// in order to create pivot in case there is many Columns and Data
|
2020-10-01 00:20:11 +08:00
|
|
|
if len(opt.Data) > 1 {
|
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
|
|
|
X: -2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
// count col fields
|
|
|
|
pt.ColFields.Count = len(pt.ColFields.Field)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:20:30 +08:00
|
|
|
// 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 {
|
2021-06-29 22:26:55 +08:00
|
|
|
order, err := f.getPivotFieldsOrder(opt)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-11 00:45:52 +08:00
|
|
|
x := 0
|
2019-09-20 00:20:30 +08:00
|
|
|
for _, name := range order {
|
2020-02-26 18:53:50 +08:00
|
|
|
if inPivotTableField(opt.Rows, name) != -1 {
|
2021-10-11 00:08:45 +08:00
|
|
|
rowOptions, ok := f.getPivotTableFieldOptions(name, opt.Rows)
|
2020-09-11 00:45:52 +08:00
|
|
|
var items []*xlsxItem
|
2021-10-11 00:08:45 +08:00
|
|
|
if !ok || !rowOptions.DefaultSubtotal {
|
2020-09-11 00:45:52 +08:00
|
|
|
items = append(items, &xlsxItem{X: &x})
|
|
|
|
} else {
|
|
|
|
items = append(items, &xlsxItem{T: "default"})
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
|
2021-10-11 00:08:45 +08:00
|
|
|
Name: f.getPivotTableFieldName(name, opt.Rows),
|
|
|
|
Axis: "axisRow",
|
|
|
|
Compact: &rowOptions.Compact,
|
|
|
|
Outline: &rowOptions.Outline,
|
|
|
|
DefaultSubtotal: &rowOptions.DefaultSubtotal,
|
2019-09-20 00:20:30 +08:00
|
|
|
Items: &xlsxItems{
|
2020-09-11 00:45:52 +08:00
|
|
|
Count: len(items),
|
|
|
|
Item: items,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2020-04-09 01:00:14 +08:00
|
|
|
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
|
|
|
|
}
|
2020-02-26 18:53:50 +08:00
|
|
|
if inPivotTableField(opt.Columns, name) != -1 {
|
2021-10-11 00:08:45 +08:00
|
|
|
columnOptions, ok := f.getPivotTableFieldOptions(name, opt.Columns)
|
2020-09-11 00:45:52 +08:00
|
|
|
var items []*xlsxItem
|
2021-10-11 00:08:45 +08:00
|
|
|
if !ok || !columnOptions.DefaultSubtotal {
|
2020-09-11 00:45:52 +08:00
|
|
|
items = append(items, &xlsxItem{X: &x})
|
|
|
|
} else {
|
|
|
|
items = append(items, &xlsxItem{T: "default"})
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
|
2021-10-11 00:08:45 +08:00
|
|
|
Name: f.getPivotTableFieldName(name, opt.Columns),
|
|
|
|
Axis: "axisCol",
|
|
|
|
Compact: &columnOptions.Compact,
|
|
|
|
Outline: &columnOptions.Outline,
|
|
|
|
DefaultSubtotal: &columnOptions.DefaultSubtotal,
|
2019-09-20 00:20:30 +08:00
|
|
|
Items: &xlsxItems{
|
2020-09-11 00:45:52 +08:00
|
|
|
Count: len(items),
|
|
|
|
Item: items,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2020-02-26 18:53:50 +08:00
|
|
|
if inPivotTableField(opt.Data, name) != -1 {
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
|
|
|
|
DataField: true,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// countPivotTables provides a function to get drawing files count storage in
|
|
|
|
// the folder xl/pivotTables.
|
|
|
|
func (f *File) countPivotTables() int {
|
|
|
|
count := 0
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Range(func(k, v interface{}) bool {
|
|
|
|
if strings.Contains(k.(string), "xl/pivotTables/pivotTable") {
|
2019-09-20 00:20:30 +08:00
|
|
|
count++
|
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
return true
|
|
|
|
})
|
2019-09-20 00:20:30 +08:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// countPivotCache provides a function to get drawing files count storage in
|
|
|
|
// the folder xl/pivotCache.
|
|
|
|
func (f *File) countPivotCache() int {
|
|
|
|
count := 0
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Range(func(k, v interface{}) bool {
|
|
|
|
if strings.Contains(k.(string), "xl/pivotCache/pivotCacheDefinition") {
|
2019-09-20 00:20:30 +08:00
|
|
|
count++
|
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
return true
|
|
|
|
})
|
2019-09-20 00:20:30 +08:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotFieldsIndex convert the column of the first row in the data region
|
|
|
|
// to a sequential index by given fields and pivot option.
|
2020-02-26 18:53:50 +08:00
|
|
|
func (f *File) getPivotFieldsIndex(fields []PivotTableField, opt *PivotTableOption) ([]int, error) {
|
2022-03-24 00:19:30 +08:00
|
|
|
var pivotFieldsIndex []int
|
2021-06-29 22:26:55 +08:00
|
|
|
orders, err := f.getPivotFieldsOrder(opt)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return pivotFieldsIndex, err
|
|
|
|
}
|
|
|
|
for _, field := range fields {
|
2022-02-13 00:06:30 +08:00
|
|
|
if pos := inStrSlice(orders, field.Data, true); pos != -1 {
|
2019-09-20 00:20:30 +08:00
|
|
|
pivotFieldsIndex = append(pivotFieldsIndex, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotFieldsIndex, nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// getPivotTableFieldsSubtotal prepare fields subtotal by given pivot table fields.
|
|
|
|
func (f *File) getPivotTableFieldsSubtotal(fields []PivotTableField) []string {
|
|
|
|
field := make([]string, len(fields))
|
|
|
|
enums := []string{"average", "count", "countNums", "max", "min", "product", "stdDev", "stdDevp", "sum", "var", "varp"}
|
|
|
|
inEnums := func(enums []string, val string) string {
|
|
|
|
for _, enum := range enums {
|
2021-02-15 00:09:35 +08:00
|
|
|
if strings.EqualFold(enum, val) {
|
2020-02-26 18:53:50 +08:00
|
|
|
return enum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "sum"
|
|
|
|
}
|
|
|
|
for idx, fld := range fields {
|
|
|
|
field[idx] = inEnums(enums, fld.Subtotal)
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotTableFieldsName prepare fields name list by given pivot table
|
|
|
|
// fields.
|
|
|
|
func (f *File) getPivotTableFieldsName(fields []PivotTableField) []string {
|
|
|
|
field := make([]string, len(fields))
|
|
|
|
for idx, fld := range fields {
|
2021-11-16 00:40:44 +08:00
|
|
|
if len(fld.Name) > MaxFieldLength {
|
|
|
|
field[idx] = fld.Name[:MaxFieldLength]
|
2020-02-26 18:53:50 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
field[idx] = fld.Name
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotTableFieldName prepare field name by given pivot table fields.
|
|
|
|
func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) string {
|
|
|
|
fieldsName := f.getPivotTableFieldsName(fields)
|
|
|
|
for idx, field := range fields {
|
|
|
|
if field.Data == name {
|
|
|
|
return fieldsName[idx]
|
2020-02-21 23:07:43 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 18:53:50 +08:00
|
|
|
return ""
|
2020-02-21 23:07:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 00:08:45 +08:00
|
|
|
// getPivotTableFieldOptions return options for specific field by given field name.
|
|
|
|
func (f *File) getPivotTableFieldOptions(name string, fields []PivotTableField) (options PivotTableField, ok bool) {
|
2020-09-11 00:45:52 +08:00
|
|
|
for _, field := range fields {
|
|
|
|
if field.Data == name {
|
2021-10-11 00:08:45 +08:00
|
|
|
options, ok = field, true
|
|
|
|
return
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 00:08:45 +08:00
|
|
|
return
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:28:20 +08:00
|
|
|
// addWorkbookPivotCache add the association ID of the pivot cache in workbook.xml.
|
2019-09-20 00:20:30 +08:00
|
|
|
func (f *File) addWorkbookPivotCache(RID int) int {
|
|
|
|
wb := f.workbookReader()
|
|
|
|
if wb.PivotCaches == nil {
|
|
|
|
wb.PivotCaches = &xlsxPivotCaches{}
|
|
|
|
}
|
|
|
|
cacheID := 1
|
|
|
|
for _, pivotCache := range wb.PivotCaches.PivotCache {
|
|
|
|
if pivotCache.CacheID > cacheID {
|
|
|
|
cacheID = pivotCache.CacheID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cacheID++
|
|
|
|
wb.PivotCaches.PivotCache = append(wb.PivotCaches.PivotCache, xlsxPivotCache{
|
|
|
|
CacheID: cacheID,
|
|
|
|
RID: fmt.Sprintf("rId%d", RID),
|
|
|
|
})
|
|
|
|
return cacheID
|
|
|
|
}
|