Browse Source

added int64(), setint64(), and (*Row)WriteSlice() functions

Eric 11 years ago
parent
commit
651c094057
3 changed files with 122 additions and 3 deletions
  1. 19 0
      cell.go
  2. 61 3
      write.go
  3. 42 0
      write_test.go

+ 19 - 0
cell.go

@@ -90,6 +90,23 @@ func (c *Cell) Float() (float64, error) {
 	return f, nil
 }
 
+// Set a 64-bit integer
+func (c *Cell) SetInt64(n int64) {
+	c.Value = fmt.Sprintf("%d", n)
+	c.numFmt = "0"
+	c.formula = ""
+	c.cellType = CellTypeNumeric
+}
+
+// Returns the value of cell as 64-bit integer
+func (c *Cell) Int64() (int64, error) {
+	f, err := strconv.ParseInt(c.Value, 10, 64)
+	if err != nil {
+		return -1, err
+	}
+	return f, nil
+}
+
 // Set integer
 func (c *Cell) SetInt(n int) {
 	c.Value = fmt.Sprintf("%d", n)
@@ -99,6 +116,8 @@ func (c *Cell) SetInt(n int) {
 }
 
 // Returns the value of cell as integer
+// Has max 53 bits of precision
+// See: float64(int64(math.MaxInt))
 func (c *Cell) Int() (int, error) {
 	f, err := strconv.ParseFloat(c.Value, 64)
 	if err != nil {

+ 61 - 3
write.go

@@ -2,8 +2,64 @@ package xlsx
 
 import "reflect"
 
+// Writes an array to row r. Accepts a pointer to array type 'e',
+// and writes the number of columns to write, 'cols'. If 'cols' is < 0,
+// the entire array will be written if possible. Retuens -1 if the 'e'
+// doesn't point to an array, otherwise the number of columns written.
+func (r *Row) WriteSlice(e interface{}, cols int) int {
+	if cols == 0 {
+		return cols
+	}
+
+	t := reflect.TypeOf(e).Elem()
+	if t.Kind() != reflect.Slice { // is 'e' even a slice?
+		return -1
+	}
+
+	// it's a slice, so open up its values
+	v := reflect.ValueOf(e).Elem()
+
+	n := v.Len()
+	if cols < n && cols > 0 {
+		n = cols
+	}
+
+	var i int
+	switch t.Elem().Kind() { // underlying type of slice
+	case reflect.String:
+		for i = 0; i < n; i++ {
+			cell := r.AddCell()
+			cell.SetString(v.Index(i).Interface().(string))
+		}
+	case reflect.Int, reflect.Int8,
+		reflect.Int16, reflect.Int32:
+		for i = 0; i < n; i++ {
+			cell := r.AddCell()
+			cell.SetInt(v.Index(i).Interface().(int))
+		}
+	case reflect.Int64:
+		for i = 0; i < n; i++ {
+			cell := r.AddCell()
+			cell.SetInt64(v.Index(i).Interface().(int64))
+		}
+	case reflect.Bool:
+		for i = 0; i < n; i++ {
+			cell := r.AddCell()
+			cell.SetBool(v.Index(i).Interface().(bool))
+		}
+	case reflect.Float64, reflect.Float32:
+		for i = 0; i < n; i++ {
+			cell := r.AddCell()
+			cell.SetFloat(v.Index(i).Interface().(float64))
+		}
+	}
+
+	return i
+}
+
 // Writes a struct to row r. Accepts a pointer to struct type 'e',
-// and the number of columns to write, `cols`. Returns -1 if the 'e'
+// and the number of columns to write, `cols`. If 'cols' is < 0,
+// the entire struct will be written if possible. Returns -1 if the 'e'
 // doesn't point to a struct, otherwise the number of columns written
 func (r *Row) WriteStruct(e interface{}, cols int) int {
 	if cols == 0 {
@@ -26,9 +82,11 @@ func (r *Row) WriteStruct(e interface{}, cols int) int {
 		cell := r.AddCell()
 
 		switch f {
-		case reflect.Int, reflect.Int8, reflect.Int16,
-			reflect.Int32, reflect.Int64:
+		case reflect.Int, reflect.Int8,
+			reflect.Int16, reflect.Int32:
 			cell.SetInt(v.Field(i).Interface().(int))
+		case reflect.Int64:
+			cell.SetInt64(v.Field(i).Interface().(int64))
 		case reflect.String:
 			cell.SetString(v.Field(i).Interface().(string))
 		case reflect.Float64, reflect.Float32:

+ 42 - 0
write_test.go

@@ -42,3 +42,45 @@ func (r *RowSuite) TestWriteStruct(c *C) {
 	c.Assert(e1, Equals, nil)
 	c.Assert(e2, Equals, nil)
 }
+
+// Test if we can write a slice to a row
+func (r *RowSuite) TestWriteSlice(c *C) {
+	var f *File
+	f = NewFile()
+	sheet := f.AddSheet("Test1")
+
+	type strA []string
+	type intA []int
+	type floatA []float64
+	type boolA []bool
+
+	s0 := strA{"Eric"}
+	row0 := sheet.AddRow()
+	row0.WriteSlice(&s0, -1)
+	c.Assert(row0, NotNil)
+	c0 := row0.Cells[0].String()
+	c.Assert(c0, Equals, "Eric")
+
+	s1 := intA{10}
+	row1 := sheet.AddRow()
+	row1.WriteSlice(&s1, -1)
+	c.Assert(row1, NotNil)
+	c1, e1 := row1.Cells[0].Int()
+	c.Assert(e1, Equals, nil)
+	c.Assert(c1, Equals, 10)
+
+	s2 := floatA{3.94}
+	row2 := sheet.AddRow()
+	row2.WriteSlice(&s2, -1)
+	c.Assert(row2, NotNil)
+	c2, e2 := row2.Cells[0].Float()
+	c.Assert(e2, Equals, nil)
+	c.Assert(c2, Equals, 3.94)
+
+	s3 := boolA{true}
+	row3 := sheet.AddRow()
+	row3.WriteSlice(&s3, -1)
+	c.Assert(row3, NotNil)
+	c3 := row3.Cells[0].Bool()
+	c.Assert(c3, Equals, true)
+}