浏览代码

Merge pull request #517 from hengmyj/fix/cell_lock

make set cell value is concurrency safe
xuri 5 年之前
父节点
当前提交
0dd616b18f
共有 1 个文件被更改,包括 13 次插入0 次删除
  1. 13 0
      cell.go

+ 13 - 0
cell.go

@@ -16,6 +16,7 @@ import (
 	"reflect"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 )
 
@@ -30,6 +31,8 @@ const (
 	STCellFormulaTypeShared = "shared"
 )
 
+var rwMutex sync.RWMutex
+
 // GetCellValue provides a function to get formatted value from cell by given
 // worksheet name and axis in XLSX file. If it is possible to apply a format
 // to the cell value, it will do so, if not then an error will be returned,
@@ -179,6 +182,8 @@ func setCellDuration(value time.Duration) (t string, v string) {
 // SetCellInt provides a function to set int type value of a cell by given
 // worksheet name, cell coordinates and cell value.
 func (f *File) SetCellInt(sheet, axis string, value int) error {
+	rwMutex.Lock()
+	defer rwMutex.Unlock()
 	xlsx, err := f.workSheetReader(sheet)
 	if err != nil {
 		return err
@@ -200,6 +205,8 @@ func setCellInt(value int) (t string, v string) {
 // SetCellBool provides a function to set bool type value of a cell by given
 // worksheet name, cell name and cell value.
 func (f *File) SetCellBool(sheet, axis string, value bool) error {
+	rwMutex.Lock()
+	defer rwMutex.Unlock()
 	xlsx, err := f.workSheetReader(sheet)
 	if err != nil {
 		return err
@@ -233,6 +240,8 @@ func setCellBool(value bool) (t string, v string) {
 //    f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32)
 //
 func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) error {
+	rwMutex.Lock()
+	defer rwMutex.Unlock()
 	xlsx, err := f.workSheetReader(sheet)
 	if err != nil {
 		return err
@@ -254,6 +263,8 @@ func setCellFloat(value float64, prec, bitSize int) (t string, v string) {
 // SetCellStr provides a function to set string type value of a cell. Total
 // number of characters that a cell can contain 32767 characters.
 func (f *File) SetCellStr(sheet, axis, value string) error {
+	rwMutex.Lock()
+	defer rwMutex.Unlock()
 	xlsx, err := f.workSheetReader(sheet)
 	if err != nil {
 		return err
@@ -327,6 +338,8 @@ type FormulaOpts struct {
 // SetCellFormula provides a function to set cell formula by given string and
 // worksheet name.
 func (f *File) SetCellFormula(sheet, axis, formula string, opts ...FormulaOpts) error {
+	rwMutex.Lock()
+	defer rwMutex.Unlock()
 	xlsx, err := f.workSheetReader(sheet)
 	if err != nil {
 		return err