picture.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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.8 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"
  51. // )
  52. //
  53. // func main() {
  54. // xlsx := excelize.NewFile()
  55. // // Insert a picture.
  56. // err := xlsx.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 = xlsx.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 = xlsx.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 = xlsx.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"
  110. // )
  111. //
  112. // func main() {
  113. // xlsx := excelize.NewFile()
  114. //
  115. // file, err := ioutil.ReadFile("./image1.jpg")
  116. // if err != nil {
  117. // fmt.Println(err)
  118. // }
  119. // err = xlsx.AddPictureFromBytes("Sheet1", "A2", "", "Excel Logo", ".jpg", file)
  120. // if err != nil {
  121. // fmt.Println(err)
  122. // }
  123. // err = xlsx.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 err error
  131. var drawingHyperlinkRID int
  132. var hyperlinkType string
  133. ext, ok := supportImageTypes[extension]
  134. if !ok {
  135. return errors.New("unsupported image extension")
  136. }
  137. formatSet, err := parseFormatPictureSet(format)
  138. if err != nil {
  139. return err
  140. }
  141. img, _, err := image.DecodeConfig(bytes.NewReader(file))
  142. if err != nil {
  143. return err
  144. }
  145. // Read sheet data.
  146. xlsx := f.workSheetReader(sheet)
  147. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  148. drawingID := f.countDrawings() + 1
  149. pictureID := f.countMedia() + 1
  150. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  151. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  152. drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext, hyperlinkType)
  153. // Add picture with hyperlink.
  154. if formatSet.Hyperlink != "" && formatSet.HyperlinkType != "" {
  155. if formatSet.HyperlinkType == "External" {
  156. hyperlinkType = formatSet.HyperlinkType
  157. }
  158. drawingHyperlinkRID = f.addDrawingRelationships(drawingID, SourceRelationshipHyperLink, formatSet.Hyperlink, hyperlinkType)
  159. }
  160. err = f.addDrawingPicture(sheet, drawingXML, cell, name, img.Width, img.Height, drawingRID, drawingHyperlinkRID, formatSet)
  161. if err != nil {
  162. return err
  163. }
  164. f.addMedia(file, ext)
  165. f.addContentTypePart(drawingID, "drawings")
  166. return err
  167. }
  168. // addSheetRelationships provides a function to add
  169. // xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name, relationship
  170. // type and target.
  171. func (f *File) addSheetRelationships(sheet, relType, target, targetMode string) int {
  172. name, ok := f.sheetMap[trimSheetName(sheet)]
  173. if !ok {
  174. name = strings.ToLower(sheet) + ".xml"
  175. }
  176. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  177. sheetRels := f.workSheetRelsReader(rels)
  178. if sheetRels == nil {
  179. sheetRels = &xlsxWorkbookRels{}
  180. }
  181. var rID = 1
  182. var ID bytes.Buffer
  183. ID.WriteString("rId")
  184. ID.WriteString(strconv.Itoa(rID))
  185. ID.Reset()
  186. rID = len(sheetRels.Relationships) + 1
  187. ID.WriteString("rId")
  188. ID.WriteString(strconv.Itoa(rID))
  189. sheetRels.Relationships = append(sheetRels.Relationships, xlsxWorkbookRelation{
  190. ID: ID.String(),
  191. Type: relType,
  192. Target: target,
  193. TargetMode: targetMode,
  194. })
  195. f.WorkSheetRels[rels] = sheetRels
  196. return rID
  197. }
  198. // deleteSheetRelationships provides a function to delete relationships in
  199. // xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
  200. // relationship index.
  201. func (f *File) deleteSheetRelationships(sheet, rID string) {
  202. name, ok := f.sheetMap[trimSheetName(sheet)]
  203. if !ok {
  204. name = strings.ToLower(sheet) + ".xml"
  205. }
  206. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  207. sheetRels := f.workSheetRelsReader(rels)
  208. if sheetRels == nil {
  209. sheetRels = &xlsxWorkbookRels{}
  210. }
  211. for k, v := range sheetRels.Relationships {
  212. if v.ID == rID {
  213. sheetRels.Relationships = append(sheetRels.Relationships[:k], sheetRels.Relationships[k+1:]...)
  214. }
  215. }
  216. f.WorkSheetRels[rels] = sheetRels
  217. }
  218. // addSheetLegacyDrawing provides a function to add legacy drawing element to
  219. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  220. func (f *File) addSheetLegacyDrawing(sheet string, rID int) {
  221. xlsx := f.workSheetReader(sheet)
  222. xlsx.LegacyDrawing = &xlsxLegacyDrawing{
  223. RID: "rId" + strconv.Itoa(rID),
  224. }
  225. }
  226. // addSheetDrawing provides a function to add drawing element to
  227. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  228. func (f *File) addSheetDrawing(sheet string, rID int) {
  229. xlsx := f.workSheetReader(sheet)
  230. xlsx.Drawing = &xlsxDrawing{
  231. RID: "rId" + strconv.Itoa(rID),
  232. }
  233. }
  234. // addSheetPicture provides a function to add picture element to
  235. // xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
  236. func (f *File) addSheetPicture(sheet string, rID int) {
  237. xlsx := f.workSheetReader(sheet)
  238. xlsx.Picture = &xlsxPicture{
  239. RID: "rId" + strconv.Itoa(rID),
  240. }
  241. }
  242. // countDrawings provides a function to get drawing files count storage in the
  243. // folder xl/drawings.
  244. func (f *File) countDrawings() int {
  245. c1, c2 := 0, 0
  246. for k := range f.XLSX {
  247. if strings.Contains(k, "xl/drawings/drawing") {
  248. c1++
  249. }
  250. }
  251. for rel := range f.Drawings {
  252. if strings.Contains(rel, "xl/drawings/drawing") {
  253. c2++
  254. }
  255. }
  256. if c1 < c2 {
  257. return c2
  258. }
  259. return c1
  260. }
  261. // addDrawingPicture provides a function to add picture by given sheet,
  262. // drawingXML, cell, file name, width, height relationship index and format
  263. // sets.
  264. func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, height, rID, hyperlinkRID int, formatSet *formatPicture) error {
  265. col, row, err := CellNameToCoordinates(cell)
  266. if err != nil {
  267. return err
  268. }
  269. width = int(float64(width) * formatSet.XScale)
  270. height = int(float64(height) * formatSet.YScale)
  271. col--
  272. row--
  273. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  274. f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
  275. content, cNvPrID := f.drawingParser(drawingXML)
  276. twoCellAnchor := xdrCellAnchor{}
  277. twoCellAnchor.EditAs = formatSet.Positioning
  278. from := xlsxFrom{}
  279. from.Col = colStart
  280. from.ColOff = formatSet.OffsetX * EMU
  281. from.Row = rowStart
  282. from.RowOff = formatSet.OffsetY * EMU
  283. to := xlsxTo{}
  284. to.Col = colEnd
  285. to.ColOff = x2 * EMU
  286. to.Row = rowEnd
  287. to.RowOff = y2 * EMU
  288. twoCellAnchor.From = &from
  289. twoCellAnchor.To = &to
  290. pic := xlsxPic{}
  291. pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
  292. pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1
  293. pic.NvPicPr.CNvPr.Descr = file
  294. pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
  295. if hyperlinkRID != 0 {
  296. pic.NvPicPr.CNvPr.HlinkClick = &xlsxHlinkClick{
  297. R: SourceRelationship,
  298. RID: "rId" + strconv.Itoa(hyperlinkRID),
  299. }
  300. }
  301. pic.BlipFill.Blip.R = SourceRelationship
  302. pic.BlipFill.Blip.Embed = "rId" + strconv.Itoa(rID)
  303. pic.SpPr.PrstGeom.Prst = "rect"
  304. twoCellAnchor.Pic = &pic
  305. twoCellAnchor.ClientData = &xdrClientData{
  306. FLocksWithSheet: formatSet.FLocksWithSheet,
  307. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  308. }
  309. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  310. f.Drawings[drawingXML] = content
  311. return err
  312. }
  313. // addDrawingRelationships provides a function to add image part relationships
  314. // in the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
  315. // relationship type and target.
  316. func (f *File) addDrawingRelationships(index int, relType, target, targetMode string) int {
  317. var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
  318. var rID = 1
  319. var ID bytes.Buffer
  320. ID.WriteString("rId")
  321. ID.WriteString(strconv.Itoa(rID))
  322. drawingRels := f.drawingRelsReader(rels)
  323. if drawingRels == nil {
  324. drawingRels = &xlsxWorkbookRels{}
  325. }
  326. ID.Reset()
  327. rID = len(drawingRels.Relationships) + 1
  328. ID.WriteString("rId")
  329. ID.WriteString(strconv.Itoa(rID))
  330. drawingRels.Relationships = append(drawingRels.Relationships, xlsxWorkbookRelation{
  331. ID: ID.String(),
  332. Type: relType,
  333. Target: target,
  334. TargetMode: targetMode,
  335. })
  336. f.DrawingRels[rels] = drawingRels
  337. return rID
  338. }
  339. // countMedia provides a function to get media files count storage in the
  340. // folder xl/media/image.
  341. func (f *File) countMedia() int {
  342. count := 0
  343. for k := range f.XLSX {
  344. if strings.Contains(k, "xl/media/image") {
  345. count++
  346. }
  347. }
  348. return count
  349. }
  350. // addMedia provides a function to add picture into folder xl/media/image by
  351. // given file and extension name.
  352. func (f *File) addMedia(file []byte, ext string) {
  353. count := f.countMedia()
  354. media := "xl/media/image" + strconv.Itoa(count+1) + ext
  355. f.XLSX[media] = file
  356. }
  357. // setContentTypePartImageExtensions provides a function to set the content
  358. // type for relationship parts and the Main Document part.
  359. func (f *File) setContentTypePartImageExtensions() {
  360. var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
  361. content := f.contentTypesReader()
  362. for _, v := range content.Defaults {
  363. _, ok := imageTypes[v.Extension]
  364. if ok {
  365. imageTypes[v.Extension] = true
  366. }
  367. }
  368. for k, v := range imageTypes {
  369. if !v {
  370. content.Defaults = append(content.Defaults, xlsxDefault{
  371. Extension: k,
  372. ContentType: "image/" + k,
  373. })
  374. }
  375. }
  376. }
  377. // setContentTypePartVMLExtensions provides a function to set the content type
  378. // for relationship parts and the Main Document part.
  379. func (f *File) setContentTypePartVMLExtensions() {
  380. vml := false
  381. content := f.contentTypesReader()
  382. for _, v := range content.Defaults {
  383. if v.Extension == "vml" {
  384. vml = true
  385. }
  386. }
  387. if !vml {
  388. content.Defaults = append(content.Defaults, xlsxDefault{
  389. Extension: "vml",
  390. ContentType: "application/vnd.openxmlformats-officedocument.vmlDrawing",
  391. })
  392. }
  393. }
  394. // addContentTypePart provides a function to add content type part
  395. // relationships in the file [Content_Types].xml by given index.
  396. func (f *File) addContentTypePart(index int, contentType string) {
  397. setContentType := map[string]func(){
  398. "comments": f.setContentTypePartVMLExtensions,
  399. "drawings": f.setContentTypePartImageExtensions,
  400. }
  401. partNames := map[string]string{
  402. "chart": "/xl/charts/chart" + strconv.Itoa(index) + ".xml",
  403. "comments": "/xl/comments" + strconv.Itoa(index) + ".xml",
  404. "drawings": "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml",
  405. "table": "/xl/tables/table" + strconv.Itoa(index) + ".xml",
  406. }
  407. contentTypes := map[string]string{
  408. "chart": "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
  409. "comments": "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
  410. "drawings": "application/vnd.openxmlformats-officedocument.drawing+xml",
  411. "table": "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
  412. }
  413. s, ok := setContentType[contentType]
  414. if ok {
  415. s()
  416. }
  417. content := f.contentTypesReader()
  418. for _, v := range content.Overrides {
  419. if v.PartName == partNames[contentType] {
  420. return
  421. }
  422. }
  423. content.Overrides = append(content.Overrides, xlsxOverride{
  424. PartName: partNames[contentType],
  425. ContentType: contentTypes[contentType],
  426. })
  427. }
  428. // getSheetRelationshipsTargetByID provides a function to get Target attribute
  429. // value in xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
  430. // relationship index.
  431. func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
  432. name, ok := f.sheetMap[trimSheetName(sheet)]
  433. if !ok {
  434. name = strings.ToLower(sheet) + ".xml"
  435. }
  436. var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
  437. sheetRels := f.workSheetRelsReader(rels)
  438. if sheetRels == nil {
  439. sheetRels = &xlsxWorkbookRels{}
  440. }
  441. for _, v := range sheetRels.Relationships {
  442. if v.ID == rID {
  443. return v.Target
  444. }
  445. }
  446. return ""
  447. }
  448. // GetPicture provides a function to get picture base name and raw content
  449. // embed in XLSX by given worksheet and cell name. This function returns the
  450. // file name in XLSX and file contents as []byte data types. For example:
  451. //
  452. // xlsx, err := excelize.OpenFile("./Book1.xlsx")
  453. // if err != nil {
  454. // fmt.Println(err)
  455. // return
  456. // }
  457. // file, raw, err := xlsx.GetPicture("Sheet1", "A2")
  458. // if file == "" {
  459. // return
  460. // }
  461. // err := ioutil.WriteFile(file, raw, 0644)
  462. // if err != nil {
  463. // fmt.Println(err)
  464. // }
  465. //
  466. func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
  467. col, row, err := CellNameToCoordinates(cell)
  468. if err != nil {
  469. return "", []byte{}, err
  470. }
  471. col--
  472. row--
  473. xlsx := f.workSheetReader(sheet)
  474. if xlsx.Drawing == nil {
  475. return "", []byte{}, err
  476. }
  477. target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  478. drawingXML := strings.Replace(target, "..", "xl", -1)
  479. drawingRelationships := strings.Replace(
  480. strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1)
  481. wsDr, _ := f.drawingParser(drawingXML)
  482. for _, anchor := range wsDr.TwoCellAnchor {
  483. if anchor.From != nil && anchor.Pic != nil {
  484. if anchor.From.Col == col && anchor.From.Row == row {
  485. xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships,
  486. anchor.Pic.BlipFill.Blip.Embed)
  487. _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
  488. if ok {
  489. return filepath.Base(xlsxWorkbookRelation.Target),
  490. []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target,
  491. "..", "xl", -1)]), err
  492. }
  493. }
  494. }
  495. }
  496. _, ok := f.XLSX[drawingXML]
  497. if !ok {
  498. return "", nil, err
  499. }
  500. decodeWsDr := decodeWsDr{}
  501. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr)
  502. for _, anchor := range decodeWsDr.TwoCellAnchor {
  503. decodeTwoCellAnchor := decodeTwoCellAnchor{}
  504. _ = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+anchor.Content+"</decodeTwoCellAnchor>"), &decodeTwoCellAnchor)
  505. if decodeTwoCellAnchor.From != nil && decodeTwoCellAnchor.Pic != nil {
  506. if decodeTwoCellAnchor.From.Col == col && decodeTwoCellAnchor.From.Row == row {
  507. xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed)
  508. _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
  509. if ok {
  510. return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), err
  511. }
  512. }
  513. }
  514. }
  515. return "", []byte{}, err
  516. }
  517. // getDrawingRelationships provides a function to get drawing relationships
  518. // from xl/drawings/_rels/drawing%s.xml.rels by given file name and
  519. // relationship ID.
  520. func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation {
  521. if drawingRels := f.drawingRelsReader(rels); drawingRels != nil {
  522. for _, v := range drawingRels.Relationships {
  523. if v.ID == rID {
  524. return &v
  525. }
  526. }
  527. }
  528. return nil
  529. }
  530. // drawingRelsReader provides a function to get the pointer to the structure
  531. // after deserialization of xl/drawings/_rels/drawing%d.xml.rels.
  532. func (f *File) drawingRelsReader(rel string) *xlsxWorkbookRels {
  533. if f.DrawingRels[rel] == nil {
  534. _, ok := f.XLSX[rel]
  535. if ok {
  536. d := xlsxWorkbookRels{}
  537. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rel)), &d)
  538. f.DrawingRels[rel] = &d
  539. }
  540. }
  541. return f.DrawingRels[rel]
  542. }
  543. // drawingRelsWriter provides a function to save
  544. // xl/drawings/_rels/drawing%d.xml.rels after serialize structure.
  545. func (f *File) drawingRelsWriter() {
  546. for path, d := range f.DrawingRels {
  547. if d != nil {
  548. v, _ := xml.Marshal(d)
  549. f.saveFileList(path, v)
  550. }
  551. }
  552. }
  553. // drawingsWriter provides a function to save xl/drawings/drawing%d.xml after
  554. // serialize structure.
  555. func (f *File) drawingsWriter() {
  556. for path, d := range f.Drawings {
  557. if d != nil {
  558. v, _ := xml.Marshal(d)
  559. f.saveFileList(path, v)
  560. }
  561. }
  562. }