This closes #1903, made GetCellStyle function concurrency safe

- Update comment of the function and unit test
This commit is contained in:
xuri 2024-05-25 14:54:59 +08:00
parent 42ad4d6959
commit 0c3dfb1605
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 9 additions and 3 deletions

View File

@ -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{

View File

@ -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
}