qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.68k stars 1.69k forks source link

getPivotTable use wrong sheetName #1937

Closed ShowerBandV closed 1 week ago

ShowerBandV commented 1 week ago

Description Reading a cross sheet pivot table, it was found that the pivot table data source was obtained but not used, using the current sheetName

func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (PivotTableOptions, error) {
    ...
    opts = PivotTableOptions{
        pivotTableXML:   pivotTableXML,
        pivotCacheXML:   pivotCacheXML,
        pivotSheetName:  sheet,
                //sheet is currentsheet,but dataRange need pc.CacheSource.WorksheetSource.Sheet
        DataRange:       fmt.Sprintf("%s!%s", sheet, pc.CacheSource.WorksheetSource.Ref),
        PivotTableRange: fmt.Sprintf("%s!%s", sheet, pt.Location.Ref),
        Name:            pt.Name,
    }
    ...
}

here is demo code and a xlsx file:

func TestGetPivotTable(t *testing.T) {
    file, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {
        panic(err)
    }
    tables, err := file.GetPivotTables("Sheet2")
    if err != nil {
        panic(err)
    }
    fmt.Println(tables)
}

Book1.xlsx

xuri commented 1 week ago

Hi @ShowerBandV, thanks for your issue. Would you like to create a pull request to help us fix that?