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.

171 lines
6.2KB

  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. assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
  12. assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
  13. visible, err := f.GetColVisible("Sheet1", "F")
  14. assert.Equal(t, true, visible)
  15. assert.NoError(t, err)
  16. // Test get column visiable on not exists worksheet.
  17. _, err = f.GetColVisible("SheetN", "F")
  18. assert.EqualError(t, err, "sheet SheetN is not exist")
  19. // Test get column visiable with illegal cell coordinates.
  20. _, err = f.GetColVisible("Sheet1", "*")
  21. assert.EqualError(t, err, `invalid column name "*"`)
  22. assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)
  23. f.NewSheet("Sheet3")
  24. assert.NoError(t, f.SetColVisible("Sheet3", "E", false))
  25. assert.EqualError(t, f.SetColVisible("SheetN", "E", false), "sheet SheetN is not exist")
  26. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColumnVisibility.xlsx")))
  27. })
  28. t.Run("TestBook3", func(t *testing.T) {
  29. f, err := prepareTestBook3()
  30. assert.NoError(t, err)
  31. f.GetColVisible("Sheet1", "B")
  32. })
  33. }
  34. func TestOutlineLevel(t *testing.T) {
  35. f := NewFile()
  36. f.GetColOutlineLevel("Sheet1", "D")
  37. f.NewSheet("Sheet2")
  38. assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
  39. f.GetColOutlineLevel("Sheet1", "D")
  40. f.GetColOutlineLevel("Shee2", "A")
  41. assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
  42. assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
  43. assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
  44. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
  45. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
  46. // Test set row outline level on not exists worksheet.
  47. assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist")
  48. // Test get row outline level on not exists worksheet.
  49. _, err := f.GetRowOutlineLevel("SheetN", 1)
  50. assert.EqualError(t, err, "sheet SheetN is not exist")
  51. // Test set and get column outline level with illegal cell coordinates.
  52. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
  53. _, err = f.GetColOutlineLevel("Sheet1", "*")
  54. assert.EqualError(t, err, `invalid column name "*"`)
  55. // Test set column outline level on not exists worksheet.
  56. assert.EqualError(t, f.SetColOutlineLevel("SheetN", "E", 2), "sheet SheetN is not exist")
  57. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
  58. level, err := f.GetRowOutlineLevel("Sheet1", 2)
  59. assert.NoError(t, err)
  60. assert.Equal(t, uint8(7), level)
  61. _, err = f.GetRowOutlineLevel("Sheet1", 0)
  62. assert.EqualError(t, err, `invalid row number 0`)
  63. level, err = f.GetRowOutlineLevel("Sheet1", 10)
  64. assert.NoError(t, err)
  65. assert.Equal(t, uint8(0), level)
  66. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
  67. f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
  68. assert.NoError(t, err)
  69. f.SetColOutlineLevel("Sheet2", "B", 2)
  70. }
  71. func TestSetColStyle(t *testing.T) {
  72. f := NewFile()
  73. style, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#94d3a2"],"pattern":1}}`)
  74. assert.NoError(t, err)
  75. // Test set column style on not exists worksheet.
  76. assert.EqualError(t, f.SetColStyle("SheetN", "E", style), "sheet SheetN is not exist")
  77. // Test set column style with illegal cell coordinates.
  78. assert.EqualError(t, f.SetColStyle("Sheet1", "*", style), `invalid column name "*"`)
  79. assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", style), `invalid column name "*"`)
  80. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  81. // Test set column style with already exists column with style.
  82. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  83. assert.NoError(t, f.SetColStyle("Sheet1", "D:C", style))
  84. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetColStyle.xlsx")))
  85. }
  86. func TestColWidth(t *testing.T) {
  87. f := NewFile()
  88. f.SetColWidth("Sheet1", "B", "A", 12)
  89. f.SetColWidth("Sheet1", "A", "B", 12)
  90. f.GetColWidth("Sheet1", "A")
  91. f.GetColWidth("Sheet1", "C")
  92. // Test set and get column width with illegal cell coordinates.
  93. _, err := f.GetColWidth("Sheet1", "*")
  94. assert.EqualError(t, err, `invalid column name "*"`)
  95. assert.EqualError(t, f.SetColWidth("Sheet1", "*", "B", 1), `invalid column name "*"`)
  96. assert.EqualError(t, f.SetColWidth("Sheet1", "A", "*", 1), `invalid column name "*"`)
  97. // Test set column width on not exists worksheet.
  98. assert.EqualError(t, f.SetColWidth("SheetN", "B", "A", 12), "sheet SheetN is not exist")
  99. // Test get column width on not exists worksheet.
  100. _, err = f.GetColWidth("SheetN", "A")
  101. assert.EqualError(t, err, "sheet SheetN is not exist")
  102. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColWidth.xlsx")))
  103. convertRowHeightToPixels(0)
  104. }
  105. func TestInsertCol(t *testing.T) {
  106. f := NewFile()
  107. sheet1 := f.GetSheetName(1)
  108. fillCells(f, sheet1, 10, 10)
  109. f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  110. f.MergeCell(sheet1, "A1", "C3")
  111. assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
  112. assert.NoError(t, f.InsertCol(sheet1, "A"))
  113. // Test insert column with illegal cell coordinates.
  114. assert.EqualError(t, f.InsertCol("Sheet1", "*"), `invalid column name "*"`)
  115. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestInsertCol.xlsx")))
  116. }
  117. func TestRemoveCol(t *testing.T) {
  118. f := NewFile()
  119. sheet1 := f.GetSheetName(1)
  120. fillCells(f, sheet1, 10, 15)
  121. f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  122. f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External")
  123. f.MergeCell(sheet1, "A1", "B1")
  124. f.MergeCell(sheet1, "A2", "B2")
  125. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  126. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  127. // Test remove column with illegal cell coordinates.
  128. assert.EqualError(t, f.RemoveCol("Sheet1", "*"), `invalid column name "*"`)
  129. // Test remove column on not exists worksheet.
  130. assert.EqualError(t, f.RemoveCol("SheetN", "B"), "sheet SheetN is not exist")
  131. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
  132. }