xmlStyle.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. "strconv"
  10. "strings"
  11. )
  12. // xlsxStyle directly maps the style element in the namespace
  13. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  14. // currently I have not checked it for completeness - it does as much
  15. // as I need.
  16. type xlsxStyles struct {
  17. Fonts []xlsxFont `xml:"fonts>font,"`
  18. Fills []xlsxFill `xml:"fills>fill"`
  19. Borders []xlsxBorder `xml:"borders>border"`
  20. CellStyleXfs []xlsxXf `xml:"cellStyleXfs>xf"`
  21. CellXfs []xlsxXf `xml:"cellXfs>xf"`
  22. NumFmts []xlsxNumFmt `xml:numFmts>numFmt"`
  23. }
  24. // xlsxNumFmt directly maps the numFmt element in the namespace
  25. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  26. // currently I have not checked it for completeness - it does as much
  27. // as I need.
  28. type xlsxNumFmt struct {
  29. NumFmtId int `xml:"numFmtId"`
  30. FormatCode string `xml:"formatCode"`
  31. }
  32. // xlsxFont directly maps the font element in the namespace
  33. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  34. // currently I have not checked it for completeness - it does as much
  35. // as I need.
  36. type xlsxFont struct {
  37. Sz xlsxVal `xml:"sz"`
  38. Name xlsxVal `xml:"name"`
  39. Family xlsxVal `xml:"family"`
  40. Charset xlsxVal `xml:"charset"`
  41. }
  42. // xlsxVal directly maps the val 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 xlsxVal struct {
  47. Val string `xml:"val,attr"`
  48. }
  49. // xlsxFill directly maps the fill element in the namespace
  50. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  51. // currently I have not checked it for completeness - it does as much
  52. // as I need.
  53. type xlsxFill struct {
  54. PatternFill xlsxPatternFill `xml:"patternFill"`
  55. }
  56. // xlsxPatternFill directly maps the patternFill element in the namespace
  57. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  58. // currently I have not checked it for completeness - it does as much
  59. // as I need.
  60. type xlsxPatternFill struct {
  61. PatternType string `xml:"patternType,attr"`
  62. FgColor xlsxColor `xml:"fgColor"`
  63. BgColor xlsxColor `xml:"bgColor"`
  64. }
  65. // xlsxColor is a common mapping used for both the fgColor and bgColor
  66. // elements in the namespace
  67. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  68. // currently I have not checked it for completeness - it does as much
  69. // as I need.
  70. type xlsxColor struct {
  71. RGB string `xml:"rgb,attr"`
  72. }
  73. // xlsxBorder directly maps the border element in the namespace
  74. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  75. // currently I have not checked it for completeness - it does as much
  76. // as I need.
  77. type xlsxBorder struct {
  78. Left xlsxLine `xml:"left"`
  79. Right xlsxLine `xml:"right"`
  80. Top xlsxLine `xml:"top"`
  81. Bottom xlsxLine `xml:"bottom"`
  82. }
  83. // xlsxLine directly maps the line style 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 xlsxLine struct {
  88. Style string `xml:"style,attr"`
  89. }
  90. // xlsxXf directly maps the xf element in the namespace
  91. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  92. // currently I have not checked it for completeness - it does as much
  93. // as I need.
  94. type xlsxXf struct {
  95. ApplyAlignment bool `xml:"applyAlignment,attr"`
  96. ApplyBorder bool `xml:"applyBorder,attr"`
  97. ApplyFont bool `xml:"applyFont,attr"`
  98. ApplyFill bool `xml:"applyFill,attr"`
  99. ApplyProtection bool `xml:"applyProtection,attr"`
  100. BorderId int `xml:"borderId,attr"`
  101. FillId int `xml:"fillId,attr"`
  102. FontId int `xml:"fontId,attr"`
  103. NumFmtId int `xml:"numFmtId,attr"`
  104. alignment xlsxAlignment `xml:"alignement"`
  105. }
  106. type xlsxAlignment struct {
  107. Horizontal string `xml:"horizontal,attr"`
  108. Indent int `xml:"indent,attr"`
  109. ShrinkToFit bool `xml:"shrinkToFit,attr"`
  110. TextRotation int `xml:"textRotation,attr"`
  111. Vertical string `xml:"vertical,attr"`
  112. WrapText bool `xml:"wrapText,attr"`
  113. }
  114. func (styles *xlsxStyles) getStyle(styleIndex int) (style Style) {
  115. var styleXf xlsxXf
  116. style = Style{}
  117. style.Border = Border{}
  118. style.Fill = Fill{}
  119. style.Font = Font{}
  120. xfCount := len(styles.CellXfs)
  121. if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
  122. xf := styles.CellXfs[styleIndex]
  123. // Google docs can produce output that has fewer
  124. // CellStyleXfs than CellXfs - this copes with that.
  125. if styleIndex < len(styles.CellStyleXfs) {
  126. styleXf = styles.CellStyleXfs[styleIndex]
  127. } else {
  128. styleXf = xlsxXf{}
  129. }
  130. style.ApplyBorder = xf.ApplyBorder || styleXf.ApplyBorder
  131. style.ApplyFill = xf.ApplyFill || styleXf.ApplyFill
  132. style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
  133. if xf.BorderId > -1 && xf.BorderId < len(styles.Borders) {
  134. style.Border.Left = styles.Borders[xf.BorderId].Left.Style
  135. style.Border.Right = styles.Borders[xf.BorderId].Right.Style
  136. style.Border.Top = styles.Borders[xf.BorderId].Top.Style
  137. style.Border.Bottom = styles.Borders[xf.BorderId].Bottom.Style
  138. }
  139. if xf.FillId > -1 && xf.FillId < len(styles.Fills) {
  140. xFill := styles.Fills[xf.FillId]
  141. style.Fill.PatternType = xFill.PatternFill.PatternType
  142. style.Fill.FgColor = xFill.PatternFill.FgColor.RGB
  143. style.Fill.BgColor = xFill.PatternFill.BgColor.RGB
  144. }
  145. if xf.FontId > -1 && xf.FontId < len(styles.Fonts) {
  146. xfont := styles.Fonts[xf.FontId]
  147. style.Font.Size, _ = strconv.Atoi(xfont.Sz.Val)
  148. style.Font.Name = xfont.Name.Val
  149. style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
  150. style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
  151. }
  152. }
  153. return style
  154. }
  155. func (styles *xlsxStyles) getNumberFormat(styleIndex int, numFmtRefTable map[int]xlsxNumFmt) string {
  156. if styles.CellXfs == nil {
  157. return ""
  158. }
  159. var numberFormat string = ""
  160. if styleIndex > -1 && styleIndex <= len(styles.CellXfs) {
  161. xf := styles.CellXfs[styleIndex]
  162. numFmt := numFmtRefTable[xf.NumFmtId]
  163. numberFormat = numFmt.FormatCode
  164. }
  165. return strings.ToLower(numberFormat)
  166. }
  167. func (styles *xlsxStyles) addFont(xFont xlsxFont) int {
  168. styles.Fonts = append(styles.Fonts, xFont)
  169. return len(styles.Fonts) - 1
  170. }
  171. func (styles *xlsxStyles) addFill(xFill xlsxFill) int {
  172. styles.Fills = append(styles.Fills, xFill)
  173. return len(styles.Fills) - 1
  174. }
  175. func (styles *xlsxStyles) addBorder(xBorder xlsxBorder) int {
  176. styles.Borders = append(styles.Borders, xBorder)
  177. return len(styles.Borders) - 1
  178. }
  179. func (styles *xlsxStyles) addCellStyleXf(xCellStyleXf xlsxXf) int {
  180. styles.CellStyleXfs = append(styles.CellStyleXfs, xCellStyleXf)
  181. return len(styles.CellStyleXfs) - 1
  182. }
  183. func (styles *xlsxStyles) addCellXf(xCellXf xlsxXf) int {
  184. styles.CellXfs = append(styles.CellXfs, xCellXf)
  185. return len(styles.CellXfs) - 1
  186. }