xmlDrawing.go 18 KB

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