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. c.Value = strconv.FormatFloat(n, 'e', -1, 64)
  78. c.numFmt = format
  79. c.formula = ""
  80. c.cellType = CellTypeNumeric
  81. }
  82. // Float returns the value of cell as a number.
  83. func (c *Cell) Float() (float64, error) {
  84. f, err := strconv.ParseFloat(c.Value, 64)
  85. if err != nil {
  86. return math.NaN(), err
  87. }
  88. return f, nil
  89. }
  90. // SetInt64 sets a cell's value to a 64-bit integer.
  91. func (c *Cell) SetInt64(n int64) {
  92. c.Value = fmt.Sprintf("%d", n)
  93. c.numFmt = "0"
  94. c.formula = ""
  95. c.cellType = CellTypeNumeric
  96. }
  97. // Int64 returns the value of cell as 64-bit integer.
  98. func (c *Cell) Int64() (int64, error) {
  99. f, err := strconv.ParseInt(c.Value, 10, 64)
  100. if err != nil {
  101. return -1, err
  102. }
  103. return f, nil
  104. }
  105. // SetInt sets a cell's value to an integer.
  106. func (c *Cell) SetInt(n int) {
  107. c.Value = fmt.Sprintf("%d", n)
  108. c.numFmt = "0"
  109. c.formula = ""
  110. c.cellType = CellTypeNumeric
  111. }
  112. // Int returns the value of cell as integer.
  113. // Has max 53 bits of precision
  114. // See: float64(int64(math.MaxInt))
  115. func (c *Cell) Int() (int, error) {
  116. f, err := strconv.ParseFloat(c.Value, 64)
  117. if err != nil {
  118. return -1, err
  119. }
  120. return int(f), nil
  121. }
  122. // SetBool sets a cell's value to a boolean.
  123. func (c *Cell) SetBool(b bool) {
  124. if b {
  125. c.Value = "1"
  126. } else {
  127. c.Value = "0"
  128. }
  129. c.cellType = CellTypeBool
  130. }
  131. // Bool returns a boolean from a cell's value.
  132. // TODO: Determine if the current return value is
  133. // appropriate for types other than CellTypeBool.
  134. func (c *Cell) Bool() bool {
  135. // If bool, just return the value.
  136. if c.cellType == CellTypeBool {
  137. return c.Value == "1"
  138. }
  139. // If numeric, base it on a non-zero.
  140. if c.cellType == CellTypeNumeric {
  141. return c.Value != "0"
  142. }
  143. // Return whether there's an empty string.
  144. return c.Value != ""
  145. }
  146. // SetFormula sets the format string for a cell.
  147. func (c *Cell) SetFormula(formula string) {
  148. c.formula = formula
  149. c.cellType = CellTypeFormula
  150. }
  151. // Formula returns the formula string for the cell.
  152. func (c *Cell) Formula() string {
  153. return c.formula
  154. }
  155. // GetStyle returns the Style associated with a Cell
  156. func (c *Cell) GetStyle() *Style {
  157. return c.style
  158. }
  159. // SetStyle sets the style of a cell.
  160. func (c *Cell) SetStyle(style *Style) {
  161. c.style = style
  162. }
  163. // GetNumberFormat returns the number format string for a cell.
  164. func (c *Cell) GetNumberFormat() string {
  165. return c.numFmt
  166. }
  167. func (c *Cell) formatToTime(format string) string {
  168. f, err := strconv.ParseFloat(c.Value, 64)
  169. if err != nil {
  170. return err.Error()
  171. }
  172. return TimeFromExcelTime(f, c.date1904).Format(format)
  173. }
  174. func (c *Cell) formatToFloat(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, f)
  180. }
  181. func (c *Cell) formatToInt(format string) string {
  182. f, err := strconv.ParseFloat(c.Value, 64)
  183. if err != nil {
  184. return err.Error()
  185. }
  186. return fmt.Sprintf(format, int(f))
  187. }
  188. // FormattedValue returns the formatted version of the value.
  189. // If it's a string type, c.Value will just be returned. Otherwise,
  190. // it will attempt to apply Excel formatting to the value.
  191. func (c *Cell) FormattedValue() string {
  192. var numberFormat = c.GetNumberFormat()
  193. switch numberFormat {
  194. case "general", "@":
  195. return c.Value
  196. case "0", "#,##0":
  197. return c.formatToInt("%d")
  198. case "0.00", "#,##0.00":
  199. return c.formatToFloat("%.2f")
  200. case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
  201. f, err := strconv.ParseFloat(c.Value, 64)
  202. if err != nil {
  203. return err.Error()
  204. }
  205. if f < 0 {
  206. i := int(math.Abs(f))
  207. return fmt.Sprintf("(%d)", i)
  208. }
  209. i := int(f)
  210. return fmt.Sprintf("%d", i)
  211. case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
  212. f, err := strconv.ParseFloat(c.Value, 64)
  213. if err != nil {
  214. return err.Error()
  215. }
  216. if f < 0 {
  217. return fmt.Sprintf("(%.2f)", f)
  218. }
  219. return fmt.Sprintf("%.2f", f)
  220. case "0%":
  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("%d%%", int(f))
  227. case "0.00%":
  228. f, err := strconv.ParseFloat(c.Value, 64)
  229. if err != nil {
  230. return err.Error()
  231. }
  232. f = f * 100
  233. return fmt.Sprintf("%.2f%%", f)
  234. case "0.00e+00", "##0.0e+0":
  235. return c.formatToFloat("%e")
  236. case "mm-dd-yy":
  237. return c.formatToTime("01-02-06")
  238. case "d-mmm-yy":
  239. return c.formatToTime("2-Jan-06")
  240. case "d-mmm":
  241. return c.formatToTime("2-Jan")
  242. case "mmm-yy":
  243. return c.formatToTime("Jan-06")
  244. case "h:mm am/pm":
  245. return c.formatToTime("3:04 pm")
  246. case "h:mm:ss am/pm":
  247. return c.formatToTime("3:04:05 pm")
  248. case "h:mm":
  249. return c.formatToTime("15:04")
  250. case "h:mm:ss":
  251. return c.formatToTime("15:04:05")
  252. case "m/d/yy h:mm":
  253. return c.formatToTime("1/2/06 15:04")
  254. case "mm:ss":
  255. return c.formatToTime("04:05")
  256. case "[h]:mm:ss":
  257. f, err := strconv.ParseFloat(c.Value, 64)
  258. if err != nil {
  259. return err.Error()
  260. }
  261. t := TimeFromExcelTime(f, c.date1904)
  262. if t.Hour() > 0 {
  263. return t.Format("15:04:05")
  264. }
  265. return t.Format("04:05")
  266. case "mmss.0":
  267. f, err := strconv.ParseFloat(c.Value, 64)
  268. if err != nil {
  269. return err.Error()
  270. }
  271. t := TimeFromExcelTime(f, c.date1904)
  272. return fmt.Sprintf("%0d%0d.%d", t.Minute(), t.Second(), t.Nanosecond()/1000)
  273. case "yyyy\\-mm\\-dd", "yyyy\\-mm\\-dd;@":
  274. return c.formatToTime("2006\\-01\\-02")
  275. case "dd/mm/yy":
  276. return c.formatToTime("02/01/06")
  277. case "hh:mm:ss":
  278. return c.formatToTime("15:04:05")
  279. case "dd/mm/yy\\ hh:mm":
  280. return c.formatToTime("02/01/06\\ 15:04")
  281. case "dd/mm/yyyy hh:mm:ss":
  282. return c.formatToTime("02/01/2006 15:04:05")
  283. case "yyyy/mm/dd":
  284. return c.formatToTime("2006/01/02")
  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. }