cell.go 3.0 KB

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