style.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Right: right,
  102. Top: top,
  103. Bottom: bottom,
  104. }
  105. }
  106. // Fill is a high level structure intended to provide user access to
  107. // the contents of background and foreground color index within an Sheet.
  108. type Fill struct {
  109. PatternType string
  110. BgColor string
  111. FgColor string
  112. }
  113. func NewFill(patternType, fgColor, bgColor string) *Fill {
  114. return &Fill{
  115. PatternType: patternType,
  116. FgColor: fgColor,
  117. BgColor: bgColor,
  118. }
  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 = 12
  142. var defaultFontName = "Verdana"
  143. func SetDefaultFont(size int, name string) {
  144. defaultFontSize = size
  145. defaultFontName = name
  146. }
  147. func DefaultFont() *Font {
  148. return NewFont(defaultFontSize, defaultFontName)
  149. }
  150. func DefaultFill() *Fill {
  151. return NewFill("none", "FFFFFFFF", "00000000")
  152. }
  153. func DefaultBorder() *Border {
  154. return NewBorder("none", "none", "none", "none")
  155. }
  156. func DefaultAlignment() *Alignment {
  157. return &Alignment{
  158. Horizontal: "general",
  159. Vertical: "bottom",
  160. }
  161. }