excelize_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 read cell value with given axis large than exists row.
  33. f1.GetCellValue("Sheet2", "E13")
  34. for i := 1; i <= 300; i++ {
  35. f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
  36. }
  37. err = f1.Save()
  38. if err != nil {
  39. t.Log(err)
  40. }
  41. // Test write file to given path.
  42. err = f1.WriteTo("./test/Workbook_2.xlsx")
  43. if err != nil {
  44. t.Log(err)
  45. }
  46. // Test write file to not exist directory.
  47. err = f1.WriteTo("")
  48. if err != nil {
  49. t.Log(err)
  50. }
  51. // Test write file with broken file struct.
  52. f2 := File{}
  53. err = f2.Save()
  54. if err != nil {
  55. t.Log(err)
  56. }
  57. // Test write file with broken file struct with given path.
  58. err = f2.WriteTo("./test/Workbook_3.xlsx")
  59. if err != nil {
  60. t.Log(err)
  61. }
  62. // Test create a XLSX file.
  63. f3 := CreateFile()
  64. f3.NewSheet(2, "XLSXSheet2")
  65. f3.NewSheet(3, "XLSXSheet3")
  66. f3.SetCellInt("Sheet2", "A23", 56)
  67. f3.SetCellStr("SHEET1", "B20", "42")
  68. f3.SetActiveSheet(0)
  69. err = f3.WriteTo("./test/Workbook_3.xlsx")
  70. if err != nil {
  71. t.Log(err)
  72. }
  73. // Test open a XLSX file with given illegal path.
  74. _, err = OpenFile("./test/Workbook.xlsx")
  75. if err != nil {
  76. t.Log(err)
  77. }
  78. }