From 0c3dfb16054d60dd22ce43429fb5f0ddf64b1a4f Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 25 May 2024 14:54:59 +0800 Subject: [PATCH] This closes #1903, made GetCellStyle function concurrency safe - Update comment of the function and unit test --- cell_test.go | 3 +++ styles.go | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cell_test.go b/cell_test.go index 7517d28f6e..be974d5a3c 100644 --- a/cell_test.go +++ b/cell_test.go @@ -42,6 +42,9 @@ func TestConcurrency(t *testing.T) { assert.NoError(t, err) // Concurrency set cell style assert.NoError(t, f.SetCellStyle("Sheet1", "A3", "A3", style)) + // Concurrency get cell style + _, err = f.GetCellStyle("Sheet1", "A3") + assert.NoError(t, err) // Concurrency add picture assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{ diff --git a/styles.go b/styles.go index ccd758a6c2..7aee0ff575 100644 --- a/styles.go +++ b/styles.go @@ -2186,19 +2186,22 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a } // GetCellStyle provides a function to get cell style index by given worksheet -// name and cell reference. +// name and cell reference. This function is concurrency safe. func (f *File) GetCellStyle(sheet, cell string) (int, error) { + f.mu.Lock() ws, err := f.workSheetReader(sheet) if err != nil { + f.mu.Unlock() return 0, err } + f.mu.Unlock() + ws.mu.Lock() + defer ws.mu.Unlock() col, row, err := CellNameToCoordinates(cell) if err != nil { return 0, err } ws.prepareSheetXML(col, row) - ws.mu.Lock() - defer ws.mu.Unlock() return ws.prepareCellStyle(col, row, ws.SheetData.Row[row-1].C[col-1].S), err }