Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting the classic layout for pivot table #1993

Closed
Zncl2222 opened this issue Sep 19, 2024 · 1 comment
Closed

Add support for setting the classic layout for pivot table #1993

Zncl2222 opened this issue Sep 19, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Zncl2222
Copy link
Contributor

Description

It seems that excelize does not support setting the classic layout for pivot tables currently.

image

Howevere I needed this function, so I made some adjustments to implement it.

You can find implementation here:

Zncl2222@5f7a4ce

The classic layout need to set Compact and CompactData to false, set GridDropZones to true in PivotTableDefinition, and set Outline and Compact in each field to false

I am happy to submit a PR if you find this idea valuable. If you approve this proposal, I will modify the commit message to follow the style of excelize's commit messages and add more test cases if needed.

Example

In this implementation Zncl2222@5f7a4ce, user only need to set the ClassicLayout to true in PivotTableOptions to configure the pivot table in classic layout.

The code snippet to create the pivot table with classic layout

package main

import (
	"fmt"

	"github.com/xuri/excelize/v2"

	"golang.org/x/exp/rand"
)

func main() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	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 row := 2; row < 32; row++ {
		f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), month[rand.Intn(12)])
		f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), year[rand.Intn(3)])
		f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), types[rand.Intn(4)])
		f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(5000))
		f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)])
	}

	if err := f.AddPivotTable(&excelize.PivotTableOptions{
		DataRange:       "Sheet1!A1:E31",
		PivotTableRange: "Sheet1!G2:M34",
		Rows: []excelize.PivotTableField{
			{Data: "Month", Name: "I Am Month", DefaultSubtotal: true}, {Data: "Year", Name: "I Am Year"}},
		Filter: []excelize.PivotTableField{
			{Data: "Region"}},
		Columns: []excelize.PivotTableField{
			{Data: "Type", DefaultSubtotal: true, Name: "I Am Columns"}},
		Data: []excelize.PivotTableField{
			{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}},
		RowGrandTotals: true,
		ColGrandTotals: true,
		ShowDrill:      true,
		ShowRowHeaders: true,
		ShowColHeaders: true,
		ShowLastColumn: true,
		ClassicLayout:  true,
	}); err != nil {
		fmt.Println(err)
		return
	}

	if err := f.SaveAs("PivotTable.xlsx"); err != nil {
		fmt.Println(err)
	}
}

image

If the ClassicLayout property is unset or set to false, the pivot table will look like this:

image

@xuri xuri added the enhancement New feature or request label Sep 21, 2024
@xuri xuri closed this as completed in 41c7dd3 Sep 21, 2024
@xuri
Copy link
Member

xuri commented Sep 21, 2024

Thanks for your issue. I have added get and set pivot table classical layout support by referenced your code. Please upgrade the master branch code by go get -u github.com/xuri/excelize/v2@master, and this feature will be released in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Status: Features
Development

No branches or pull requests

2 participants