format_code_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package xlsx
  2. import (
  3. "time"
  4. . "gopkg.in/check.v1"
  5. )
  6. func (s *CellSuite) TestMoreFormattingFeatures(c *C) {
  7. cell := Cell{}
  8. cell.SetFloat(0)
  9. date, err := cell.GetTime(false)
  10. c.Assert(err, Equals, nil)
  11. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  12. cell.SetFloat(39813.0)
  13. date, err = cell.GetTime(true)
  14. c.Assert(err, Equals, nil)
  15. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  16. cell.Value = "d"
  17. _, err = cell.GetTime(false)
  18. c.Assert(err, NotNil)
  19. }
  20. func (l *CellSuite) TestFormatStringSupport(c *C) {
  21. testCases := []struct {
  22. formatString string
  23. value string
  24. formattedValueOutput string
  25. cellType CellType
  26. expectError bool
  27. }{
  28. {
  29. formatString: `[red]0`,
  30. value: "18.989999999999998",
  31. formattedValueOutput: "19",
  32. cellType: CellTypeNumeric,
  33. },
  34. {
  35. formatString: `[blue]0`,
  36. value: "18.989999999999998",
  37. formattedValueOutput: "19",
  38. cellType: CellTypeNumeric,
  39. },
  40. {
  41. formatString: `[color50]0`,
  42. value: "18.989999999999998",
  43. formattedValueOutput: "19",
  44. cellType: CellTypeNumeric,
  45. },
  46. {
  47. formatString: `[$$-409]0`,
  48. value: "18.989999999999998",
  49. formattedValueOutput: "$19",
  50. cellType: CellTypeNumeric,
  51. },
  52. {
  53. formatString: `[$¥-409]0`,
  54. value: "18.989999999999998",
  55. formattedValueOutput: "¥19",
  56. cellType: CellTypeNumeric,
  57. },
  58. {
  59. formatString: `[$€-409]0`,
  60. value: "18.989999999999998",
  61. formattedValueOutput: "€19",
  62. cellType: CellTypeNumeric,
  63. },
  64. {
  65. formatString: `[$£-409]0`,
  66. value: "18.989999999999998",
  67. formattedValueOutput: "£19",
  68. cellType: CellTypeNumeric,
  69. },
  70. {
  71. formatString: `[$USD-409] 0`,
  72. value: "18.989999999999998",
  73. formattedValueOutput: "USD 19",
  74. cellType: CellTypeNumeric,
  75. },
  76. {
  77. formatString: `0[$USD-409]`,
  78. value: "18.989999999999998",
  79. formattedValueOutput: "19USD",
  80. cellType: CellTypeNumeric,
  81. },
  82. {
  83. formatString: `-[$USD-409]0`,
  84. value: "18.989999999999998",
  85. formattedValueOutput: "-USD19",
  86. cellType: CellTypeNumeric,
  87. },
  88. {
  89. formatString: `\[0`,
  90. value: "18.989999999999998",
  91. formattedValueOutput: "[19",
  92. cellType: CellTypeNumeric,
  93. },
  94. {
  95. formatString: `"["0`,
  96. value: "18.989999999999998",
  97. formattedValueOutput: "[19",
  98. cellType: CellTypeNumeric,
  99. },
  100. {
  101. formatString: "_[0",
  102. value: "18.989999999999998",
  103. formattedValueOutput: "19",
  104. cellType: CellTypeNumeric,
  105. },
  106. {
  107. formatString: `"asdf"0`,
  108. value: "18.989999999999998",
  109. formattedValueOutput: "asdf19",
  110. cellType: CellTypeNumeric,
  111. },
  112. {
  113. formatString: `"$"0`,
  114. value: "18.989999999999998",
  115. formattedValueOutput: "$19",
  116. cellType: CellTypeNumeric,
  117. },
  118. {
  119. formatString: `$0`,
  120. value: "18.989999999999998",
  121. formattedValueOutput: "$19",
  122. cellType: CellTypeNumeric,
  123. },
  124. {
  125. formatString: `%0`, // The percent sign can be anywhere in the format.
  126. value: "18.989999999999998",
  127. formattedValueOutput: "%1899",
  128. cellType: CellTypeNumeric,
  129. },
  130. {
  131. formatString: `$-+/()!^&'~{}<>=: 0 :=><}{~'&^)(/+-$`,
  132. value: "18.989999999999998",
  133. formattedValueOutput: "$-+/()!^&'~{}<>=: 19 :=><}{~'&^)(/+-$",
  134. cellType: CellTypeNumeric,
  135. },
  136. {
  137. formatString: `0;-0;"zero"`,
  138. value: "18.989999999999998",
  139. formattedValueOutput: "19",
  140. cellType: CellTypeNumeric,
  141. },
  142. { // 2 formats
  143. formatString: `0;(0)`,
  144. value: "0",
  145. formattedValueOutput: "0",
  146. cellType: CellTypeNumeric,
  147. },
  148. { // 2 formats
  149. formatString: `0;(0)`,
  150. value: "4.1",
  151. formattedValueOutput: "4",
  152. cellType: CellTypeNumeric,
  153. },
  154. { // 2 formats
  155. formatString: `0;(0)`,
  156. value: "-1",
  157. formattedValueOutput: "(1)",
  158. cellType: CellTypeNumeric,
  159. },
  160. { // 2 formats
  161. formatString: `0;(0)`,
  162. value: "asdf",
  163. formattedValueOutput: "asdf",
  164. cellType: CellTypeNumeric,
  165. expectError: true,
  166. },
  167. { // 2 formats
  168. formatString: `0;(0)`,
  169. value: "asdf",
  170. formattedValueOutput: "asdf",
  171. cellType: CellTypeString,
  172. },
  173. { // 3 formats
  174. formatString: `0;(0);"zero"`,
  175. value: "59.6",
  176. formattedValueOutput: "60",
  177. cellType: CellTypeNumeric,
  178. },
  179. { // 3 formats
  180. formatString: `0;(0);"zero"`,
  181. value: "-39",
  182. formattedValueOutput: "(39)",
  183. cellType: CellTypeNumeric,
  184. },
  185. { // 3 formats
  186. formatString: `0;(0);"zero"`,
  187. value: "0",
  188. formattedValueOutput: "zero",
  189. cellType: CellTypeNumeric,
  190. },
  191. { // 3 formats
  192. formatString: `0;(0);"zero"`,
  193. value: "asdf",
  194. formattedValueOutput: "asdf",
  195. cellType: CellTypeNumeric,
  196. expectError: true,
  197. },
  198. { // 3 formats
  199. formatString: `0;(0);"zero"`,
  200. value: "asdf",
  201. formattedValueOutput: "asdf",
  202. cellType: CellTypeString,
  203. },
  204. { // 4 formats, also note that the case of the format is maintained. Format codes should not be lower cased.
  205. formatString: `0;(0);"zero";"Behold: "@`,
  206. value: "asdf",
  207. formattedValueOutput: "Behold: asdf",
  208. cellType: CellTypeString,
  209. },
  210. { // 4 formats
  211. formatString: `0;(0);"zero";"Behold": @`,
  212. value: "asdf",
  213. formattedValueOutput: "Behold: asdf",
  214. cellType: CellTypeString,
  215. },
  216. { // 4 formats. This format contains an extra
  217. formatString: `0;(0);"zero";"Behold; "@`,
  218. value: "asdf",
  219. formattedValueOutput: "Behold; asdf",
  220. cellType: CellTypeString,
  221. },
  222. }
  223. for _, testCase := range testCases {
  224. cell := &Cell{
  225. cellType: testCase.cellType,
  226. NumFmt: testCase.formatString,
  227. Value: testCase.value,
  228. }
  229. val, err := cell.FormattedValue()
  230. if err != nil != testCase.expectError {
  231. c.Fatal(err, testCase)
  232. }
  233. if val != testCase.formattedValueOutput {
  234. c.Fatalf("Expected %v but got %v", testCase.formattedValueOutput, val)
  235. }
  236. }
  237. }