picture.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.10 or later.
  9. package excelize
  10. import (
  11. "bytes"
  12. "encoding/json"
  13. "encoding/xml"
  14. "errors"
  15. "image"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. )
  23. // parseFormatPictureSet provides a function to parse the format settings of
  24. // the picture with default value.
  25. func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
  26. format := formatPicture{
  27. FPrintsWithSheet: true,
  28. FLocksWithSheet: false,
  29. NoChangeAspect: false,
  30. OffsetX: 0,
  31. OffsetY: 0,
  32. XScale: 1.0,
  33. YScale: 1.0,
  34. }
  35. err := json.Unmarshal(parseFormatSet(formatSet), &format)
  36. return &format, err
  37. }
  38. // AddPicture provides the method to add picture in a sheet by given picture
  39. // format set (such as offset, scale, aspect ratio setting and print settings)
  40. // and file path. For example:
  41. //
  42. // package main
  43. //
  44. // import (
  45. // "fmt"
  46. // _ "image/gif"
  47. // _ "image/jpeg"
  48. // _ "image/png"
  49. //
  50. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  51. // )
  52. //
  53. // func main() {
  54. // f := excelize.NewFile()
  55. // // Insert a picture.
  56. // err := f.AddPicture("Sheet1", "A2", "./image1.jpg", "")
  57. // if err != nil {
  58. // fmt.Println(err)
  59. // }
  60. // // Insert a picture scaling in the cell with location hyperlink.
  61. // err = f.AddPicture("Sheet1", "D2", "./image1.png", `{"x_scale": 0.5, "y_scale": 0.5, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
  62. // if err != nil {
  63. // fmt.Println(err)
  64. // }
  65. // // Insert a picture offset in the cell with external hyperlink, printing and positioning support.
  66. // err = f.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "print_obj": true, "lock_aspect_ratio": false, "locked": false, "positioning": "oneCell"}`)
  67. // if err != nil {
  68. // fmt.Println(err)
  69. // }
  70. // err = f.SaveAs("./Book1.xlsx")
  71. // if err != nil {
  72. // fmt.Println(err)
  73. // }
  74. // }
  75. //
  76. // LinkType defines two types of hyperlink "External" for web site or
  77. // "Location" for moving to one of cell in this workbook. When the
  78. // "hyperlink_type" is "Location", coordinates need to start with "#".
  79. //
  80. // Positioning defines two types of the position of a picture in an Excel
  81. // spreadsheet, "oneCell" (Move but don't size with cells) or "absolute"
  82. // (Don't move or size with cells). If you don't set this parameter, default
  83. // positioning is move and size with cells.
  84. func (f *File) AddPicture(sheet, cell, picture, format string) error {
  85. var err error
  86. // Check picture exists first.
  87. if _, err = os.Stat(picture); os.IsNotExist(err) {
  88. return err
  89. }
  90. ext, ok := supportImageTypes[path.Ext(picture)]
  91. if !ok {
  92. return errors.New("unsupported image extension")
  93. }
  94. file, _ := ioutil.ReadFile(picture)
  95. _, name := filepath.Split(picture)
  96. return f.AddPictureFromBytes(sheet, cell, format, name, ext, file)
  97. }
  98. // AddPictureFromBytes provides the method to add picture in a sheet by given
  99. // picture format set (such as offset, scale, aspect ratio setting and print
  100. // settings), file base name, extension name and file bytes. For example:
  101. //
  102. // package main
  103. //
  104. // import (
  105. // "fmt"
  106. // _ "image/jpeg"
  107. // "io/ioutil"
  108. //
  109. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  110. // )
  111. //
  112. // func main() {
  113. // f := excelize.NewFile()
  114. //
  115. // file, err := ioutil.ReadFile("./image1.jpg")
  116. // if err != nil {
  117. // fmt.Println(err)
  118. // }
  119. // err = f.AddPictureFromBytes("Sheet1", "A2", "", "Excel Logo", ".jpg", file)
  120. // if err != nil {
  121. // fmt.Println(err)
  122. // }
  123. // err = f.SaveAs("./Book1.xlsx")
  124. // if err != nil {
  125. // fmt.Println(err)
  126. // }
  127. // }
  128. //
  129. func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error {
  130. var drawingHyperlinkRID int
  131. var hyperlinkType string
  132. ext, ok := supportImageTypes[extension]
  133. if !ok {
  134. return errors.New("unsupported image extension")
  135. }
  136. formatSet, err := parseFormatPictureSet(format)
  137. if err != nil {
  138. return err
  139. }
  140. img, _, err := image.DecodeConfig(bytes.NewReader(file))
  141. if err != nil {
  142. return err
  143. }
  144. // Read sheet data.
  145. xlsx, err := f.workSheetReader(sheet)
  146. if err != nil {
  147. return err
  148. }
  149. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  150. drawingID := f.countDrawings() + 1
  151. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  152. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  153. mediaStr := ".." + strings.TrimPrefix(f.addMedia(file, ext), "xl")
  154. drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipImage, mediaStr, hyperlinkType)
  155. // Add picture with hyperlink.
  156. if formatSet.Hyperlink != "" && formatSet.HyperlinkType != "" {
  157. if formatSet.HyperlinkType == "External" {
  158. hyperlinkType = formatSet.HyperlinkType
  159. }
  160. drawingHyperlinkRID = f.addDrawingRelationships(drawingID, SourceRelationshipHyperLink, formatSet.Hyperlink, hyperlinkType)
  161. }
  162. err = f.addDrawingPicture(sheet, drawingXML, cell, name, img.Width, img.Height, drawingRID, drawingHyperlinkRID, formatSet)
  163. if err != nil {
  164. return err
  165. }
  166. f.addContentTypePart(drawingID, "drawings")
  167. return err
  168. }
  169. // addSheetRelationships provides a function to add
  170. // xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name, relationship
  171. // type and target.
  172. func (f *File) addSheetRelationships(sheet, relType, target, targetMode string) int {
  173. name, ok := f.sheetMap[trimSheetName(sheet)]
  174. if !ok {
  175. name = strings.ToLower(sheet) + ".xml"
  176. }
  177. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  178. sheetRels := f.workSheetRelsReader(rels)
  179. if sheetRels == nil {
  180. sheetRels = &xlsxWorkbookRels{}
  181. }
  182. var rID = 1
  183. var ID bytes.Buffer
  184. ID.WriteString("rId")
  185. ID.WriteString(strconv.Itoa(rID))
  186. ID.Reset()
  187. rID = len(sheetRels.Relationships) + 1
  188. ID.WriteString("rId")
  189. ID.WriteString(strconv.Itoa(rID))
  190. sheetRels.Relationships = append(sheetRels.Relationships, xlsxWorkbookRelation{
  191. ID: ID.String(),
  192. Type: relType,
  193. Target: target,
  194. TargetMode: targetMode,
  195. })
  196. f.WorkSheetRels[rels] = sheetRels
  197. return rID
  198. }
  199. // deleteSheetRelationships provides a function to delete relationships in
  200. // xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
  201. // relationship index.
  202. func (f *File) deleteSheetRelationships(sheet, rID string) {
  203. name, ok := f.sheetMap[trimSheetName(sheet)]
  204. if !ok {
  205. name = strings.ToLower(sheet) + ".xml"
  206. }
  207. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  208. sheetRels := f.workSheetRelsReader(rels)
  209. if sheetRels == nil {
  210. sheetRels = &xlsxWorkbookRels{}
  211. }
  212. for k, v := range sheetRels.Relationships {
  213. if v.ID == rID {
  214. sheetRels.Relationships = append(sheetRels.Relationships[:k], sheetRels.Relationships[k+1:]...)
  215. }
  216. }
  217. f.WorkSheetRels[rels] = sheetRels
  218. }
  219. // addSheetLegacyDrawing provides a function to add legacy drawing element to
  220. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  221. func (f *File) addSheetLegacyDrawing(sheet string, rID int) {
  222. xlsx, _ := f.workSheetReader(sheet)
  223. xlsx.LegacyDrawing = &xlsxLegacyDrawing{
  224. RID: "rId" + strconv.Itoa(rID),
  225. }
  226. }
  227. // addSheetDrawing provides a function to add drawing element to
  228. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  229. func (f *File) addSheetDrawing(sheet string, rID int) {
  230. xlsx, _ := f.workSheetReader(sheet)
  231. xlsx.Drawing = &xlsxDrawing{
  232. RID: "rId" + strconv.Itoa(rID),
  233. }
  234. }
  235. // addSheetPicture provides a function to add picture element to
  236. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  237. func (f *File) addSheetPicture(sheet string, rID int) {
  238. xlsx, _ := f.workSheetReader(sheet)
  239. xlsx.Picture = &xlsxPicture{
  240. RID: "rId" + strconv.Itoa(rID),
  241. }
  242. }
  243. // countDrawings provides a function to get drawing files count storage in the
  244. // folder xl/drawings.
  245. func (f *File) countDrawings() int {
  246. c1, c2 := 0, 0
  247. for k := range f.XLSX {
  248. if strings.Contains(k, "xl/drawings/drawing") {
  249. c1++
  250. }
  251. }
  252. for rel := range f.Drawings {
  253. if strings.Contains(rel, "xl/drawings/drawing") {
  254. c2++
  255. }
  256. }
  257. if c1 < c2 {
  258. return c2
  259. }
  260. return c1
  261. }
  262. // addDrawingPicture provides a function to add picture by given sheet,
  263. // drawingXML, cell, file name, width, height relationship index and format
  264. // sets.
  265. func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, height, rID, hyperlinkRID int, formatSet *formatPicture) error {
  266. col, row, err := CellNameToCoordinates(cell)
  267. if err != nil {
  268. return err
  269. }
  270. width = int(float64(width) * formatSet.XScale)
  271. height = int(float64(height) * formatSet.YScale)
  272. col--
  273. row--
  274. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  275. f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
  276. content, cNvPrID := f.drawingParser(drawingXML)
  277. twoCellAnchor := xdrCellAnchor{}
  278. twoCellAnchor.EditAs = formatSet.Positioning
  279. from := xlsxFrom{}
  280. from.Col = colStart
  281. from.ColOff = formatSet.OffsetX * EMU
  282. from.Row = rowStart
  283. from.RowOff = formatSet.OffsetY * EMU
  284. to := xlsxTo{}
  285. to.Col = colEnd
  286. to.ColOff = x2 * EMU
  287. to.Row = rowEnd
  288. to.RowOff = y2 * EMU
  289. twoCellAnchor.From = &from
  290. twoCellAnchor.To = &to
  291. pic := xlsxPic{}
  292. pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
  293. pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1
  294. pic.NvPicPr.CNvPr.Descr = file
  295. pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
  296. if hyperlinkRID != 0 {
  297. pic.NvPicPr.CNvPr.HlinkClick = &xlsxHlinkClick{
  298. R: SourceRelationship,
  299. RID: "rId" + strconv.Itoa(hyperlinkRID),
  300. }
  301. }
  302. pic.BlipFill.Blip.R = SourceRelationship
  303. pic.BlipFill.Blip.Embed = "rId" + strconv.Itoa(rID)
  304. pic.SpPr.PrstGeom.Prst = "rect"
  305. twoCellAnchor.Pic = &pic
  306. twoCellAnchor.ClientData = &xdrClientData{
  307. FLocksWithSheet: formatSet.FLocksWithSheet,
  308. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  309. }
  310. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  311. f.Drawings[drawingXML] = content
  312. return err
  313. }
  314. // addDrawingRelationships provides a function to add image part relationships
  315. // in the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
  316. // relationship type and target.
  317. func (f *File) addDrawingRelationships(index int, relType, target, targetMode string) int {
  318. var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
  319. var rID = 1
  320. var ID bytes.Buffer
  321. ID.WriteString("rId")
  322. ID.WriteString(strconv.Itoa(rID))
  323. drawingRels := f.drawingRelsReader(rels)
  324. if drawingRels == nil {
  325. drawingRels = &xlsxWorkbookRels{}
  326. }
  327. ID.Reset()
  328. rID = len(drawingRels.Relationships) + 1
  329. ID.WriteString("rId")
  330. ID.WriteString(strconv.Itoa(rID))
  331. drawingRels.Relationships = append(drawingRels.Relationships, xlsxWorkbookRelation{
  332. ID: ID.String(),
  333. Type: relType,
  334. Target: target,
  335. TargetMode: targetMode,
  336. })
  337. f.DrawingRels[rels] = drawingRels
  338. return rID
  339. }
  340. // countMedia provides a function to get media files count storage in the
  341. // folder xl/media/image.
  342. func (f *File) countMedia() int {
  343. count := 0
  344. for k := range f.XLSX {
  345. if strings.Contains(k, "xl/media/image") {
  346. count++
  347. }
  348. }
  349. return count
  350. }
  351. // addMedia provides a function to add a picture into folder xl/media/image by
  352. // given file and extension name. Duplicate images are only actually stored once
  353. // and drawings that use it will reference the same image.
  354. func (f *File) addMedia(file []byte, ext string) string {
  355. count := f.countMedia()
  356. for name, existing := range f.XLSX {
  357. if !strings.HasPrefix(name, "xl/media/image") {
  358. continue
  359. }
  360. if bytes.Equal(file, existing) {
  361. return name
  362. }
  363. }
  364. media := "xl/media/image" + strconv.Itoa(count+1) + ext
  365. f.XLSX[media] = file
  366. return media
  367. }
  368. // setContentTypePartImageExtensions provides a function to set the content
  369. // type for relationship parts and the Main Document part.
  370. func (f *File) setContentTypePartImageExtensions() {
  371. var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
  372. content := f.contentTypesReader()
  373. for _, v := range content.Defaults {
  374. _, ok := imageTypes[v.Extension]
  375. if ok {
  376. imageTypes[v.Extension] = true
  377. }
  378. }
  379. for k, v := range imageTypes {
  380. if !v {
  381. content.Defaults = append(content.Defaults, xlsxDefault{
  382. Extension: k,
  383. ContentType: "image/" + k,
  384. })
  385. }
  386. }
  387. }
  388. // setContentTypePartVMLExtensions provides a function to set the content type
  389. // for relationship parts and the Main Document part.
  390. func (f *File) setContentTypePartVMLExtensions() {
  391. vml := false
  392. content := f.contentTypesReader()
  393. for _, v := range content.Defaults {
  394. if v.Extension == "vml" {
  395. vml = true
  396. }
  397. }
  398. if !vml {
  399. content.Defaults = append(content.Defaults, xlsxDefault{
  400. Extension: "vml",
  401. ContentType: "application/vnd.openxmlformats-officedocument.vmlDrawing",
  402. })
  403. }
  404. }
  405. // addContentTypePart provides a function to add content type part
  406. // relationships in the file [Content_Types].xml by given index.
  407. func (f *File) addContentTypePart(index int, contentType string) {
  408. setContentType := map[string]func(){
  409. "comments": f.setContentTypePartVMLExtensions,
  410. "drawings": f.setContentTypePartImageExtensions,
  411. }
  412. partNames := map[string]string{
  413. "chart": "/xl/charts/chart" + strconv.Itoa(index) + ".xml",
  414. "comments": "/xl/comments" + strconv.Itoa(index) + ".xml",
  415. "drawings": "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml",
  416. "table": "/xl/tables/table" + strconv.Itoa(index) + ".xml",
  417. }
  418. contentTypes := map[string]string{
  419. "chart": "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
  420. "comments": "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  421. "drawings": "application/vnd.openxmlformats-officedocument.drawing+xml",
  422. "table": "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
  423. }
  424. s, ok := setContentType[contentType]
  425. if ok {
  426. s()
  427. }
  428. content := f.contentTypesReader()
  429. for _, v := range content.Overrides {
  430. if v.PartName == partNames[contentType] {
  431. return
  432. }
  433. }
  434. content.Overrides = append(content.Overrides, xlsxOverride{
  435. PartName: partNames[contentType],
  436. ContentType: contentTypes[contentType],
  437. })
  438. }
  439. // getSheetRelationshipsTargetByID provides a function to get Target attribute
  440. // value in xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
  441. // relationship index.
  442. func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
  443. name, ok := f.sheetMap[trimSheetName(sheet)]
  444. if !ok {
  445. name = strings.ToLower(sheet) + ".xml"
  446. }
  447. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  448. sheetRels := f.workSheetRelsReader(rels)
  449. if sheetRels == nil {
  450. sheetRels = &xlsxWorkbookRels{}
  451. }
  452. for _, v := range sheetRels.Relationships {
  453. if v.ID == rID {
  454. return v.Target
  455. }
  456. }
  457. return ""
  458. }
  459. // GetPicture provides a function to get picture base name and raw content
  460. // embed in XLSX by given worksheet and cell name. This function returns the
  461. // file name in XLSX and file contents as []byte data types. For example:
  462. //
  463. // f, err := excelize.OpenFile("./Book1.xlsx")
  464. // if err != nil {
  465. // fmt.Println(err)
  466. // return
  467. // }
  468. // file, raw, err := f.GetPicture("Sheet1", "A2")
  469. // if err != nil {
  470. // fmt.Println(err)
  471. // return
  472. // }
  473. // err = ioutil.WriteFile(file, raw, 0644)
  474. // if err != nil {
  475. // fmt.Println(err)
  476. // }
  477. //
  478. func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
  479. col, row, err := CellNameToCoordinates(cell)
  480. if err != nil {
  481. return "", nil, err
  482. }
  483. col--
  484. row--
  485. xlsx, err := f.workSheetReader(sheet)
  486. if err != nil {
  487. return "", nil, err
  488. }
  489. if xlsx.Drawing == nil {
  490. return "", nil, err
  491. }
  492. target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  493. drawingXML := strings.Replace(target, "..", "xl", -1)
  494. _, ok := f.XLSX[drawingXML]
  495. if !ok {
  496. return "", nil, err
  497. }
  498. drawingRelationships := strings.Replace(
  499. strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1)
  500. return f.getPicture(row, col, drawingXML, drawingRelationships)
  501. }
  502. // getPicture provides a function to get picture base name and raw content
  503. // embed in XLSX by given coordinates and drawing relationships.
  504. func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) (string, []byte, error) {
  505. wsDr, _ := f.drawingParser(drawingXML)
  506. for _, anchor := range wsDr.TwoCellAnchor {
  507. if anchor.From != nil && anchor.Pic != nil {
  508. if anchor.From.Col == col && anchor.From.Row == row {
  509. xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships,
  510. anchor.Pic.BlipFill.Blip.Embed)
  511. _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
  512. if ok {
  513. return filepath.Base(xlsxWorkbookRelation.Target),
  514. []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target,
  515. "..", "xl", -1)]), nil
  516. }
  517. }
  518. }
  519. }
  520. decodeWsDr := decodeWsDr{}
  521. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr)
  522. for _, anchor := range decodeWsDr.TwoCellAnchor {
  523. decodeTwoCellAnchor := decodeTwoCellAnchor{}
  524. _ = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+anchor.Content+"</decodeTwoCellAnchor>"), &decodeTwoCellAnchor)
  525. if decodeTwoCellAnchor.From != nil && decodeTwoCellAnchor.Pic != nil {
  526. if decodeTwoCellAnchor.From.Col == col && decodeTwoCellAnchor.From.Row == row {
  527. xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed)
  528. _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
  529. if ok {
  530. return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), nil
  531. }
  532. }
  533. }
  534. }
  535. return "", nil, nil
  536. }
  537. // getDrawingRelationships provides a function to get drawing relationships
  538. // from xl/drawings/_rels/drawing%s.xml.rels by given file name and
  539. // relationship ID.
  540. func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation {
  541. if drawingRels := f.drawingRelsReader(rels); drawingRels != nil {
  542. for _, v := range drawingRels.Relationships {
  543. if v.ID == rID {
  544. return &v
  545. }
  546. }
  547. }
  548. return nil
  549. }
  550. // drawingRelsReader provides a function to get the pointer to the structure
  551. // after deserialization of xl/drawings/_rels/drawing%d.xml.rels.
  552. func (f *File) drawingRelsReader(rel string) *xlsxWorkbookRels {
  553. if f.DrawingRels[rel] == nil {
  554. _, ok := f.XLSX[rel]
  555. if ok {
  556. d := xlsxWorkbookRels{}
  557. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rel)), &d)
  558. f.DrawingRels[rel] = &d
  559. }
  560. }
  561. return f.DrawingRels[rel]
  562. }
  563. // drawingRelsWriter provides a function to save
  564. // xl/drawings/_rels/drawing%d.xml.rels after serialize structure.
  565. func (f *File) drawingRelsWriter() {
  566. for path, d := range f.DrawingRels {
  567. if d != nil {
  568. v, _ := xml.Marshal(d)
  569. f.saveFileList(path, v)
  570. }
  571. }
  572. }
  573. // drawingsWriter provides a function to save xl/drawings/drawing%d.xml after
  574. // serialize structure.
  575. func (f *File) drawingsWriter() {
  576. for path, d := range f.Drawings {
  577. if d != nil {
  578. v, _ := xml.Marshal(d)
  579. f.saveFileList(path, v)
  580. }
  581. }
  582. }