xmlStyle.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // xslx is a package designed to help with reading data from
  2. // spreadsheets stored in the XLSX format used in recent versions of
  3. // Microsoft's Excel spreadsheet.
  4. //
  5. // For a concise example of how to use this library why not check out
  6. // the source for xlsx2csv here: https://github.com/tealeg/xlsx2csv
  7. package xlsx
  8. import (
  9. "encoding/xml"
  10. "strconv"
  11. "strings"
  12. )
  13. // xlsxStyle directly maps the styleSheet element in the namespace
  14. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  15. // currently I have not checked it for completeness - it does as much
  16. // as I need.
  17. type xlsxStyleSheet struct {
  18. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
  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. NumFmts []xlsxNumFmt `xml:numFmts>numFmt,omitempty"`
  25. }
  26. // xlsxNumFmt directly maps the numFmt element in the namespace
  27. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  28. // currently I have not checked it for completeness - it does as much
  29. // as I need.
  30. type xlsxNumFmt struct {
  31. NumFmtId int `xml:"numFmtId,omitempty"`
  32. FormatCode string `xml:"formatCode,omitempty"`
  33. }
  34. // xlsxFonts directly maps the fonts element in the namespace
  35. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  36. // currently I have not checked it for completeness - it does as much
  37. // as I need.
  38. type xlsxFonts struct {
  39. Count int `xml:"count,attr"`
  40. Font []xlsxFont `xml:"font,omitempty"`
  41. }
  42. // xlsxFont directly maps the font element in the namespace
  43. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  44. // currently I have not checked it for completeness - it does as much
  45. // as I need.
  46. type xlsxFont struct {
  47. Sz xlsxVal `xml:"sz,omitempty"`
  48. Name xlsxVal `xml:"name,omitempty"`
  49. Family xlsxVal `xml:"family,omitempty"`
  50. Charset xlsxVal `xml:"charset,omitempty"`
  51. Color xlsxColor `xml:"color,omitempty"`
  52. }
  53. // xlsxVal directly maps the val element in the namespace
  54. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  55. // currently I have not checked it for completeness - it does as much
  56. // as I need.
  57. type xlsxVal struct {
  58. Val string `xml:"val,attr,omitempty"`
  59. }
  60. // xlsxFills directly maps the fills element in the namespace
  61. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  62. // currently I have not checked it for completeness - it does as much
  63. // as I need.
  64. type xlsxFills struct {
  65. Count int `xml:"count,attr"`
  66. Fill []xlsxFill `xml:"fill,omitempty"`
  67. }
  68. // xlsxFill directly maps the fill element in the namespace
  69. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  70. // currently I have not checked it for completeness - it does as much
  71. // as I need.
  72. type xlsxFill struct {
  73. PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
  74. }
  75. // xlsxPatternFill directly maps the patternFill element in the namespace
  76. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  77. // currently I have not checked it for completeness - it does as much
  78. // as I need.
  79. type xlsxPatternFill struct {
  80. PatternType string `xml:"patternType,attr,omitempty"`
  81. FgColor xlsxColor `xml:"fgColor,omitempty"`
  82. BgColor xlsxColor `xml:"bgColor,omitempty"`
  83. }
  84. // xlsxColor is a common mapping used for both the fgColor and bgColor
  85. // elements in the namespace
  86. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  87. // currently I have not checked it for completeness - it does as much
  88. // as I need.
  89. type xlsxColor struct {
  90. RGB string `xml:"rgb,attr,omitempty"`
  91. }
  92. // xlsxBorders directly maps the borders element in the namespace
  93. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  94. // currently I have not checked it for completeness - it does as much
  95. // as I need.
  96. type xlsxBorders struct {
  97. Count int `xml:"count,attr"`
  98. Border []xlsxBorder `xml:"border,omitempty"`
  99. }
  100. // xlsxBorder directly maps the border element in the namespace
  101. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  102. // currently I have not checked it for completeness - it does as much
  103. // as I need.
  104. type xlsxBorder struct {
  105. Left xlsxLine `xml:"left,omitempty"`
  106. Right xlsxLine `xml:"right,omitempty"`
  107. Top xlsxLine `xml:"top,omitempty"`
  108. Bottom xlsxLine `xml:"bottom,omitempty"`
  109. }
  110. // xlsxLine directly maps the line style element in the namespace
  111. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  112. // currently I have not checked it for completeness - it does as much
  113. // as I need.
  114. type xlsxLine struct {
  115. Style string `xml:"style,attr,omitempty"`
  116. }
  117. // xlsxCellStyleXfs directly maps the cellStyleXfs element in the
  118. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  119. // - currently I have not checked it for completeness - it does as
  120. // much as I need.
  121. type xlsxCellStyleXfs struct {
  122. Count int `xml:"count,attr"`
  123. Xf []xlsxXf `xml:"xf,omitempty"`
  124. }
  125. // xlsxCellXfs directly maps the cellXfs element in the namespace
  126. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  127. // currently I have not checked it for completeness - it does as much
  128. // as I need.
  129. type xlsxCellXfs struct {
  130. Count int `xml:"count,attr"`
  131. Xf []xlsxXf `xml:"xf,omitempty"`
  132. }
  133. // xlsxXf directly maps the xf element in the namespace
  134. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  135. // currently I have not checked it for completeness - it does as much
  136. // as I need.
  137. type xlsxXf struct {
  138. ApplyAlignment bool `xml:"applyAlignment,attr"`
  139. ApplyBorder bool `xml:"applyBorder,attr"`
  140. ApplyFont bool `xml:"applyFont,attr"`
  141. ApplyFill bool `xml:"applyFill,attr"`
  142. ApplyProtection bool `xml:"applyProtection,attr"`
  143. BorderId int `xml:"borderId,attr"`
  144. FillId int `xml:"fillId,attr"`
  145. FontId int `xml:"fontId,attr"`
  146. NumFmtId int `xml:"numFmtId,attr"`
  147. alignment xlsxAlignment `xml:"alignment"`
  148. }
  149. type xlsxAlignment struct {
  150. Horizontal string `xml:"horizontal,attr"`
  151. Indent int `xml:"indent,attr"`
  152. ShrinkToFit bool `xml:"shrinkToFit,attr"`
  153. TextRotation int `xml:"textRotation,attr"`
  154. Vertical string `xml:"vertical,attr"`
  155. WrapText bool `xml:"wrapText,attr"`
  156. }
  157. func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style Style) {
  158. var styleXf xlsxXf
  159. style = Style{}
  160. style.Border = Border{}
  161. style.Fill = Fill{}
  162. style.Font = Font{}
  163. xfCount := styles.CellXfs.Count
  164. if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
  165. xf := styles.CellXfs.Xf[styleIndex]
  166. // Google docs can produce output that has fewer
  167. // CellStyleXfs than CellXfs - this copes with that.
  168. if styleIndex < styles.CellStyleXfs.Count {
  169. styleXf = styles.CellStyleXfs.Xf[styleIndex]
  170. } else {
  171. styleXf = xlsxXf{}
  172. }
  173. style.ApplyBorder = xf.ApplyBorder || styleXf.ApplyBorder
  174. style.ApplyFill = xf.ApplyFill || styleXf.ApplyFill
  175. style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
  176. if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
  177. style.Border.Left = styles.Borders.Border[xf.BorderId].Left.Style
  178. style.Border.Right = styles.Borders.Border[xf.BorderId].Right.Style
  179. style.Border.Top = styles.Borders.Border[xf.BorderId].Top.Style
  180. style.Border.Bottom = styles.Borders.Border[xf.BorderId].Bottom.Style
  181. }
  182. if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
  183. xFill := styles.Fills.Fill[xf.FillId]
  184. style.Fill.PatternType = xFill.PatternFill.PatternType
  185. style.Fill.FgColor = xFill.PatternFill.FgColor.RGB
  186. style.Fill.BgColor = xFill.PatternFill.BgColor.RGB
  187. }
  188. if xf.FontId > -1 && xf.FontId < styles.Fonts.Count {
  189. xfont := styles.Fonts.Font[xf.FontId]
  190. style.Font.Size, _ = strconv.Atoi(xfont.Sz.Val)
  191. style.Font.Name = xfont.Name.Val
  192. style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
  193. style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
  194. }
  195. }
  196. return style
  197. }
  198. func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int, numFmtRefTable map[int]xlsxNumFmt) string {
  199. if styles.CellXfs.Xf == nil {
  200. return ""
  201. }
  202. var numberFormat string = ""
  203. if styleIndex > -1 && styleIndex <= styles.CellXfs.Count {
  204. xf := styles.CellXfs.Xf[styleIndex]
  205. numFmt := numFmtRefTable[xf.NumFmtId]
  206. numberFormat = numFmt.FormatCode
  207. }
  208. return strings.ToLower(numberFormat)
  209. }
  210. func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
  211. styles.Fonts.Font = append(styles.Fonts.Font, xFont)
  212. index = styles.Fonts.Count
  213. styles.Fonts.Count += 1
  214. return
  215. }
  216. func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
  217. styles.Fills.Fill = append(styles.Fills.Fill, xFill)
  218. index = styles.Fills.Count
  219. styles.Fills.Count += 1
  220. return
  221. }
  222. func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
  223. styles.Borders.Border = append(styles.Borders.Border, xBorder)
  224. index = styles.Borders.Count
  225. styles.Borders.Count += 1
  226. return
  227. }
  228. func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
  229. styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
  230. index = styles.CellStyleXfs.Count
  231. styles.CellStyleXfs.Count += 1
  232. return
  233. }
  234. func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
  235. styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
  236. index = styles.CellXfs.Count
  237. styles.CellXfs.Count += 1
  238. return
  239. }