Skip to content

Commit 0c3dfb1

Browse files
committed
This closes #1903, made GetCellStyle function concurrency safe
- Update comment of the function and unit test
1 parent 42ad4d6 commit 0c3dfb1

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

cell_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func TestConcurrency(t *testing.T) {
4242
assert.NoError(t, err)
4343
// Concurrency set cell style
4444
assert.NoError(t, f.SetCellStyle("Sheet1", "A3", "A3", style))
45+
// Concurrency get cell style
46+
_, err = f.GetCellStyle("Sheet1", "A3")
47+
assert.NoError(t, err)
4548
// Concurrency add picture
4649
assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
4750
&GraphicOptions{

styles.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,19 +2186,22 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a
21862186
}
21872187

21882188
// GetCellStyle provides a function to get cell style index by given worksheet
2189-
// name and cell reference.
2189+
// name and cell reference. This function is concurrency safe.
21902190
func (f *File) GetCellStyle(sheet, cell string) (int, error) {
2191+
f.mu.Lock()
21912192
ws, err := f.workSheetReader(sheet)
21922193
if err != nil {
2194+
f.mu.Unlock()
21932195
return 0, err
21942196
}
2197+
f.mu.Unlock()
2198+
ws.mu.Lock()
2199+
defer ws.mu.Unlock()
21952200
col, row, err := CellNameToCoordinates(cell)
21962201
if err != nil {
21972202
return 0, err
21982203
}
21992204
ws.prepareSheetXML(col, row)
2200-
ws.mu.Lock()
2201-
defer ws.mu.Unlock()
22022205
return ws.prepareCellStyle(col, row, ws.SheetData.Row[row-1].C[col-1].S), err
22032206
}
22042207

0 commit comments

Comments
 (0)