style.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, xCellXf xlsxXf) {
  27. xFont = xlsxFont{}
  28. xFill = xlsxFill{}
  29. xBorder = xlsxBorder{}
  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. if style.NamedStyleIndex != nil {
  78. xCellXf.XfId = style.NamedStyleIndex
  79. }
  80. return
  81. }
  82. func makeXLSXCellElement() (xCellXf xlsxXf) {
  83. xCellXf.NumFmtId = 0
  84. return
  85. }
  86. // Border is a high level structure intended to provide user access to
  87. // the contents of Border Style within an Sheet.
  88. type Border struct {
  89. Left string
  90. LeftColor string
  91. Right string
  92. RightColor string
  93. Top string
  94. TopColor string
  95. Bottom string
  96. BottomColor string
  97. }
  98. func NewBorder(left, right, top, bottom string) *Border {
  99. return &Border{
  100. Left: left,
  101. LeftColor: "",
  102. Right: right,
  103. RightColor: "",
  104. Top: top,
  105. TopColor: "",
  106. Bottom: bottom,
  107. BottomColor: "",
  108. }
  109. }
  110. // Fill is a high level structure intended to provide user access to
  111. // the contents of background and foreground color index within an Sheet.
  112. type Fill struct {
  113. PatternType string
  114. BgColor string
  115. FgColor string
  116. }
  117. func NewFill(patternType, fgColor, bgColor string) *Fill {
  118. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  119. }
  120. type Font struct {
  121. Size int
  122. Name string
  123. Family int
  124. Charset int
  125. Color string
  126. Bold bool
  127. Italic bool
  128. Underline bool
  129. }
  130. func NewFont(size int, name string) *Font {
  131. return &Font{Size: size, Name: name}
  132. }
  133. type Alignment struct {
  134. Horizontal string
  135. Indent int
  136. ShrinkToFit bool
  137. TextRotation int
  138. Vertical string
  139. WrapText bool
  140. }
  141. var defaultFontSize int
  142. var defaultFontName string
  143. func init() {
  144. defaultFontSize = 12
  145. defaultFontName = "Verdana"
  146. }
  147. func SetDefaultFont(size int, name string) {
  148. defaultFontSize = size
  149. defaultFontName = name
  150. }
  151. func DefaultFont() *Font {
  152. return NewFont(defaultFontSize, defaultFontName)
  153. }
  154. func DefaultFill() *Fill {
  155. return NewFill("none", "FFFFFFFF", "00000000")
  156. }
  157. func DefaultBorder() *Border {
  158. return NewBorder("", "", "", "")
  159. }
  160. func DefaultAlignment() *Alignment {
  161. return &Alignment{
  162. Horizontal: "general",
  163. Indent: 0,
  164. ShrinkToFit: false,
  165. TextRotation: 0,
  166. Vertical: "bottom",
  167. WrapText: false,
  168. }
  169. }