excelize_test.go 2.8 KB

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