style.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package xlsx
  2. import "strconv"
  3. // Style is a high level structure intended to provide user access to
  4. // the contents of Style within an XLSX file.
  5. type Style struct {
  6. Border Border
  7. Fill Fill
  8. Font Font
  9. ApplyBorder bool
  10. ApplyFill bool
  11. ApplyFont bool
  12. ApplyAlignment bool
  13. Alignment Alignment
  14. NamedStyleIndex *int
  15. }
  16. // Return a new Style structure initialised with the default values.
  17. func NewStyle() *Style {
  18. return &Style{
  19. Alignment: *DefaultAlignment(),
  20. Border: *DefaultBorder(),
  21. Fill: *DefaultFill(),
  22. Font: *DefaultFont(),
  23. }
  24. }
  25. // Generate the underlying XLSX style elements that correspond to the Style.
  26. func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
  27. xFont = xlsxFont{}
  28. xFill = xlsxFill{}
  29. xBorder = xlsxBorder{}
  30. xCellStyleXf = xlsxXf{}
  31. xCellXf = xlsxXf{}
  32. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  33. xFont.Name.Val = style.Font.Name
  34. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  35. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  36. xFont.Color.RGB = style.Font.Color
  37. if style.Font.Bold {
  38. xFont.B = &xlsxVal{}
  39. } else {
  40. xFont.B = nil
  41. }
  42. if style.Font.Italic {
  43. xFont.I = &xlsxVal{}
  44. } else {
  45. xFont.I = nil
  46. }
  47. if style.Font.Underline {
  48. xFont.U = &xlsxVal{}
  49. } else {
  50. xFont.U = nil
  51. }
  52. xPatternFill := xlsxPatternFill{}
  53. xPatternFill.PatternType = style.Fill.PatternType
  54. xPatternFill.FgColor.RGB = style.Fill.FgColor
  55. xPatternFill.BgColor.RGB = style.Fill.BgColor
  56. xFill.PatternFill = xPatternFill
  57. xBorder.Left = xlsxLine{
  58. Style: style.Border.Left,
  59. Color: xlsxColor{RGB: style.Border.LeftColor},
  60. }
  61. xBorder.Right = xlsxLine{
  62. Style: style.Border.Right,
  63. Color: xlsxColor{RGB: style.Border.RightColor},
  64. }
  65. xBorder.Top = xlsxLine{
  66. Style: style.Border.Top,
  67. Color: xlsxColor{RGB: style.Border.TopColor},
  68. }
  69. xBorder.Bottom = xlsxLine{
  70. Style: style.Border.Bottom,
  71. Color: xlsxColor{RGB: style.Border.BottomColor},
  72. }
  73. xCellXf = makeXLSXCellElement()
  74. xCellXf.ApplyBorder = style.ApplyBorder
  75. xCellXf.ApplyFill = style.ApplyFill
  76. xCellXf.ApplyFont = style.ApplyFont
  77. xCellXf.ApplyAlignment = style.ApplyAlignment
  78. xCellStyleXf.ApplyBorder = style.ApplyBorder
  79. xCellStyleXf.ApplyFill = style.ApplyFill
  80. xCellStyleXf.ApplyFont = style.ApplyFont
  81. xCellStyleXf.ApplyAlignment = style.ApplyAlignment
  82. xCellStyleXf.NumFmtId = 0
  83. xCellStyleXf.Alignment.Horizontal = style.Alignment.Horizontal
  84. xCellStyleXf.Alignment.Indent = style.Alignment.Indent
  85. xCellStyleXf.Alignment.ShrinkToFit = style.Alignment.ShrinkToFit
  86. xCellStyleXf.Alignment.TextRotation = style.Alignment.TextRotation
  87. xCellStyleXf.Alignment.Vertical = style.Alignment.Vertical
  88. xCellStyleXf.Alignment.WrapText = style.Alignment.WrapText
  89. return
  90. }
  91. func makeXLSXCellElement() (xCellXf xlsxXf) {
  92. xCellXf.NumFmtId = 0
  93. return
  94. }
  95. // Border is a high level structure intended to provide user access to
  96. // the contents of Border Style within an Sheet.
  97. type Border struct {
  98. Left string
  99. LeftColor string
  100. Right string
  101. RightColor string
  102. Top string
  103. TopColor string
  104. Bottom string
  105. BottomColor string
  106. }
  107. func NewBorder(left, right, top, bottom string) *Border {
  108. return &Border{
  109. Left: left,
  110. LeftColor: "",
  111. Right: right,
  112. RightColor: "",
  113. Top: top,
  114. TopColor: "",
  115. Bottom: bottom,
  116. BottomColor: "",
  117. }
  118. }
  119. // Fill is a high level structure intended to provide user access to
  120. // the contents of background and foreground color index within an Sheet.
  121. type Fill struct {
  122. PatternType string
  123. BgColor string
  124. FgColor string
  125. }
  126. func NewFill(patternType, fgColor, bgColor string) *Fill {
  127. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  128. }
  129. type Font struct {
  130. Size int
  131. Name string
  132. Family int
  133. Charset int
  134. Color string
  135. Bold bool
  136. Italic bool
  137. Underline bool
  138. }
  139. func NewFont(size int, name string) *Font {
  140. return &Font{Size: size, Name: name}
  141. }
  142. type Alignment struct {
  143. Horizontal string
  144. Indent int
  145. ShrinkToFit bool
  146. TextRotation int
  147. Vertical string
  148. WrapText bool
  149. }
  150. var defaultFontSize int
  151. var defaultFontName string
  152. func init() {
  153. defaultFontSize = 12
  154. defaultFontName = "Verdana"
  155. }
  156. func SetDefaultFont(size int, name string) {
  157. defaultFontSize = size
  158. defaultFontName = name
  159. }
  160. func DefaultFont() *Font {
  161. return NewFont(defaultFontSize, defaultFontName)
  162. }
  163. func DefaultFill() *Fill {
  164. return NewFill("none", "FFFFFFFF", "00000000")
  165. }
  166. func DefaultBorder() *Border {
  167. return NewBorder("", "", "", "")
  168. }
  169. func DefaultAlignment() *Alignment {
  170. return &Alignment{
  171. Horizontal: "general",
  172. Indent: 0,
  173. ShrinkToFit: false,
  174. TextRotation: 0,
  175. Vertical: "bottom",
  176. WrapText: false,
  177. }
  178. }