shape.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "strconv"
  6. "strings"
  7. )
  8. // parseFormatShapeSet provides function to parse the format settings of the
  9. // shape with default value.
  10. func parseFormatShapeSet(formatSet string) *formatShape {
  11. format := formatShape{
  12. Width: 160,
  13. Height: 160,
  14. Format: formatPicture{
  15. FPrintsWithSheet: true,
  16. FLocksWithSheet: false,
  17. NoChangeAspect: false,
  18. OffsetX: 0,
  19. OffsetY: 0,
  20. XScale: 1.0,
  21. YScale: 1.0,
  22. },
  23. Text: " ",
  24. }
  25. json.Unmarshal([]byte(formatSet), &format)
  26. return &format
  27. }
  28. // AddShape provides the method to add shape in a sheet by given worksheet
  29. // index, shape format set (such as offset, scale, aspect ratio setting and
  30. // print settings) and properties set. For example, add text box (rect shape) in
  31. // Sheet1:
  32. //
  33. // xlsx.AddShape("Sheet1", "G6", `{"type":"rect", "text":"Rectangle Shape", "color":{"line":"#4286F4","fill":"#8eb9ff"}, "width": 180, "height": 90}`)
  34. //
  35. // The following shows the type of chart supported by excelize:
  36. //
  37. // accentBorderCallout1 (Callout 1 with Border and Accent Shape)
  38. // accentBorderCallout2 (Callout 2 with Border and Accent Shape)
  39. // accentBorderCallout3 (Callout 3 with Border and Accent Shape)
  40. // accentCallout1 (Callout 1 Shape)
  41. // accentCallout2 (Callout 2 Shape)
  42. // accentCallout3 (Callout 3 Shape)
  43. // actionButtonBackPrevious (Back or Previous Button Shape)
  44. // actionButtonBeginning (Beginning Button Shape)
  45. // actionButtonBlank (Blank Button Shape)
  46. // actionButtonDocument (Document Button Shape)
  47. // actionButtonEnd (End Button Shape)
  48. // actionButtonForwardNext (Forward or Next Button Shape)
  49. // actionButtonHelp (Help Button Shape)
  50. // actionButtonHome (Home Button Shape)
  51. // actionButtonInformation (Information Button Shape)
  52. // actionButtonMovie (Movie Button Shape)
  53. // actionButtonReturn (Return Button Shape)
  54. // actionButtonSound (Sound Button Shape)
  55. // arc (Curved Arc Shape)
  56. // bentArrow (Bent Arrow Shape)
  57. // bentConnector2 (Bent Connector 2 Shape)
  58. // bentConnector3 (Bent Connector 3 Shape)
  59. // bentConnector4 (Bent Connector 4 Shape)
  60. // bentConnector5 (Bent Connector 5 Shape)
  61. // bentUpArrow (Bent Up Arrow Shape)
  62. // bevel (Bevel Shape)
  63. // blockArc (Block Arc Shape)
  64. // borderCallout1 (Callout 1 with Border Shape)
  65. // borderCallout2 (Callout 2 with Border Shape)
  66. // borderCallout3 (Callout 3 with Border Shape)
  67. // bracePair (Brace Pair Shape)
  68. // bracketPair (Bracket Pair Shape)
  69. // callout1 (Callout 1 Shape)
  70. // callout2 (Callout 2 Shape)
  71. // callout3 (Callout 3 Shape)
  72. // can (Can Shape)
  73. // chartPlus (Chart Plus Shape)
  74. // chartStar (Chart Star Shape)
  75. // chartX (Chart X Shape)
  76. // chevron (Chevron Shape)
  77. // chord (Chord Shape)
  78. // circularArrow (Circular Arrow Shape)
  79. // cloud (Cloud Shape)
  80. // cloudCallout (Callout Cloud Shape)
  81. // corner (Corner Shape)
  82. // cornerTabs (Corner Tabs Shape)
  83. // cube (Cube Shape)
  84. // curvedConnector2 (Curved Connector 2 Shape)
  85. // curvedConnector3 (Curved Connector 3 Shape)
  86. // curvedConnector4 (Curved Connector 4 Shape)
  87. // curvedConnector5 (Curved Connector 5 Shape)
  88. // curvedDownArrow (Curved Down Arrow Shape)
  89. // curvedLeftArrow (Curved Left Arrow Shape)
  90. // curvedRightArrow (Curved Right Arrow Shape)
  91. // curvedUpArrow (Curved Up Arrow Shape)
  92. // decagon (Decagon Shape)
  93. // diagStripe (Diagonal Stripe Shape)
  94. // diamond (Diamond Shape)
  95. // dodecagon (Dodecagon Shape)
  96. // donut (Donut Shape)
  97. // doubleWave (Double Wave Shape)
  98. // downArrow (Down Arrow Shape)
  99. // downArrowCallout (Callout Down Arrow Shape)
  100. // ellipse (Ellipse Shape)
  101. // ellipseRibbon (Ellipse Ribbon Shape)
  102. // ellipseRibbon2 (Ellipse Ribbon 2 Shape)
  103. // flowChartAlternateProcess (Alternate Process Flow Shape)
  104. // flowChartCollate (Collate Flow Shape)
  105. // flowChartConnector (Connector Flow Shape)
  106. // flowChartDecision (Decision Flow Shape)
  107. // flowChartDelay (Delay Flow Shape)
  108. // flowChartDisplay (Display Flow Shape)
  109. // flowChartDocument (Document Flow Shape)
  110. // flowChartExtract (Extract Flow Shape)
  111. // flowChartInputOutput (Input Output Flow Shape)
  112. // flowChartInternalStorage (Internal Storage Flow Shape)
  113. // flowChartMagneticDisk (Magnetic Disk Flow Shape)
  114. // flowChartMagneticDrum (Magnetic Drum Flow Shape)
  115. // flowChartMagneticTape (Magnetic Tape Flow Shape)
  116. // flowChartManualInput (Manual Input Flow Shape)
  117. // flowChartManualOperation (Manual Operation Flow Shape)
  118. // flowChartMerge (Merge Flow Shape)
  119. // flowChartMultidocument (Multi-Document Flow Shape)
  120. // flowChartOfflineStorage (Offline Storage Flow Shape)
  121. // flowChartOffpageConnector (Off-Page Connector Flow Shape)
  122. // flowChartOnlineStorage (Online Storage Flow Shape)
  123. // flowChartOr (Or Flow Shape)
  124. // flowChartPredefinedProcess (Predefined Process Flow Shape)
  125. // flowChartPreparation (Preparation Flow Shape)
  126. // flowChartProcess (Process Flow Shape)
  127. // flowChartPunchedCard (Punched Card Flow Shape)
  128. // flowChartPunchedTape (Punched Tape Flow Shape)
  129. // flowChartSort (Sort Flow Shape)
  130. // flowChartSummingJunction (Summing Junction Flow Shape)
  131. // flowChartTerminator (Terminator Flow Shape)
  132. // foldedCorner (Folded Corner Shape)
  133. // frame (Frame Shape)
  134. // funnel (Funnel Shape)
  135. // gear6 (Gear 6 Shape)
  136. // gear9 (Gear 9 Shape)
  137. // halfFrame (Half Frame Shape)
  138. // heart (Heart Shape)
  139. // heptagon (Heptagon Shape)
  140. // hexagon (Hexagon Shape)
  141. // homePlate (Home Plate Shape)
  142. // horizontalScroll (Horizontal Scroll Shape)
  143. // irregularSeal1 (Irregular Seal 1 Shape)
  144. // irregularSeal2 (Irregular Seal 2 Shape)
  145. // leftArrow (Left Arrow Shape)
  146. // leftArrowCallout (Callout Left Arrow Shape)
  147. // leftBrace (Left Brace Shape)
  148. // leftBracket (Left Bracket Shape)
  149. // leftCircularArrow (Left Circular Arrow Shape)
  150. // leftRightArrow (Left Right Arrow Shape)
  151. // leftRightArrowCallout (Callout Left Right Arrow Shape)
  152. // leftRightCircularArrow (Left Right Circular Arrow Shape)
  153. // leftRightRibbon (Left Right Ribbon Shape)
  154. // leftRightUpArrow (Left Right Up Arrow Shape)
  155. // leftUpArrow (Left Up Arrow Shape)
  156. // lightningBolt (Lightning Bolt Shape)
  157. // line (Line Shape)
  158. // lineInv (Line Inverse Shape)
  159. // mathDivide (Divide Math Shape)
  160. // mathEqual (Equal Math Shape)
  161. // mathMinus (Minus Math Shape)
  162. // mathMultiply (Multiply Math Shape)
  163. // mathNotEqual (Not Equal Math Shape)
  164. // mathPlus (Plus Math Shape)
  165. // moon (Moon Shape)
  166. // nonIsoscelesTrapezoid (Non-Isosceles Trapezoid Shape)
  167. // noSmoking (No Smoking Shape)
  168. // notchedRightArrow (Notched Right Arrow Shape)
  169. // octagon (Octagon Shape)
  170. // parallelogram (Parallelogram Shape)
  171. // pentagon (Pentagon Shape)
  172. // pie (Pie Shape)
  173. // pieWedge (Pie Wedge Shape)
  174. // plaque (Plaque Shape)
  175. // plaqueTabs (Plaque Tabs Shape)
  176. // plus (Plus Shape)
  177. // quadArrow (Quad-Arrow Shape)
  178. // quadArrowCallout (Callout Quad-Arrow Shape)
  179. // rect (Rectangle Shape)
  180. // ribbon (Ribbon Shape)
  181. // ribbon2 (Ribbon 2 Shape)
  182. // rightArrow (Right Arrow Shape)
  183. // rightArrowCallout (Callout Right Arrow Shape)
  184. // rightBrace (Right Brace Shape)
  185. // rightBracket (Right Bracket Shape)
  186. // round1Rect (One Round Corner Rectangle Shape)
  187. // round2DiagRect (Two Diagonal Round Corner Rectangle Shape)
  188. // round2SameRect (Two Same-side Round Corner Rectangle Shape)
  189. // roundRect (Round Corner Rectangle Shape)
  190. // rtTriangle (Right Triangle Shape)
  191. // smileyFace (Smiley Face Shape)
  192. // snip1Rect (One Snip Corner Rectangle Shape)
  193. // snip2DiagRect (Two Diagonal Snip Corner Rectangle Shape)
  194. // snip2SameRect (Two Same-side Snip Corner Rectangle Shape)
  195. // snipRoundRect (One Snip One Round Corner Rectangle Shape)
  196. // squareTabs (Square Tabs Shape)
  197. // star10 (Ten Pointed Star Shape)
  198. // star12 (Twelve Pointed Star Shape)
  199. // star16 (Sixteen Pointed Star Shape)
  200. // star24 (Twenty Four Pointed Star Shape)
  201. // star32 (Thirty Two Pointed Star Shape)
  202. // star4 (Four Pointed Star Shape)
  203. // star5 (Five Pointed Star Shape)
  204. // star6 (Six Pointed Star Shape)
  205. // star7 (Seven Pointed Star Shape)
  206. // star8 (Eight Pointed Star Shape)
  207. // straightConnector1 (Straight Connector 1 Shape)
  208. // stripedRightArrow (Striped Right Arrow Shape)
  209. // sun (Sun Shape)
  210. // swooshArrow (Swoosh Arrow Shape)
  211. // teardrop (Teardrop Shape)
  212. // trapezoid (Trapezoid Shape)
  213. // triangle (Triangle Shape)
  214. // upArrow (Up Arrow Shape)
  215. // upArrowCallout (Callout Up Arrow Shape)
  216. // upDownArrow (Up Down Arrow Shape)
  217. // upDownArrowCallout (Callout Up Down Arrow Shape)
  218. // uturnArrow (U-Turn Arrow Shape)
  219. // verticalScroll (Vertical Scroll Shape)
  220. // wave (Wave Shape)
  221. // wedgeEllipseCallout (Callout Wedge Ellipse Shape)
  222. // wedgeRectCallout (Callout Wedge Rectangle Shape)
  223. // wedgeRoundRectCallout (Callout Wedge Round Rectangle Shape)
  224. //
  225. func (f *File) AddShape(sheet, cell, format string) {
  226. formatSet := parseFormatShapeSet(format)
  227. // Read sheet data.
  228. xlsx := f.workSheetReader(sheet)
  229. // Add first shape for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  230. drawingID := f.countDrawings() + 1
  231. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  232. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  233. if xlsx.Drawing != nil {
  234. // The worksheet already has a shape or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  235. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  236. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  237. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  238. } else {
  239. // Add first shape for given sheet.
  240. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  241. f.addSheetDrawing(sheet, rID)
  242. }
  243. f.addDrawingShape(sheet, drawingXML, cell, formatSet)
  244. f.addDrawingContentTypePart(drawingID)
  245. }
  246. // addDrawingShape
  247. func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *formatShape) {
  248. cell = strings.ToUpper(cell)
  249. fromCol := string(strings.Map(letterOnlyMapF, cell))
  250. fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  251. row := fromRow - 1
  252. col := titleToNumber(fromCol)
  253. width := int(float64(formatSet.Width) * formatSet.Format.XScale)
  254. height := int(float64(formatSet.Height) * formatSet.Format.YScale)
  255. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.Format.OffsetX, formatSet.Format.OffsetY, width, height)
  256. content := xlsxWsDr{}
  257. content.A = NameSpaceDrawingML
  258. content.Xdr = NameSpaceDrawingMLSpreadSheet
  259. cNvPrID := 1
  260. _, ok := f.XLSX[drawingXML]
  261. if ok { // Append Model
  262. decodeWsDr := decodeWsDr{}
  263. xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
  264. cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
  265. for _, v := range decodeWsDr.OneCellAnchor {
  266. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  267. EditAs: v.EditAs,
  268. GraphicFrame: v.Content,
  269. })
  270. }
  271. for _, v := range decodeWsDr.TwoCellAnchor {
  272. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  273. EditAs: v.EditAs,
  274. GraphicFrame: v.Content,
  275. })
  276. }
  277. }
  278. twoCellAnchor := xdrCellAnchor{}
  279. twoCellAnchor.EditAs = "oneCell"
  280. from := xlsxFrom{}
  281. from.Col = colStart
  282. from.ColOff = formatSet.Format.OffsetX * EMU
  283. from.Row = rowStart
  284. from.RowOff = formatSet.Format.OffsetY * EMU
  285. to := xlsxTo{}
  286. to.Col = colEnd
  287. to.ColOff = x2 * EMU
  288. to.Row = rowEnd
  289. to.RowOff = y2 * EMU
  290. twoCellAnchor.From = &from
  291. twoCellAnchor.To = &to
  292. shape := xdrSp{
  293. NvSpPr: &xdrNvSpPr{
  294. CNvPr: &xlsxCNvPr{
  295. ID: cNvPrID,
  296. Name: "Shape " + strconv.Itoa(cNvPrID),
  297. },
  298. CNvSpPr: &xdrCNvSpPr{
  299. TxBox: true,
  300. },
  301. },
  302. SpPr: &xlsxSpPr{
  303. PrstGeom: xlsxPrstGeom{
  304. Prst: formatSet.Type,
  305. },
  306. },
  307. Style: &xdrStyle{
  308. LnRef: setShapeRef(formatSet.Color.Line, 2),
  309. FillRef: setShapeRef(formatSet.Color.Fill, 1),
  310. EffectRef: setShapeRef(formatSet.Color.Effect, 0),
  311. FontRef: &aFontRef{
  312. Idx: "minor",
  313. SchemeClr: &attrValString{
  314. Val: "tx1",
  315. },
  316. },
  317. },
  318. TxBody: &xdrTxBody{
  319. BodyPr: &aBodyPr{
  320. VertOverflow: "clip",
  321. HorzOverflow: "clip",
  322. Wrap: "none",
  323. RtlCol: false,
  324. Anchor: "t",
  325. },
  326. P: &aP{
  327. R: &aR{
  328. RPr: aRPr{
  329. Lang: "en-US",
  330. AltLang: "en-US",
  331. Sz: 1100,
  332. },
  333. T: formatSet.Text,
  334. },
  335. EndParaRPr: &aEndParaRPr{
  336. Lang: "en-US",
  337. },
  338. },
  339. },
  340. }
  341. twoCellAnchor.Sp = &shape
  342. twoCellAnchor.ClientData = &xdrClientData{
  343. FLocksWithSheet: formatSet.Format.FLocksWithSheet,
  344. FPrintsWithSheet: formatSet.Format.FPrintsWithSheet,
  345. }
  346. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  347. output, _ := xml.Marshal(content)
  348. f.saveFileList(drawingXML, string(output))
  349. }
  350. // setShapeRef provides function to set color with hex model by given actual
  351. // color value.
  352. func setShapeRef(color string, i int) *aRef {
  353. if color == "" {
  354. return &aRef{
  355. Idx: 0,
  356. ScrgbClr: &aScrgbClr{
  357. R: 0,
  358. G: 0,
  359. B: 0,
  360. },
  361. }
  362. }
  363. return &aRef{
  364. Idx: i,
  365. SrgbClr: &attrValString{
  366. Val: strings.Replace(strings.ToUpper(color), "#", "", -1),
  367. },
  368. }
  369. }