|
|
@@ -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:
|