xmlStyles.go 16 KB

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