xmlDrawing.go 17 KB

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