cell_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. f := NewFile()
  11. expectedTrueCellInAreaList := [][2]string{
  12. {"c2", "A1:AAZ32"},
  13. {"B9", "A1:B9"},
  14. {"C2", "C2:C2"},
  15. }
  16. for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
  17. cell := expectedTrueCellInArea[0]
  18. area := expectedTrueCellInArea[1]
  19. ok, err := f.checkCellInArea(cell, area)
  20. assert.NoError(t, err)
  21. assert.Truef(t, ok,
  22. "Expected cell %v to be in area %v, got false\n", cell, area)
  23. }
  24. expectedFalseCellInAreaList := [][2]string{
  25. {"c2", "A4:AAZ32"},
  26. {"C4", "D6:A1"}, // weird case, but you never know
  27. {"AEF42", "BZ40:AEF41"},
  28. }
  29. for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
  30. cell := expectedFalseCellInArea[0]
  31. area := expectedFalseCellInArea[1]
  32. ok, err := f.checkCellInArea(cell, area)
  33. assert.NoError(t, err)
  34. assert.Falsef(t, ok,
  35. "Expected cell %v not to be inside of area %v, but got true\n", cell, area)
  36. }
  37. ok, err := f.checkCellInArea("A1", "A:B")
  38. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  39. assert.False(t, ok)
  40. ok, err = f.checkCellInArea("AA0", "Z0:AB1")
  41. assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`)
  42. assert.False(t, ok)
  43. }
  44. func TestSetCellFloat(t *testing.T) {
  45. sheet := "Sheet1"
  46. t.Run("with no decimal", func(t *testing.T) {
  47. f := NewFile()
  48. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.0, -1, 64))
  49. assert.NoError(t, f.SetCellFloat(sheet, "A2", 123.0, 1, 64))
  50. val, err := f.GetCellValue(sheet, "A1")
  51. assert.NoError(t, err)
  52. assert.Equal(t, "123", val, "A1 should be 123")
  53. val, err = f.GetCellValue(sheet, "A2")
  54. assert.NoError(t, err)
  55. assert.Equal(t, "123.0", val, "A2 should be 123.0")
  56. })
  57. t.Run("with a decimal and precision limit", func(t *testing.T) {
  58. f := NewFile()
  59. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, 1, 64))
  60. val, err := f.GetCellValue(sheet, "A1")
  61. assert.NoError(t, err)
  62. assert.Equal(t, "123.4", val, "A1 should be 123.4")
  63. })
  64. t.Run("with a decimal and no limit", func(t *testing.T) {
  65. f := NewFile()
  66. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, -1, 64))
  67. val, err := f.GetCellValue(sheet, "A1")
  68. assert.NoError(t, err)
  69. assert.Equal(t, "123.42", val, "A1 should be 123.42")
  70. })
  71. f := NewFile()
  72. assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  73. }
  74. func TestSetCellValue(t *testing.T) {
  75. f := NewFile()
  76. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  77. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  78. }
  79. func TestSetCellBool(t *testing.T) {
  80. f := NewFile()
  81. assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  82. }
  83. func TestGetCellFormula(t *testing.T) {
  84. // Test get cell formula on not exist worksheet.
  85. f := NewFile()
  86. _, err := f.GetCellFormula("SheetN", "A1")
  87. assert.EqualError(t, err, "sheet SheetN is not exist")
  88. // Test get cell formula on no formula cell.
  89. assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
  90. _, err = f.GetCellFormula("Sheet1", "A1")
  91. assert.NoError(t, err)
  92. }
  93. func ExampleFile_SetCellFloat() {
  94. f := NewFile()
  95. var x = 3.14159265
  96. if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
  97. fmt.Println(err)
  98. }
  99. val, _ := f.GetCellValue("Sheet1", "A1")
  100. fmt.Println(val)
  101. // Output: 3.14
  102. }
  103. func BenchmarkSetCellValue(b *testing.B) {
  104. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  105. cols := []string{"A", "B", "C", "D", "E", "F"}
  106. f := NewFile()
  107. b.ResetTimer()
  108. for i := 0; i < b.N; i++ {
  109. for j := 0; j < len(values); j++ {
  110. if err := f.SetCellValue("Sheet1", fmt.Sprint(cols[j], i), values[j]); err != nil {
  111. b.Error(err)
  112. }
  113. }
  114. }
  115. }
  116. func TestOverflowNumericCell(t *testing.T) {
  117. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  118. if !assert.NoError(t, err) {
  119. t.FailNow()
  120. }
  121. val, err := f.GetCellValue("Sheet1", "A1")
  122. assert.NoError(t, err)
  123. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  124. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  125. }