style.go 4.7 KB

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