cell_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. f.SetCellFloat(sheet, "A1", 123.0, -1, 64)
  49. 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. 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. 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. f := NewFile()
  85. f.GetCellFormula("Sheet", "A1")
  86. }
  87. func ExampleFile_SetCellFloat() {
  88. f := NewFile()
  89. var x = 3.14159265
  90. f.SetCellFloat("Sheet1", "A1", x, 2, 64)
  91. val, _ := f.GetCellValue("Sheet1", "A1")
  92. fmt.Println(val)
  93. // Output: 3.14
  94. }
  95. func BenchmarkSetCellValue(b *testing.B) {
  96. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  97. cols := []string{"A", "B", "C", "D", "E", "F"}
  98. f := NewFile()
  99. b.ResetTimer()
  100. for i := 0; i < b.N; i++ {
  101. for j := 0; j < len(values); j++ {
  102. f.SetCellValue("Sheet1", fmt.Sprint(cols[j], i), values[j])
  103. }
  104. }
  105. }
  106. func TestOverflowNumericCell(t *testing.T) {
  107. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  108. if !assert.NoError(t, err) {
  109. t.FailNow()
  110. }
  111. val, err := f.GetCellValue("Sheet1", "A1")
  112. assert.NoError(t, err)
  113. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  114. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  115. }