Jelajahi Sumber

Fixed: unsafe when generate new NumFmtId;

Hugh Gao 10 tahun lalu
induk
melakukan
30b05c0ae3
3 mengubah file dengan 13 tambahan dan 15 penghapusan
  1. 1 5
      sheet.go
  2. 11 9
      xmlStyle.go
  3. 1 1
      xmlStyle_test.go

+ 1 - 5
sheet.go

@@ -110,12 +110,8 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 				xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
 				xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
 				fontId := styles.addFont(xFont)
 				fontId := styles.addFont(xFont)
 				fillId := styles.addFill(xFill)
 				fillId := styles.addFill(xFill)
-				// generate NumFmtId
+				// generate NumFmtId and add new NumFmt
 				xNumFmt := styles.newNumFmt(cell.numFmt)
 				xNumFmt := styles.newNumFmt(cell.numFmt)
-				// the NumFmtId of NumFmt user defined is greater than 164
-				if xNumFmt.NumFmtId >= 164 {
-					styles.addNumFmt(xNumFmt)
-				}
 
 
 				// HACK - adding light grey fill, as in OO and Google
 				// HACK - adding light grey fill, as in OO and Google
 				greyfill := xlsxFill{}
 				greyfill := xlsxFill{}

+ 11 - 9
xmlStyle.go

@@ -15,10 +15,14 @@ import (
 	"sync"
 	"sync"
 )
 )
 
 
+// Excel styles can reference number formats that are built-in, all of which
+// have an id less than 164.
+const builtinNumFmtsCount = 163
+
 // Excel styles can reference number formats that are built-in, all of which
 // Excel styles can reference number formats that are built-in, all of which
 // have an id less than 164. This is a possibly incomplete list comprised of as
 // have an id less than 164. This is a possibly incomplete list comprised of as
 // many of them as I could find.
 // many of them as I could find.
-var builtInNumFmt map[int]string = map[int]string{
+var builtInNumFmt = map[int]string{
 	0:  "general",
 	0:  "general",
 	1:  "0",
 	1:  "0",
 	2:  "0.00",
 	2:  "0.00",
@@ -176,12 +180,7 @@ func (styles *xlsxStyleSheet) argbValue(color xlsxColor) string {
 // have an id less than 164. This is a possibly incomplete list comprised of as
 // have an id less than 164. This is a possibly incomplete list comprised of as
 // many of them as I could find.
 // many of them as I could find.
 func getBuiltinNumberFormat(numFmtId int) string {
 func getBuiltinNumberFormat(numFmtId int) string {
-	formatCode, ok := builtInNumFmt[numFmtId]
-	if ok {
-		return formatCode
-	} else {
-		return ""
-	}
+	return builtInNumFmt[numFmtId]
 }
 }
 
 
 func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
 func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
@@ -287,21 +286,24 @@ func (styles *xlsxStyleSheet) newNumFmt(formatCode string) xlsxNumFmt {
 	}
 	}
 
 
 	// The user define NumFmtId. The one less than 164 in built in.
 	// The user define NumFmtId. The one less than 164 in built in.
-	numFmtId = 164
+	numFmtId = builtinNumFmtsCount + 1
+	styles.lock.Lock()
 	for {
 	for {
 		// get a unused NumFmtId
 		// get a unused NumFmtId
 		if _, ok = styles.numFmtRefTable[numFmtId]; ok {
 		if _, ok = styles.numFmtRefTable[numFmtId]; ok {
 			numFmtId += 1
 			numFmtId += 1
 		} else {
 		} else {
+			styles.addNumFmt(xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode})
 			break
 			break
 		}
 		}
 	}
 	}
+	styles.lock.Unlock()
 	return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
 	return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
 }
 }
 
 
 func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) (index int) {
 func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) (index int) {
 	// don't add built in NumFmt
 	// don't add built in NumFmt
-	if xNumFmt.NumFmtId < 164 {
+	if xNumFmt.NumFmtId <= builtinNumFmtsCount {
 		return -1
 		return -1
 	}
 	}
 	numFmt, ok := styles.numFmtRefTable[xNumFmt.NumFmtId]
 	numFmt, ok := styles.numFmtRefTable[xNumFmt.NumFmtId]

+ 1 - 1
xmlStyle_test.go

@@ -304,7 +304,7 @@ func (x *XMLStyleSuite) TestXfEquals(c *C) {
 }
 }
 
 
 func (s *CellSuite) TestNewNumFmt(c *C) {
 func (s *CellSuite) TestNewNumFmt(c *C) {
-	styles := &xlsxStyleSheet{}
+	styles := newXlsxStyleSheet(nil)
 	styles.NumFmts = xlsxNumFmts{}
 	styles.NumFmts = xlsxNumFmts{}
 	styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
 	styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)