cell.go 4.3 KB

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