picture_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package excelize
  2. import (
  3. _ "image/gif"
  4. _ "image/jpeg"
  5. _ "image/png"
  6. _ "golang.org/x/image/tiff"
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func BenchmarkAddPictureFromBytes(b *testing.B) {
  16. f := NewFile()
  17. imgFile, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
  18. if err != nil {
  19. b.Error("unable to load image for benchmark")
  20. }
  21. b.ResetTimer()
  22. for i := 1; i <= b.N; i++ {
  23. if err := f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "excel", ".png", imgFile); err != nil {
  24. b.Error(err)
  25. }
  26. }
  27. }
  28. func TestAddPicture(t *testing.T) {
  29. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  30. if !assert.NoError(t, err) {
  31. t.FailNow()
  32. }
  33. // Test add picture to worksheet with offset and location hyperlink.
  34. assert.NoError(t, f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
  35. `{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`))
  36. // Test add picture to worksheet with offset, external hyperlink and positioning.
  37. assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
  38. `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`))
  39. file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
  40. assert.NoError(t, err)
  41. // Test add picture to worksheet from bytes.
  42. assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
  43. // Test add picture to worksheet from bytes with illegal cell coordinates.
  44. assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  45. assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
  46. assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
  47. assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
  48. // Test write file to given path.
  49. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
  50. }
  51. func TestAddPictureErrors(t *testing.T) {
  52. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  53. assert.NoError(t, err)
  54. // Test add picture to worksheet with invalid file path.
  55. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "not_exists_dir", "not_exists.icon"), "")
  56. if assert.Error(t, err) {
  57. assert.True(t, os.IsNotExist(err), "Expected os.IsNotExist(err) == true")
  58. }
  59. // Test add picture to worksheet with unsupport file type.
  60. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "Book1.xlsx"), "")
  61. assert.EqualError(t, err, "unsupported image extension")
  62. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", "jpg", make([]byte, 1))
  63. assert.EqualError(t, err, "unsupported image extension")
  64. // Test add picture to worksheet with invalid file data.
  65. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
  66. assert.EqualError(t, err, "image: unknown format")
  67. }
  68. func TestGetPicture(t *testing.T) {
  69. f, err := prepareTestBook1()
  70. if !assert.NoError(t, err) {
  71. t.FailNow()
  72. }
  73. file, raw, err := f.GetPicture("Sheet1", "F21")
  74. assert.NoError(t, err)
  75. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  76. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  77. t.FailNow()
  78. }
  79. // Try to get picture from a worksheet with illegal cell coordinates.
  80. _, _, err = f.GetPicture("Sheet1", "A")
  81. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  82. // Try to get picture from a worksheet that doesn't contain any images.
  83. file, raw, err = f.GetPicture("Sheet3", "I9")
  84. assert.EqualError(t, err, "sheet Sheet3 is not exist")
  85. assert.Empty(t, file)
  86. assert.Empty(t, raw)
  87. // Try to get picture from a cell that doesn't contain an image.
  88. file, raw, err = f.GetPicture("Sheet2", "A2")
  89. assert.NoError(t, err)
  90. assert.Empty(t, file)
  91. assert.Empty(t, raw)
  92. f.getDrawingRelationships("xl/worksheets/_rels/sheet1.xml.rels", "rId8")
  93. f.getDrawingRelationships("", "")
  94. f.getSheetRelationshipsTargetByID("", "")
  95. f.deleteSheetRelationships("", "")
  96. // Try to get picture from a local storage file.
  97. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx")))
  98. f, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx"))
  99. assert.NoError(t, err)
  100. file, raw, err = f.GetPicture("Sheet1", "F21")
  101. assert.NoError(t, err)
  102. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  103. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  104. t.FailNow()
  105. }
  106. // Try to get picture from a local storage file that doesn't contain an image.
  107. file, raw, err = f.GetPicture("Sheet1", "F22")
  108. assert.NoError(t, err)
  109. assert.Empty(t, file)
  110. assert.Empty(t, raw)
  111. // Test get picture from none drawing worksheet.
  112. f = NewFile()
  113. file, raw, err = f.GetPicture("Sheet1", "F22")
  114. assert.NoError(t, err)
  115. assert.Empty(t, file)
  116. assert.Empty(t, raw)
  117. }
  118. func TestAddDrawingPicture(t *testing.T) {
  119. // testing addDrawingPicture with illegal cell coordinates.
  120. f := NewFile()
  121. assert.EqualError(t, f.addDrawingPicture("sheet1", "", "A", "", 0, 0, 0, 0, nil), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  122. }
  123. func TestAddPictureFromBytes(t *testing.T) {
  124. f := NewFile()
  125. imgFile, err := ioutil.ReadFile("logo.png")
  126. assert.NoError(t, err, "Unable to load logo for test")
  127. assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 1), "", "logo", ".png", imgFile))
  128. assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 50), "", "logo", ".png", imgFile))
  129. imageCount := 0
  130. for fileName := range f.XLSX {
  131. if strings.Contains(fileName, "media/image") {
  132. imageCount++
  133. }
  134. }
  135. assert.Equal(t, 1, imageCount, "Duplicate image should only be stored once.")
  136. assert.EqualError(t, f.AddPictureFromBytes("SheetN", fmt.Sprint("A", 1), "", "logo", ".png", imgFile), "sheet SheetN is not exist")
  137. }
  138. func TestDeletePicture(t *testing.T) {
  139. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  140. assert.NoError(t, err)
  141. assert.NoError(t, f.DeletePicture("Sheet1", "A1"))
  142. assert.NoError(t, f.AddPicture("Sheet1", "P1", filepath.Join("test", "images", "excel.jpg"), ""))
  143. assert.NoError(t, f.DeletePicture("Sheet1", "P1"))
  144. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeletePicture.xlsx")))
  145. // Test delete picture on not exists worksheet.
  146. assert.EqualError(t, f.DeletePicture("SheetN", "A1"), "sheet SheetN is not exist")
  147. // Test delete picture with invalid coordinates.
  148. assert.EqualError(t, f.DeletePicture("Sheet1", ""), `cannot convert cell "" to coordinates: invalid cell name ""`)
  149. // Test delete picture on no chart worksheet.
  150. assert.NoError(t, NewFile().DeletePicture("Sheet1", "A1"))
  151. }