cell.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // If bool, just return the value.
  138. if c.cellType == CellTypeBool {
  139. return c.Value == "1"
  140. }
  141. // If numeric, base it on a non-zero.
  142. if c.cellType == CellTypeNumeric {
  143. return c.Value != "0"
  144. }
  145. // Return whether there's an empty string.
  146. return c.Value != ""
  147. }
  148. // SetFormula sets the format string for a cell.
  149. func (c *Cell) SetFormula(formula string) {
  150. c.formula = formula
  151. c.cellType = CellTypeFormula
  152. }
  153. // Formula returns the formula string for the cell.
  154. func (c *Cell) Formula() string {
  155. return c.formula
  156. }
  157. // GetStyle returns the Style associated with a Cell
  158. func (c *Cell) GetStyle() *Style {
  159. return c.style
  160. }
  161. // SetStyle sets the style of a cell.
  162. func (c *Cell) SetStyle(style *Style) {
  163. c.style = style
  164. }
  165. // GetNumberFormat returns the number format string for a cell.
  166. func (c *Cell) GetNumberFormat() string {
  167. return c.numFmt
  168. }
  169. func (c *Cell) formatToTime(format string) string {
  170. f, err := strconv.ParseFloat(c.Value, 64)
  171. if err != nil {
  172. return err.Error()
  173. }
  174. return TimeFromExcelTime(f, c.date1904).Format(format)
  175. }
  176. func (c *Cell) formatToFloat(format string) string {
  177. f, err := strconv.ParseFloat(c.Value, 64)
  178. if err != nil {
  179. return err.Error()
  180. }
  181. return fmt.Sprintf(format, f)
  182. }
  183. func (c *Cell) formatToInt(format string) string {
  184. f, err := strconv.ParseFloat(c.Value, 64)
  185. if err != nil {
  186. return err.Error()
  187. }
  188. return fmt.Sprintf(format, int(f))
  189. }
  190. // FormattedValue returns the formatted version of the value.
  191. // If it's a string type, c.Value will just be returned. Otherwise,
  192. // it will attempt to apply Excel formatting to the value.
  193. func (c *Cell) FormattedValue() string {
  194. var numberFormat = c.GetNumberFormat()
  195. switch numberFormat {
  196. case "general", "@":
  197. return c.Value
  198. case "0", "#,##0":
  199. return c.formatToInt("%d")
  200. case "0.00", "#,##0.00":
  201. return c.formatToFloat("%.2f")
  202. case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
  203. f, err := strconv.ParseFloat(c.Value, 64)
  204. if err != nil {
  205. return err.Error()
  206. }
  207. if f < 0 {
  208. i := int(math.Abs(f))
  209. return fmt.Sprintf("(%d)", i)
  210. }
  211. i := int(f)
  212. return fmt.Sprintf("%d", i)
  213. case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
  214. f, err := strconv.ParseFloat(c.Value, 64)
  215. if err != nil {
  216. return err.Error()
  217. }
  218. if f < 0 {
  219. return fmt.Sprintf("(%.2f)", f)
  220. }
  221. return fmt.Sprintf("%.2f", f)
  222. case "0%":
  223. f, err := strconv.ParseFloat(c.Value, 64)
  224. if err != nil {
  225. return err.Error()
  226. }
  227. f = f * 100
  228. return fmt.Sprintf("%d%%", int(f))
  229. case "0.00%":
  230. f, err := strconv.ParseFloat(c.Value, 64)
  231. if err != nil {
  232. return err.Error()
  233. }
  234. f = f * 100
  235. return fmt.Sprintf("%.2f%%", f)
  236. case "0.00e+00", "##0.0e+0":
  237. return c.formatToFloat("%e")
  238. case "mm-dd-yy":
  239. return c.formatToTime("01-02-06")
  240. case "d-mmm-yy":
  241. return c.formatToTime("2-Jan-06")
  242. case "d-mmm":
  243. return c.formatToTime("2-Jan")
  244. case "mmm-yy":
  245. return c.formatToTime("Jan-06")
  246. case "h:mm am/pm":
  247. return c.formatToTime("3:04 pm")
  248. case "h:mm:ss am/pm":
  249. return c.formatToTime("3:04:05 pm")
  250. case "h:mm":
  251. return c.formatToTime("15:04")
  252. case "h:mm:ss":
  253. return c.formatToTime("15:04:05")
  254. case "m/d/yy h:mm":
  255. return c.formatToTime("1/2/06 15:04")
  256. case "mm:ss":
  257. return c.formatToTime("04:05")
  258. case "[h]:mm:ss":
  259. f, err := strconv.ParseFloat(c.Value, 64)
  260. if err != nil {
  261. return err.Error()
  262. }
  263. t := TimeFromExcelTime(f, c.date1904)
  264. if t.Hour() > 0 {
  265. return t.Format("15:04:05")
  266. }
  267. return t.Format("04:05")
  268. case "mmss.0":
  269. f, err := strconv.ParseFloat(c.Value, 64)
  270. if err != nil {
  271. return err.Error()
  272. }
  273. t := TimeFromExcelTime(f, c.date1904)
  274. return fmt.Sprintf("%0d%0d.%d", t.Minute(), t.Second(), t.Nanosecond()/1000)
  275. case "yyyy\\-mm\\-dd":
  276. return c.formatToTime("2006\\-01\\-02")
  277. case "dd/mm/yy":
  278. return c.formatToTime("02/01/06")
  279. case "hh:mm:ss":
  280. return c.formatToTime("15:04:05")
  281. case "dd/mm/yy\\ hh:mm":
  282. return c.formatToTime("02/01/06\\ 15:04")
  283. case "dd/mm/yyyy hh:mm:ss":
  284. return c.formatToTime("02/01/2006 15:04:05")
  285. case "yy-mm-dd":
  286. return c.formatToTime("06-01-02")
  287. case "d-mmm-yyyy":
  288. return c.formatToTime("2-Jan-2006")
  289. case "m/d/yy":
  290. return c.formatToTime("1/2/06")
  291. case "m/d/yyyy":
  292. return c.formatToTime("1/2/2006")
  293. case "dd-mmm-yyyy":
  294. return c.formatToTime("02-Jan-2006")
  295. case "dd/mm/yyyy":
  296. return c.formatToTime("02/01/2006")
  297. case "mm/dd/yy hh:mm am/pm":
  298. return c.formatToTime("01/02/06 03:04 pm")
  299. case "mm/dd/yyyy hh:mm:ss":
  300. return c.formatToTime("01/02/2006 15:04:05")
  301. case "yyyy-mm-dd hh:mm:ss":
  302. return c.formatToTime("2006-01-02 15:04:05")
  303. }
  304. return c.Value
  305. }