xmlStyle.go 9.6 KB

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