picture.go 19 KB

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