col_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package excelize
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestCols(t *testing.T) {
  9. const sheet2 = "Sheet2"
  10. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  11. if !assert.NoError(t, err) {
  12. t.FailNow()
  13. }
  14. cols, err := f.Cols(sheet2)
  15. if !assert.NoError(t, err) {
  16. t.FailNow()
  17. }
  18. var collectedRows [][]string
  19. for cols.Next() {
  20. rows, err := cols.Rows()
  21. assert.NoError(t, err)
  22. collectedRows = append(collectedRows, trimSliceSpace(rows))
  23. }
  24. if !assert.NoError(t, cols.Error()) {
  25. t.FailNow()
  26. }
  27. returnedColumns, err := f.GetCols(sheet2)
  28. assert.NoError(t, err)
  29. for i := range returnedColumns {
  30. returnedColumns[i] = trimSliceSpace(returnedColumns[i])
  31. }
  32. if !assert.Equal(t, collectedRows, returnedColumns) {
  33. t.FailNow()
  34. }
  35. f = NewFile()
  36. cells := []string{"C2", "C3", "C4"}
  37. for _, cell := range cells {
  38. assert.NoError(t, f.SetCellValue("Sheet1", cell, 1))
  39. }
  40. _, err = f.Rows("Sheet1")
  41. assert.NoError(t, err)
  42. f.Sheet["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
  43. Dimension: &xlsxDimension{
  44. Ref: "C2:C4",
  45. },
  46. }
  47. _, err = f.Rows("Sheet1")
  48. assert.NoError(t, err)
  49. }
  50. func TestColumnsIterator(t *testing.T) {
  51. const (
  52. sheet2 = "Sheet2"
  53. expectedNumCol = 9
  54. )
  55. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  56. require.NoError(t, err)
  57. cols, err := f.Cols(sheet2)
  58. require.NoError(t, err)
  59. var colCount int
  60. for cols.Next() {
  61. colCount++
  62. require.True(t, colCount <= expectedNumCol, "colCount is greater than expected")
  63. }
  64. assert.Equal(t, expectedNumCol, colCount)
  65. f = NewFile()
  66. cells := []string{"C2", "C3", "C4", "D2", "D3", "D4"}
  67. for _, cell := range cells {
  68. assert.NoError(t, f.SetCellValue("Sheet1", cell, 1))
  69. }
  70. cols, err = f.Cols("Sheet1")
  71. require.NoError(t, err)
  72. colCount = 0
  73. for cols.Next() {
  74. colCount++
  75. require.True(t, colCount <= 4, "colCount is greater than expected")
  76. }
  77. assert.Equal(t, 4, colCount)
  78. }
  79. func TestColsError(t *testing.T) {
  80. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  81. if !assert.NoError(t, err) {
  82. t.FailNow()
  83. }
  84. _, err = f.Cols("SheetN")
  85. assert.EqualError(t, err, "sheet SheetN is not exist")
  86. }
  87. func TestGetColsError(t *testing.T) {
  88. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  89. if !assert.NoError(t, err) {
  90. t.FailNow()
  91. }
  92. _, err = f.GetCols("SheetN")
  93. assert.EqualError(t, err, "sheet SheetN is not exist")
  94. f = NewFile()
  95. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  96. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<worksheet><sheetData><row r="A"><c r="2" t="str"><v>B</v></c></row></sheetData></worksheet>`)
  97. f.checked = nil
  98. _, err = f.GetCols("Sheet1")
  99. assert.EqualError(t, err, `strconv.Atoi: parsing "A": invalid syntax`)
  100. f = NewFile()
  101. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  102. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<worksheet><sheetData><row r="2"><c r="A" t="str"><v>B</v></c></row></sheetData></worksheet>`)
  103. f.checked = nil
  104. _, err = f.GetCols("Sheet1")
  105. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  106. f = NewFile()
  107. cols, err := f.Cols("Sheet1")
  108. assert.NoError(t, err)
  109. cols.totalRow = 2
  110. cols.totalCol = 2
  111. cols.curCol = 1
  112. cols.sheetXML = []byte(`<worksheet><sheetData><row r="1"><c r="A" t="str"><v>A</v></c></row></sheetData></worksheet>`)
  113. _, err = cols.Rows()
  114. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  115. }
  116. func TestColsRows(t *testing.T) {
  117. f := NewFile()
  118. f.NewSheet("Sheet1")
  119. _, err := f.Cols("Sheet1")
  120. assert.NoError(t, err)
  121. assert.NoError(t, f.SetCellValue("Sheet1", "A1", 1))
  122. f.Sheet["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
  123. Dimension: &xlsxDimension{
  124. Ref: "A1:A1",
  125. },
  126. }
  127. f = NewFile()
  128. f.XLSX["xl/worksheets/sheet1.xml"] = nil
  129. _, err = f.Cols("Sheet1")
  130. if !assert.NoError(t, err) {
  131. t.FailNow()
  132. }
  133. f = NewFile()
  134. cols, err := f.Cols("Sheet1")
  135. if !assert.NoError(t, err) {
  136. t.FailNow()
  137. }
  138. _, err = cols.Rows()
  139. assert.NoError(t, err)
  140. cols.stashCol, cols.curCol = 0, 1
  141. // Test if token is nil
  142. cols.sheetXML = nil
  143. _, err = cols.Rows()
  144. assert.NoError(t, err)
  145. }
  146. func TestColumnVisibility(t *testing.T) {
  147. t.Run("TestBook1", func(t *testing.T) {
  148. f, err := prepareTestBook1()
  149. assert.NoError(t, err)
  150. // Hide/display a column with SetColVisible
  151. assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
  152. assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
  153. visible, err := f.GetColVisible("Sheet1", "F")
  154. assert.Equal(t, true, visible)
  155. assert.NoError(t, err)
  156. // Test hiding a few columns SetColVisible(...false)...
  157. assert.NoError(t, f.SetColVisible("Sheet1", "F:V", false))
  158. visible, err = f.GetColVisible("Sheet1", "F")
  159. assert.Equal(t, false, visible)
  160. assert.NoError(t, err)
  161. visible, err = f.GetColVisible("Sheet1", "U")
  162. assert.Equal(t, false, visible)
  163. assert.NoError(t, err)
  164. visible, err = f.GetColVisible("Sheet1", "V")
  165. assert.Equal(t, false, visible)
  166. assert.NoError(t, err)
  167. // ...and displaying them back SetColVisible(...true)
  168. assert.NoError(t, f.SetColVisible("Sheet1", "V:F", true))
  169. visible, err = f.GetColVisible("Sheet1", "F")
  170. assert.Equal(t, true, visible)
  171. assert.NoError(t, err)
  172. visible, err = f.GetColVisible("Sheet1", "U")
  173. assert.Equal(t, true, visible)
  174. assert.NoError(t, err)
  175. visible, err = f.GetColVisible("Sheet1", "G")
  176. assert.Equal(t, true, visible)
  177. assert.NoError(t, err)
  178. // Test get column visible on an inexistent worksheet.
  179. _, err = f.GetColVisible("SheetN", "F")
  180. assert.EqualError(t, err, "sheet SheetN is not exist")
  181. // Test get column visible with illegal cell coordinates.
  182. _, err = f.GetColVisible("Sheet1", "*")
  183. assert.EqualError(t, err, `invalid column name "*"`)
  184. assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)
  185. f.NewSheet("Sheet3")
  186. assert.NoError(t, f.SetColVisible("Sheet3", "E", false))
  187. assert.EqualError(t, f.SetColVisible("Sheet1", "A:-1", true), "invalid column name \"-1\"")
  188. assert.EqualError(t, f.SetColVisible("SheetN", "E", false), "sheet SheetN is not exist")
  189. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColumnVisibility.xlsx")))
  190. })
  191. t.Run("TestBook3", func(t *testing.T) {
  192. f, err := prepareTestBook3()
  193. assert.NoError(t, err)
  194. visible, err := f.GetColVisible("Sheet1", "B")
  195. assert.Equal(t, true, visible)
  196. assert.NoError(t, err)
  197. })
  198. }
  199. func TestOutlineLevel(t *testing.T) {
  200. f := NewFile()
  201. level, err := f.GetColOutlineLevel("Sheet1", "D")
  202. assert.Equal(t, uint8(0), level)
  203. assert.NoError(t, err)
  204. f.NewSheet("Sheet2")
  205. assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
  206. level, err = f.GetColOutlineLevel("Sheet1", "D")
  207. assert.Equal(t, uint8(4), level)
  208. assert.NoError(t, err)
  209. level, err = f.GetColOutlineLevel("Shee2", "A")
  210. assert.Equal(t, uint8(0), level)
  211. assert.EqualError(t, err, "sheet Shee2 is not exist")
  212. assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
  213. assert.EqualError(t, f.SetColWidth("Sheet2", "A", "D", MaxColumnWidth+1), "the width of the column must be smaller than or equal to 255 characters")
  214. assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
  215. assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
  216. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
  217. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
  218. // Test set row outline level on not exists worksheet.
  219. assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist")
  220. // Test get row outline level on not exists worksheet.
  221. _, err = f.GetRowOutlineLevel("SheetN", 1)
  222. assert.EqualError(t, err, "sheet SheetN is not exist")
  223. // Test set and get column outline level with illegal cell coordinates.
  224. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
  225. _, err = f.GetColOutlineLevel("Sheet1", "*")
  226. assert.EqualError(t, err, `invalid column name "*"`)
  227. // Test set column outline level on not exists worksheet.
  228. assert.EqualError(t, f.SetColOutlineLevel("SheetN", "E", 2), "sheet SheetN is not exist")
  229. assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
  230. level, err = f.GetRowOutlineLevel("Sheet1", 2)
  231. assert.NoError(t, err)
  232. assert.Equal(t, uint8(7), level)
  233. _, err = f.GetRowOutlineLevel("Sheet1", 0)
  234. assert.EqualError(t, err, `invalid row number 0`)
  235. level, err = f.GetRowOutlineLevel("Sheet1", 10)
  236. assert.NoError(t, err)
  237. assert.Equal(t, uint8(0), level)
  238. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
  239. f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
  240. assert.NoError(t, err)
  241. assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
  242. }
  243. func TestSetColStyle(t *testing.T) {
  244. f := NewFile()
  245. style, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#94d3a2"],"pattern":1}}`)
  246. assert.NoError(t, err)
  247. // Test set column style on not exists worksheet.
  248. assert.EqualError(t, f.SetColStyle("SheetN", "E", style), "sheet SheetN is not exist")
  249. // Test set column style with illegal cell coordinates.
  250. assert.EqualError(t, f.SetColStyle("Sheet1", "*", style), `invalid column name "*"`)
  251. assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", style), `invalid column name "*"`)
  252. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  253. // Test set column style with already exists column with style.
  254. assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
  255. assert.NoError(t, f.SetColStyle("Sheet1", "D:C", style))
  256. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetColStyle.xlsx")))
  257. }
  258. func TestColWidth(t *testing.T) {
  259. f := NewFile()
  260. assert.NoError(t, f.SetColWidth("Sheet1", "B", "A", 12))
  261. assert.NoError(t, f.SetColWidth("Sheet1", "A", "B", 12))
  262. width, err := f.GetColWidth("Sheet1", "A")
  263. assert.Equal(t, float64(12), width)
  264. assert.NoError(t, err)
  265. width, err = f.GetColWidth("Sheet1", "C")
  266. assert.Equal(t, defaultColWidth, width)
  267. assert.NoError(t, err)
  268. // Test set and get column width with illegal cell coordinates.
  269. width, err = f.GetColWidth("Sheet1", "*")
  270. assert.Equal(t, defaultColWidth, width)
  271. assert.EqualError(t, err, `invalid column name "*"`)
  272. assert.EqualError(t, f.SetColWidth("Sheet1", "*", "B", 1), `invalid column name "*"`)
  273. assert.EqualError(t, f.SetColWidth("Sheet1", "A", "*", 1), `invalid column name "*"`)
  274. // Test set column width on not exists worksheet.
  275. assert.EqualError(t, f.SetColWidth("SheetN", "B", "A", 12), "sheet SheetN is not exist")
  276. // Test get column width on not exists worksheet.
  277. _, err = f.GetColWidth("SheetN", "A")
  278. assert.EqualError(t, err, "sheet SheetN is not exist")
  279. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColWidth.xlsx")))
  280. convertRowHeightToPixels(0)
  281. }
  282. func TestInsertCol(t *testing.T) {
  283. f := NewFile()
  284. sheet1 := f.GetSheetName(0)
  285. fillCells(f, sheet1, 10, 10)
  286. assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External"))
  287. assert.NoError(t, f.MergeCell(sheet1, "A1", "C3"))
  288. assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
  289. assert.NoError(t, f.InsertCol(sheet1, "A"))
  290. // Test insert column with illegal cell coordinates.
  291. assert.EqualError(t, f.InsertCol("Sheet1", "*"), `invalid column name "*"`)
  292. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestInsertCol.xlsx")))
  293. }
  294. func TestRemoveCol(t *testing.T) {
  295. f := NewFile()
  296. sheet1 := f.GetSheetName(0)
  297. fillCells(f, sheet1, 10, 15)
  298. assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External"))
  299. assert.NoError(t, f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External"))
  300. assert.NoError(t, f.MergeCell(sheet1, "A1", "B1"))
  301. assert.NoError(t, f.MergeCell(sheet1, "A2", "B2"))
  302. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  303. assert.NoError(t, f.RemoveCol(sheet1, "A"))
  304. // Test remove column with illegal cell coordinates.
  305. assert.EqualError(t, f.RemoveCol("Sheet1", "*"), `invalid column name "*"`)
  306. // Test remove column on not exists worksheet.
  307. assert.EqualError(t, f.RemoveCol("SheetN", "B"), "sheet SheetN is not exist")
  308. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
  309. }
  310. func TestConvertColWidthToPixels(t *testing.T) {
  311. assert.Equal(t, -11.0, convertColWidthToPixels(-1))
  312. }