Просмотр исходного кода

Merge pull request #244 from klniu/master

make SetInt, SetInt64, SetFloat set value with CellTypeGeneral
Geoffrey J. Teale 9 лет назад
Родитель
Сommit
a58d60d3bb
3 измененных файлов с 11 добавлено и 14 удалено
  1. 4 10
      cell.go
  2. 6 3
      cell_test.go
  3. 1 1
      xmlWorksheet.go

+ 4 - 10
cell.go

@@ -74,7 +74,7 @@ func (c *Cell) String() (string, error) {
 
 // SetFloat sets the value of a cell to a float.
 func (c *Cell) SetFloat(n float64) {
-	c.SetFloatWithFormat(n, builtInNumFmt[builtInNumFmtIndex_GENERAL])
+	c.SetValue(n)
 }
 
 /*
@@ -144,10 +144,7 @@ func (c *Cell) Float() (float64, error) {
 
 // SetInt64 sets a cell's value to a 64-bit integer.
 func (c *Cell) SetInt64(n int64) {
-	c.Value = fmt.Sprintf("%d", n)
-	c.NumFmt = builtInNumFmt[builtInNumFmtIndex_INT]
-	c.formula = ""
-	c.cellType = CellTypeNumeric
+	c.SetValue(n)
 }
 
 // Int64 returns the value of cell as 64-bit integer.
@@ -161,10 +158,7 @@ func (c *Cell) Int64() (int64, error) {
 
 // SetInt sets a cell's value to an integer.
 func (c *Cell) SetInt(n int) {
-	c.Value = fmt.Sprintf("%d", n)
-	c.NumFmt = builtInNumFmt[builtInNumFmtIndex_INT]
-	c.formula = ""
-	c.cellType = CellTypeNumeric
+	c.SetValue(n)
 }
 
 // SetInt sets a cell's value to an integer.
@@ -227,7 +221,7 @@ func (c *Cell) Bool() bool {
 		return c.Value == "1"
 	}
 	// If numeric, base it on a non-zero.
-	if c.cellType == CellTypeNumeric {
+	if c.cellType == CellTypeNumeric || c.cellType == CellTypeGeneral {
 		return c.Value != "0"
 	}
 	// Return whether there's an empty string.

+ 6 - 3
cell_test.go

@@ -387,19 +387,22 @@ func (s *CellSuite) TestSetterGetters(c *C) {
 	cell.SetInt(1024)
 	intValue, _ := cell.Int()
 	c.Assert(intValue, Equals, 1024)
-	c.Assert(cell.Type(), Equals, CellTypeNumeric)
+	c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
+	c.Assert(cell.Type(), Equals, CellTypeGeneral)
 
 	cell.SetInt64(1024)
 	int64Value, _ := cell.Int64()
 	c.Assert(int64Value, Equals, int64(1024))
-	c.Assert(cell.Type(), Equals, CellTypeNumeric)
+	c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
+	c.Assert(cell.Type(), Equals, CellTypeGeneral)
 
 	cell.SetFloat(1.024)
 	float, _ := cell.Float()
 	intValue, _ = cell.Int() // convert
 	c.Assert(float, Equals, 1.024)
 	c.Assert(intValue, Equals, 1)
-	c.Assert(cell.Type(), Equals, CellTypeNumeric)
+	c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
+	c.Assert(cell.Type(), Equals, CellTypeGeneral)
 
 	cell.SetFormula("10+20")
 	c.Assert(cell.Formula(), Equals, "10+20")

+ 1 - 1
xmlWorksheet.go

@@ -277,8 +277,8 @@ type xlsxC struct {
 	R string `xml:"r,attr"`           // Cell ID, e.g. A1
 	S int    `xml:"s,attr,omitempty"` // Style reference.
 	T string `xml:"t,attr,omitempty"` // Type.
-	V string `xml:"v,omitempty"`      // Value
 	F *xlsxF `xml:"f,omitempty"`      // Formula
+	V string `xml:"v,omitempty"`      // Value
 }
 
 // xlsxF directly maps the f element in the namespace