write.go 2.3 KB

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