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

The SetCellValue method has a bug #1528

Closed
heliangrui opened this issue Apr 21, 2023 · 8 comments
Closed

The SetCellValue method has a bug #1528

heliangrui opened this issue Apr 21, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@heliangrui
Copy link

The official documentation says that the secondary method is thread-safe, but multithreading starts with an error:
go f.SetCellValue(e.sheetName, s, nowValue)
errors: panic: runtime error: index out of range [4] with length 1

@xuri
Copy link
Member

xuri commented Apr 21, 2023

Thanks for your feedback. Could you follow the issue template, and show us a complete, standalone example program or reproducible demo?

@xuri xuri added the needs more info This issue can't reproduce, need more info label Apr 21, 2023
@heliangrui
Copy link
Author

Of course.You can see the sample: github.com/heliangrui/excel; Run the test-main method to execute the test case. You can execute the test case without modifying it. Modify the exportData method in excel/ Excel. go and add the go flag to 179,183

@xuri
Copy link
Member

xuri commented Apr 22, 2023

I have tested with the repository you mention, and it works well. I don't understand what means you said: "Modify the exportData method in excel/ Excel. go and add the go flag to 179,183". Could you follow the issue template, provide a reproducible demo with simplified codes, and provide what's the results you expected?

@heliangrui
Copy link
Author

image
excel/excel.go file

@xuri
Copy link
Member

xuri commented Apr 23, 2023

Thanks for your feedback. The statements already exist same with you want to change them. Could you follow the issue template, provide a reproducible demo with simplified codes, and provide what're the results you expected? That will be helpful in quickly understanding and resolving this problem.

@heliangrui
Copy link
Author

image
I have submitted this error, repull:
https://github.com/heliangrui/excel
Run the test/main.go file under the main method

errorMessage:
panic: runtime error: index out of range [4] with length 2

goroutine 81895 [running]:
github.com/xuri/excelize/v2.(*File).prepareCell(0xc000160160?, 0xc00091a1c0, {0xc01a4836f0?, 0xc01a60beb0?})
E:/project/pkg/mod/github.com/xuri/excelize/v2@v2.7.1/cell.go:1266 +0x25e
github.com/xuri/excelize/v2.(*File).SetCellStr(0xc01a60bf78?, {0xfb3150, 0x9}, {0xc01a4836f0, 0x6}, {0x0, 0x0})
E:/project/pkg/mod/github.com/xuri/excelize/v2@v2.7.1/cell.go:383 +0xab
github.com/xuri/excelize/v2.(*File).SetCellValue(0x0?, {0xfb3150, 0x9}, {0xc01a4836f0, 0x6}, {0xfa8520?, 0xc01a477b90?})
E:/project/pkg/mod/github.com/xuri/excelize/v2@v2.7.1/cell.go:153 +0x43d
created by github.com/heliangrui/excel.(*Export[...]).exportData
E:/project/excel/excel.go:179 +0x3ab

@xuri xuri added bug Something isn't working in progress Working in progress and removed needs more info This issue can't reproduce, need more info labels Apr 24, 2023
@xuri xuri closed this as completed in 612f6f1 Apr 25, 2023
@xuri
Copy link
Member

xuri commented Apr 25, 2023

Thank you for your issue. I have fixed race condition issues, please upgrade to the master branch code, and this patch will be released in the next version. In addition, you need to wait for all the goroutines launched here to finish in your code excel.go:

diff --git a/excel.go b/excel.go
index f1371b5..15ebb92 100644
--- a/excel.go
+++ b/excel.go
@@ -165,7 +166,6 @@ func (e *Export[T]) setHeadStyle(style *excelize.Style) *Export[T] {
 
 func (e *Export[T]) exportData(object []T, start int) *Export[T] {
 	obLen := len(object)
-	e.wg.Add(1)
 	e.setRowHeight(start, obLen)
 	for i := 0; i < obLen; i++ {
 		mod := object[i]
@@ -175,16 +175,20 @@ func (e *Export[T]) exportData(object []T, start int) *Export[T] {
 			nowValue := value.FieldByName(fieldName)
 			name, _ := excelize.ColumnNumberToName(r + 1)
 			s := name + strconv.Itoa(i+start+e.headRowHeight)
-			if m.toExcelFormat == "" {
-				go e.f.SetCellValue(e.sheetName, s, nowValue)
-			} else {
-				toExcelFun := value.MethodByName(m.toExcelFormat)
-				call := toExcelFun.Call(nil)
-				go e.f.SetCellValue(e.sheetName, s, call[0])
-			}
+			e.wg.Add(1)
+			go func() {
+				defer e.wg.Done()
+				if m.toExcelFormat == "" {
+					e.f.SetCellValue(e.sheetName, s, nowValue)
+				} else {
+					toExcelFun := value.MethodByName(m.toExcelFormat)
+					call := toExcelFun.Call(nil)
+					e.f.SetCellValue(e.sheetName, s, call[0])
+				}
+			}()
 		}
 	}
-	e.wg.Done()
+	e.wg.Wait()
 	return e
 }

@xuri xuri removed the in progress Working in progress label Apr 25, 2023
@heliangrui
Copy link
Author

Thanks♪(・ω・)ノ

xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
- Avoid format text cell value as a numeric
- Fix race conditions for concurrency safety functions
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
- Avoid format text cell value as a numeric
- Fix race conditions for concurrency safety functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants