write.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package xlsx
  2. import "reflect"
  3. // Writes an array to row r. Accepts a pointer to array type 'e',
  4. // and writes the number of columns to write, 'cols'. If 'cols' is < 0,
  5. // the entire array will be written if possible. Returns -1 if the 'e'
  6. // doesn't point to an array, otherwise the number of columns written.
  7. func (r *Row) WriteSlice(e interface{}, cols int) int {
  8. if cols == 0 {
  9. return cols
  10. }
  11. // it's a slice, so open up its values
  12. v := reflect.ValueOf(e).Elem()
  13. if v.Kind() != reflect.Slice { // is 'e' even a slice?
  14. return -1
  15. }
  16. n := v.Len()
  17. if cols < n && cols > 0 {
  18. n = cols
  19. }
  20. var setCell func(reflect.Value)
  21. setCell = func(val reflect.Value) {
  22. switch val.Kind() { // underlying type of slice
  23. case reflect.String:
  24. cell := r.AddCell()
  25. cell.SetString(val.Interface().(string))
  26. case reflect.Int, reflect.Int8,
  27. reflect.Int16, reflect.Int32:
  28. cell := r.AddCell()
  29. cell.SetInt(val.Interface().(int))
  30. case reflect.Int64:
  31. cell := r.AddCell()
  32. cell.SetInt64(val.Interface().(int64))
  33. case reflect.Bool:
  34. cell := r.AddCell()
  35. cell.SetBool(val.Interface().(bool))
  36. case reflect.Float64, reflect.Float32:
  37. cell := r.AddCell()
  38. cell.SetFloat(val.Interface().(float64))
  39. case reflect.Interface:
  40. setCell(reflect.ValueOf(val.Interface()))
  41. }
  42. }
  43. var i int
  44. for i = 0; i < n; i++ {
  45. setCell(v.Index(i))
  46. }
  47. return i
  48. }
  49. // Writes a struct to row r. Accepts a pointer to struct type 'e',
  50. // and the number of columns to write, `cols`. If 'cols' is < 0,
  51. // the entire struct will be written if possible. Returns -1 if the 'e'
  52. // doesn't point to a struct, otherwise the number of columns written
  53. func (r *Row) WriteStruct(e interface{}, cols int) int {
  54. if cols == 0 {
  55. return cols
  56. }
  57. v := reflect.ValueOf(e).Elem()
  58. if v.Kind() != reflect.Struct {
  59. return -1 // bail if it's not a struct
  60. }
  61. n := v.NumField() // number of fields in struct
  62. if cols < n && cols > 0 {
  63. n = cols
  64. }
  65. var k int
  66. for i := 0; i < n; i, k = i+1, k+1 {
  67. f := v.Field(i).Kind()
  68. cell := r.AddCell()
  69. switch f {
  70. case reflect.Int, reflect.Int8,
  71. reflect.Int16, reflect.Int32:
  72. cell.SetInt(v.Field(i).Interface().(int))
  73. case reflect.Int64:
  74. cell.SetInt64(v.Field(i).Interface().(int64))
  75. case reflect.String:
  76. cell.SetString(v.Field(i).Interface().(string))
  77. case reflect.Float64, reflect.Float32:
  78. cell.SetFloat(v.Field(i).Interface().(float64))
  79. case reflect.Bool:
  80. cell.SetBool(v.Field(i).Interface().(bool))
  81. default:
  82. k-- // nothing set so reset to previous
  83. }
  84. }
  85. return k
  86. }