excelize_test.go 5.0 KB

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