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.62k stars 1.68k forks source link

concurrent use GetCellStyle got panic:index out of range #1903

Closed ShowerBandV closed 1 month ago

ShowerBandV commented 1 month ago

Description hi,I want to parse an xlsx file with 3000 rows and 50 columns. If the parsing time for each cell is too long, I will concurrently retrieve the properties of each cell by column, such as value content, style, cell type, etc. Then I found that the GetCellStyle method will report the following error in concurrent situations.

goroutine 29 [running]:
github.com/xuri/excelize/v2.(*File).GetCellStyle(0x5a39a0?, {0x50c341?, 0xc00009df90?}, {0xc0003fe436, 0x2})
    D:/Users/xxxx/go/pkg/mod/github.com/xuri/excelize/v2@v2.8.1/styles.go:2202 +0x295

here is my demo code and demo file

func TestGetStyle(t *testing.T) {file, err := excelize.OpenFile("1.xlsx")
    if err != nil {
        panic(err)
    }
    var wg sync.WaitGroup
        rows,column:=200,24
    for i := 0; i < column; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            for row := 0; row < rows; row++ {
                axis := AxisEncode(i, row)
                style, err := file.GetCellStyle("Sheet1", axis)
                if err != nil {
                    panic(err)
                }
                fmt.Println(style)
            }

        }(i)
    }
    wg.Wait()
}
func AxisEncode(colIndex, rowIndex int) string {
    if rowIndex > excelize.TotalRows {
        rowIndex = excelize.TotalRows - 1
    }
    if colIndex > excelize.MaxColumns {
        rowIndex = excelize.MaxColumns - 1
    }
    cellName, err := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
    if err == nil {
        return cellName
    }
    return ""
}

1.xlsx

xuri commented 1 month ago

Thanks for your issue. All the concurrency safe functions have been noted in the comments. Currently, the GetCellStyle function doesn't support calling in concurrency. I'll certainly accept that patch if somebody did that.

xuri commented 1 month ago

I have made function GetCellStyle concurrency safe, please upgrade to the master branch code, and this feature will be released in the next version.