cell_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestCheckCellInArea(t *testing.T) {
  11. f := NewFile()
  12. expectedTrueCellInAreaList := [][2]string{
  13. {"c2", "A1:AAZ32"},
  14. {"B9", "A1:B9"},
  15. {"C2", "C2:C2"},
  16. }
  17. for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
  18. cell := expectedTrueCellInArea[0]
  19. area := expectedTrueCellInArea[1]
  20. ok, err := f.checkCellInArea(cell, area)
  21. assert.NoError(t, err)
  22. assert.Truef(t, ok,
  23. "Expected cell %v to be in area %v, got false\n", cell, area)
  24. }
  25. expectedFalseCellInAreaList := [][2]string{
  26. {"c2", "A4:AAZ32"},
  27. {"C4", "D6:A1"}, // weird case, but you never know
  28. {"AEF42", "BZ40:AEF41"},
  29. }
  30. for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
  31. cell := expectedFalseCellInArea[0]
  32. area := expectedFalseCellInArea[1]
  33. ok, err := f.checkCellInArea(cell, area)
  34. assert.NoError(t, err)
  35. assert.Falsef(t, ok,
  36. "Expected cell %v not to be inside of area %v, but got true\n", cell, area)
  37. }
  38. ok, err := f.checkCellInArea("A1", "A:B")
  39. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  40. assert.False(t, ok)
  41. ok, err = f.checkCellInArea("AA0", "Z0:AB1")
  42. assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`)
  43. assert.False(t, ok)
  44. }
  45. func TestSetCellFloat(t *testing.T) {
  46. sheet := "Sheet1"
  47. t.Run("with no decimal", func(t *testing.T) {
  48. f := NewFile()
  49. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.0, -1, 64))
  50. assert.NoError(t, f.SetCellFloat(sheet, "A2", 123.0, 1, 64))
  51. val, err := f.GetCellValue(sheet, "A1")
  52. assert.NoError(t, err)
  53. assert.Equal(t, "123", val, "A1 should be 123")
  54. val, err = f.GetCellValue(sheet, "A2")
  55. assert.NoError(t, err)
  56. assert.Equal(t, "123.0", val, "A2 should be 123.0")
  57. })
  58. t.Run("with a decimal and precision limit", func(t *testing.T) {
  59. f := NewFile()
  60. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, 1, 64))
  61. val, err := f.GetCellValue(sheet, "A1")
  62. assert.NoError(t, err)
  63. assert.Equal(t, "123.4", val, "A1 should be 123.4")
  64. })
  65. t.Run("with a decimal and no limit", func(t *testing.T) {
  66. f := NewFile()
  67. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, -1, 64))
  68. val, err := f.GetCellValue(sheet, "A1")
  69. assert.NoError(t, err)
  70. assert.Equal(t, "123.42", val, "A1 should be 123.42")
  71. })
  72. f := NewFile()
  73. assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  74. }
  75. func TestSetCellValue(t *testing.T) {
  76. f := NewFile()
  77. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  78. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  79. }
  80. func TestSetCellBool(t *testing.T) {
  81. f := NewFile()
  82. assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  83. }
  84. func TestGetCellFormula(t *testing.T) {
  85. // Test get cell formula on not exist worksheet.
  86. f := NewFile()
  87. _, err := f.GetCellFormula("SheetN", "A1")
  88. assert.EqualError(t, err, "sheet SheetN is not exist")
  89. // Test get cell formula on no formula cell.
  90. assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
  91. _, err = f.GetCellFormula("Sheet1", "A1")
  92. assert.NoError(t, err)
  93. }
  94. func ExampleFile_SetCellFloat() {
  95. f := NewFile()
  96. var x = 3.14159265
  97. if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
  98. fmt.Println(err)
  99. }
  100. val, _ := f.GetCellValue("Sheet1", "A1")
  101. fmt.Println(val)
  102. // Output: 3.14
  103. }
  104. func BenchmarkSetCellValue(b *testing.B) {
  105. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  106. cols := []string{"A", "B", "C", "D", "E", "F"}
  107. f := NewFile()
  108. b.ResetTimer()
  109. for i := 1; i <= b.N; i++ {
  110. for j := 0; j < len(values); j++ {
  111. if err := f.SetCellValue("Sheet1", cols[j]+strconv.Itoa(i), values[j]); err != nil {
  112. b.Error(err)
  113. }
  114. }
  115. }
  116. }
  117. func TestOverflowNumericCell(t *testing.T) {
  118. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  119. if !assert.NoError(t, err) {
  120. t.FailNow()
  121. }
  122. val, err := f.GetCellValue("Sheet1", "A1")
  123. assert.NoError(t, err)
  124. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  125. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  126. }
  127. func TestSetCellRichText(t *testing.T) {
  128. f := NewFile()
  129. assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))
  130. assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44))
  131. richTextRun := []RichTextRun{
  132. {
  133. Text: "blod",
  134. Font: &Font{
  135. Bold: true,
  136. Color: "2354e8",
  137. Family: "Times New Roman",
  138. },
  139. },
  140. {
  141. Text: " and ",
  142. Font: &Font{
  143. Family: "Times New Roman",
  144. },
  145. },
  146. {
  147. Text: "italic ",
  148. Font: &Font{
  149. Bold: true,
  150. Color: "e83723",
  151. Italic: true,
  152. Family: "Times New Roman",
  153. },
  154. },
  155. {
  156. Text: "text with color and font-family,",
  157. Font: &Font{
  158. Bold: true,
  159. Color: "2354e8",
  160. Family: "Times New Roman",
  161. },
  162. },
  163. {
  164. Text: "\r\nlarge text with ",
  165. Font: &Font{
  166. Size: 14,
  167. Color: "ad23e8",
  168. },
  169. },
  170. {
  171. Text: "strike",
  172. Font: &Font{
  173. Color: "e89923",
  174. Strike: true,
  175. },
  176. },
  177. {
  178. Text: " and ",
  179. Font: &Font{
  180. Size: 14,
  181. Color: "ad23e8",
  182. },
  183. },
  184. {
  185. Text: "underline.",
  186. Font: &Font{
  187. Color: "23e833",
  188. Underline: "single",
  189. },
  190. },
  191. }
  192. assert.NoError(t, f.SetCellRichText("Sheet1", "A1", richTextRun))
  193. assert.NoError(t, f.SetCellRichText("Sheet1", "A2", richTextRun))
  194. style, err := f.NewStyle(&Style{
  195. Alignment: &Alignment{
  196. WrapText: true,
  197. },
  198. })
  199. assert.NoError(t, err)
  200. assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
  201. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
  202. // Test set cell rich text on not exists worksheet
  203. assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
  204. // Test set cell rich text with illegal cell coordinates
  205. assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  206. }