excelize_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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", "E13")
  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 open a XLSX file with given illegal path.
  76. _, err = OpenFile("./test/Workbook.xlsx")
  77. if err != nil {
  78. t.Log(err)
  79. }
  80. }