excelize_test.go 3.3 KB

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