Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ShowerBandV opened this issue May 23, 2024 · 2 comments
Closed

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

ShowerBandV opened this issue May 23, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@ShowerBandV
Copy link
Contributor

ShowerBandV commented May 23, 2024

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/[email protected]/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
Copy link
Member

xuri commented May 25, 2024

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 xuri added the enhancement New feature or request label May 25, 2024
@xuri xuri closed this as completed in 5f05180 May 25, 2024
xuri added a commit that referenced this issue May 25, 2024
… functions concurrency safe

- Update comments of the functions and unit tests
xuri added a commit that referenced this issue May 25, 2024
… functions concurrency safe

- Update comments of the functions and unit tests
xuri added a commit that referenced this issue May 25, 2024
- Update comment of the function and unit test
@xuri
Copy link
Member

xuri commented May 25, 2024

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

@xuri xuri moved this to Features in Excelize v2.9.0 Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Status: Features
Development

No branches or pull requests

2 participants