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

how to set pivot column format #1965

Closed
xinliangliang1 opened this issue Jul 23, 2024 · 1 comment
Closed

how to set pivot column format #1965

xinliangliang1 opened this issue Jul 23, 2024 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@xinliangliang1
Copy link

Description
Sheet A I have these data
A B C
1 10% eth
2 20% eth
3. 30% btc
4. 40% btc

Sheet B is a pivot view of sheet A
eth 30%
btc 70%

however it only show like this
eth 0.3
btc 0.7

what to do to slove the problem

@xuri xuri added duplicate This issue or pull request already exists in progress Working in progress labels Aug 17, 2024
@xuri xuri moved this to Features in Excelize v2.9.0 Aug 17, 2024
@xuri xuri closed this as completed in 9a38657 Aug 17, 2024
@xuri
Copy link
Member

xuri commented Aug 17, 2024

Thanks for your issue. This issue was duplicated with #720. I just added a new NumFmt field in the PivotTableField data type, now you can set and get built-in number format of the pivot table data filed cells. For example:

package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    for idx, row := range [][]interface{}{
        {"Val", "Cat"},
        {0.1, "eth"},
        {0.2, "eth"},
        {0.3, "btc"},
        {0.4, "btc"},
    } {
        cell, err := excelize.CoordinatesToCellName(1, idx+1)
        if err != nil {
            fmt.Println(err)
            return
        }
        if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
            fmt.Println(err)
            return
        }
    }
    if err := f.AddPivotTable(&excelize.PivotTableOptions{
        DataRange:       "Sheet1!A1:B5",
        PivotTableRange: "Sheet1!G3:H6",
        Rows:            []excelize.PivotTableField{{Data: "Cat"}},
        Data:            []excelize.PivotTableField{{Data: "Val", Name: "Sum of Val", Subtotal: "Sum", NumFmt: 9}},
        RowGrandTotals:  true,
        ColGrandTotals:  true,
        ShowRowHeaders:  true,
        ShowColHeaders:  true,
        ShowLastColumn:  true,
    }); err != nil {
        fmt.Println(err)
        return
    }
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Please upgrade to the master branch code by go get -u github.com/xuri/excelize/v2@master, and this feature will be released in the next version.

@xuri xuri removed the in progress Working in progress label Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
No open projects
Status: Features
Development

No branches or pull requests

2 participants