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.

728 lines
18KB

  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestRows(t *testing.T) {
  10. const sheet2 = "Sheet2"
  11. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  12. if !assert.NoError(t, err) {
  13. t.FailNow()
  14. }
  15. rows, err := xlsx.Rows(sheet2)
  16. if !assert.NoError(t, err) {
  17. t.FailNow()
  18. }
  19. var collectedRows [][]string
  20. for rows.Next() {
  21. columns, err := rows.Columns()
  22. assert.NoError(t, err)
  23. collectedRows = append(collectedRows, trimSliceSpace(columns))
  24. }
  25. if !assert.NoError(t, rows.Error()) {
  26. t.FailNow()
  27. }
  28. returnedRows, err := xlsx.GetRows(sheet2)
  29. assert.NoError(t, err)
  30. for i := range returnedRows {
  31. returnedRows[i] = trimSliceSpace(returnedRows[i])
  32. }
  33. if !assert.Equal(t, collectedRows, returnedRows) {
  34. t.FailNow()
  35. }
  36. }
  37. func TestRowsIterator(t *testing.T) {
  38. const (
  39. sheet2 = "Sheet2"
  40. expectedNumRow = 11
  41. )
  42. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  43. require.NoError(t, err)
  44. rows, err := xlsx.Rows(sheet2)
  45. require.NoError(t, err)
  46. var rowCount int
  47. for rows.Next() {
  48. rowCount++
  49. require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
  50. }
  51. assert.Equal(t, expectedNumRow, rowCount)
  52. rows = &Rows{f: xlsx, rows: []xlsxRow{{C: []xlsxC{{R: "A"}}}}, curRow: 1}
  53. _, err = rows.Columns()
  54. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  55. }
  56. func TestRowsError(t *testing.T) {
  57. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  58. if !assert.NoError(t, err) {
  59. t.FailNow()
  60. }
  61. _, err = xlsx.Rows("SheetN")
  62. assert.EqualError(t, err, "sheet SheetN is not exist")
  63. }
  64. func TestRowHeight(t *testing.T) {
  65. xlsx := NewFile()
  66. sheet1 := xlsx.GetSheetName(1)
  67. assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
  68. _, err := xlsx.GetRowHeight("Sheet1", 0)
  69. assert.EqualError(t, err, "invalid row number 0")
  70. assert.NoError(t, xlsx.SetRowHeight(sheet1, 1, 111.0))
  71. height, err := xlsx.GetRowHeight(sheet1, 1)
  72. assert.NoError(t, err)
  73. assert.Equal(t, 111.0, height)
  74. assert.NoError(t, xlsx.SetRowHeight(sheet1, 4, 444.0))
  75. height, err = xlsx.GetRowHeight(sheet1, 4)
  76. assert.NoError(t, err)
  77. assert.Equal(t, 444.0, height)
  78. // Test get row height that rows index over exists rows.
  79. height, err = xlsx.GetRowHeight(sheet1, 5)
  80. assert.NoError(t, err)
  81. assert.Equal(t, defaultRowHeightPixels, height)
  82. // Test get row height that rows heights haven't changed.
  83. height, err = xlsx.GetRowHeight(sheet1, 3)
  84. assert.NoError(t, err)
  85. assert.Equal(t, defaultRowHeightPixels, height)
  86. // Test set and get row height on not exists worksheet.
  87. assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")
  88. _, err = xlsx.GetRowHeight("SheetN", 3)
  89. assert.EqualError(t, err, "sheet SheetN is not exist")
  90. err = xlsx.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
  91. if !assert.NoError(t, err) {
  92. t.FailNow()
  93. }
  94. convertColWidthToPixels(0)
  95. }
  96. func TestRowVisibility(t *testing.T) {
  97. f, err := prepareTestBook1()
  98. if !assert.NoError(t, err) {
  99. t.FailNow()
  100. }
  101. f.NewSheet("Sheet3")
  102. assert.NoError(t, f.SetRowVisible("Sheet3", 2, false))
  103. assert.NoError(t, f.SetRowVisible("Sheet3", 2, true))
  104. f.GetRowVisible("Sheet3", 2)
  105. f.GetRowVisible("Sheet3", 25)
  106. assert.EqualError(t, f.SetRowVisible("Sheet3", 0, true), "invalid row number 0")
  107. assert.EqualError(t, f.SetRowVisible("SheetN", 2, false), "sheet SheetN is not exist")
  108. visible, err := f.GetRowVisible("Sheet3", 0)
  109. assert.Equal(t, false, visible)
  110. assert.EqualError(t, err, "invalid row number 0")
  111. _, err = f.GetRowVisible("SheetN", 1)
  112. assert.EqualError(t, err, "sheet SheetN is not exist")
  113. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx")))
  114. }
  115. func TestRemoveRow(t *testing.T) {
  116. xlsx := NewFile()
  117. sheet1 := xlsx.GetSheetName(1)
  118. r, err := xlsx.workSheetReader(sheet1)
  119. assert.NoError(t, err)
  120. const (
  121. colCount = 10
  122. rowCount = 10
  123. )
  124. fillCells(xlsx, sheet1, colCount, rowCount)
  125. xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  126. assert.EqualError(t, xlsx.RemoveRow(sheet1, -1), "invalid row number -1")
  127. assert.EqualError(t, xlsx.RemoveRow(sheet1, 0), "invalid row number 0")
  128. assert.NoError(t, xlsx.RemoveRow(sheet1, 4))
  129. if !assert.Len(t, r.SheetData.Row, rowCount-1) {
  130. t.FailNow()
  131. }
  132. xlsx.MergeCell(sheet1, "B3", "B5")
  133. assert.NoError(t, xlsx.RemoveRow(sheet1, 2))
  134. if !assert.Len(t, r.SheetData.Row, rowCount-2) {
  135. t.FailNow()
  136. }
  137. assert.NoError(t, xlsx.RemoveRow(sheet1, 4))
  138. if !assert.Len(t, r.SheetData.Row, rowCount-3) {
  139. t.FailNow()
  140. }
  141. err = xlsx.AutoFilter(sheet1, "A2", "A2", `{"column":"A","expression":"x != blanks"}`)
  142. if !assert.NoError(t, err) {
  143. t.FailNow()
  144. }
  145. assert.NoError(t, xlsx.RemoveRow(sheet1, 1))
  146. if !assert.Len(t, r.SheetData.Row, rowCount-4) {
  147. t.FailNow()
  148. }
  149. assert.NoError(t, xlsx.RemoveRow(sheet1, 2))
  150. if !assert.Len(t, r.SheetData.Row, rowCount-5) {
  151. t.FailNow()
  152. }
  153. assert.NoError(t, xlsx.RemoveRow(sheet1, 1))
  154. if !assert.Len(t, r.SheetData.Row, rowCount-6) {
  155. t.FailNow()
  156. }
  157. assert.NoError(t, xlsx.RemoveRow(sheet1, 10))
  158. assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRemoveRow.xlsx")))
  159. }
  160. func TestInsertRow(t *testing.T) {
  161. xlsx := NewFile()
  162. sheet1 := xlsx.GetSheetName(1)
  163. r, err := xlsx.workSheetReader(sheet1)
  164. assert.NoError(t, err)
  165. const (
  166. colCount = 10
  167. rowCount = 10
  168. )
  169. fillCells(xlsx, sheet1, colCount, rowCount)
  170. xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
  171. assert.EqualError(t, xlsx.InsertRow(sheet1, -1), "invalid row number -1")
  172. assert.EqualError(t, xlsx.InsertRow(sheet1, 0), "invalid row number 0")
  173. assert.NoError(t, xlsx.InsertRow(sheet1, 1))
  174. if !assert.Len(t, r.SheetData.Row, rowCount+1) {
  175. t.FailNow()
  176. }
  177. assert.NoError(t, xlsx.InsertRow(sheet1, 4))
  178. if !assert.Len(t, r.SheetData.Row, rowCount+2) {
  179. t.FailNow()
  180. }
  181. assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestInsertRow.xlsx")))
  182. }
  183. // Testing internal sructure state after insert operations.
  184. // It is important for insert workflow to be constant to avoid side effect with functions related to internal structure.
  185. func TestInsertRowInEmptyFile(t *testing.T) {
  186. xlsx := NewFile()
  187. sheet1 := xlsx.GetSheetName(1)
  188. r, err := xlsx.workSheetReader(sheet1)
  189. assert.NoError(t, err)
  190. assert.NoError(t, xlsx.InsertRow(sheet1, 1))
  191. assert.Len(t, r.SheetData.Row, 0)
  192. assert.NoError(t, xlsx.InsertRow(sheet1, 2))
  193. assert.Len(t, r.SheetData.Row, 0)
  194. assert.NoError(t, xlsx.InsertRow(sheet1, 99))
  195. assert.Len(t, r.SheetData.Row, 0)
  196. assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestInsertRowInEmptyFile.xlsx")))
  197. }
  198. func TestDuplicateRowFromSingleRow(t *testing.T) {
  199. const sheet = "Sheet1"
  200. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  201. cells := map[string]string{
  202. "A1": "A1 Value",
  203. "A2": "A2 Value",
  204. "A3": "A3 Value",
  205. "B1": "B1 Value",
  206. "B2": "B2 Value",
  207. "B3": "B3 Value",
  208. }
  209. t.Run("FromSingleRow", func(t *testing.T) {
  210. xlsx := NewFile()
  211. xlsx.SetCellStr(sheet, "A1", cells["A1"])
  212. xlsx.SetCellStr(sheet, "B1", cells["B1"])
  213. assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
  214. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FromSingleRow_1"))) {
  215. t.FailNow()
  216. }
  217. expect := map[string]string{
  218. "A1": cells["A1"], "B1": cells["B1"],
  219. "A2": cells["A1"], "B2": cells["B1"],
  220. }
  221. for cell, val := range expect {
  222. v, err := xlsx.GetCellValue(sheet, cell)
  223. assert.NoError(t, err)
  224. if !assert.Equal(t, val, v, cell) {
  225. t.FailNow()
  226. }
  227. }
  228. assert.NoError(t, xlsx.DuplicateRow(sheet, 2))
  229. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FromSingleRow_2"))) {
  230. t.FailNow()
  231. }
  232. expect = map[string]string{
  233. "A1": cells["A1"], "B1": cells["B1"],
  234. "A2": cells["A1"], "B2": cells["B1"],
  235. "A3": cells["A1"], "B3": cells["B1"],
  236. }
  237. for cell, val := range expect {
  238. v, err := xlsx.GetCellValue(sheet, cell)
  239. assert.NoError(t, err)
  240. if !assert.Equal(t, val, v, cell) {
  241. t.FailNow()
  242. }
  243. }
  244. })
  245. }
  246. func TestDuplicateRowUpdateDuplicatedRows(t *testing.T) {
  247. const sheet = "Sheet1"
  248. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  249. cells := map[string]string{
  250. "A1": "A1 Value",
  251. "A2": "A2 Value",
  252. "A3": "A3 Value",
  253. "B1": "B1 Value",
  254. "B2": "B2 Value",
  255. "B3": "B3 Value",
  256. }
  257. t.Run("UpdateDuplicatedRows", func(t *testing.T) {
  258. xlsx := NewFile()
  259. xlsx.SetCellStr(sheet, "A1", cells["A1"])
  260. xlsx.SetCellStr(sheet, "B1", cells["B1"])
  261. assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
  262. xlsx.SetCellStr(sheet, "A2", cells["A2"])
  263. xlsx.SetCellStr(sheet, "B2", cells["B2"])
  264. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.UpdateDuplicatedRows"))) {
  265. t.FailNow()
  266. }
  267. expect := map[string]string{
  268. "A1": cells["A1"], "B1": cells["B1"],
  269. "A2": cells["A2"], "B2": cells["B2"],
  270. }
  271. for cell, val := range expect {
  272. v, err := xlsx.GetCellValue(sheet, cell)
  273. assert.NoError(t, err)
  274. if !assert.Equal(t, val, v, cell) {
  275. t.FailNow()
  276. }
  277. }
  278. })
  279. }
  280. func TestDuplicateRowFirstOfMultipleRows(t *testing.T) {
  281. const sheet = "Sheet1"
  282. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  283. cells := map[string]string{
  284. "A1": "A1 Value",
  285. "A2": "A2 Value",
  286. "A3": "A3 Value",
  287. "B1": "B1 Value",
  288. "B2": "B2 Value",
  289. "B3": "B3 Value",
  290. }
  291. newFileWithDefaults := func() *File {
  292. f := NewFile()
  293. for cell, val := range cells {
  294. f.SetCellStr(sheet, cell, val)
  295. }
  296. return f
  297. }
  298. t.Run("FirstOfMultipleRows", func(t *testing.T) {
  299. xlsx := newFileWithDefaults()
  300. assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
  301. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FirstOfMultipleRows"))) {
  302. t.FailNow()
  303. }
  304. expect := map[string]string{
  305. "A1": cells["A1"], "B1": cells["B1"],
  306. "A2": cells["A1"], "B2": cells["B1"],
  307. "A3": cells["A2"], "B3": cells["B2"],
  308. "A4": cells["A3"], "B4": cells["B3"],
  309. }
  310. for cell, val := range expect {
  311. v, err := xlsx.GetCellValue(sheet, cell)
  312. assert.NoError(t, err)
  313. if !assert.Equal(t, val, v, cell) {
  314. t.FailNow()
  315. }
  316. }
  317. })
  318. }
  319. func TestDuplicateRowZeroWithNoRows(t *testing.T) {
  320. const sheet = "Sheet1"
  321. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  322. t.Run("ZeroWithNoRows", func(t *testing.T) {
  323. xlsx := NewFile()
  324. assert.EqualError(t, xlsx.DuplicateRow(sheet, 0), "invalid row number 0")
  325. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.ZeroWithNoRows"))) {
  326. t.FailNow()
  327. }
  328. val, err := xlsx.GetCellValue(sheet, "A1")
  329. assert.NoError(t, err)
  330. assert.Equal(t, "", val)
  331. val, err = xlsx.GetCellValue(sheet, "B1")
  332. assert.NoError(t, err)
  333. assert.Equal(t, "", val)
  334. val, err = xlsx.GetCellValue(sheet, "A2")
  335. assert.NoError(t, err)
  336. assert.Equal(t, "", val)
  337. val, err = xlsx.GetCellValue(sheet, "B2")
  338. assert.NoError(t, err)
  339. assert.Equal(t, "", val)
  340. assert.NoError(t, err)
  341. expect := map[string]string{
  342. "A1": "", "B1": "",
  343. "A2": "", "B2": "",
  344. }
  345. for cell, val := range expect {
  346. v, err := xlsx.GetCellValue(sheet, cell)
  347. assert.NoError(t, err)
  348. if !assert.Equal(t, val, v, cell) {
  349. t.FailNow()
  350. }
  351. }
  352. })
  353. }
  354. func TestDuplicateRowMiddleRowOfEmptyFile(t *testing.T) {
  355. const sheet = "Sheet1"
  356. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  357. t.Run("MiddleRowOfEmptyFile", func(t *testing.T) {
  358. xlsx := NewFile()
  359. assert.NoError(t, xlsx.DuplicateRow(sheet, 99))
  360. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.MiddleRowOfEmptyFile"))) {
  361. t.FailNow()
  362. }
  363. expect := map[string]string{
  364. "A98": "",
  365. "A99": "",
  366. "A100": "",
  367. }
  368. for cell, val := range expect {
  369. v, err := xlsx.GetCellValue(sheet, cell)
  370. assert.NoError(t, err)
  371. if !assert.Equal(t, val, v, cell) {
  372. t.FailNow()
  373. }
  374. }
  375. })
  376. }
  377. func TestDuplicateRowWithLargeOffsetToMiddleOfData(t *testing.T) {
  378. const sheet = "Sheet1"
  379. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  380. cells := map[string]string{
  381. "A1": "A1 Value",
  382. "A2": "A2 Value",
  383. "A3": "A3 Value",
  384. "B1": "B1 Value",
  385. "B2": "B2 Value",
  386. "B3": "B3 Value",
  387. }
  388. newFileWithDefaults := func() *File {
  389. f := NewFile()
  390. for cell, val := range cells {
  391. f.SetCellStr(sheet, cell, val)
  392. }
  393. return f
  394. }
  395. t.Run("WithLargeOffsetToMiddleOfData", func(t *testing.T) {
  396. xlsx := newFileWithDefaults()
  397. assert.NoError(t, xlsx.DuplicateRowTo(sheet, 1, 3))
  398. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.WithLargeOffsetToMiddleOfData"))) {
  399. t.FailNow()
  400. }
  401. expect := map[string]string{
  402. "A1": cells["A1"], "B1": cells["B1"],
  403. "A2": cells["A2"], "B2": cells["B2"],
  404. "A3": cells["A1"], "B3": cells["B1"],
  405. "A4": cells["A3"], "B4": cells["B3"],
  406. }
  407. for cell, val := range expect {
  408. v, err := xlsx.GetCellValue(sheet, cell)
  409. assert.NoError(t, err)
  410. if !assert.Equal(t, val, v, cell) {
  411. t.FailNow()
  412. }
  413. }
  414. })
  415. }
  416. func TestDuplicateRowWithLargeOffsetToEmptyRows(t *testing.T) {
  417. const sheet = "Sheet1"
  418. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  419. cells := map[string]string{
  420. "A1": "A1 Value",
  421. "A2": "A2 Value",
  422. "A3": "A3 Value",
  423. "B1": "B1 Value",
  424. "B2": "B2 Value",
  425. "B3": "B3 Value",
  426. }
  427. newFileWithDefaults := func() *File {
  428. f := NewFile()
  429. for cell, val := range cells {
  430. f.SetCellStr(sheet, cell, val)
  431. }
  432. return f
  433. }
  434. t.Run("WithLargeOffsetToEmptyRows", func(t *testing.T) {
  435. xlsx := newFileWithDefaults()
  436. assert.NoError(t, xlsx.DuplicateRowTo(sheet, 1, 7))
  437. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.WithLargeOffsetToEmptyRows"))) {
  438. t.FailNow()
  439. }
  440. expect := map[string]string{
  441. "A1": cells["A1"], "B1": cells["B1"],
  442. "A2": cells["A2"], "B2": cells["B2"],
  443. "A3": cells["A3"], "B3": cells["B3"],
  444. "A7": cells["A1"], "B7": cells["B1"],
  445. }
  446. for cell, val := range expect {
  447. v, err := xlsx.GetCellValue(sheet, cell)
  448. assert.NoError(t, err)
  449. if !assert.Equal(t, val, v, cell) {
  450. t.FailNow()
  451. }
  452. }
  453. })
  454. }
  455. func TestDuplicateRowInsertBefore(t *testing.T) {
  456. const sheet = "Sheet1"
  457. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  458. cells := map[string]string{
  459. "A1": "A1 Value",
  460. "A2": "A2 Value",
  461. "A3": "A3 Value",
  462. "B1": "B1 Value",
  463. "B2": "B2 Value",
  464. "B3": "B3 Value",
  465. }
  466. newFileWithDefaults := func() *File {
  467. f := NewFile()
  468. for cell, val := range cells {
  469. f.SetCellStr(sheet, cell, val)
  470. }
  471. return f
  472. }
  473. t.Run("InsertBefore", func(t *testing.T) {
  474. xlsx := newFileWithDefaults()
  475. assert.NoError(t, xlsx.DuplicateRowTo(sheet, 2, 1))
  476. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.InsertBefore"))) {
  477. t.FailNow()
  478. }
  479. expect := map[string]string{
  480. "A1": cells["A2"], "B1": cells["B2"],
  481. "A2": cells["A1"], "B2": cells["B1"],
  482. "A3": cells["A2"], "B3": cells["B2"],
  483. "A4": cells["A3"], "B4": cells["B3"],
  484. }
  485. for cell, val := range expect {
  486. v, err := xlsx.GetCellValue(sheet, cell)
  487. assert.NoError(t, err)
  488. if !assert.Equal(t, val, v, cell) {
  489. t.FailNow()
  490. }
  491. }
  492. })
  493. }
  494. func TestDuplicateRowInsertBeforeWithLargeOffset(t *testing.T) {
  495. const sheet = "Sheet1"
  496. outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
  497. cells := map[string]string{
  498. "A1": "A1 Value",
  499. "A2": "A2 Value",
  500. "A3": "A3 Value",
  501. "B1": "B1 Value",
  502. "B2": "B2 Value",
  503. "B3": "B3 Value",
  504. }
  505. newFileWithDefaults := func() *File {
  506. f := NewFile()
  507. for cell, val := range cells {
  508. f.SetCellStr(sheet, cell, val)
  509. }
  510. return f
  511. }
  512. t.Run("InsertBeforeWithLargeOffset", func(t *testing.T) {
  513. xlsx := newFileWithDefaults()
  514. assert.NoError(t, xlsx.DuplicateRowTo(sheet, 3, 1))
  515. if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.InsertBeforeWithLargeOffset"))) {
  516. t.FailNow()
  517. }
  518. expect := map[string]string{
  519. "A1": cells["A3"], "B1": cells["B3"],
  520. "A2": cells["A1"], "B2": cells["B1"],
  521. "A3": cells["A2"], "B3": cells["B2"],
  522. "A4": cells["A3"], "B4": cells["B3"],
  523. }
  524. for cell, val := range expect {
  525. v, err := xlsx.GetCellValue(sheet, cell)
  526. assert.NoError(t, err)
  527. if !assert.Equal(t, val, v) {
  528. t.FailNow()
  529. }
  530. }
  531. })
  532. }
  533. func TestDuplicateRowInvalidRownum(t *testing.T) {
  534. const sheet = "Sheet1"
  535. outFile := filepath.Join("test", "TestDuplicateRowInvalidRownum.%s.xlsx")
  536. cells := map[string]string{
  537. "A1": "A1 Value",
  538. "A2": "A2 Value",
  539. "A3": "A3 Value",
  540. "B1": "B1 Value",
  541. "B2": "B2 Value",
  542. "B3": "B3 Value",
  543. }
  544. invalidIndexes := []int{-100, -2, -1, 0}
  545. for _, row := range invalidIndexes {
  546. name := fmt.Sprintf("%d", row)
  547. t.Run(name, func(t *testing.T) {
  548. xlsx := NewFile()
  549. for col, val := range cells {
  550. xlsx.SetCellStr(sheet, col, val)
  551. }
  552. assert.EqualError(t, xlsx.DuplicateRow(sheet, row), fmt.Sprintf("invalid row number %d", row))
  553. for col, val := range cells {
  554. v, err := xlsx.GetCellValue(sheet, col)
  555. assert.NoError(t, err)
  556. if !assert.Equal(t, val, v) {
  557. t.FailNow()
  558. }
  559. }
  560. assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
  561. })
  562. }
  563. for _, row1 := range invalidIndexes {
  564. for _, row2 := range invalidIndexes {
  565. name := fmt.Sprintf("[%d,%d]", row1, row2)
  566. t.Run(name, func(t *testing.T) {
  567. xlsx := NewFile()
  568. for col, val := range cells {
  569. xlsx.SetCellStr(sheet, col, val)
  570. }
  571. assert.EqualError(t, xlsx.DuplicateRowTo(sheet, row1, row2), fmt.Sprintf("invalid row number %d", row1))
  572. for col, val := range cells {
  573. v, err := xlsx.GetCellValue(sheet, col)
  574. assert.NoError(t, err)
  575. if !assert.Equal(t, val, v) {
  576. t.FailNow()
  577. }
  578. }
  579. assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
  580. })
  581. }
  582. }
  583. }
  584. func TestErrSheetNotExistError(t *testing.T) {
  585. err := ErrSheetNotExist{SheetName: "Sheet1"}
  586. assert.EqualValues(t, err.Error(), "Sheet Sheet1 is not exist")
  587. }
  588. func BenchmarkRows(b *testing.B) {
  589. f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
  590. for i := 0; i < b.N; i++ {
  591. rows, _ := f.Rows("Sheet2")
  592. for rows.Next() {
  593. row, _ := rows.Columns()
  594. for i := range row {
  595. if i >= 0 {
  596. continue
  597. }
  598. }
  599. }
  600. }
  601. }
  602. func trimSliceSpace(s []string) []string {
  603. for {
  604. if len(s) > 0 && s[len(s)-1] == "" {
  605. s = s[:len(s)-1]
  606. } else {
  607. break
  608. }
  609. }
  610. return s
  611. }