cell_test.go 7.6 KB

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