cell.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package xlsx
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. )
  7. // CellType is an int type for storing metadata about the data type in the cell.
  8. type CellType int
  9. // Known types for cell values.
  10. const (
  11. CellTypeString CellType = iota
  12. CellTypeFormula
  13. CellTypeNumeric
  14. CellTypeBool
  15. CellTypeInline
  16. CellTypeError
  17. )
  18. // Cell is a high level structure intended to provide user access to
  19. // the contents of Cell within an xlsx.Row.
  20. type Cell struct {
  21. Row *Row
  22. Value string
  23. formula string
  24. style *Style
  25. numFmt string
  26. date1904 bool
  27. Hidden bool
  28. HMerge int
  29. VMerge int
  30. cellType CellType
  31. }
  32. // CellInterface defines the public API of the Cell.
  33. type CellInterface interface {
  34. String() string
  35. FormattedValue() string
  36. }
  37. // NewCell creates a cell and adds it to a row.
  38. func NewCell(r *Row) *Cell {
  39. return &Cell{style: NewStyle(), Row: r}
  40. }
  41. // Merge with other cells, horizontally and/or vertically.
  42. func (c *Cell) Merge(hcells, vcells int) {
  43. c.HMerge = hcells
  44. c.VMerge = vcells
  45. }
  46. // Type returns the CellType of a cell. See CellType constants for more details.
  47. func (c *Cell) Type() CellType {
  48. return c.cellType
  49. }
  50. // SetString sets the value of a cell to a string.
  51. func (c *Cell) SetString(s string) {
  52. c.Value = s
  53. c.formula = ""
  54. c.cellType = CellTypeString
  55. }
  56. // String returns the value of a Cell as a string.
  57. func (c *Cell) String() string {
  58. return c.FormattedValue()
  59. }
  60. // SetFloat sets the value of a cell to a float.
  61. func (c *Cell) SetFloat(n float64) {
  62. c.SetFloatWithFormat(n, "0.00e+00")
  63. }
  64. /*
  65. The following are samples of format samples.
  66. * "0.00e+00"
  67. * "0", "#,##0"
  68. * "0.00", "#,##0.00", "@"
  69. * "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)"
  70. * "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)"
  71. * "0%", "0.00%"
  72. * "0.00e+00", "##0.0e+0"
  73. */
  74. // SetFloatWithFormat sets the value of a cell to a float and applies
  75. // formatting to the cell.
  76. func (c *Cell) SetFloatWithFormat(n float64, format string) {
  77. // tmp value. final value is formatted by FormattedValue() method
  78. c.Value = fmt.Sprintf("%e", n)
  79. c.numFmt = format
  80. c.Value = c.FormattedValue()
  81. c.formula = ""
  82. c.cellType = CellTypeNumeric
  83. }
  84. // Float returns the value of cell as a number.
  85. func (c *Cell) Float() (float64, error) {
  86. f, err := strconv.ParseFloat(c.Value, 64)
  87. if err != nil {
  88. return math.NaN(), err
  89. }
  90. return f, nil
  91. }
  92. // SetInt64 sets a cell's value to a 64-bit integer.
  93. func (c *Cell) SetInt64(n int64) {
  94. c.Value = fmt.Sprintf("%d", n)
  95. c.numFmt = "0"
  96. c.formula = ""
  97. c.cellType = CellTypeNumeric
  98. }
  99. // Int64 returns the value of cell as 64-bit integer.
  100. func (c *Cell) Int64() (int64, error) {
  101. f, err := strconv.ParseInt(c.Value, 10, 64)
  102. if err != nil {
  103. return -1, err
  104. }
  105. return f, nil
  106. }
  107. // SetInt sets a cell's value to an integer.
  108. func (c *Cell) SetInt(n int) {
  109. c.Value = fmt.Sprintf("%d", n)
  110. c.numFmt = "0"
  111. c.formula = ""
  112. c.cellType = CellTypeNumeric
  113. }
  114. // Int returns the value of cell as integer.
  115. // Has max 53 bits of precision
  116. // See: float64(int64(math.MaxInt))
  117. func (c *Cell) Int() (int, error) {
  118. f, err := strconv.ParseFloat(c.Value, 64)
  119. if err != nil {
  120. return -1, err
  121. }
  122. return int(f), nil
  123. }
  124. // SetBool sets a cell's value to a boolean.
  125. func (c *Cell) SetBool(b bool) {
  126. if b {
  127. c.Value = "1"
  128. } else {
  129. c.Value = "0"
  130. }
  131. c.cellType = CellTypeBool
  132. }
  133. // Bool returns a boolean from a cell's value.
  134. // TODO: Determine if the current return value is
  135. // appropriate for types other than CellTypeBool.
  136. func (c *Cell) Bool() bool {
  137. return c.Value == "1"
  138. }
  139. // SetFormula sets the format string for a cell.
  140. func (c *Cell) SetFormula(formula string) {
  141. c.formula = formula
  142. c.cellType = CellTypeFormula
  143. }
  144. // Formula returns the formula string for the cell.
  145. func (c *Cell) Formula() string {
  146. return c.formula
  147. }
  148. // GetStyle returns the Style associated with a Cell
  149. func (c *Cell) GetStyle() *Style {
  150. return c.style
  151. }
  152. // SetStyle sets the style of a cell.
  153. func (c *Cell) SetStyle(style *Style) {
  154. c.style = style
  155. }
  156. // GetNumberFormat returns the number format string for a cell.
  157. func (c *Cell) GetNumberFormat() string {
  158. return c.numFmt
  159. }
  160. func (c *Cell) formatToTime(format string) string {
  161. f, err := strconv.ParseFloat(c.Value, 64)
  162. if err != nil {
  163. return err.Error()
  164. }
  165. return TimeFromExcelTime(f, c.date1904).Format(format)
  166. }
  167. func (c *Cell) formatToFloat(format string) string {
  168. f, err := strconv.ParseFloat(c.Value, 64)
  169. if err != nil {
  170. return err.Error()
  171. }
  172. return fmt.Sprintf(format, f)
  173. }
  174. func (c *Cell) formatToInt(format string) string {
  175. f, err := strconv.ParseFloat(c.Value, 64)
  176. if err != nil {
  177. return err.Error()
  178. }
  179. return fmt.Sprintf(format, int(f))
  180. }
  181. // FormattedValue returns the formatted version of the value.
  182. // If it's a string type, c.Value will just be returned. Otherwise,
  183. // it will attempt to apply Excel formatting to the value.
  184. func (c *Cell) FormattedValue() string {
  185. var numberFormat = c.GetNumberFormat()
  186. switch numberFormat {
  187. case "general", "@":
  188. return c.Value
  189. case "0", "#,##0":
  190. return c.formatToInt("%d")
  191. case "0.00", "#,##0.00":
  192. return c.formatToFloat("%.2f")
  193. case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
  194. f, err := strconv.ParseFloat(c.Value, 64)
  195. if err != nil {
  196. return err.Error()
  197. }
  198. if f < 0 {
  199. i := int(math.Abs(f))
  200. return fmt.Sprintf("(%d)", i)
  201. }
  202. i := int(f)
  203. return fmt.Sprintf("%d", i)
  204. case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
  205. f, err := strconv.ParseFloat(c.Value, 64)
  206. if err != nil {
  207. return err.Error()
  208. }
  209. if f < 0 {
  210. return fmt.Sprintf("(%.2f)", f)
  211. }
  212. return fmt.Sprintf("%.2f", f)
  213. case "0%":
  214. f, err := strconv.ParseFloat(c.Value, 64)
  215. if err != nil {
  216. return err.Error()
  217. }
  218. f = f * 100
  219. return fmt.Sprintf("%d%%", int(f))
  220. case "0.00%":
  221. f, err := strconv.ParseFloat(c.Value, 64)
  222. if err != nil {
  223. return err.Error()
  224. }
  225. f = f * 100
  226. return fmt.Sprintf("%.2f%%", f)
  227. case "0.00e+00", "##0.0e+0":
  228. return c.formatToFloat("%e")
  229. case "mm-dd-yy":
  230. return c.formatToTime("01-02-06")
  231. case "d-mmm-yy":
  232. return c.formatToTime("2-Jan-06")
  233. case "d-mmm":
  234. return c.formatToTime("2-Jan")
  235. case "mmm-yy":
  236. return c.formatToTime("Jan-06")
  237. case "h:mm am/pm":
  238. return c.formatToTime("3:04 pm")
  239. case "h:mm:ss am/pm":
  240. return c.formatToTime("3:04:05 pm")
  241. case "h:mm":
  242. return c.formatToTime("15:04")
  243. case "h:mm:ss":
  244. return c.formatToTime("15:04:05")
  245. case "m/d/yy h:mm":
  246. return c.formatToTime("1/2/06 15:04")
  247. case "mm:ss":
  248. return c.formatToTime("04:05")
  249. case "[h]:mm:ss":
  250. f, err := strconv.ParseFloat(c.Value, 64)
  251. if err != nil {
  252. return err.Error()
  253. }
  254. t := TimeFromExcelTime(f, c.date1904)
  255. if t.Hour() > 0 {
  256. return t.Format("15:04:05")
  257. }
  258. return t.Format("04:05")
  259. case "mmss.0":
  260. f, err := strconv.ParseFloat(c.Value, 64)
  261. if err != nil {
  262. return err.Error()
  263. }
  264. t := TimeFromExcelTime(f, c.date1904)
  265. return fmt.Sprintf("%0d%0d.%d", t.Minute(), t.Second(), t.Nanosecond()/1000)
  266. case "yyyy\\-mm\\-dd":
  267. return c.formatToTime("2006\\-01\\-02")
  268. case "dd/mm/yy":
  269. return c.formatToTime("02/01/06")
  270. case "hh:mm:ss":
  271. return c.formatToTime("15:04:05")
  272. case "dd/mm/yy\\ hh:mm":
  273. return c.formatToTime("02/01/06\\ 15:04")
  274. case "dd/mm/yyyy hh:mm:ss":
  275. return c.formatToTime("02/01/2006 15:04:05")
  276. case "yy-mm-dd":
  277. return c.formatToTime("06-01-02")
  278. case "d-mmm-yyyy":
  279. return c.formatToTime("2-Jan-2006")
  280. case "m/d/yy":
  281. return c.formatToTime("1/2/06")
  282. case "m/d/yyyy":
  283. return c.formatToTime("1/2/2006")
  284. case "dd-mmm-yyyy":
  285. return c.formatToTime("02-Jan-2006")
  286. case "dd/mm/yyyy":
  287. return c.formatToTime("02/01/2006")
  288. case "mm/dd/yy hh:mm am/pm":
  289. return c.formatToTime("01/02/06 03:04 pm")
  290. case "mm/dd/yyyy hh:mm:ss":
  291. return c.formatToTime("01/02/2006 15:04:05")
  292. case "yyyy-mm-dd hh:mm:ss":
  293. return c.formatToTime("2006-01-02 15:04:05")
  294. }
  295. return c.Value
  296. }