cell.go 4.4 KB

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