xmlStyles.go 15 KB

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