cell.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Border is a high level structure intended to provide user access to
  13. // the contents of Border Style within an Sheet.
  14. type Border struct {
  15. Left string
  16. Right string
  17. Top string
  18. Bottom string
  19. }
  20. // Fill is a high level structure intended to provide user access to
  21. // the contents of background and foreground color index within an Sheet.
  22. type Fill struct {
  23. PatternType string
  24. BgColor string
  25. FgColor string
  26. }
  27. type Font struct {
  28. Size int
  29. Name string
  30. Family int
  31. Charset int
  32. }
  33. // Cell is a high level structure intended to provide user access to
  34. // the contents of Cell within an xlsx.Row.
  35. type Cell struct {
  36. Value string
  37. styleIndex int
  38. styles *xlsxStyles
  39. }
  40. // CellInterface defines the public API of the Cell.
  41. type CellInterface interface {
  42. String() string
  43. }
  44. // String returns the value of a Cell as a string.
  45. func (c *Cell) String() string {
  46. return c.Value
  47. }
  48. // GetStyle returns the Style associated with a Cell
  49. func (c *Cell) GetStyle() *Style {
  50. style := &Style{}
  51. if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
  52. xf := c.styles.CellXfs[c.styleIndex-1]
  53. if xf.ApplyBorder {
  54. var border Border
  55. border.Left = c.styles.Borders[xf.BorderId].Left.Style
  56. border.Right = c.styles.Borders[xf.BorderId].Right.Style
  57. border.Top = c.styles.Borders[xf.BorderId].Top.Style
  58. border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
  59. style.Border = border
  60. }
  61. if xf.ApplyFill {
  62. var fill Fill
  63. fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
  64. fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
  65. fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
  66. style.Fill = fill
  67. }
  68. if xf.ApplyFont {
  69. font := c.styles.Fonts[xf.FontId]
  70. style.Font = Font{}
  71. style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
  72. style.Font.Name = font.Name.Val
  73. style.Font.Family, _ = strconv.Atoi(font.Family.Val)
  74. style.Font.Charset, _ = strconv.Atoi(font.Charset.Val)
  75. }
  76. }
  77. return style
  78. }