cell.go 4.3 KB

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