excelize_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. for i := 1; i <= 300; i++ {
  56. f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
  57. }
  58. err = f1.Save()
  59. if err != nil {
  60. t.Log(err)
  61. }
  62. // Test write file to given path.
  63. err = f1.WriteTo("./test/Workbook_2.xlsx")
  64. if err != nil {
  65. t.Log(err)
  66. }
  67. // Test write file to not exist directory.
  68. err = f1.WriteTo("")
  69. if err != nil {
  70. t.Log(err)
  71. }
  72. // Test write file with broken file struct.
  73. f2 := File{}
  74. err = f2.Save()
  75. if err != nil {
  76. t.Log(err)
  77. }
  78. // Test write file with broken file struct with given path.
  79. err = f2.WriteTo("./test/Workbook_3.xlsx")
  80. if err != nil {
  81. t.Log(err)
  82. }
  83. // Test create a XLSX file.
  84. f3 := CreateFile()
  85. f3.NewSheet(2, "XLSXSheet2")
  86. f3.NewSheet(3, "XLSXSheet3")
  87. f3.SetCellInt("Sheet2", "A23", 56)
  88. f3.SetCellStr("SHEET1", "B20", "42")
  89. f3.SetActiveSheet(0)
  90. err = f3.WriteTo("./test/Workbook_3.xlsx")
  91. if err != nil {
  92. t.Log(err)
  93. }
  94. // Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
  95. f4, err := OpenFile("./test/badWorkbook.xlsx")
  96. f4.SetActiveSheet(2)
  97. if err != nil {
  98. t.Log(err)
  99. }
  100. // Test open a XLSX file with given illegal path.
  101. _, err = OpenFile("./test/Workbook.xlsx")
  102. if err != nil {
  103. t.Log(err)
  104. }
  105. }