write_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package xlsx
  2. import (
  3. "database/sql"
  4. "math"
  5. "time"
  6. . "gopkg.in/check.v1"
  7. )
  8. type WriteSuite struct{}
  9. var _ = Suite(&WriteSuite{})
  10. type testStringerImpl struct {
  11. Value string
  12. }
  13. func (this testStringerImpl) String() string {
  14. return this.Value
  15. }
  16. // Test if we can write a struct to a row
  17. func (r *RowSuite) TestWriteStruct(c *C) {
  18. var f *File
  19. f = NewFile()
  20. sheet, _ := f.AddSheet("Test1")
  21. row := sheet.AddRow()
  22. type e struct {
  23. FirstName string
  24. Age int
  25. GPA float64
  26. LikesPHP bool
  27. Stringer testStringerImpl
  28. StringerPtr *testStringerImpl
  29. Time time.Time
  30. LastName sql.NullString
  31. HasPhd sql.NullBool
  32. GithubStars sql.NullInt64
  33. Raiting sql.NullFloat64
  34. NullLastName sql.NullString
  35. NullHasPhd sql.NullBool
  36. NullGithubStars sql.NullInt64
  37. NullRaiting sql.NullFloat64
  38. }
  39. testStruct := e{
  40. "Eric",
  41. 20,
  42. 3.94,
  43. false,
  44. testStringerImpl{"Stringer"},
  45. &testStringerImpl{"Pointer to Stringer"},
  46. time.Unix(0, 0),
  47. sql.NullString{String: `Smith`, Valid: true},
  48. sql.NullBool{Bool: false, Valid: true},
  49. sql.NullInt64{Int64: 100, Valid: true},
  50. sql.NullFloat64{Float64: 0.123, Valid: true},
  51. sql.NullString{String: `What ever`, Valid: false},
  52. sql.NullBool{Bool: true, Valid: false},
  53. sql.NullInt64{Int64: 100, Valid: false},
  54. sql.NullFloat64{Float64: 0.123, Valid: false},
  55. }
  56. cnt := row.WriteStruct(&testStruct, -1)
  57. c.Assert(cnt, Equals, 15)
  58. c.Assert(row, NotNil)
  59. var (
  60. c0, c4, c5, c7, c11, c12, c13, c14 string
  61. err error
  62. c6 float64
  63. )
  64. if c0, err = row.Cells[0].FormattedValue(); err != nil {
  65. c.Error(err)
  66. }
  67. c1, e1 := row.Cells[1].Int()
  68. c2, e2 := row.Cells[2].Float()
  69. c3 := row.Cells[3].Bool()
  70. if c4, err = row.Cells[4].FormattedValue(); err != nil {
  71. c.Error(err)
  72. }
  73. if c5, err = row.Cells[5].FormattedValue(); err != nil {
  74. c.Error(err)
  75. }
  76. if c6, err = row.Cells[6].Float(); err != nil {
  77. c.Error(err)
  78. }
  79. if c7, err = row.Cells[7].FormattedValue(); err != nil {
  80. c.Error(err)
  81. }
  82. c8 := row.Cells[8].Bool()
  83. c9, e9 := row.Cells[9].Int()
  84. c10, e10 := row.Cells[10].Float()
  85. if c11, err = row.Cells[11].FormattedValue(); err != nil {
  86. c.Error(err)
  87. }
  88. if c12, err = row.Cells[12].FormattedValue(); err != nil {
  89. c.Error(err)
  90. }
  91. if c13, err = row.Cells[13].FormattedValue(); err != nil {
  92. c.Error(err)
  93. }
  94. if c14, err = row.Cells[14].FormattedValue(); err != nil {
  95. c.Error(err)
  96. }
  97. c.Assert(c0, Equals, "Eric")
  98. c.Assert(c1, Equals, 20)
  99. c.Assert(c2, Equals, 3.94)
  100. c.Assert(c3, Equals, false)
  101. c.Assert(c4, Equals, "Stringer")
  102. c.Assert(c5, Equals, "Pointer to Stringer")
  103. c.Assert(math.Floor(c6), Equals, 25569.0)
  104. c.Assert(c7, Equals, `Smith`)
  105. c.Assert(c8, Equals, false)
  106. c.Assert(c9, Equals, 100)
  107. c.Assert(c10, Equals, 0.123)
  108. c.Assert(c11, Equals, ``)
  109. c.Assert(c12, Equals, ``)
  110. c.Assert(c13, Equals, ``)
  111. c.Assert(c14, Equals, ``)
  112. c.Assert(e1, Equals, nil)
  113. c.Assert(e2, Equals, nil)
  114. c.Assert(e9, Equals, nil)
  115. c.Assert(e10, Equals, nil)
  116. }
  117. // Test if we can write a slice to a row
  118. func (r *RowSuite) TestWriteSlice(c *C) {
  119. var f *File
  120. f = NewFile()
  121. sheet, _ := f.AddSheet("Test1")
  122. type strA []string
  123. type intA []int
  124. type floatA []float64
  125. type boolA []bool
  126. type interfaceA []interface{}
  127. type stringerA []testStringerImpl
  128. type stringerPtrA []*testStringerImpl
  129. type nullStringA []sql.NullString
  130. type nullBoolA []sql.NullBool
  131. type nullFloatA []sql.NullFloat64
  132. type nullIntA []sql.NullInt64
  133. s0 := strA{"Eric"}
  134. row0 := sheet.AddRow()
  135. row0.WriteSlice(&s0, -1)
  136. c.Assert(row0, NotNil)
  137. if val, err := row0.Cells[0].FormattedValue(); err != nil {
  138. c.Error(err)
  139. } else {
  140. c.Assert(val, Equals, "Eric")
  141. }
  142. s1 := intA{10}
  143. row1 := sheet.AddRow()
  144. row1.WriteSlice(&s1, -1)
  145. c.Assert(row1, NotNil)
  146. c1, e1 := row1.Cells[0].Int()
  147. c.Assert(e1, Equals, nil)
  148. c.Assert(c1, Equals, 10)
  149. s2 := floatA{3.94}
  150. row2 := sheet.AddRow()
  151. row2.WriteSlice(&s2, -1)
  152. c.Assert(row2, NotNil)
  153. c2, e2 := row2.Cells[0].Float()
  154. c.Assert(e2, Equals, nil)
  155. c.Assert(c2, Equals, 3.94)
  156. s3 := boolA{true}
  157. row3 := sheet.AddRow()
  158. row3.WriteSlice(&s3, -1)
  159. c.Assert(row3, NotNil)
  160. c3 := row3.Cells[0].Bool()
  161. c.Assert(c3, Equals, true)
  162. s4 := interfaceA{"Eric", 10, 3.94, true, time.Unix(0, 0)}
  163. row4 := sheet.AddRow()
  164. row4.WriteSlice(&s4, -1)
  165. c.Assert(row4, NotNil)
  166. if val, err := row4.Cells[0].FormattedValue(); err != nil {
  167. c.Error(err)
  168. } else {
  169. c.Assert(val, Equals, "Eric")
  170. }
  171. c41, e41 := row4.Cells[1].Int()
  172. c.Assert(e41, Equals, nil)
  173. c.Assert(c41, Equals, 10)
  174. c42, e42 := row4.Cells[2].Float()
  175. c.Assert(e42, Equals, nil)
  176. c.Assert(c42, Equals, 3.94)
  177. c43 := row4.Cells[3].Bool()
  178. c.Assert(c43, Equals, true)
  179. c44, e44 := row4.Cells[4].Float()
  180. c.Assert(e44, Equals, nil)
  181. c.Assert(math.Floor(c44), Equals, 25569.0)
  182. s5 := stringerA{testStringerImpl{"Stringer"}}
  183. row5 := sheet.AddRow()
  184. row5.WriteSlice(&s5, -1)
  185. c.Assert(row5, NotNil)
  186. if val, err := row5.Cells[0].FormattedValue(); err != nil {
  187. c.Error(err)
  188. } else {
  189. c.Assert(val, Equals, "Stringer")
  190. }
  191. s6 := stringerPtrA{&testStringerImpl{"Pointer to Stringer"}}
  192. row6 := sheet.AddRow()
  193. row6.WriteSlice(&s6, -1)
  194. c.Assert(row6, NotNil)
  195. if val, err := row6.Cells[0].FormattedValue(); err != nil {
  196. c.Error(err)
  197. } else {
  198. c.Assert(val, Equals, "Pointer to Stringer")
  199. }
  200. s7 := "expects -1 on non pointer to slice"
  201. row7 := sheet.AddRow()
  202. c.Assert(row7, NotNil)
  203. s7_ret := row7.WriteSlice(s7, -1)
  204. c.Assert(s7_ret, Equals, -1)
  205. s7_ret = row7.WriteSlice(&s7, -1)
  206. c.Assert(s7_ret, Equals, -1)
  207. s7_ret = row7.WriteSlice([]string{s7}, -1)
  208. c.Assert(s7_ret, Equals, -1)
  209. s8 := nullStringA{sql.NullString{String: "Smith", Valid: true}, sql.NullString{String: `What ever`, Valid: false}}
  210. row8 := sheet.AddRow()
  211. row8.WriteSlice(&s8, -1)
  212. c.Assert(row8, NotNil)
  213. if val, err := row8.Cells[0].FormattedValue(); err != nil {
  214. c.Error(err)
  215. } else {
  216. c.Assert(val, Equals, "Smith")
  217. }
  218. // check second cell on empty string ""
  219. if val2, err := row8.Cells[1].FormattedValue(); err != nil {
  220. c.Error(err)
  221. } else {
  222. c.Assert(val2, Equals, "")
  223. }
  224. s9 := nullBoolA{sql.NullBool{Bool: false, Valid: true}, sql.NullBool{Bool: true, Valid: false}}
  225. row9 := sheet.AddRow()
  226. row9.WriteSlice(&s9, -1)
  227. c.Assert(row9, NotNil)
  228. c9 := row9.Cells[0].Bool()
  229. c9Null := row9.Cells[1].String()
  230. c.Assert(c9, Equals, false)
  231. c.Assert(c9Null, Equals, "")
  232. s10 := nullIntA{sql.NullInt64{Int64: 100, Valid: true}, sql.NullInt64{Int64: 100, Valid: false}}
  233. row10 := sheet.AddRow()
  234. row10.WriteSlice(&s10, -1)
  235. c.Assert(row10, NotNil)
  236. c10, e10 := row10.Cells[0].Int()
  237. c10Null, e10Null := row10.Cells[1].FormattedValue()
  238. c.Assert(e10, Equals, nil)
  239. c.Assert(c10, Equals, 100)
  240. c.Assert(e10Null, Equals, nil)
  241. c.Assert(c10Null, Equals, "")
  242. s11 := nullFloatA{sql.NullFloat64{Float64: 0.123, Valid: true}, sql.NullFloat64{Float64: 0.123, Valid: false}}
  243. row11 := sheet.AddRow()
  244. row11.WriteSlice(&s11, -1)
  245. c.Assert(row11, NotNil)
  246. c11, e11 := row11.Cells[0].Float()
  247. c11Null, e11Null := row11.Cells[1].FormattedValue()
  248. c.Assert(e11, Equals, nil)
  249. c.Assert(c11, Equals, 0.123)
  250. c.Assert(e11Null, Equals, nil)
  251. c.Assert(c11Null, Equals, "")
  252. }