You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

212 lines
7.8 KiB

  1. package excelize
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestColumnVisibility(t *testing.T) {
  8. t.Run("TestBook1", func(t *testing.T) {
  9. f, err := prepareTestBook1()
  10. assert.NoError(t, err)
  11. // Hide/display a column with SetColVisible
  12. assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
  13. assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
  14. visible, err := f.GetColVisible("Sheet1", "F")
  15. assert.Equal(t, true, visible)
  16. assert.NoError(t, err)
  17. // Test hiding a few columns SetColVisible(...false)...
  18. assert.NoError(t, f.SetColVisible("Sheet1", "F:V", false))
  19. visible, err = f.GetColVisible("Sheet1", "F")
  20. assert.Equal(t, false, visible)
  21. assert.NoError(t, err)
  22. visible, err = f.GetColVisible("Sheet1", "U")
  23. assert.Equal(t, false, visible)
  24. assert.NoError(t, err)
  25. visible, err = f.GetColVisible("Sheet1", "V")
  26. assert.Equal(t, false, visible)
  27. assert.NoError(t, err)
  28. // ...and displaying them back SetColVisible(...true)
  29. assert.NoError(t, f.SetColVisible("Sheet1", "V:F", true))
  30. visible, err = f.GetColVisible("Sheet1", "F")
  31. assert.Equal(t, true, visible)
  32. assert.NoError(t, err)
  33. visible, err = f.GetColVisible("Sheet1", "U")
  34. assert.Equal(t, true, visible)
  35. assert.NoError(t, err)
  36. visible, err = f.GetColVisible("Sheet1", "G")
  37. assert.Equal(t, true, visible)
  38. assert.NoError(t, err)
  39. // Test get column visible on an inexistent worksheet.
  40. _, err = f.GetColVisible("SheetN", "F")
  41. assert.EqualError(t, err, "sheet SheetN is not exist")
  42. // Test get column visible with illegal cell coordinates.
  43. _, err = f.GetColVisible("Sheet1", "*")
  44. assert.EqualError(t, err, `invalid column name "*"`)
  45. assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)
  46. f.NewSheet("Sheet3")
  47. assert.NoError(t, f.SetColVisible("Sheet3", "E", false))
  48. assert.EqualError(t, f.SetColVisible("Sheet1", "A:-1", true), "invalid column name \"-1\"")
  49. assert.EqualError(t, f.SetColVisible("SheetN", "E", false), "sheet SheetN is not exist")
  50. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColumnVisibility.xlsx")))
  51. })
  52. t.Run("TestBook3", func(t *testing.T) {
  53. f, err := prepareTestBook3()
  54. assert.NoError(t, err)
  55. visible, err := f.GetColVisible("Sheet1", "B")
  56. assert.Equal(t, true, visible)
  57. assert.NoError(t, err)
  58. })
  59. }
  60. func TestOutlineLevel(t *testing.T) {
  61. f := NewFile()
  62. level, err := f.GetColOutlineLevel("Sheet1", "D")
  63. assert.Equal(t, uint8(0), level)
  64. assert.NoError(t, err)
  65. f.NewSheet("Sheet2")
  66. assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
  67. level, err = f.GetColOutlineLevel("Sheet1", "D")
  68. assert.Equal(t, uint8(4), level)
  69. assert.NoError(t, err)
  70. level, err = f.GetColOutlineLevel("Shee2", "A")
  71. assert.Equal(t, uint8(0), level)
  72. assert.EqualError(t, err, "sheet Shee2 is not exist")
  73. assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
  74. assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
  75. assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
  76. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
  77. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
  78. // Test set row outline level on not exists worksheet.
  79. assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist")
  80. // Test get row outline level on not exists worksheet.
  81. _, err = f.GetRowOutlineLevel("SheetN", 1)
  82. assert.EqualError(t, err, "sheet SheetN is not exist")
  83. // Test set and get column outline level with illegal cell coordinates.
  84. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
  85. _, err = f.GetColOutlineLevel("Sheet1", "*")
  86. assert.EqualError(t, err, `invalid column name "*"`)
  87. // Test set column outline level on not exists worksheet.
  88. assert.EqualError(t, f.SetColOutlineLevel("SheetN", "E", 2), "sheet SheetN is not exist")
  89. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
  90. level, err = f.GetRowOutlineLevel("Sheet1", 2)
  91. assert.NoError(t, err)
  92. assert.Equal(t, uint8(7), level)
  93. _, err = f.GetRowOutlineLevel("Sheet1", 0)
  94. assert.EqualError(t, err, `invalid row number 0`)
  95. level, err = f.GetRowOutlineLevel("Sheet1", 10)
  96. assert.NoError(t, err)
  97. assert.Equal(t, uint8(0), level)
  98. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
  99. f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
  100. assert.NoError(t, err)
  101. assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
  102. }
  103. func TestSetColStyle(t *testing.T) {
  104. f := NewFile()
  105. style, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#94d3a2"],"pattern":1}}`)
  106. assert.NoError(t, err)
  107. // Test set column style on not exists worksheet.
  108. assert.EqualError(t, f.SetColStyle("SheetN", "E", style), "sheet SheetN is not exist")
  109. // Test set column style with illegal cell coordinates.
  110. assert.EqualError(t, f.SetColStyle("Sheet1", "*", style), `invalid column name "*"`)
  111. assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", style), `invalid column name "*"`)
  112. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  113. // Test set column style with already exists column with style.
  114. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  115. assert.NoError(t, f.SetColStyle("Sheet1", "D:C", style))
  116. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetColStyle.xlsx")))
  117. }
  118. func TestColWidth(t *testing.T) {
  119. f := NewFile()
  120. assert.NoError(t, f.SetColWidth("Sheet1", "B", "A", 12))
  121. assert.NoError(t, f.SetColWidth("Sheet1", "A", "B", 12))
  122. width, err := f.GetColWidth("Sheet1", "A")
  123. assert.Equal(t, float64(12), width)
  124. assert.NoError(t, err)
  125. width, err = f.GetColWidth("Sheet1", "C")
  126. assert.Equal(t, float64(64), width)
  127. assert.NoError(t, err)
  128. // Test set and get column width with illegal cell coordinates.
  129. width, err = f.GetColWidth("Sheet1", "*")
  130. assert.Equal(t, float64(64), width)
  131. assert.EqualError(t, err, `invalid column name "*"`)
  132. assert.EqualError(t, f.SetColWidth("Sheet1", "*", "B", 1), `invalid column name "*"`)
  133. assert.EqualError(t, f.SetColWidth("Sheet1", "A", "*", 1), `invalid column name "*"`)
  134. // Test set column width on not exists worksheet.
  135. assert.EqualError(t, f.SetColWidth("SheetN", "B", "A", 12), "sheet SheetN is not exist")
  136. // Test get column width on not exists worksheet.
  137. _, err = f.GetColWidth("SheetN", "A")
  138. assert.EqualError(t, err, "sheet SheetN is not exist")
  139. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColWidth.xlsx")))
  140. convertRowHeightToPixels(0)
  141. }
  142. func TestInsertCol(t *testing.T) {
  143. f := NewFile()
  144. sheet1 := f.GetSheetName(0)
  145. fillCells(f, sheet1, 10, 10)
  146. assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External"))
  147. assert.NoError(t, f.MergeCell(sheet1, "A1", "C3"))
  148. assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
  149. assert.NoError(t, f.InsertCol(sheet1, "A"))
  150. // Test insert column with illegal cell coordinates.
  151. assert.EqualError(t, f.InsertCol("Sheet1", "*"), `invalid column name "*"`)
  152. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestInsertCol.xlsx")))
  153. }
  154. func TestRemoveCol(t *testing.T) {
  155. f := NewFile()
  156. sheet1 := f.GetSheetName(0)
  157. fillCells(f, sheet1, 10, 15)
  158. assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External"))
  159. assert.NoError(t, f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External"))
  160. assert.NoError(t, f.MergeCell(sheet1, "A1", "B1"))
  161. assert.NoError(t, f.MergeCell(sheet1, "A2", "B2"))
  162. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  163. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  164. // Test remove column with illegal cell coordinates.
  165. assert.EqualError(t, f.RemoveCol("Sheet1", "*"), `invalid column name "*"`)
  166. // Test remove column on not exists worksheet.
  167. assert.EqualError(t, f.RemoveCol("SheetN", "B"), "sheet SheetN is not exist")
  168. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
  169. }