picture_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 with autofit.
  42. assert.NoError(t, f.AddPicture("Sheet1", "A30", filepath.Join("test", "images", "excel.jpg"), `{"autofit": true}`))
  43. assert.NoError(t, f.AddPicture("Sheet1", "B30", filepath.Join("test", "images", "excel.jpg"), `{"x_offset": 10, "y_offset": 10, "autofit": true}`))
  44. f.NewSheet("AddPicture")
  45. assert.NoError(t, f.SetRowHeight("AddPicture", 10, 30))
  46. assert.NoError(t, f.MergeCell("AddPicture", "B3", "D9"))
  47. assert.NoError(t, f.AddPicture("AddPicture", "C6", filepath.Join("test", "images", "excel.jpg"), `{"autofit": true}`))
  48. assert.NoError(t, f.AddPicture("AddPicture", "A1", filepath.Join("test", "images", "excel.jpg"), `{"autofit": true}`))
  49. // Test add picture to worksheet from bytes.
  50. assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
  51. // Test add picture to worksheet from bytes with illegal cell coordinates.
  52. assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  53. assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
  54. assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
  55. assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
  56. // Test write file to given path.
  57. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
  58. }
  59. func TestAddPictureErrors(t *testing.T) {
  60. xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  61. assert.NoError(t, err)
  62. // Test add picture to worksheet with invalid file path.
  63. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "not_exists_dir", "not_exists.icon"), "")
  64. if assert.Error(t, err) {
  65. assert.True(t, os.IsNotExist(err), "Expected os.IsNotExist(err) == true")
  66. }
  67. // Test add picture to worksheet with unsupport file type.
  68. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "Book1.xlsx"), "")
  69. assert.EqualError(t, err, "unsupported image extension")
  70. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", "jpg", make([]byte, 1))
  71. assert.EqualError(t, err, "unsupported image extension")
  72. // Test add picture to worksheet with invalid file data.
  73. err = xlsx.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
  74. assert.EqualError(t, err, "image: unknown format")
  75. }
  76. func TestGetPicture(t *testing.T) {
  77. f, err := prepareTestBook1()
  78. if !assert.NoError(t, err) {
  79. t.FailNow()
  80. }
  81. file, raw, err := f.GetPicture("Sheet1", "F21")
  82. assert.NoError(t, err)
  83. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  84. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  85. t.FailNow()
  86. }
  87. // Try to get picture from a worksheet with illegal cell coordinates.
  88. _, _, err = f.GetPicture("Sheet1", "A")
  89. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  90. // Try to get picture from a worksheet that doesn't contain any images.
  91. file, raw, err = f.GetPicture("Sheet3", "I9")
  92. assert.EqualError(t, err, "sheet Sheet3 is not exist")
  93. assert.Empty(t, file)
  94. assert.Empty(t, raw)
  95. // Try to get picture from a cell that doesn't contain an image.
  96. file, raw, err = f.GetPicture("Sheet2", "A2")
  97. assert.NoError(t, err)
  98. assert.Empty(t, file)
  99. assert.Empty(t, raw)
  100. f.getDrawingRelationships("xl/worksheets/_rels/sheet1.xml.rels", "rId8")
  101. f.getDrawingRelationships("", "")
  102. f.getSheetRelationshipsTargetByID("", "")
  103. f.deleteSheetRelationships("", "")
  104. // Try to get picture from a local storage file.
  105. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx")))
  106. f, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx"))
  107. assert.NoError(t, err)
  108. file, raw, err = f.GetPicture("Sheet1", "F21")
  109. assert.NoError(t, err)
  110. if !assert.NotEmpty(t, filepath.Join("test", file)) || !assert.NotEmpty(t, raw) ||
  111. !assert.NoError(t, ioutil.WriteFile(filepath.Join("test", file), raw, 0644)) {
  112. t.FailNow()
  113. }
  114. // Try to get picture from a local storage file that doesn't contain an image.
  115. file, raw, err = f.GetPicture("Sheet1", "F22")
  116. assert.NoError(t, err)
  117. assert.Empty(t, file)
  118. assert.Empty(t, raw)
  119. // Test get picture from none drawing worksheet.
  120. f = NewFile()
  121. file, raw, err = f.GetPicture("Sheet1", "F22")
  122. assert.NoError(t, err)
  123. assert.Empty(t, file)
  124. assert.Empty(t, raw)
  125. f, err = prepareTestBook1()
  126. assert.NoError(t, err)
  127. f.XLSX["xl/drawings/drawing1.xml"] = MacintoshCyrillicCharset
  128. _, _, err = f.getPicture(20, 5, "xl/drawings/drawing1.xml", "xl/drawings/_rels/drawing2.xml.rels")
  129. assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
  130. }
  131. func TestAddDrawingPicture(t *testing.T) {
  132. // testing addDrawingPicture with illegal cell coordinates.
  133. f := NewFile()
  134. assert.EqualError(t, f.addDrawingPicture("sheet1", "", "A", "", 0, 0, 0, 0, nil), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  135. }
  136. func TestAddPictureFromBytes(t *testing.T) {
  137. f := NewFile()
  138. imgFile, err := ioutil.ReadFile("logo.png")
  139. assert.NoError(t, err, "Unable to load logo for test")
  140. assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 1), "", "logo", ".png", imgFile))
  141. assert.NoError(t, f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", 50), "", "logo", ".png", imgFile))
  142. imageCount := 0
  143. for fileName := range f.XLSX {
  144. if strings.Contains(fileName, "media/image") {
  145. imageCount++
  146. }
  147. }
  148. assert.Equal(t, 1, imageCount, "Duplicate image should only be stored once.")
  149. assert.EqualError(t, f.AddPictureFromBytes("SheetN", fmt.Sprint("A", 1), "", "logo", ".png", imgFile), "sheet SheetN is not exist")
  150. }
  151. func TestDeletePicture(t *testing.T) {
  152. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  153. assert.NoError(t, err)
  154. assert.NoError(t, f.DeletePicture("Sheet1", "A1"))
  155. assert.NoError(t, f.AddPicture("Sheet1", "P1", filepath.Join("test", "images", "excel.jpg"), ""))
  156. assert.NoError(t, f.DeletePicture("Sheet1", "P1"))
  157. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeletePicture.xlsx")))
  158. // Test delete picture on not exists worksheet.
  159. assert.EqualError(t, f.DeletePicture("SheetN", "A1"), "sheet SheetN is not exist")
  160. // Test delete picture with invalid coordinates.
  161. assert.EqualError(t, f.DeletePicture("Sheet1", ""), `cannot convert cell "" to coordinates: invalid cell name ""`)
  162. // Test delete picture on no chart worksheet.
  163. assert.NoError(t, NewFile().DeletePicture("Sheet1", "A1"))
  164. }
  165. func TestDrawingResize(t *testing.T) {
  166. f := NewFile()
  167. // Test calculate drawing resize on not exists worksheet.
  168. _, _, _, _, err := f.drawingResize("SheetN", "A1", 1, 1, nil)
  169. assert.EqualError(t, err, "sheet SheetN is not exist")
  170. // Test calculate drawing resize with invalid coordinates.
  171. _, _, _, _, err = f.drawingResize("Sheet1", "", 1, 1, nil)
  172. assert.EqualError(t, err, `cannot convert cell "" to coordinates: invalid cell name ""`)
  173. f.Sheet["xl/worksheets/sheet1.xml"].MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}}
  174. assert.EqualError(t, f.AddPicture("Sheet1", "A1", filepath.Join("test", "images", "excel.jpg"), `{"autofit": true}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  175. }