picture.go 19 KB

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