cell.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package xlsx
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. )
  7. // Cell is a high level structure intended to provide user access to
  8. // the contents of Cell within an xlsx.Row.
  9. type Cell struct {
  10. Row Row
  11. Value string
  12. style Style
  13. numFmt string
  14. date1904 bool
  15. Hidden bool
  16. }
  17. // CellInterface defines the public API of the Cell.
  18. type CellInterface interface {
  19. String() string
  20. FormattedValue() string
  21. }
  22. func NewCell(r Row) *Cell {
  23. return &Cell{style: *NewStyle(), Row: r}
  24. }
  25. // String returns the value of a Cell as a string.
  26. func (c *Cell) String() string {
  27. return c.FormattedValue()
  28. }
  29. // GetStyle returns the Style associated with a Cell
  30. func (c *Cell) GetStyle() Style {
  31. return c.style
  32. }
  33. // SetStyle sets the style of a cell.
  34. func (c *Cell) SetStyle(style Style) {
  35. c.style = style
  36. }
  37. // The number format string is returnable from a cell.
  38. func (c *Cell) GetNumberFormat() string {
  39. return c.numFmt
  40. }
  41. func (c *Cell) formatToTime(format string) string {
  42. f, err := strconv.ParseFloat(c.Value, 64)
  43. if err != nil {
  44. return err.Error()
  45. }
  46. return TimeFromExcelTime(f, c.date1904).Format(format)
  47. }
  48. func (c *Cell) formatToFloat(format string) string {
  49. f, err := strconv.ParseFloat(c.Value, 64)
  50. if err != nil {
  51. return err.Error()
  52. }
  53. return fmt.Sprintf(format, f)
  54. }
  55. func (c *Cell) formatToInt(format string) string {
  56. f, err := strconv.ParseFloat(c.Value, 64)
  57. if err != nil {
  58. return err.Error()
  59. }
  60. return fmt.Sprintf(format, int(f))
  61. }
  62. // Return the formatted version of the value.
  63. func (c *Cell) FormattedValue() string {
  64. var numberFormat string = c.GetNumberFormat()
  65. switch numberFormat {
  66. case "general":
  67. return c.Value
  68. case "0", "#,##0":
  69. return c.formatToInt("%d")
  70. case "0.00", "#,##0.00", "@":
  71. return c.formatToFloat("%.2f")
  72. case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
  73. f, err := strconv.ParseFloat(c.Value, 64)
  74. if err != nil {
  75. return err.Error()
  76. }
  77. if f < 0 {
  78. i := int(math.Abs(f))
  79. return fmt.Sprintf("(%d)", i)
  80. }
  81. i := int(f)
  82. return fmt.Sprintf("%d", i)
  83. case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
  84. f, err := strconv.ParseFloat(c.Value, 64)
  85. if err != nil {
  86. return err.Error()
  87. }
  88. if f < 0 {
  89. return fmt.Sprintf("(%.2f)", f)
  90. }
  91. return fmt.Sprintf("%.2f", f)
  92. case "0%":
  93. f, err := strconv.ParseFloat(c.Value, 64)
  94. if err != nil {
  95. return err.Error()
  96. }
  97. f = f * 100
  98. return fmt.Sprintf("%d%%", int(f))
  99. case "0.00%":
  100. f, err := strconv.ParseFloat(c.Value, 64)
  101. if err != nil {
  102. return err.Error()
  103. }
  104. f = f * 100
  105. return fmt.Sprintf("%.2f%%", f)
  106. case "0.00e+00", "##0.0e+0":
  107. return c.formatToFloat("%e")
  108. case "mm-dd-yy":
  109. return c.formatToTime("01-02-06")
  110. case "d-mmm-yy":
  111. return c.formatToTime("2-Jan-06")
  112. case "d-mmm":
  113. return c.formatToTime("2-Jan")
  114. case "mmm-yy":
  115. return c.formatToTime("Jan-06")
  116. case "h:mm am/pm":
  117. return c.formatToTime("3:04 pm")
  118. case "h:mm:ss am/pm":
  119. return c.formatToTime("3:04:05 pm")
  120. case "h:mm":
  121. return c.formatToTime("15:04")
  122. case "h:mm:ss":
  123. return c.formatToTime("15:04:05")
  124. case "m/d/yy h:mm":
  125. return c.formatToTime("1/2/06 15:04")
  126. case "mm:ss":
  127. return c.formatToTime("04:05")
  128. case "[h]:mm:ss":
  129. f, err := strconv.ParseFloat(c.Value, 64)
  130. if err != nil {
  131. return err.Error()
  132. }
  133. t := TimeFromExcelTime(f, c.date1904)
  134. if t.Hour() > 0 {
  135. return t.Format("15:04:05")
  136. }
  137. return t.Format("04:05")
  138. case "mmss.0":
  139. f, err := strconv.ParseFloat(c.Value, 64)
  140. if err != nil {
  141. return err.Error()
  142. }
  143. t := TimeFromExcelTime(f, c.date1904)
  144. return fmt.Sprintf("%0d%0d.%d", t.Minute(), t.Second(), t.Nanosecond()/1000)
  145. case "yyyy\\-mm\\-dd":
  146. return c.formatToTime("2006\\-01\\-02")
  147. case "dd/mm/yy":
  148. return c.formatToTime("02/01/06")
  149. case "hh:mm:ss":
  150. return c.formatToTime("15:04:05")
  151. case "dd/mm/yy\\ hh:mm":
  152. return c.formatToTime("02/01/06\\ 15:04")
  153. case "dd/mm/yyyy hh:mm:ss":
  154. return c.formatToTime("02/01/2006 15:04:05")
  155. case "yy-mm-dd":
  156. return c.formatToTime("06-01-02")
  157. case "d-mmm-yyyy":
  158. return c.formatToTime("2-Jan-2006")
  159. case "m/d/yy":
  160. return c.formatToTime("1/2/06")
  161. case "m/d/yyyy":
  162. return c.formatToTime("1/2/2006")
  163. case "dd-mmm-yyyy":
  164. return c.formatToTime("02-Jan-2006")
  165. case "dd/mm/yyyy":
  166. return c.formatToTime("02/01/2006")
  167. case "mm/dd/yy hh:mm am/pm":
  168. return c.formatToTime("01/02/06 03:04 pm")
  169. case "mm/dd/yyyy hh:mm:ss":
  170. return c.formatToTime("01/02/2006 15:04:05")
  171. case "yyyy-mm-dd hh:mm:ss":
  172. return c.formatToTime("2006-01-02 15:04:05")
  173. }
  174. return c.Value
  175. }