cell_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestCheckCellInArea(t *testing.T) {
  10. expectedTrueCellInAreaList := [][2]string{
  11. {"c2", "A1:AAZ32"},
  12. {"B9", "A1:B9"},
  13. {"C2", "C2:C2"},
  14. }
  15. for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
  16. cell := expectedTrueCellInArea[0]
  17. area := expectedTrueCellInArea[1]
  18. ok, err := checkCellInArea(cell, area)
  19. assert.NoError(t, err)
  20. assert.Truef(t, ok,
  21. "Expected cell %v to be in area %v, got false\n", cell, area)
  22. }
  23. expectedFalseCellInAreaList := [][2]string{
  24. {"c2", "A4:AAZ32"},
  25. {"C4", "D6:A1"}, // weird case, but you never know
  26. {"AEF42", "BZ40:AEF41"},
  27. }
  28. for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
  29. cell := expectedFalseCellInArea[0]
  30. area := expectedFalseCellInArea[1]
  31. ok, err := checkCellInArea(cell, area)
  32. assert.NoError(t, err)
  33. assert.Falsef(t, ok,
  34. "Expected cell %v not to be inside of area %v, but got true\n", cell, area)
  35. }
  36. ok, err := checkCellInArea("AA0", "Z0:AB1")
  37. assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`)
  38. assert.False(t, ok)
  39. }
  40. func TestSetCellFloat(t *testing.T) {
  41. sheet := "Sheet1"
  42. t.Run("with no decimal", func(t *testing.T) {
  43. f := NewFile()
  44. f.SetCellFloat(sheet, "A1", 123.0, -1, 64)
  45. f.SetCellFloat(sheet, "A2", 123.0, 1, 64)
  46. val, err := f.GetCellValue(sheet, "A1")
  47. assert.NoError(t, err)
  48. assert.Equal(t, "123", val, "A1 should be 123")
  49. val, err = f.GetCellValue(sheet, "A2")
  50. assert.NoError(t, err)
  51. assert.Equal(t, "123.0", val, "A2 should be 123.0")
  52. })
  53. t.Run("with a decimal and precision limit", func(t *testing.T) {
  54. f := NewFile()
  55. f.SetCellFloat(sheet, "A1", 123.42, 1, 64)
  56. val, err := f.GetCellValue(sheet, "A1")
  57. assert.NoError(t, err)
  58. assert.Equal(t, "123.4", val, "A1 should be 123.4")
  59. })
  60. t.Run("with a decimal and no limit", func(t *testing.T) {
  61. f := NewFile()
  62. f.SetCellFloat(sheet, "A1", 123.42, -1, 64)
  63. val, err := f.GetCellValue(sheet, "A1")
  64. assert.NoError(t, err)
  65. assert.Equal(t, "123.42", val, "A1 should be 123.42")
  66. })
  67. f := NewFile()
  68. assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  69. }
  70. func TestSetCellValue(t *testing.T) {
  71. f := NewFile()
  72. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  73. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  74. }
  75. func TestSetCellBool(t *testing.T) {
  76. f := NewFile()
  77. assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  78. }
  79. func TestGetCellFormula(t *testing.T) {
  80. f := NewFile()
  81. f.GetCellFormula("Sheet", "A1")
  82. }
  83. func TestMergeCell(t *testing.T) {
  84. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  85. if !assert.NoError(t, err) {
  86. t.FailNow()
  87. }
  88. assert.EqualError(t, f.MergeCell("Sheet1", "A", "B"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  89. f.MergeCell("Sheet1", "D9", "D9")
  90. f.MergeCell("Sheet1", "D9", "E9")
  91. f.MergeCell("Sheet1", "H14", "G13")
  92. f.MergeCell("Sheet1", "C9", "D8")
  93. f.MergeCell("Sheet1", "F11", "G13")
  94. f.MergeCell("Sheet1", "H7", "B15")
  95. f.MergeCell("Sheet1", "D11", "F13")
  96. f.MergeCell("Sheet1", "G10", "K12")
  97. f.SetCellValue("Sheet1", "G11", "set value in merged cell")
  98. f.SetCellInt("Sheet1", "H11", 100)
  99. f.SetCellValue("Sheet1", "I11", float64(0.5))
  100. f.SetCellHyperLink("Sheet1", "J11", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  101. f.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)")
  102. f.GetCellValue("Sheet1", "H11")
  103. f.GetCellValue("Sheet2", "A6") // Merged cell ref is single coordinate.
  104. f.GetCellFormula("Sheet1", "G12")
  105. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestMergeCell.xlsx")))
  106. }
  107. func ExampleFile_SetCellFloat() {
  108. f := NewFile()
  109. var x = 3.14159265
  110. f.SetCellFloat("Sheet1", "A1", x, 2, 64)
  111. val, _ := f.GetCellValue("Sheet1", "A1")
  112. fmt.Println(val)
  113. // Output: 3.14
  114. }
  115. func BenchmarkSetCellValue(b *testing.B) {
  116. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  117. cols := []string{"A", "B", "C", "D", "E", "F"}
  118. f := NewFile()
  119. b.ResetTimer()
  120. for i := 0; i < b.N; i++ {
  121. for j := 0; j < len(values); j++ {
  122. f.SetCellValue("Sheet1", fmt.Sprint(cols[j], i), values[j])
  123. }
  124. }
  125. }
  126. func TestOverflowNumericCell(t *testing.T) {
  127. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  128. if !assert.NoError(t, err) {
  129. t.FailNow()
  130. }
  131. val, err := f.GetCellValue("Sheet1", "A1")
  132. assert.NoError(t, err)
  133. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  134. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  135. }