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

Open
Zncl2222 opened this issue Sep 19, 2024 · 0 comments
Open

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

Zncl2222 opened this issue Sep 19, 2024 · 0 comments

Comments

@Zncl2222
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant