excelize_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))
  24. f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
  25. f1.SetCellInt("SHEET2", "A1", 100)
  26. f1.SetCellStr("SHEET2", "C11", "Knowns")
  27. f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.")
  28. f1.SetCellInt("Sheet3", "A23", 10)
  29. f1.SetCellStr("SHEET3", "b230", "10")
  30. f1.SetCellStr("SHEET10", "b230", "10")
  31. f1.SetActiveSheet(2)
  32. f1.GetCellFormula("Sheet1", "B19") // Test get cell formula with given rows number.
  33. f1.GetCellFormula("Sheet2", "B20") // Test get cell formula with illegal sheet index.
  34. f1.GetCellFormula("Sheet1", "B20") // Test get cell formula with illegal rows number.
  35. // Test read cell value with given illegal rows number.
  36. f1.GetCellValue("Sheet2", "a-1")
  37. // Test read cell value with given lowercase column number.
  38. f1.GetCellValue("Sheet2", "a5")
  39. f1.GetCellValue("Sheet2", "C11")
  40. f1.GetCellValue("Sheet2", "D11")
  41. f1.GetCellValue("Sheet2", "D12")
  42. // Test SetCellValue function.
  43. f1.SetCellValue("Sheet2", "F1", "Hello")
  44. f1.SetCellValue("Sheet2", "G1", []byte("World"))
  45. f1.SetCellValue("Sheet2", "F2", 42)
  46. f1.SetCellValue("Sheet2", "F2", int8(42))
  47. f1.SetCellValue("Sheet2", "F2", int16(42))
  48. f1.SetCellValue("Sheet2", "F2", int32(42))
  49. f1.SetCellValue("Sheet2", "F2", int64(42))
  50. f1.SetCellValue("Sheet2", "F2", float32(42.65418))
  51. f1.SetCellValue("Sheet2", "F2", float64(-42.65418))
  52. f1.SetCellValue("Sheet2", "F2", float32(42))
  53. f1.SetCellValue("Sheet2", "F2", float64(42))
  54. f1.SetCellValue("Sheet2", "G2", nil)
  55. // Test completion column.
  56. f1.SetCellValue("Sheet2", "M2", nil)
  57. // Test read cell value with given axis large than exists row.
  58. f1.GetCellValue("Sheet2", "E231")
  59. // Test get active sheet of XLSX and get sheet name of XLSX by given sheet index.
  60. f1.GetSheetName(f1.GetActiveSheetIndex())
  61. // Test get sheet name of XLSX by given invalid sheet index.
  62. f1.GetSheetName(4)
  63. // Test get sheet map of XLSX.
  64. f1.GetSheetMap()
  65. for i := 1; i <= 300; i++ {
  66. f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
  67. }
  68. err = f1.Save()
  69. if err != nil {
  70. t.Log(err)
  71. }
  72. // Test add picture to sheet.
  73. err = f1.AddPicture("Sheet2", "I1", "L10", "./test/images/excel.jpg")
  74. if err != nil {
  75. t.Log(err)
  76. }
  77. err = f1.AddPicture("Sheet1", "F21", "G25", "./test/images/excel.png")
  78. if err != nil {
  79. t.Log(err)
  80. }
  81. err = f1.AddPicture("Sheet2", "L1", "O10", "./test/images/excel.bmp")
  82. if err != nil {
  83. t.Log(err)
  84. }
  85. err = f1.AddPicture("Sheet1", "G21", "H25", "./test/images/excel.ico")
  86. if err != nil {
  87. t.Log(err)
  88. }
  89. // Test add picture to sheet with unsupport file type.
  90. err = f1.AddPicture("Sheet1", "G21", "H25", "./test/images/excel.icon")
  91. if err != nil {
  92. t.Log(err)
  93. }
  94. // Test add picture to sheet with invalid file path.
  95. err = f1.AddPicture("Sheet1", "G21", "H25", "./test/Workbook1.xlsx")
  96. if err != nil {
  97. t.Log(err)
  98. }
  99. // Test write file to given path.
  100. err = f1.WriteTo("./test/Workbook_2.xlsx")
  101. if err != nil {
  102. t.Log(err)
  103. }
  104. // Test write file to not exist directory.
  105. err = f1.WriteTo("")
  106. if err != nil {
  107. t.Log(err)
  108. }
  109. // Test write file with broken file struct.
  110. f2 := File{}
  111. err = f2.Save()
  112. if err != nil {
  113. t.Log(err)
  114. }
  115. // Test write file with broken file struct with given path.
  116. err = f2.WriteTo("./test/Workbook_3.xlsx")
  117. if err != nil {
  118. t.Log(err)
  119. }
  120. // Test create a XLSX file.
  121. f3 := CreateFile()
  122. f3.NewSheet(2, "XLSXSheet2")
  123. f3.NewSheet(3, "XLSXSheet3")
  124. f3.SetCellInt("Sheet2", "A23", 56)
  125. f3.SetCellStr("SHEET1", "B20", "42")
  126. f3.SetActiveSheet(0)
  127. // Test add picture to sheet.
  128. err = f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
  129. if err != nil {
  130. t.Log(err)
  131. }
  132. err = f3.AddPicture("Sheet1", "C2", "F12", "./test/images/excel.tif")
  133. if err != nil {
  134. t.Log(err)
  135. }
  136. err = f3.WriteTo("./test/Workbook_3.xlsx")
  137. if err != nil {
  138. t.Log(err)
  139. }
  140. // Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
  141. f4, err := OpenFile("./test/badWorkbook.xlsx")
  142. f4.SetActiveSheet(2)
  143. if err != nil {
  144. t.Log(err)
  145. }
  146. // Test open a XLSX file with given illegal path.
  147. _, err = OpenFile("./test/Workbook.xlsx")
  148. if err != nil {
  149. t.Log(err)
  150. }
  151. }