xmlStyles.go 15 KB

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