picture.go 19 KB

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