xmlDrawing.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Copyright 2016 - 2018 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 "encoding/xml"
  11. // Source relationship and namespace.
  12. const (
  13. SourceRelationship = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  14. SourceRelationshipChart = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
  15. SourceRelationshipComments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
  16. SourceRelationshipImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
  17. SourceRelationshipTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"
  18. SourceRelationshipDrawingML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
  19. SourceRelationshipDrawingVML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
  20. SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
  21. SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
  22. SourceRelationshipChart201506 = "http://schemas.microsoft.com/office/drawing/2015/06/chart"
  23. SourceRelationshipChart20070802 = "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"
  24. SourceRelationshipChart2014 = "http://schemas.microsoft.com/office/drawing/2014/chart"
  25. SourceRelationshipCompatibility = "http://schemas.openxmlformats.org/markup-compatibility/2006"
  26. NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main"
  27. NameSpaceDrawingMLChart = "http://schemas.openxmlformats.org/drawingml/2006/chart"
  28. NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
  29. NameSpaceSpreadSheet = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  30. NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
  31. )
  32. var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}
  33. // xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
  34. // element specifies non-visual canvas properties. This allows for additional
  35. // information that does not affect the appearance of the picture to be stored.
  36. type xlsxCNvPr struct {
  37. ID int `xml:"id,attr"`
  38. Name string `xml:"name,attr"`
  39. Descr string `xml:"descr,attr"`
  40. Title string `xml:"title,attr,omitempty"`
  41. HlinkClick *xlsxHlinkClick `xml:"a:hlinkClick"`
  42. }
  43. // xlsxHlinkClick (Click Hyperlink) Specifies the on-click hyperlink
  44. // information to be applied to a run of text. When the hyperlink text is
  45. // clicked the link is fetched.
  46. type xlsxHlinkClick struct {
  47. R string `xml:"xmlns:r,attr,omitempty"`
  48. RID string `xml:"r:id,attr,omitempty"`
  49. InvalidURL string `xml:"invalidUrl,attr,omitempty"`
  50. Action string `xml:"action,attr,omitempty"`
  51. TgtFrame string `xml:"tgtFrame,attr,omitempty"`
  52. Tooltip string `xml:"tooltip,attr,omitempty"`
  53. History bool `xml:"history,attr,omitempty"`
  54. HighlightClick bool `xml:"highlightClick,attr,omitempty"`
  55. EndSnd bool `xml:"endSnd,attr,omitempty"`
  56. }
  57. // xlsxPicLocks directly maps the picLocks (Picture Locks). This element
  58. // specifies all locking properties for a graphic frame. These properties inform
  59. // the generating application about specific properties that have been
  60. // previously locked and thus should not be changed.
  61. type xlsxPicLocks struct {
  62. NoAdjustHandles bool `xml:"noAdjustHandles,attr,omitempty"`
  63. NoChangeArrowheads bool `xml:"noChangeArrowheads,attr,omitempty"`
  64. NoChangeAspect bool `xml:"noChangeAspect,attr"`
  65. NoChangeShapeType bool `xml:"noChangeShapeType,attr,omitempty"`
  66. NoCrop bool `xml:"noCrop,attr,omitempty"`
  67. NoEditPoints bool `xml:"noEditPoints,attr,omitempty"`
  68. NoGrp bool `xml:"noGrp,attr,omitempty"`
  69. NoMove bool `xml:"noMove,attr,omitempty"`
  70. NoResize bool `xml:"noResize,attr,omitempty"`
  71. NoRot bool `xml:"noRot,attr,omitempty"`
  72. NoSelect bool `xml:"noSelect,attr,omitempty"`
  73. }
  74. // xlsxBlip directly maps the blip element in the namespace
  75. // http://purl.oclc.org/ooxml/officeDoc ument/relationships - This element
  76. // specifies the existence of an image (binary large image or picture) and
  77. // contains a reference to the image data.
  78. type xlsxBlip struct {
  79. Embed string `xml:"r:embed,attr"`
  80. Cstate string `xml:"cstate,attr,omitempty"`
  81. R string `xml:"xmlns:r,attr"`
  82. }
  83. // xlsxStretch directly maps the stretch element. This element specifies that a
  84. // BLIP should be stretched to fill the target rectangle. The other option is a
  85. // tile where a BLIP is tiled to fill the available area.
  86. type xlsxStretch struct {
  87. FillRect string `xml:"a:fillRect"`
  88. }
  89. // xlsxOff directly maps the colOff and rowOff element. This element is used to
  90. // specify the column offset within a cell.
  91. type xlsxOff struct {
  92. X int `xml:"x,attr"`
  93. Y int `xml:"y,attr"`
  94. }
  95. // xlsxExt directly maps the ext element.
  96. type xlsxExt struct {
  97. Cx int `xml:"cx,attr"`
  98. Cy int `xml:"cy,attr"`
  99. }
  100. // xlsxPrstGeom directly maps the prstGeom (Preset geometry). This element
  101. // specifies when a preset geometric shape should be used instead of a custom
  102. // geometric shape. The generating application should be able to render all
  103. // preset geometries enumerated in the ST_ShapeType list.
  104. type xlsxPrstGeom struct {
  105. Prst string `xml:"prst,attr"`
  106. }
  107. // xlsxXfrm directly maps the xfrm (2D Transform for Graphic Frame). This
  108. // element specifies the transform to be applied to the corresponding graphic
  109. // frame. This transformation is applied to the graphic frame just as it would
  110. // be for a shape or group shape.
  111. type xlsxXfrm struct {
  112. Off xlsxOff `xml:"a:off"`
  113. Ext xlsxExt `xml:"a:ext"`
  114. }
  115. // xlsxCNvPicPr directly maps the cNvPicPr (Non-Visual Picture Drawing
  116. // Properties). This element specifies the non-visual properties for the picture
  117. // canvas. These properties are to be used by the generating application to
  118. // determine how certain properties are to be changed for the picture object in
  119. // question.
  120. type xlsxCNvPicPr struct {
  121. PicLocks xlsxPicLocks `xml:"a:picLocks"`
  122. }
  123. // directly maps the nvPicPr (Non-Visual Properties for a Picture). This element
  124. // specifies all non-visual properties for a picture. This element is a
  125. // container for the non-visual identification properties, shape properties and
  126. // application properties that are to be associated with a picture. This allows
  127. // for additional information that does not affect the appearance of the picture
  128. // to be stored.
  129. type xlsxNvPicPr struct {
  130. CNvPr xlsxCNvPr `xml:"xdr:cNvPr"`
  131. CNvPicPr xlsxCNvPicPr `xml:"xdr:cNvPicPr"`
  132. }
  133. // xlsxBlipFill directly maps the blipFill (Picture Fill). This element
  134. // specifies the kind of picture fill that the picture object has. Because a
  135. // picture has a picture fill already by default, it is possible to have two
  136. // fills specified for a picture object.
  137. type xlsxBlipFill struct {
  138. Blip xlsxBlip `xml:"a:blip"`
  139. Stretch xlsxStretch `xml:"a:stretch"`
  140. }
  141. // xlsxSpPr directly maps the spPr (Shape Properties). This element specifies
  142. // the visual shape properties that can be applied to a picture. These are the
  143. // same properties that are allowed to describe the visual properties of a shape
  144. // but are used here to describe the visual appearance of a picture within a
  145. // document.
  146. type xlsxSpPr struct {
  147. Xfrm xlsxXfrm `xml:"a:xfrm"`
  148. PrstGeom xlsxPrstGeom `xml:"a:prstGeom"`
  149. }
  150. // xlsxPic elements encompass the definition of pictures within the DrawingML
  151. // framework. While pictures are in many ways very similar to shapes they have
  152. // specific properties that are unique in order to optimize for picture-
  153. // specific scenarios.
  154. type xlsxPic struct {
  155. NvPicPr xlsxNvPicPr `xml:"xdr:nvPicPr"`
  156. BlipFill xlsxBlipFill `xml:"xdr:blipFill"`
  157. SpPr xlsxSpPr `xml:"xdr:spPr"`
  158. }
  159. // xlsxFrom specifies the starting anchor.
  160. type xlsxFrom struct {
  161. Col int `xml:"xdr:col"`
  162. ColOff int `xml:"xdr:colOff"`
  163. Row int `xml:"xdr:row"`
  164. RowOff int `xml:"xdr:rowOff"`
  165. }
  166. // xlsxTo directly specifies the ending anchor.
  167. type xlsxTo struct {
  168. Col int `xml:"xdr:col"`
  169. ColOff int `xml:"xdr:colOff"`
  170. Row int `xml:"xdr:row"`
  171. RowOff int `xml:"xdr:rowOff"`
  172. }
  173. // xdrClientData directly maps the clientData element. An empty element which
  174. // specifies (via attributes) certain properties related to printing and
  175. // selection of the drawing object. The fLocksWithSheet attribute (either true
  176. // or false) determines whether to disable selection when the sheet is
  177. // protected, and fPrintsWithSheet attribute (either true or false) determines
  178. // whether the object is printed when the sheet is printed.
  179. type xdrClientData struct {
  180. FLocksWithSheet bool `xml:"fLocksWithSheet,attr"`
  181. FPrintsWithSheet bool `xml:"fPrintsWithSheet,attr"`
  182. }
  183. // xdrCellAnchor directly maps the oneCellAnchor (One Cell Anchor Shape Size)
  184. // and twoCellAnchor (Two Cell Anchor Shape Size). This element specifies a two
  185. // cell anchor placeholder for a group, a shape, or a drawing element. It moves
  186. // with cells and its extents are in EMU units.
  187. type xdrCellAnchor struct {
  188. EditAs string `xml:"editAs,attr,omitempty"`
  189. From *xlsxFrom `xml:"xdr:from"`
  190. To *xlsxTo `xml:"xdr:to"`
  191. Ext *xlsxExt `xml:"xdr:ext"`
  192. Sp *xdrSp `xml:"xdr:sp"`
  193. Pic *xlsxPic `xml:"xdr:pic,omitempty"`
  194. GraphicFrame string `xml:",innerxml"`
  195. ClientData *xdrClientData `xml:"xdr:clientData"`
  196. }
  197. // xlsxWsDr directly maps the root element for a part of this content type shall
  198. // wsDr.
  199. type xlsxWsDr struct {
  200. XMLName xml.Name `xml:"xdr:wsDr"`
  201. OneCellAnchor []*xdrCellAnchor `xml:"xdr:oneCellAnchor"`
  202. TwoCellAnchor []*xdrCellAnchor `xml:"xdr:twoCellAnchor"`
  203. A string `xml:"xmlns:a,attr,omitempty"`
  204. Xdr string `xml:"xmlns:xdr,attr,omitempty"`
  205. R string `xml:"xmlns:r,attr,omitempty"`
  206. }
  207. // xlsxGraphicFrame (Graphic Frame) directly maps the xdr:graphicFrame element.
  208. // This element specifies the existence of a graphics frame. This frame contains
  209. // a graphic that was generated by an external source and needs a container in
  210. // which to be displayed on the slide surface.
  211. type xlsxGraphicFrame struct {
  212. XMLName xml.Name `xml:"xdr:graphicFrame"`
  213. Macro string `xml:"macro,attr"`
  214. NvGraphicFramePr xlsxNvGraphicFramePr `xml:"xdr:nvGraphicFramePr"`
  215. Xfrm xlsxXfrm `xml:"xdr:xfrm"`
  216. Graphic *xlsxGraphic `xml:"a:graphic"`
  217. }
  218. // xlsxNvGraphicFramePr (Non-Visual Properties for a Graphic Frame) directly
  219. // maps the xdr:nvGraphicFramePr element. This element specifies all non-visual
  220. // properties for a graphic frame. This element is a container for the non-
  221. // visual identification properties, shape properties and application properties
  222. // that are to be associated with a graphic frame. This allows for additional
  223. // information that does not affect the appearance of the graphic frame to be
  224. // stored.
  225. type xlsxNvGraphicFramePr struct {
  226. CNvPr *xlsxCNvPr `xml:"xdr:cNvPr"`
  227. ChicNvGraphicFramePr string `xml:"xdr:cNvGraphicFramePr"`
  228. }
  229. // xlsxGraphic (Graphic Object) directly maps the a:graphic element. This
  230. // element specifies the existence of a single graphic object. Document authors
  231. // should refer to this element when they wish to persist a graphical object of
  232. // some kind. The specification for this graphical object is provided entirely
  233. // by the document author and referenced within the graphicData child element.
  234. type xlsxGraphic struct {
  235. GraphicData *xlsxGraphicData `xml:"a:graphicData"`
  236. }
  237. // xlsxGraphicData (Graphic Object Data) directly maps the a:graphicData
  238. // element. This element specifies the reference to a graphic object within the
  239. // document. This graphic object is provided entirely by the document authors
  240. // who choose to persist this data within the document.
  241. type xlsxGraphicData struct {
  242. URI string `xml:"uri,attr"`
  243. Chart *xlsxChart `xml:"c:chart,omitempty"`
  244. }
  245. // xlsxChart (Chart) directly maps the c:chart element.
  246. type xlsxChart struct {
  247. C string `xml:"xmlns:c,attr"`
  248. RID string `xml:"r:id,attr"`
  249. R string `xml:"xmlns:r,attr"`
  250. }
  251. // xdrSp (Shape) directly maps the xdr:sp element. This element specifies the
  252. // existence of a single shape. A shape can either be a preset or a custom
  253. // geometry, defined using the SpreadsheetDrawingML framework. In addition to a
  254. // geometry each shape can have both visual and non-visual properties attached.
  255. // Text and corresponding styling information can also be attached to a shape.
  256. // This shape is specified along with all other shapes within either the shape
  257. // tree or group shape elements.
  258. type xdrSp struct {
  259. Macro string `xml:"macro,attr"`
  260. Textlink string `xml:"textlink,attr"`
  261. NvSpPr *xdrNvSpPr `xml:"xdr:nvSpPr"`
  262. SpPr *xlsxSpPr `xml:"xdr:spPr"`
  263. Style *xdrStyle `xml:"xdr:style"`
  264. TxBody *xdrTxBody `xml:"xdr:txBody"`
  265. }
  266. // xdrNvSpPr (Non-Visual Properties for a Shape) directly maps the xdr:nvSpPr
  267. // element. This element specifies all non-visual properties for a shape. This
  268. // element is a container for the non-visual identification properties, shape
  269. // properties and application properties that are to be associated with a shape.
  270. // This allows for additional information that does not affect the appearance of
  271. // the shape to be stored.
  272. type xdrNvSpPr struct {
  273. CNvPr *xlsxCNvPr `xml:"xdr:cNvPr"`
  274. CNvSpPr *xdrCNvSpPr `xml:"xdr:cNvSpPr"`
  275. }
  276. // xdrCNvSpPr (Connection Non-Visual Shape Properties) directly maps the
  277. // xdr:cNvSpPr element. This element specifies the set of non-visual properties
  278. // for a connection shape. These properties specify all data about the
  279. // connection shape which do not affect its display within a spreadsheet.
  280. type xdrCNvSpPr struct {
  281. TxBox bool `xml:"txBox,attr"`
  282. }
  283. // xdrStyle (Shape Style) directly maps the xdr:style element. The element
  284. // specifies the style that is applied to a shape and the corresponding
  285. // references for each of the style components such as lines and fills.
  286. type xdrStyle struct {
  287. LnRef *aRef `xml:"a:lnRef"`
  288. FillRef *aRef `xml:"a:fillRef"`
  289. EffectRef *aRef `xml:"a:effectRef"`
  290. FontRef *aFontRef `xml:"a:fontRef"`
  291. }
  292. // aRef directly maps the a:lnRef, a:fillRef and a:effectRef element.
  293. type aRef struct {
  294. Idx int `xml:"idx,attr"`
  295. ScrgbClr *aScrgbClr `xml:"a:scrgbClr"`
  296. SchemeClr *attrValString `xml:"a:schemeClr"`
  297. SrgbClr *attrValString `xml:"a:srgbClr"`
  298. }
  299. // aScrgbClr (RGB Color Model - Percentage Variant) directly maps the a:scrgbClr
  300. // element. This element specifies a color using the red, green, blue RGB color
  301. // model. Each component, red, green, and blue is expressed as a percentage from
  302. // 0% to 100%. A linear gamma of 1.0 is assumed.
  303. type aScrgbClr struct {
  304. R float64 `xml:"r,attr"`
  305. G float64 `xml:"g,attr"`
  306. B float64 `xml:"b,attr"`
  307. }
  308. // aFontRef (Font Reference) directly maps the a:fontRef element. This element
  309. // represents a reference to a themed font. When used it specifies which themed
  310. // font to use along with a choice of color.
  311. type aFontRef struct {
  312. Idx string `xml:"idx,attr"`
  313. SchemeClr *attrValString `xml:"a:schemeClr"`
  314. }
  315. // xdrTxBody (Shape Text Body) directly maps the xdr:txBody element. This
  316. // element specifies the existence of text to be contained within the
  317. // corresponding shape. All visible text and visible text related properties are
  318. // contained within this element. There can be multiple paragraphs and within
  319. // paragraphs multiple runs of text.
  320. type xdrTxBody struct {
  321. BodyPr *aBodyPr `xml:"a:bodyPr"`
  322. P []*aP `xml:"a:p"`
  323. }
  324. // formatPicture directly maps the format settings of the picture.
  325. type formatPicture struct {
  326. FPrintsWithSheet bool `json:"print_obj"`
  327. FLocksWithSheet bool `json:"locked"`
  328. NoChangeAspect bool `json:"lock_aspect_ratio"`
  329. OffsetX int `json:"x_offset"`
  330. OffsetY int `json:"y_offset"`
  331. XScale float64 `json:"x_scale"`
  332. YScale float64 `json:"y_scale"`
  333. Hyperlink string `json:"hyperlink"`
  334. HyperlinkType string `json:"hyperlink_type"`
  335. Positioning string `json:"positioning"`
  336. }
  337. // formatShape directly maps the format settings of the shape.
  338. type formatShape struct {
  339. Type string `json:"type"`
  340. Width int `json:"width"`
  341. Height int `json:"height"`
  342. Format formatPicture `json:"format"`
  343. Color formatShapeColor `json:"color"`
  344. Paragraph []formatShapeParagraph `json:"paragraph"`
  345. }
  346. // formatShapeParagraph directly maps the format settings of the paragraph in
  347. // the shape.
  348. type formatShapeParagraph struct {
  349. Font formatFont `json:"font"`
  350. Text string `json:"text"`
  351. }
  352. // formatShapeColor directly maps the color settings of the shape.
  353. type formatShapeColor struct {
  354. Line string `json:"line"`
  355. Fill string `json:"fill"`
  356. Effect string `json:"effect"`
  357. }