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

Timelines/slicers in pivot tables #804

Closed
Amodio opened this issue Mar 14, 2021 · 2 comments
Closed

Timelines/slicers in pivot tables #804

Amodio opened this issue Mar 14, 2021 · 2 comments
Labels
confirmed This issue can be reproduced

Comments

@Amodio
Copy link
Contributor

Amodio commented Mar 14, 2021

Hello,

I am trying to add a timeline to a pivot table generated by Excelize.
This cannot be done even in Excel (compatibility issue), you can run & try this code:

package main

import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize"
    "math/rand"
    "time"
)

// Write a random table to a given sheetName
func createTable(f *excelize.File, sheetName string) {
    // Write headers of the table
    headers := []string{ "Animal", "Date", "Number" }
    for i, str := range headers {
        cellName, _ := excelize.CoordinatesToCellName(1 + i, 1)
        f.SetCellValue(sheetName, cellName, str)
    }
    // Fill the lines of the table
    labels := []string{ "Elephant", "Pangolin", "Alligator", "Antelope", "Ape",
        "Armadillo", "Aye-Aye", "Baboon", "Badger", "Bandicoot", "Bat(s)" }
    // Set the date format (cell style)
    style, _ := f.NewStyle(`{
    "number_format": 15,
    "alignment":
    {
        "horizontal": "center",
        "shrink_to_fit": true,
        "vertical": "center"
    }
}`)
    for i, str := range labels {
        cellName, _ := excelize.CoordinatesToCellName(1, 2 + i)
        f.SetCellValue(sheetName, cellName, str)
        cellName, _ = excelize.CoordinatesToCellName(2, 2 + i)
        f.SetCellValue(sheetName, cellName, func() (date time.Time) {
            date = time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
            return date.Add(time.Hour * time.Duration(24 * rand.Intn(1337)))
        }())
        f.SetCellStyle(sheetName, cellName, cellName, style)
        cellName, _ = excelize.CoordinatesToCellName(3, 2 + i)
        f.SetCellValue(sheetName, cellName, rand.Intn(4))
    }
    // Create the table
    if err := f.AddTable(sheetName, "A1", "C12", `{
        "table_name": "animalsTable",
        "table_style": "TableStyleMedium2",
        "show_first_column": true,
        "show_last_column": true,
        "show_row_stripes": true,
        "show_column_stripes": false
    }`); err != nil {
        panic(err)
    }
}

func createPivot(f *excelize.File) {
    if err := f.AddPivotTable(&excelize.PivotTableOption{
        DataRange:       "Sheet1!$A$1:$C$12",
        PivotTableRange: "Sheet2!$A$3:$G$16",
        Rows: []excelize.PivotTableField{
            {Data: "Animal", DefaultSubtotal: true}},
        //Filter: []excelize.PivotTableField{{Data: "Date"}},
        Columns: []excelize.PivotTableField{
            {Data: "Number", DefaultSubtotal: true}},
        Data: []excelize.PivotTableField{
            {Data: "Date", Name: "Animals", Subtotal: "Count"}},
        RowGrandTotals: true,
        ColGrandTotals: true,
        ShowDrill:      true,
        ShowRowHeaders: true,
        ShowColHeaders: true,
        ShowLastColumn: true,
    }); err != nil {
        fmt.Println(err)
    }
}

func main() {
    rand.Seed(42)
    f := excelize.NewFile()
    // Write a random table in the 1st sheet.
    createTable(f, "Sheet1")
    // Create a new sheet.
    index := f.NewSheet("Sheet2")
    // Create a pivot table in the 2nd sheet.
    createPivot(f)
    // Set active sheet of the workbook.
    f.SetActiveSheet(index)
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Can you please add this feature? This is something I need along with pivot charts.

Thanks

@xuri xuri added enhancement New feature or request confirmed This issue can be reproduced in progress Working in progress labels Mar 15, 2021
@xuri xuri closed this as completed in 9af00b9 Mar 15, 2021
@xuri
Copy link
Member

xuri commented Mar 15, 2021

Thanks for your issue, I have fixed it, please try to use the master branch code, and this patch will be released in the next version.

@xuri xuri removed enhancement New feature or request in progress Working in progress labels Mar 15, 2021
@Amodio
Copy link
Contributor Author

Amodio commented Mar 15, 2021

Thank you for the quick fix! I am eager to generate pivot charts w/ timelines by coding :)

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed This issue can be reproduced
Projects
None yet
Development

No branches or pull requests

2 participants