cell.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package xlsx
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. maxNonScientificNumber = 1e11
  11. minNonScientificNumber = 1e-9
  12. )
  13. // CellType is an int type for storing metadata about the data type in the cell.
  14. type CellType int
  15. // Known types for cell values.
  16. const (
  17. CellTypeString CellType = iota
  18. CellTypeFormula
  19. CellTypeNumeric
  20. CellTypeBool
  21. CellTypeInline
  22. CellTypeError
  23. CellTypeDate
  24. CellTypeGeneral
  25. )
  26. func (ct CellType) Ptr() *CellType {
  27. return &ct
  28. }
  29. // Cell is a high level structure intended to provide user access to
  30. // the contents of Cell within an xlsx.Row.
  31. type Cell struct {
  32. Row *Row
  33. Value string
  34. formula string
  35. style *Style
  36. NumFmt string
  37. date1904 bool
  38. Hidden bool
  39. HMerge int
  40. VMerge int
  41. cellType CellType
  42. }
  43. // CellInterface defines the public API of the Cell.
  44. type CellInterface interface {
  45. String() string
  46. FormattedValue() string
  47. }
  48. // NewCell creates a cell and adds it to a row.
  49. func NewCell(r *Row) *Cell {
  50. return &Cell{Row: r}
  51. }
  52. // Merge with other cells, horizontally and/or vertically.
  53. func (c *Cell) Merge(hcells, vcells int) {
  54. c.HMerge = hcells
  55. c.VMerge = vcells
  56. }
  57. // Type returns the CellType of a cell. See CellType constants for more details.
  58. func (c *Cell) Type() CellType {
  59. return c.cellType
  60. }
  61. // SetString sets the value of a cell to a string.
  62. func (c *Cell) SetString(s string) {
  63. c.Value = s
  64. c.formula = ""
  65. c.cellType = CellTypeString
  66. }
  67. // String returns the value of a Cell as a string. If you'd like to
  68. // see errors returned from formatting then please use
  69. // Cell.FormattedValue() instead.
  70. func (c *Cell) String() string {
  71. // To preserve the String() interface we'll throw away errors.
  72. // Not that using FormattedValue is therefore strongly
  73. // preferred.
  74. value, _ := c.FormattedValue()
  75. return value
  76. }
  77. // SetFloat sets the value of a cell to a float.
  78. func (c *Cell) SetFloat(n float64) {
  79. c.SetValue(n)
  80. }
  81. //GetTime returns the value of a Cell as a time.Time
  82. func (c *Cell) GetTime(date1904 bool) (t time.Time, err error) {
  83. f, err := c.Float()
  84. if err != nil {
  85. return t, err
  86. }
  87. return TimeFromExcelTime(f, date1904), nil
  88. }
  89. /*
  90. The following are samples of format samples.
  91. * "0.00e+00"
  92. * "0", "#,##0"
  93. * "0.00", "#,##0.00", "@"
  94. * "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)"
  95. * "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)"
  96. * "0%", "0.00%"
  97. * "0.00e+00", "##0.0e+0"
  98. */
  99. // SetFloatWithFormat sets the value of a cell to a float and applies
  100. // formatting to the cell.
  101. func (c *Cell) SetFloatWithFormat(n float64, format string) {
  102. // beauty the output when the float is small enough
  103. if n != 0 && n < 0.00001 {
  104. c.Value = strconv.FormatFloat(n, 'e', -1, 64)
  105. } else {
  106. c.Value = strconv.FormatFloat(n, 'f', -1, 64)
  107. }
  108. c.NumFmt = format
  109. c.formula = ""
  110. c.cellType = CellTypeNumeric
  111. }
  112. var timeLocationUTC, _ = time.LoadLocation("UTC")
  113. func TimeToUTCTime(t time.Time) time.Time {
  114. return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), timeLocationUTC)
  115. }
  116. func TimeToExcelTime(t time.Time) float64 {
  117. return float64(t.UnixNano())/8.64e13 + 25569.0
  118. }
  119. // DateTimeOptions are additional options for exporting times
  120. type DateTimeOptions struct {
  121. // Location allows calculating times in other timezones/locations
  122. Location *time.Location
  123. // ExcelTimeFormat is the string you want excel to use to format the datetime
  124. ExcelTimeFormat string
  125. }
  126. var (
  127. DefaultDateFormat = builtInNumFmt[14]
  128. DefaultDateTimeFormat = builtInNumFmt[22]
  129. DefaultDateOptions = DateTimeOptions{
  130. Location: timeLocationUTC,
  131. ExcelTimeFormat: DefaultDateFormat,
  132. }
  133. DefaultDateTimeOptions = DateTimeOptions{
  134. Location: timeLocationUTC,
  135. ExcelTimeFormat: DefaultDateTimeFormat,
  136. }
  137. )
  138. // SetDate sets the value of a cell to a float.
  139. func (c *Cell) SetDate(t time.Time) {
  140. c.SetDateWithOptions(t, DefaultDateOptions)
  141. }
  142. func (c *Cell) SetDateTime(t time.Time) {
  143. c.SetDateWithOptions(t, DefaultDateTimeOptions)
  144. }
  145. // SetDateWithOptions allows for more granular control when exporting dates and times
  146. func (c *Cell) SetDateWithOptions(t time.Time, options DateTimeOptions) {
  147. _, offset := t.In(options.Location).Zone()
  148. t = time.Unix(t.Unix()+int64(offset), 0)
  149. c.SetDateTimeWithFormat(TimeToExcelTime(t.In(timeLocationUTC)), options.ExcelTimeFormat)
  150. }
  151. func (c *Cell) SetDateTimeWithFormat(n float64, format string) {
  152. c.Value = strconv.FormatFloat(n, 'f', -1, 64)
  153. c.NumFmt = format
  154. c.formula = ""
  155. c.cellType = CellTypeDate
  156. }
  157. // Float returns the value of cell as a number.
  158. func (c *Cell) Float() (float64, error) {
  159. f, err := strconv.ParseFloat(c.Value, 64)
  160. if err != nil {
  161. return math.NaN(), err
  162. }
  163. return f, nil
  164. }
  165. // SetInt64 sets a cell's value to a 64-bit integer.
  166. func (c *Cell) SetInt64(n int64) {
  167. c.SetValue(n)
  168. }
  169. // Int64 returns the value of cell as 64-bit integer.
  170. func (c *Cell) Int64() (int64, error) {
  171. f, err := strconv.ParseInt(c.Value, 10, 64)
  172. if err != nil {
  173. return -1, err
  174. }
  175. return f, nil
  176. }
  177. // GeneralNumeric returns the value of the cell as a string. It is formatted very closely to the the XLSX spec for how
  178. // to display values when the storage type is Number and the format type is General. It is not 100% identical to the
  179. // spec but is as close as you can get using the built in Go formatting tools.
  180. func (c *Cell) GeneralNumeric() (string, error) {
  181. return c.generalNumericScientific(true)
  182. }
  183. // GeneralNumericWithoutScientific returns numbers that are always formatted as numbers, but it does not follow
  184. // the rules for when XLSX should switch to scientific notation, since sometimes scientific notation is not desired,
  185. // even if that is how the document is supposed to be formatted.
  186. func (c *Cell) GeneralNumericWithoutScientific() (string, error) {
  187. return c.generalNumericScientific(false)
  188. }
  189. func (c *Cell) generalNumericScientific(allowScientific bool) (string, error) {
  190. if strings.TrimSpace(c.Value) == "" {
  191. return "", nil
  192. }
  193. f, err := strconv.ParseFloat(c.Value, 64)
  194. if err != nil {
  195. return c.Value, err
  196. }
  197. if allowScientific {
  198. absF := math.Abs(f)
  199. // When using General format, numbers that are less than 1e-9 (0.000000001) and greater than or equal to
  200. // 1e11 (100,000,000,000) should be shown in scientific notation.
  201. // Numbers less than the number after zero, are assumed to be zero.
  202. if (absF >= math.SmallestNonzeroFloat64 && absF < minNonScientificNumber) || absF >= maxNonScientificNumber {
  203. return strconv.FormatFloat(f, 'E', -1, 64), nil
  204. }
  205. }
  206. // This format (fmt="f", prec=-1) will prevent padding with zeros and will never switch to scientific notation.
  207. // However, it will show more than 11 characters for very precise numbers, and this cannot be changed.
  208. // You could also use fmt="g", prec=11, which doesn't pad with zeros and allows the correct precision,
  209. // but it will use scientific notation on numbers less than 1e-4. That value is hardcoded in Go and cannot be
  210. // configured or disabled.
  211. return strconv.FormatFloat(f, 'f', -1, 64), nil
  212. }
  213. // SetInt sets a cell's value to an integer.
  214. func (c *Cell) SetInt(n int) {
  215. c.SetValue(n)
  216. }
  217. // SetInt sets a cell's value to an integer.
  218. func (c *Cell) SetValue(n interface{}) {
  219. switch t := n.(type) {
  220. case time.Time:
  221. c.SetDateTime(n.(time.Time))
  222. return
  223. case int, int8, int16, int32, int64, float32, float64:
  224. c.setGeneral(fmt.Sprintf("%v", n))
  225. case string:
  226. c.SetString(t)
  227. case []byte:
  228. c.SetString(string(t))
  229. case nil:
  230. c.SetString("")
  231. default:
  232. c.SetString(fmt.Sprintf("%v", n))
  233. }
  234. }
  235. // SetInt sets a cell's value to an integer.
  236. func (c *Cell) setGeneral(s string) {
  237. c.Value = s
  238. c.NumFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL]
  239. c.formula = ""
  240. c.cellType = CellTypeGeneral
  241. }
  242. // Int returns the value of cell as integer.
  243. // Has max 53 bits of precision
  244. // See: float64(int64(math.MaxInt))
  245. func (c *Cell) Int() (int, error) {
  246. f, err := strconv.ParseFloat(c.Value, 64)
  247. if err != nil {
  248. return -1, err
  249. }
  250. return int(f), nil
  251. }
  252. // SetBool sets a cell's value to a boolean.
  253. func (c *Cell) SetBool(b bool) {
  254. if b {
  255. c.Value = "1"
  256. } else {
  257. c.Value = "0"
  258. }
  259. c.cellType = CellTypeBool
  260. }
  261. // Bool returns a boolean from a cell's value.
  262. // TODO: Determine if the current return value is
  263. // appropriate for types other than CellTypeBool.
  264. func (c *Cell) Bool() bool {
  265. // If bool, just return the value.
  266. if c.cellType == CellTypeBool {
  267. return c.Value == "1"
  268. }
  269. // If numeric, base it on a non-zero.
  270. if c.cellType == CellTypeNumeric || c.cellType == CellTypeGeneral {
  271. return c.Value != "0"
  272. }
  273. // Return whether there's an empty string.
  274. return c.Value != ""
  275. }
  276. // SetFormula sets the format string for a cell.
  277. func (c *Cell) SetFormula(formula string) {
  278. c.formula = formula
  279. c.cellType = CellTypeFormula
  280. }
  281. // Formula returns the formula string for the cell.
  282. func (c *Cell) Formula() string {
  283. return c.formula
  284. }
  285. // GetStyle returns the Style associated with a Cell
  286. func (c *Cell) GetStyle() *Style {
  287. if c.style == nil {
  288. c.style = NewStyle()
  289. }
  290. return c.style
  291. }
  292. // SetStyle sets the style of a cell.
  293. func (c *Cell) SetStyle(style *Style) {
  294. c.style = style
  295. }
  296. // GetNumberFormat returns the number format string for a cell.
  297. func (c *Cell) GetNumberFormat() string {
  298. return c.NumFmt
  299. }
  300. func (c *Cell) formatToFloat(format string) (string, error) {
  301. f, err := strconv.ParseFloat(c.Value, 64)
  302. if err != nil {
  303. return c.Value, err
  304. }
  305. return fmt.Sprintf(format, f), nil
  306. }
  307. func (c *Cell) formatToInt(format string) (string, error) {
  308. f, err := strconv.ParseFloat(c.Value, 64)
  309. if err != nil {
  310. return c.Value, err
  311. }
  312. return fmt.Sprintf(format, int(f)), nil
  313. }
  314. // FormattedValue returns a value, and possibly an error condition
  315. // from a Cell. If it is possible to apply a format to the cell
  316. // value, it will do so, if not then an error will be returned, along
  317. // with the raw value of the Cell.
  318. //
  319. // This is the documentation of the "General" Format in the Office Open XML spec:
  320. //
  321. // Numbers
  322. // The application shall attempt to display the full number up to 11 digits (inc. decimal point). If the number is too
  323. // large*, the application shall attempt to show exponential format. If the number has too many significant digits, the
  324. // display shall be truncated. The optimal method of display is based on the available cell width. If the number cannot
  325. // be displayed using any of these formats in the available width, the application shall show "#" across the width of
  326. // the cell.
  327. //
  328. // Conditions for switching to exponential format:
  329. // 1. The cell value shall have at least five digits for xE-xx
  330. // 2. If the exponent is bigger than the size allowed, a floating point number cannot fit, so try exponential notation.
  331. // 3. Similarly, for negative exponents, check if there is space for even one (non-zero) digit in floating point format**.
  332. // 4. Finally, if there isn't room for all of the significant digits in floating point format (for a negative exponent),
  333. // exponential format shall display more digits if the exponent is less than -3. (The 3 is because E-xx takes 4
  334. // characters, and the leading 0 in floating point takes only 1 character. Thus, for an exponent less than -3, there is
  335. // more than 3 additional leading 0's, more than enough to compensate for the size of the E-xx.)
  336. //
  337. // Floating point rule:
  338. // For general formatting in cells, max overall length for cell display is 11, not including negative sign, but includes
  339. // leading zeros and decimal separator.***
  340. //
  341. // Added Notes:
  342. // * "If the number is too large" can also mean "if the number has more than 11 digits", so greater than or equal to
  343. // 1e11 and less than 1e-9.
  344. // ** Means that you should switch to scientific if there would be 9 zeros after the decimal (the decimal and first zero
  345. // count against the 11 character limit), so less than 1e9.
  346. // *** The way this is written, you can get numbers that are more than 11 characters because the golang Float fmt
  347. // does not support adjusting the precision while not padding with zeros, while also not switching to scientific
  348. // notation too early.
  349. func (c *Cell) FormattedValue() (string, error) {
  350. var numberFormat = c.GetNumberFormat()
  351. if isTimeFormat(numberFormat) {
  352. return parseTime(c)
  353. }
  354. switch numberFormat {
  355. case builtInNumFmt[builtInNumFmtIndex_GENERAL]:
  356. if c.cellType == CellTypeNumeric {
  357. // If the cell type is Numeric, format the string the way it should be shown to the user.
  358. val, err := c.GeneralNumeric()
  359. if err != nil {
  360. return c.Value, nil
  361. }
  362. return val, nil
  363. }
  364. return c.Value, nil
  365. case builtInNumFmt[builtInNumFmtIndex_STRING]:
  366. return c.Value, nil
  367. case builtInNumFmt[builtInNumFmtIndex_INT], "#,##0":
  368. return c.formatToInt("%d")
  369. case builtInNumFmt[builtInNumFmtIndex_FLOAT], "#,##0.00":
  370. return c.formatToFloat("%.2f")
  371. case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
  372. f, err := strconv.ParseFloat(c.Value, 64)
  373. if err != nil {
  374. return c.Value, err
  375. }
  376. if f < 0 {
  377. i := int(math.Abs(f))
  378. return fmt.Sprintf("(%d)", i), nil
  379. }
  380. i := int(f)
  381. return fmt.Sprintf("%d", i), nil
  382. case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
  383. f, err := strconv.ParseFloat(c.Value, 64)
  384. if err != nil {
  385. return c.Value, err
  386. }
  387. if f < 0 {
  388. return fmt.Sprintf("(%.2f)", f), nil
  389. }
  390. return fmt.Sprintf("%.2f", f), nil
  391. case "0%":
  392. f, err := strconv.ParseFloat(c.Value, 64)
  393. if err != nil {
  394. return c.Value, err
  395. }
  396. f = f * 100
  397. return fmt.Sprintf("%d%%", int(f)), nil
  398. case "0.00%":
  399. f, err := strconv.ParseFloat(c.Value, 64)
  400. if err != nil {
  401. return c.Value, err
  402. }
  403. f = f * 100
  404. return fmt.Sprintf("%.2f%%", f), nil
  405. case "0.00e+00", "##0.0e+0":
  406. return c.formatToFloat("%e")
  407. }
  408. return c.Value, nil
  409. }
  410. // parseTime returns a string parsed using time.Time
  411. func parseTime(c *Cell) (string, error) {
  412. f, err := strconv.ParseFloat(c.Value, 64)
  413. if err != nil {
  414. return c.Value, err
  415. }
  416. val := TimeFromExcelTime(f, c.date1904)
  417. format := c.GetNumberFormat()
  418. // Replace Excel placeholders with Go time placeholders.
  419. // For example, replace yyyy with 2006. These are in a specific order,
  420. // due to the fact that m is used in month, minute, and am/pm. It would
  421. // be easier to fix that with regular expressions, but if it's possible
  422. // to keep this simple it would be easier to maintain.
  423. // Full-length month and days (e.g. March, Tuesday) have letters in them that would be replaced
  424. // by other characters below (such as the 'h' in March, or the 'd' in Tuesday) below.
  425. // First we convert them to arbitrary characters unused in Excel Date formats, and then at the end,
  426. // turn them to what they should actually be.
  427. // Based off: http://www.ozgrid.com/Excel/CustomFormats.htm
  428. replacements := []struct{ xltime, gotime string }{
  429. {"yyyy", "2006"},
  430. {"yy", "06"},
  431. {"mmmm", "%%%%"},
  432. {"dddd", "&&&&"},
  433. {"dd", "02"},
  434. {"d", "2"},
  435. {"mmm", "Jan"},
  436. {"mmss", "0405"},
  437. {"ss", "05"},
  438. {"mm:", "04:"},
  439. {":mm", ":04"},
  440. {"mm", "01"},
  441. {"am/pm", "pm"},
  442. {"m/", "1/"},
  443. {"%%%%", "January"},
  444. {"&&&&", "Monday"},
  445. }
  446. // It is the presence of the "am/pm" indicator that determins
  447. // if this is a 12 hour or 24 hours time format, not the
  448. // number of 'h' characters.
  449. if is12HourTime(format) {
  450. format = strings.Replace(format, "hh", "03", 1)
  451. format = strings.Replace(format, "h", "3", 1)
  452. } else {
  453. format = strings.Replace(format, "hh", "15", 1)
  454. format = strings.Replace(format, "h", "15", 1)
  455. }
  456. for _, repl := range replacements {
  457. format = strings.Replace(format, repl.xltime, repl.gotime, 1)
  458. }
  459. // If the hour is optional, strip it out, along with the
  460. // possible dangling colon that would remain.
  461. if val.Hour() < 1 {
  462. format = strings.Replace(format, "]:", "]", 1)
  463. format = strings.Replace(format, "[03]", "", 1)
  464. format = strings.Replace(format, "[3]", "", 1)
  465. format = strings.Replace(format, "[15]", "", 1)
  466. } else {
  467. format = strings.Replace(format, "[3]", "3", 1)
  468. format = strings.Replace(format, "[15]", "15", 1)
  469. }
  470. return val.Format(format), nil
  471. }
  472. // isTimeFormat checks whether an Excel format string represents
  473. // a time.Time.
  474. func isTimeFormat(format string) bool {
  475. dateParts := []string{
  476. "yy", "hh", "h", "am/pm", "AM/PM", "A/P", "a/p", "ss", "mm", ":",
  477. }
  478. for _, part := range dateParts {
  479. if strings.Contains(format, part) {
  480. return true
  481. }
  482. }
  483. return false
  484. }
  485. // is12HourTime checks whether an Excel time format string is a 12
  486. // hours form.
  487. func is12HourTime(format string) bool {
  488. return strings.Contains(format, "am/pm") || strings.Contains(format, "AM/PM") || strings.Contains(format, "a/p") || strings.Contains(format, "A/P")
  489. }