style.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  13. // Return a new Style structure initialised with the default values.
  14. func NewStyle() *Style {
  15. return &Style{Font: *NewFont(12, "Verdana")}
  16. }
  17. // Generate the underlying XLSX style elements that correspond to the Style.
  18. func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
  19. xFont = xlsxFont{}
  20. xFill = xlsxFill{}
  21. xBorder = xlsxBorder{}
  22. xCellStyleXf = xlsxXf{}
  23. xCellXf = xlsxXf{}
  24. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  25. xFont.Name.Val = style.Font.Name
  26. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  27. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  28. xFont.Color.RGB = style.Font.Color
  29. xPatternFill := xlsxPatternFill{}
  30. xPatternFill.PatternType = style.Fill.PatternType
  31. xPatternFill.FgColor.RGB = style.Fill.FgColor
  32. xPatternFill.BgColor.RGB = style.Fill.BgColor
  33. xFill.PatternFill = xPatternFill
  34. xBorder.Left = xlsxLine{Style: style.Border.Left}
  35. xBorder.Right = xlsxLine{Style: style.Border.Right}
  36. xBorder.Top = xlsxLine{Style: style.Border.Top}
  37. xBorder.Bottom = xlsxLine{Style: style.Border.Bottom}
  38. xCellXf.ApplyBorder = style.ApplyBorder
  39. xCellXf.ApplyFill = style.ApplyFill
  40. xCellXf.ApplyFont = style.ApplyFont
  41. xCellStyleXf.ApplyBorder = style.ApplyBorder
  42. xCellStyleXf.ApplyFill = style.ApplyFill
  43. xCellStyleXf.ApplyFont = style.ApplyFont
  44. return
  45. }
  46. // Border is a high level structure intended to provide user access to
  47. // the contents of Border Style within an Sheet.
  48. type Border struct {
  49. Left string
  50. Right string
  51. Top string
  52. Bottom string
  53. }
  54. func NewBorder(left, right, top, bottom string) *Border {
  55. return &Border{Left: left, Right: right, Top: top, Bottom: bottom}
  56. }
  57. // Fill is a high level structure intended to provide user access to
  58. // the contents of background and foreground color index within an Sheet.
  59. type Fill struct {
  60. PatternType string
  61. BgColor string
  62. FgColor string
  63. }
  64. func NewFill(patternType, fgColor, bgColor string) *Fill {
  65. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  66. }
  67. type Font struct {
  68. Size int
  69. Name string
  70. Family int
  71. Charset int
  72. Color string
  73. }
  74. func NewFont(size int, name string) *Font {
  75. return &Font{Size: size, Name: name}
  76. }