xmlStyles.go 15 KB

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