cell_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 TestSetCellValues(t *testing.T) {
  101. f := NewFile()
  102. err := f.SetCellValue("Sheet1", "A1", time.Date(2010, time.December, 31, 0, 0, 0, 0, time.UTC))
  103. assert.NoError(t, err)
  104. v, err := f.GetCellValue("Sheet1", "A1")
  105. assert.NoError(t, err)
  106. assert.Equal(t, v, "12/31/10 12:00")
  107. // test date value lower than min date supported by Excel
  108. err = f.SetCellValue("Sheet1", "A1", time.Date(1600, time.December, 31, 0, 0, 0, 0, time.UTC))
  109. assert.NoError(t, err)
  110. _, err = f.GetCellValue("Sheet1", "A1")
  111. assert.EqualError(t, err, `strconv.ParseFloat: parsing "1600-12-31T00:00:00Z": invalid syntax`)
  112. }
  113. func TestSetCellBool(t *testing.T) {
  114. f := NewFile()
  115. assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  116. }
  117. func TestGetCellValue(t *testing.T) {
  118. // Test get cell value without r attribute of the row.
  119. f := NewFile()
  120. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  121. 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>`)
  122. f.checked = nil
  123. cells := []string{"A3", "A4", "B4", "A7", "B7"}
  124. rows, err := f.GetRows("Sheet1")
  125. assert.Equal(t, [][]string{nil, nil, {"A3"}, {"A4", "B4"}, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
  126. assert.NoError(t, err)
  127. for _, cell := range cells {
  128. value, err := f.GetCellValue("Sheet1", cell)
  129. assert.Equal(t, cell, value)
  130. assert.NoError(t, err)
  131. }
  132. cols, err := f.GetCols("Sheet1")
  133. assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols)
  134. assert.NoError(t, err)
  135. }
  136. func TestGetCellFormula(t *testing.T) {
  137. // Test get cell formula on not exist worksheet.
  138. f := NewFile()
  139. _, err := f.GetCellFormula("SheetN", "A1")
  140. assert.EqualError(t, err, "sheet SheetN is not exist")
  141. // Test get cell formula on no formula cell.
  142. assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
  143. _, err = f.GetCellFormula("Sheet1", "A1")
  144. assert.NoError(t, err)
  145. }
  146. func ExampleFile_SetCellFloat() {
  147. f := NewFile()
  148. var x = 3.14159265
  149. if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
  150. fmt.Println(err)
  151. }
  152. val, _ := f.GetCellValue("Sheet1", "A1")
  153. fmt.Println(val)
  154. // Output: 3.14
  155. }
  156. func BenchmarkSetCellValue(b *testing.B) {
  157. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  158. cols := []string{"A", "B", "C", "D", "E", "F"}
  159. f := NewFile()
  160. b.ResetTimer()
  161. for i := 1; i <= b.N; i++ {
  162. for j := 0; j < len(values); j++ {
  163. if err := f.SetCellValue("Sheet1", cols[j]+strconv.Itoa(i), values[j]); err != nil {
  164. b.Error(err)
  165. }
  166. }
  167. }
  168. }
  169. func TestOverflowNumericCell(t *testing.T) {
  170. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  171. if !assert.NoError(t, err) {
  172. t.FailNow()
  173. }
  174. val, err := f.GetCellValue("Sheet1", "A1")
  175. assert.NoError(t, err)
  176. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  177. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  178. }
  179. func TestSetCellRichText(t *testing.T) {
  180. f := NewFile()
  181. assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))
  182. assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44))
  183. richTextRun := []RichTextRun{
  184. {
  185. Text: "bold",
  186. Font: &Font{
  187. Bold: true,
  188. Color: "2354e8",
  189. Family: "Times New Roman",
  190. },
  191. },
  192. {
  193. Text: " and ",
  194. Font: &Font{
  195. Family: "Times New Roman",
  196. },
  197. },
  198. {
  199. Text: "italic ",
  200. Font: &Font{
  201. Bold: true,
  202. Color: "e83723",
  203. Italic: true,
  204. Family: "Times New Roman",
  205. },
  206. },
  207. {
  208. Text: "text with color and font-family,",
  209. Font: &Font{
  210. Bold: true,
  211. Color: "2354e8",
  212. Family: "Times New Roman",
  213. },
  214. },
  215. {
  216. Text: "\r\nlarge text with ",
  217. Font: &Font{
  218. Size: 14,
  219. Color: "ad23e8",
  220. },
  221. },
  222. {
  223. Text: "strike",
  224. Font: &Font{
  225. Color: "e89923",
  226. Strike: true,
  227. },
  228. },
  229. {
  230. Text: " and ",
  231. Font: &Font{
  232. Size: 14,
  233. Color: "ad23e8",
  234. },
  235. },
  236. {
  237. Text: "underline.",
  238. Font: &Font{
  239. Color: "23e833",
  240. Underline: "single",
  241. },
  242. },
  243. }
  244. assert.NoError(t, f.SetCellRichText("Sheet1", "A1", richTextRun))
  245. assert.NoError(t, f.SetCellRichText("Sheet1", "A2", richTextRun))
  246. style, err := f.NewStyle(&Style{
  247. Alignment: &Alignment{
  248. WrapText: true,
  249. },
  250. })
  251. assert.NoError(t, err)
  252. assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
  253. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
  254. // Test set cell rich text on not exists worksheet
  255. assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
  256. // Test set cell rich text with illegal cell coordinates
  257. assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  258. }
  259. func TestFormattedValue(t *testing.T) {
  260. f := NewFile()
  261. v := f.formattedValue(0, "43528")
  262. assert.Equal(t, "43528", v)
  263. v = f.formattedValue(15, "43528")
  264. assert.Equal(t, "43528", v)
  265. v = f.formattedValue(1, "43528")
  266. assert.Equal(t, "43528", v)
  267. customNumFmt := "[$-409]MM/DD/YYYY"
  268. _, err := f.NewStyle(&Style{
  269. CustomNumFmt: &customNumFmt,
  270. })
  271. assert.NoError(t, err)
  272. v = f.formattedValue(1, "43528")
  273. assert.Equal(t, "03/04/2019", v)
  274. }