xmlStyles.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. // xlsxStyleSheet directly maps the stylesheet element in the namespace
  13. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  14. // not checked it for completeness - it does as much as I need.
  15. type xlsxStyleSheet struct {
  16. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
  17. NumFmts *xlsxNumFmts `xml:"numFmts,omitempty"`
  18. Fonts *xlsxFonts `xml:"fonts,omitempty"`
  19. Fills *xlsxFills `xml:"fills,omitempty"`
  20. Borders *xlsxBorders `xml:"borders,omitempty"`
  21. CellStyleXfs *xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
  22. CellXfs *xlsxCellXfs `xml:"cellXfs,omitempty"`
  23. CellStyles *xlsxCellStyles `xml:"cellStyles,omitempty"`
  24. Dxfs *xlsxDxfs `xml:"dxfs,omitempty"`
  25. TableStyles *xlsxTableStyles `xml:"tableStyles,omitempty"`
  26. Colors *xlsxStyleColors `xml:"colors,omitempty"`
  27. ExtLst *xlsxExtLst `xml:"extLst"`
  28. }
  29. // xlsxAlignment formatting information pertaining to text alignment in cells.
  30. // There are a variety of choices for how text is aligned both horizontally and
  31. // vertically, as well as indentation settings, and so on.
  32. type xlsxAlignment struct {
  33. Horizontal string `xml:"horizontal,attr,omitempty"`
  34. Indent int `xml:"indent,attr,omitempty"`
  35. JustifyLastLine bool `xml:"justifyLastLine,attr,omitempty"`
  36. ReadingOrder uint64 `xml:"readingOrder,attr,omitempty"`
  37. RelativeIndent int `xml:"relativeIndent,attr,omitempty"`
  38. ShrinkToFit bool `xml:"shrinkToFit,attr,omitempty"`
  39. TextRotation int `xml:"textRotation,attr,omitempty"`
  40. Vertical string `xml:"vertical,attr,omitempty"`
  41. WrapText bool `xml:"wrapText,attr,omitempty"`
  42. }
  43. // xlsxProtection (Protection Properties) contains protection properties
  44. // associated with the cell. Each cell has protection properties that can be
  45. // set. The cell protection properties do not take effect unless the sheet has
  46. // been protected.
  47. type xlsxProtection struct {
  48. Hidden bool `xml:"hidden,attr"`
  49. Locked bool `xml:"locked,attr"`
  50. }
  51. // xlsxLine directly maps the line style element in the namespace
  52. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  53. // not checked it for completeness - it does as much as I need.
  54. type xlsxLine struct {
  55. Style string `xml:"style,attr,omitempty"`
  56. Color *xlsxColor `xml:"color,omitempty"`
  57. }
  58. // xlsxColor is a common mapping used for both the fgColor and bgColor elements.
  59. // Foreground color of the cell fill pattern. Cell fill patterns operate with
  60. // two colors: a background color and a foreground color. These combine together
  61. // to make a patterned cell fill. Background color of the cell fill pattern.
  62. // Cell fill patterns operate with two colors: a background color and a
  63. // foreground color. These combine together to make a patterned cell fill.
  64. type xlsxColor struct {
  65. Auto bool `xml:"auto,attr,omitempty"`
  66. RGB string `xml:"rgb,attr,omitempty"`
  67. Indexed int `xml:"indexed,attr,omitempty"`
  68. Theme *int `xml:"theme,attr"`
  69. Tint float64 `xml:"tint,attr,omitempty"`
  70. }
  71. // xlsxFonts directly maps the font element. This element contains all font
  72. // definitions for this workbook.
  73. type xlsxFonts struct {
  74. Count int `xml:"count,attr"`
  75. Font []*xlsxFont `xml:"font"`
  76. }
  77. // font directly maps the font element.
  78. type font struct {
  79. Name *attrValString `xml:"name"`
  80. Charset *attrValInt `xml:"charset"`
  81. Family *attrValInt `xml:"family"`
  82. B bool `xml:"b,omitempty"`
  83. I bool `xml:"i,omitempty"`
  84. Strike bool `xml:"strike,omitempty"`
  85. Outline bool `xml:"outline,omitempty"`
  86. Shadow bool `xml:"shadow,omitempty"`
  87. Condense bool `xml:"condense,omitempty"`
  88. Extend bool `xml:"extend,omitempty"`
  89. Color *xlsxColor `xml:"color"`
  90. Sz *attrValInt `xml:"sz"`
  91. U *attrValString `xml:"u"`
  92. Scheme *attrValString `xml:"scheme"`
  93. }
  94. // xlsxFont directly maps the font element. This element defines the properties
  95. // for one of the fonts used in this workbook.
  96. type xlsxFont struct {
  97. Font string `xml:",innerxml"`
  98. }
  99. // xlsxFills directly maps the fills element. This element defines the cell
  100. // fills portion of the Styles part, consisting of a sequence of fill records. A
  101. // cell fill consists of a background color, foreground color, and pattern to be
  102. // applied across the cell.
  103. type xlsxFills struct {
  104. Count int `xml:"count,attr"`
  105. Fill []*xlsxFill `xml:"fill,omitempty"`
  106. }
  107. // xlsxFill directly maps the fill element. This element specifies fill
  108. // formatting.
  109. type xlsxFill struct {
  110. PatternFill *xlsxPatternFill `xml:"patternFill,omitempty"`
  111. GradientFill *xlsxGradientFill `xml:"gradientFill,omitempty"`
  112. }
  113. // xlsxPatternFill directly maps the patternFill element in the namespace
  114. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  115. // not checked it for completeness - it does as much as I need. This element is
  116. // used to specify cell fill information for pattern and solid color cell fills.
  117. // For solid cell fills (no pattern), fgColor is used. For cell fills with
  118. // patterns specified, then the cell fill color is specified by the bgColor
  119. // element.
  120. type xlsxPatternFill struct {
  121. PatternType string `xml:"patternType,attr,omitempty"`
  122. FgColor xlsxColor `xml:"fgColor,omitempty"`
  123. BgColor xlsxColor `xml:"bgColor,omitempty"`
  124. }
  125. // xlsxGradientFill defines a gradient-style cell fill. Gradient cell fills can
  126. // use one or two colors as the end points of color interpolation.
  127. type xlsxGradientFill struct {
  128. Bottom float64 `xml:"bottom,attr,omitempty"`
  129. Degree float64 `xml:"degree,attr,omitempty"`
  130. Left float64 `xml:"left,attr,omitempty"`
  131. Right float64 `xml:"right,attr,omitempty"`
  132. Top float64 `xml:"top,attr,omitempty"`
  133. Type string `xml:"type,attr,omitempty"`
  134. Stop []*xlsxGradientFillStop `xml:"stop,omitempty"`
  135. }
  136. // xlsxGradientFillStop directly maps the stop element.
  137. type xlsxGradientFillStop struct {
  138. Position float64 `xml:"position,attr"`
  139. Color xlsxColor `xml:"color,omitempty"`
  140. }
  141. // xlsxBorders directly maps the borders element. This element contains borders
  142. // formatting information, specifying all border definitions for all cells in
  143. // the workbook.
  144. type xlsxBorders struct {
  145. Count int `xml:"count,attr"`
  146. Border []*xlsxBorder `xml:"border,omitempty"`
  147. }
  148. // xlsxBorder directly maps the border element. Expresses a single set of cell
  149. // border formats (left, right, top, bottom, diagonal). Color is optional. When
  150. // missing, 'automatic' is implied.
  151. type xlsxBorder struct {
  152. DiagonalDown bool `xml:"diagonalDown,attr,omitempty"`
  153. DiagonalUp bool `xml:"diagonalUp,attr,omitempty"`
  154. Outline bool `xml:"outline,attr,omitempty"`
  155. Left xlsxLine `xml:"left,omitempty"`
  156. Right xlsxLine `xml:"right,omitempty"`
  157. Top xlsxLine `xml:"top,omitempty"`
  158. Bottom xlsxLine `xml:"bottom,omitempty"`
  159. Diagonal xlsxLine `xml:"diagonal,omitempty"`
  160. }
  161. // xlsxCellStyles directly maps the cellStyles element. This element contains
  162. // the named cell styles, consisting of a sequence of named style records. A
  163. // named cell style is a collection of direct or themed formatting (e.g., cell
  164. // border, cell fill, and font type/size/style) grouped together into a single
  165. // named style, and can be applied to a cell.
  166. type xlsxCellStyles struct {
  167. XMLName xml.Name `xml:"cellStyles"`
  168. Count int `xml:"count,attr"`
  169. CellStyle []*xlsxCellStyle `xml:"cellStyle,omitempty"`
  170. }
  171. // xlsxCellStyle directly maps the cellStyle element. This element represents
  172. // the name and related formatting records for a named cell style in this
  173. // workbook.
  174. type xlsxCellStyle struct {
  175. XMLName xml.Name `xml:"cellStyle"`
  176. BuiltInID *int `xml:"builtinId,attr,omitempty"`
  177. CustomBuiltIn *bool `xml:"customBuiltin,attr,omitempty"`
  178. Hidden *bool `xml:"hidden,attr,omitempty"`
  179. ILevel *bool `xml:"iLevel,attr,omitempty"`
  180. Name string `xml:"name,attr"`
  181. XfID int `xml:"xfId,attr"`
  182. }
  183. // xlsxCellStyleXfs directly maps the cellStyleXfs element. This element
  184. // contains the master formatting records (xf's) which define the formatting for
  185. // all named cell styles in this workbook. Master formatting records reference
  186. // individual elements of formatting (e.g., number format, font definitions,
  187. // cell fills, etc) by specifying a zero-based index into those collections.
  188. // Master formatting records also specify whether to apply or ignore particular
  189. // aspects of formatting.
  190. type xlsxCellStyleXfs struct {
  191. Count int `xml:"count,attr"`
  192. Xf []xlsxXf `xml:"xf,omitempty"`
  193. }
  194. // xlsxXf directly maps the xf element. A single xf element describes all of the
  195. // formatting for a cell.
  196. type xlsxXf struct {
  197. ApplyAlignment bool `xml:"applyAlignment,attr"`
  198. ApplyBorder bool `xml:"applyBorder,attr"`
  199. ApplyFill bool `xml:"applyFill,attr"`
  200. ApplyFont bool `xml:"applyFont,attr"`
  201. ApplyNumberFormat bool `xml:"applyNumberFormat,attr"`
  202. ApplyProtection bool `xml:"applyProtection,attr"`
  203. BorderID int `xml:"borderId,attr"`
  204. FillID int `xml:"fillId,attr"`
  205. FontID int `xml:"fontId,attr"`
  206. NumFmtID int `xml:"numFmtId,attr"`
  207. PivotButton bool `xml:"pivotButton,attr,omitempty"`
  208. QuotePrefix bool `xml:"quotePrefix,attr,omitempty"`
  209. XfID *int `xml:"xfId,attr"`
  210. Alignment *xlsxAlignment `xml:"alignment"`
  211. Protection *xlsxProtection `xml:"protection"`
  212. }
  213. // xlsxCellXfs directly maps the cellXfs element. This element contains the
  214. // master formatting records (xf) which define the formatting applied to cells
  215. // in this workbook. These records are the starting point for determining the
  216. // formatting for a cell. Cells in the Sheet Part reference the xf records by
  217. // zero-based index.
  218. type xlsxCellXfs struct {
  219. Count int `xml:"count,attr"`
  220. Xf []xlsxXf `xml:"xf,omitempty"`
  221. }
  222. // xlsxDxfs directly maps the dxfs element. This element contains the master
  223. // differential formatting records (dxf's) which define formatting for all non-
  224. // cell formatting in this workbook. Whereas xf records fully specify a
  225. // particular aspect of formatting (e.g., cell borders) by referencing those
  226. // formatting definitions elsewhere in the Styles part, dxf records specify
  227. // incremental (or differential) aspects of formatting directly inline within
  228. // the dxf element. The dxf formatting is to be applied on top of or in addition
  229. // to any formatting already present on the object using the dxf record.
  230. type xlsxDxfs struct {
  231. Count int `xml:"count,attr"`
  232. Dxfs []*xlsxDxf `xml:"dxf,omitempty"`
  233. }
  234. // xlsxDxf directly maps the dxf element. A single dxf record, expressing
  235. // incremental formatting to be applied.
  236. type xlsxDxf struct {
  237. Dxf string `xml:",innerxml"`
  238. }
  239. // dxf directly maps the dxf element.
  240. type dxf struct {
  241. Font *font `xml:"font"`
  242. NumFmt *xlsxNumFmt `xml:"numFmt"`
  243. Fill *xlsxFill `xml:"fill"`
  244. Alignment *xlsxAlignment `xml:"alignment"`
  245. Border *xlsxBorder `xml:"border"`
  246. Protection *xlsxProtection `xml:"protection"`
  247. ExtLst *xlsxExt `xml:"extLst"`
  248. }
  249. // xlsxTableStyles directly maps the tableStyles element. This element
  250. // represents a collection of Table style definitions for Table styles and
  251. // PivotTable styles used in this workbook. It consists of a sequence of
  252. // tableStyle records, each defining a single Table style.
  253. type xlsxTableStyles struct {
  254. Count int `xml:"count,attr"`
  255. DefaultPivotStyle string `xml:"defaultPivotStyle,attr"`
  256. DefaultTableStyle string `xml:"defaultTableStyle,attr"`
  257. TableStyles []*xlsxTableStyle `xml:"tableStyle,omitempty"`
  258. }
  259. // xlsxTableStyle directly maps the tableStyle element. This element represents
  260. // a single table style definition that indicates how a spreadsheet application
  261. // should format and display a table.
  262. type xlsxTableStyle struct {
  263. Name string `xml:"name,attr,omitempty"`
  264. Pivot int `xml:"pivot,attr"`
  265. Count int `xml:"count,attr,omitempty"`
  266. Table bool `xml:"table,attr,omitempty"`
  267. TableStyleElement string `xml:",innerxml"`
  268. }
  269. // xlsxNumFmts directly maps the numFmts element. This element defines the
  270. // number formats in this workbook, consisting of a sequence of numFmt records,
  271. // where each numFmt record defines a particular number format, indicating how
  272. // to format and render the numeric value of a cell.
  273. type xlsxNumFmts struct {
  274. Count int `xml:"count,attr"`
  275. NumFmt []*xlsxNumFmt `xml:"numFmt,omitempty"`
  276. }
  277. // xlsxNumFmt directly maps the numFmt element. This element specifies number
  278. // format properties which indicate how to format and render the numeric value
  279. // of a cell.
  280. type xlsxNumFmt struct {
  281. NumFmtID int `xml:"numFmtId,attr,omitempty"`
  282. FormatCode string `xml:"formatCode,attr,omitempty"`
  283. }
  284. // xlsxStyleColors directly maps the colors element. Color information
  285. // associated with this stylesheet. This collection is written whenever the
  286. // legacy color palette has been modified (backwards compatibility settings) or
  287. // a custom color has been selected while using this workbook.
  288. type xlsxStyleColors struct {
  289. Color string `xml:",innerxml"`
  290. }
  291. // formatFont directly maps the styles settings of the fonts.
  292. type formatFont struct {
  293. Bold bool `json:"bold"`
  294. Italic bool `json:"italic"`
  295. Underline string `json:"underline"`
  296. Family string `json:"family"`
  297. Size int `json:"size"`
  298. Color string `json:"color"`
  299. }
  300. // formatStyle directly maps the styles settings of the cells.
  301. type formatStyle struct {
  302. Border []struct {
  303. Type string `json:"type"`
  304. Color string `json:"color"`
  305. Style int `json:"style"`
  306. } `json:"border"`
  307. Fill struct {
  308. Type string `json:"type"`
  309. Pattern int `json:"pattern"`
  310. Color []string `json:"color"`
  311. Shading int `json:"shading"`
  312. } `json:"fill"`
  313. Font *formatFont `json:"font"`
  314. Alignment *struct {
  315. Horizontal string `json:"horizontal"`
  316. Indent int `json:"indent"`
  317. JustifyLastLine bool `json:"justify_last_line"`
  318. ReadingOrder uint64 `json:"reading_order"`
  319. RelativeIndent int `json:"relative_indent"`
  320. ShrinkToFit bool `json:"shrink_to_fit"`
  321. TextRotation int `json:"text_rotation"`
  322. Vertical string `json:"vertical"`
  323. WrapText bool `json:"wrap_text"`
  324. } `json:"alignment"`
  325. Protection *struct {
  326. Hidden bool `json:"hidden"`
  327. Locked bool `json:"locked"`
  328. } `json:"protection"`
  329. NumFmt int `json:"number_format"`
  330. DecimalPlaces int `json:"decimal_places"`
  331. CustomNumFmt *string `json:"custom_number_format"`
  332. Lang string `json:"lang"`
  333. NegRed bool `json:"negred"`
  334. }