cell_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 TestGetCellValue(t *testing.T) {
  85. // Test get cell value without r attribute of the row.
  86. f := NewFile()
  87. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  88. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData><row r="3"><c t="str"><v>A3</v></c></row><row><c t="str"><v>A4</v></c><c t="str"><v>B4</v></c></row><row r="7"><c t="str"><v>A7</v></c><c t="str"><v>B7</v></c></row><row><c t="str"><v>A8</v></c><c t="str"><v>B8</v></c></row></sheetData></worksheet>`)
  89. f.checked = nil
  90. cells := []string{"A3", "A4", "B4", "A7", "B7"}
  91. rows, err := f.GetRows("Sheet1")
  92. assert.Equal(t, [][]string{nil, nil, {"A3"}, {"A4", "B4"}, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
  93. assert.NoError(t, err)
  94. for _, cell := range cells {
  95. value, err := f.GetCellValue("Sheet1", cell)
  96. assert.Equal(t, cell, value)
  97. assert.NoError(t, err)
  98. }
  99. }
  100. func TestGetCellFormula(t *testing.T) {
  101. // Test get cell formula on not exist worksheet.
  102. f := NewFile()
  103. _, err := f.GetCellFormula("SheetN", "A1")
  104. assert.EqualError(t, err, "sheet SheetN is not exist")
  105. // Test get cell formula on no formula cell.
  106. assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
  107. _, err = f.GetCellFormula("Sheet1", "A1")
  108. assert.NoError(t, err)
  109. }
  110. func ExampleFile_SetCellFloat() {
  111. f := NewFile()
  112. var x = 3.14159265
  113. if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
  114. fmt.Println(err)
  115. }
  116. val, _ := f.GetCellValue("Sheet1", "A1")
  117. fmt.Println(val)
  118. // Output: 3.14
  119. }
  120. func BenchmarkSetCellValue(b *testing.B) {
  121. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  122. cols := []string{"A", "B", "C", "D", "E", "F"}
  123. f := NewFile()
  124. b.ResetTimer()
  125. for i := 1; i <= b.N; i++ {
  126. for j := 0; j < len(values); j++ {
  127. if err := f.SetCellValue("Sheet1", cols[j]+strconv.Itoa(i), values[j]); err != nil {
  128. b.Error(err)
  129. }
  130. }
  131. }
  132. }
  133. func TestOverflowNumericCell(t *testing.T) {
  134. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  135. if !assert.NoError(t, err) {
  136. t.FailNow()
  137. }
  138. val, err := f.GetCellValue("Sheet1", "A1")
  139. assert.NoError(t, err)
  140. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  141. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  142. }
  143. func TestSetCellRichText(t *testing.T) {
  144. f := NewFile()
  145. assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))
  146. assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44))
  147. richTextRun := []RichTextRun{
  148. {
  149. Text: "blod",
  150. Font: &Font{
  151. Bold: true,
  152. Color: "2354e8",
  153. Family: "Times New Roman",
  154. },
  155. },
  156. {
  157. Text: " and ",
  158. Font: &Font{
  159. Family: "Times New Roman",
  160. },
  161. },
  162. {
  163. Text: "italic ",
  164. Font: &Font{
  165. Bold: true,
  166. Color: "e83723",
  167. Italic: true,
  168. Family: "Times New Roman",
  169. },
  170. },
  171. {
  172. Text: "text with color and font-family,",
  173. Font: &Font{
  174. Bold: true,
  175. Color: "2354e8",
  176. Family: "Times New Roman",
  177. },
  178. },
  179. {
  180. Text: "\r\nlarge text with ",
  181. Font: &Font{
  182. Size: 14,
  183. Color: "ad23e8",
  184. },
  185. },
  186. {
  187. Text: "strike",
  188. Font: &Font{
  189. Color: "e89923",
  190. Strike: true,
  191. },
  192. },
  193. {
  194. Text: " and ",
  195. Font: &Font{
  196. Size: 14,
  197. Color: "ad23e8",
  198. },
  199. },
  200. {
  201. Text: "underline.",
  202. Font: &Font{
  203. Color: "23e833",
  204. Underline: "single",
  205. },
  206. },
  207. }
  208. assert.NoError(t, f.SetCellRichText("Sheet1", "A1", richTextRun))
  209. assert.NoError(t, f.SetCellRichText("Sheet1", "A2", richTextRun))
  210. style, err := f.NewStyle(&Style{
  211. Alignment: &Alignment{
  212. WrapText: true,
  213. },
  214. })
  215. assert.NoError(t, err)
  216. assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
  217. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
  218. // Test set cell rich text on not exists worksheet
  219. assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
  220. // Test set cell rich text with illegal cell coordinates
  221. assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  222. }