cell.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package xlsx
  2. import (
  3. "strconv"
  4. )
  5. // Style is a high level structure intended to provide user access to
  6. // the contents of Style within an XLSX file.
  7. type Style struct {
  8. Border Border
  9. Fill Fill
  10. Font Font
  11. }
  12. func NewStyle() *Style {
  13. return &Style{}
  14. }
  15. func (s *Style) SetFont(font Font) {
  16. s.Font = font
  17. }
  18. func (s *Style) SetFill(fill Fill) {
  19. s.Fill = fill
  20. }
  21. // Border is a high level structure intended to provide user access to
  22. // the contents of Border Style within an Sheet.
  23. type Border struct {
  24. Left string
  25. Right string
  26. Top string
  27. Bottom string
  28. }
  29. // Fill is a high level structure intended to provide user access to
  30. // the contents of background and foreground color index within an Sheet.
  31. type Fill struct {
  32. PatternType string
  33. BgColor string
  34. FgColor string
  35. }
  36. func NewFill(patternType, fgColor, bgColor string) *Fill {
  37. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  38. }
  39. type Font struct {
  40. Size int
  41. Name string
  42. Family int
  43. Charset int
  44. }
  45. func NewFont(size int, name string) *Font {
  46. return &Font{Size: size, Name: name}
  47. }
  48. // Cell is a high level structure intended to provide user access to
  49. // the contents of Cell within an xlsx.Row.
  50. type Cell struct {
  51. Value string
  52. styleIndex int
  53. styles *xlsxStyles
  54. }
  55. // CellInterface defines the public API of the Cell.
  56. type CellInterface interface {
  57. String() string
  58. }
  59. // String returns the value of a Cell as a string.
  60. func (c *Cell) String() string {
  61. return c.Value
  62. }
  63. // GetStyle returns the Style associated with a Cell
  64. func (c *Cell) GetStyle() *Style {
  65. style := &Style{}
  66. if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
  67. xf := c.styles.CellXfs[c.styleIndex-1]
  68. if xf.ApplyBorder {
  69. var border Border
  70. border.Left = c.styles.Borders[xf.BorderId].Left.Style
  71. border.Right = c.styles.Borders[xf.BorderId].Right.Style
  72. border.Top = c.styles.Borders[xf.BorderId].Top.Style
  73. border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
  74. style.Border = border
  75. }
  76. if xf.ApplyFill {
  77. var fill Fill
  78. fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
  79. fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
  80. fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
  81. style.Fill = fill
  82. }
  83. if xf.ApplyFont {
  84. font := c.styles.Fonts[xf.FontId]
  85. style.Font = Font{}
  86. style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
  87. style.Font.Name = font.Name.Val
  88. style.Font.Family, _ = strconv.Atoi(font.Family.Val)
  89. style.Font.Charset, _ = strconv.Atoi(font.Charset.Val)
  90. }
  91. }
  92. return style
  93. }
  94. func (c *Cell) SetStyle(style *Style) int {
  95. if c.styles == nil {
  96. c.styles = &xlsxStyles{}
  97. }
  98. index := len(c.styles.Fonts)
  99. xFont := xlsxFont{}
  100. xFill := xlsxFill{}
  101. xBorder := xlsxBorder{}
  102. xCellStyleXf := xlsxXf{}
  103. xCellXf := xlsxXf{}
  104. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  105. xFont.Name.Val = style.Font.Name
  106. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  107. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  108. xPatternFill := xlsxPatternFill{}
  109. xPatternFill.PatternType = style.Fill.PatternType
  110. xPatternFill.FgColor.RGB = style.Fill.FgColor
  111. xPatternFill.BgColor.RGB = style.Fill.BgColor
  112. xFill.PatternFill = xPatternFill
  113. c.styles.Fonts = append(c.styles.Fonts, xFont)
  114. c.styles.Fills = append(c.styles.Fills, xFill)
  115. c.styles.Borders = append(c.styles.Borders, xBorder)
  116. c.styles.CellStyleXfs = append(c.styles.CellStyleXfs, xCellStyleXf)
  117. c.styles.CellXfs = append(c.styles.CellXfs, xCellXf)
  118. return index
  119. }