excelize_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. f1.UpdateLinkedValue()
  13. f1.SetCellInt("SHEET2", "A1", 100)
  14. f1.SetCellStr("SHEET2", "C11", "Knowns")
  15. f1.NewSheet(3, "TestSheet")
  16. f1.SetCellInt("Sheet3", "A23", 10)
  17. f1.SetCellStr("SHEET3", "b230", "10")
  18. f1.SetCellStr("SHEET10", "b230", "10")
  19. f1.SetActiveSheet(2)
  20. // Test read cell value with given illegal rows number.
  21. f1.GetCellValue("Sheet2", "a-1")
  22. // Test read cell value with given lowercase column number.
  23. f1.GetCellValue("Sheet2", "a5")
  24. f1.GetCellValue("Sheet2", "C11")
  25. f1.GetCellValue("Sheet2", "D11")
  26. f1.GetCellValue("Sheet2", "D12")
  27. // Test SetCellValue function.
  28. f1.SetCellValue("Sheet2", "F1", "Hello")
  29. f1.SetCellValue("Sheet2", "G1", []byte("World"))
  30. f1.SetCellValue("Sheet2", "F2", 42)
  31. f1.SetCellValue("Sheet2", "G2", nil)
  32. // Test completion column.
  33. f1.SetCellValue("Sheet2", "M2", nil)
  34. // Test read cell value with given axis large than exists row.
  35. f1.GetCellValue("Sheet2", "E231")
  36. for i := 1; i <= 300; i++ {
  37. f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
  38. }
  39. err = f1.Save()
  40. if err != nil {
  41. t.Log(err)
  42. }
  43. // Test write file to given path.
  44. err = f1.WriteTo("./test/Workbook_2.xlsx")
  45. if err != nil {
  46. t.Log(err)
  47. }
  48. // Test write file to not exist directory.
  49. err = f1.WriteTo("")
  50. if err != nil {
  51. t.Log(err)
  52. }
  53. // Test write file with broken file struct.
  54. f2 := File{}
  55. err = f2.Save()
  56. if err != nil {
  57. t.Log(err)
  58. }
  59. // Test write file with broken file struct with given path.
  60. err = f2.WriteTo("./test/Workbook_3.xlsx")
  61. if err != nil {
  62. t.Log(err)
  63. }
  64. // Test create a XLSX file.
  65. f3 := CreateFile()
  66. f3.NewSheet(2, "XLSXSheet2")
  67. f3.NewSheet(3, "XLSXSheet3")
  68. f3.SetCellInt("Sheet2", "A23", 56)
  69. f3.SetCellStr("SHEET1", "B20", "42")
  70. f3.SetActiveSheet(0)
  71. err = f3.WriteTo("./test/Workbook_3.xlsx")
  72. if err != nil {
  73. t.Log(err)
  74. }
  75. // Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
  76. f4, err := OpenFile("./test/badWorkbook.xlsx")
  77. f4.SetActiveSheet(2)
  78. if err != nil {
  79. t.Log(err)
  80. }
  81. // Test open a XLSX file with given illegal path.
  82. _, err = OpenFile("./test/Workbook.xlsx")
  83. if err != nil {
  84. t.Log(err)
  85. }
  86. }