Skip to content

Commit

Permalink
This closes #1903, made GetCellStyle function concurrency safe
Browse files Browse the repository at this point in the history
- Update comment of the function and unit test
  • Loading branch information
xuri committed May 25, 2024
1 parent 42ad4d6 commit 0c3dfb1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 6 additions & 3 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0c3dfb1

Please sign in to comment.