xmlDrawing.go 18 KB

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