excelize_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package excelize
  2. import (
  3. "strconv"
  4. "testing"
  5. )
  6. func TestExcelize(t *testing.T) {
  7. // Test update a XLSX file.
  8. f1, err := OpenFile("./test/Workbook1.xlsx")
  9. if err != nil {
  10. t.Log(err)
  11. }
  12. // Test get all the rows in a not exists sheet.
  13. rows := f1.GetRows("Sheet4")
  14. // Test get all the rows in a sheet.
  15. rows = f1.GetRows("Sheet2")
  16. for _, row := range rows {
  17. for _, cell := range row {
  18. t.Log(cell, "\t")
  19. }
  20. t.Log("\r\n")
  21. }
  22. f1.UpdateLinkedValue()
  23. f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))
  24. f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
  25. f1.SetCellInt("SHEET2", "A1", 100)
  26. f1.SetCellStr("SHEET2", "C11", "Knowns")
  27. f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.")
  28. f1.SetCellInt("Sheet3", "A23", 10)
  29. f1.SetCellStr("SHEET3", "b230", "10")
  30. f1.SetCellStr("SHEET10", "b230", "10")
  31. f1.SetActiveSheet(2)
  32. f1.GetCellFormula("Sheet1", "B19") // Test get cell formula with given rows number.
  33. f1.GetCellFormula("Sheet2", "B20") // Test get cell formula with illegal sheet index.
  34. f1.GetCellFormula("Sheet1", "B20") // Test get cell formula with illegal rows number.
  35. // Test read cell value with given illegal rows number.
  36. f1.GetCellValue("Sheet2", "a-1")
  37. // Test read cell value with given lowercase column number.
  38. f1.GetCellValue("Sheet2", "a5")
  39. f1.GetCellValue("Sheet2", "C11")
  40. f1.GetCellValue("Sheet2", "D11")
  41. f1.GetCellValue("Sheet2", "D12")
  42. // Test SetCellValue function.
  43. f1.SetCellValue("Sheet2", "F1", "Hello")
  44. f1.SetCellValue("Sheet2", "G1", []byte("World"))
  45. f1.SetCellValue("Sheet2", "F2", 42)
  46. f1.SetCellValue("Sheet2", "F2", int8(42))
  47. f1.SetCellValue("Sheet2", "F2", int16(42))
  48. f1.SetCellValue("Sheet2", "F2", int32(42))
  49. f1.SetCellValue("Sheet2", "F2", int64(42))
  50. f1.SetCellValue("Sheet2", "F2", float32(42.65418))
  51. f1.SetCellValue("Sheet2", "F2", float64(-42.65418))
  52. f1.SetCellValue("Sheet2", "F2", float32(42))
  53. f1.SetCellValue("Sheet2", "F2", float64(42))
  54. f1.SetCellValue("Sheet2", "G2", nil)
  55. // Test completion column.
  56. f1.SetCellValue("Sheet2", "M2", nil)
  57. // Test read cell value with given axis large than exists row.
  58. f1.GetCellValue("Sheet2", "E231")
  59. // Test get active sheet of XLSX and get sheet name of XLSX by given sheet index.
  60. f1.GetSheetName(f1.GetActiveSheetIndex())
  61. // Test get sheet name of XLSX by given invalid sheet index.
  62. f1.GetSheetName(4)
  63. // Test get sheet map of XLSX.
  64. f1.GetSheetMap()
  65. for i := 1; i <= 300; i++ {
  66. f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
  67. }
  68. err = f1.Save()
  69. if err != nil {
  70. t.Log(err)
  71. }
  72. // Test write file to given path.
  73. err = f1.WriteTo("./test/Workbook_2.xlsx")
  74. if err != nil {
  75. t.Log(err)
  76. }
  77. // Test write file to not exist directory.
  78. err = f1.WriteTo("")
  79. if err != nil {
  80. t.Log(err)
  81. }
  82. // Test write file with broken file struct.
  83. f2 := File{}
  84. err = f2.Save()
  85. if err != nil {
  86. t.Log(err)
  87. }
  88. // Test write file with broken file struct with given path.
  89. err = f2.WriteTo("./test/Workbook_3.xlsx")
  90. if err != nil {
  91. t.Log(err)
  92. }
  93. // Test create a XLSX file.
  94. f3 := CreateFile()
  95. f3.NewSheet(2, "XLSXSheet2")
  96. f3.NewSheet(3, "XLSXSheet3")
  97. f3.SetCellInt("Sheet2", "A23", 56)
  98. f3.SetCellStr("SHEET1", "B20", "42")
  99. f3.SetActiveSheet(0)
  100. err = f3.WriteTo("./test/Workbook_3.xlsx")
  101. if err != nil {
  102. t.Log(err)
  103. }
  104. // Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
  105. f4, err := OpenFile("./test/badWorkbook.xlsx")
  106. f4.SetActiveSheet(2)
  107. if err != nil {
  108. t.Log(err)
  109. }
  110. // Test open a XLSX file with given illegal path.
  111. _, err = OpenFile("./test/Workbook.xlsx")
  112. if err != nil {
  113. t.Log(err)
  114. }
  115. }