styles.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. // Excel styles can reference number formats that are built-in, all of which
  11. // have an id less than 164. This is a possibly incomplete list comprised of as
  12. // many of them as I could find.
  13. var builtInNumFmt = map[int]string{
  14. 0: "general",
  15. 1: "0",
  16. 2: "0.00",
  17. 3: "#,##0",
  18. 4: "#,##0.00",
  19. 9: "0%",
  20. 10: "0.00%",
  21. 11: "0.00e+00",
  22. 12: "# ?/?",
  23. 13: "# ??/??",
  24. 14: "mm-dd-yy",
  25. 15: "d-mmm-yy",
  26. 16: "d-mmm",
  27. 17: "mmm-yy",
  28. 18: "h:mm am/pm",
  29. 19: "h:mm:ss am/pm",
  30. 20: "h:mm",
  31. 21: "h:mm:ss",
  32. 22: "m/d/yy h:mm",
  33. 37: "#,##0 ;(#,##0)",
  34. 38: "#,##0 ;[red](#,##0)",
  35. 39: "#,##0.00;(#,##0.00)",
  36. 40: "#,##0.00;[red](#,##0.00)",
  37. 41: `_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)`,
  38. 42: `_("$"* #,##0_);_("$* \(#,##0\);_("$"* "-"_);_(@_)`,
  39. 43: `_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)`,
  40. 44: `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`,
  41. 45: "mm:ss",
  42. 46: "[h]:mm:ss",
  43. 47: "mmss.0",
  44. 48: "##0.0e+0",
  45. 49: "@",
  46. }
  47. // langNumFmt defined number format code (with unicode values provided for
  48. // language glyphs where they occur) in different language.
  49. var langNumFmt = map[string]map[int]string{
  50. "zh-tw": map[int]string{
  51. 27: "[$-404]e/m/d",
  52. 28: `[$-404]e"年"m"月"d"日"`,
  53. 29: `[$-404]e"年"m"月"d"日"`,
  54. 30: "m/d/yy",
  55. 31: `yyyy"年"m"月"d"日"`,
  56. 32: `hh"時"mm"分"`,
  57. 33: `hh"時"mm"分"ss"秒"`,
  58. 34: `上午/下午 hh"時"mm"分"`,
  59. 35: `上午/下午 hh"時"mm"分"ss"秒"`,
  60. 36: "[$-404]e/m/d",
  61. 50: "[$-404]e/m/d",
  62. 51: `[$-404]e"年"m"月"d"日"`,
  63. 52: `上午/下午 hh"時"mm"分"`,
  64. 53: `上午/下午 hh"時"mm"分"ss"秒"`,
  65. 54: `[$-404]e"年"m"月"d"日"`,
  66. 55: `上午/下午 hh"時"mm"分"`,
  67. 56: `上午/下午 hh"時"mm"分"ss"秒"`,
  68. 57: "[$-404]e/m/d",
  69. 58: `[$-404]e"年"m"月"d"日"`,
  70. },
  71. "zh-cn": map[int]string{
  72. 27: `yyyy"年"m"月"`,
  73. 28: `m"月"d"日"`,
  74. 29: `m"月"d"日"`,
  75. 30: "m-d-yy",
  76. 31: `yyyy"年"m"月"d"日"`,
  77. 32: `h"时"mm"分"`,
  78. 33: `h"时"mm"分"ss"秒"`,
  79. 34: `上午/下午 h"时"mm"分"`,
  80. 35: `上午/下午 h"时"mm"分"ss"秒"`,
  81. 36: `yyyy"年"m"月"`,
  82. 50: `yyyy"年"m"月"`,
  83. 51: `m"月"d"日"`,
  84. 52: `yyyy"年"m"月"`,
  85. 53: `m"月"d"日"`,
  86. 54: `m"月"d"日"`,
  87. 55: `上午/下午 h"时"mm"分"`,
  88. 56: `上午/下午 h"时"mm"分"ss"秒"`,
  89. 57: `yyyy"年"m"月"`,
  90. 58: `m"月"d"日"`,
  91. },
  92. "zh-tw_unicode": map[int]string{
  93. 27: "[$-404]e/m/d",
  94. 28: `[$-404]e"5E74"m"6708"d"65E5"`,
  95. 29: `[$-404]e"5E74"m"6708"d"65E5"`,
  96. 30: "m/d/yy",
  97. 31: `yyyy"5E74"m"6708"d"65E5"`,
  98. 32: `hh"6642"mm"5206"`,
  99. 33: `hh"6642"mm"5206"ss"79D2"`,
  100. 34: `4E0A5348/4E0B5348hh"6642"mm"5206"`,
  101. 35: `4E0A5348/4E0B5348hh"6642"mm"5206"ss"79D2"`,
  102. 36: "[$-404]e/m/d",
  103. 50: "[$-404]e/m/d",
  104. 51: `[$-404]e"5E74"m"6708"d"65E5"`,
  105. 52: `4E0A5348/4E0B5348hh"6642"mm"5206"`,
  106. 53: `4E0A5348/4E0B5348hh"6642"mm"5206"ss"79D2"`,
  107. 54: `[$-404]e"5E74"m"6708"d"65E5"`,
  108. 55: `4E0A5348/4E0B5348hh"6642"mm"5206"`,
  109. 56: `4E0A5348/4E0B5348hh"6642"mm"5206"ss"79D2"`,
  110. 57: "[$-404]e/m/d",
  111. 58: `[$-404]e"5E74"m"6708"d"65E5"`,
  112. },
  113. "zh-cn_unicode": map[int]string{
  114. 27: `yyyy"5E74"m"6708"`,
  115. 28: `m"6708"d"65E5"`,
  116. 29: `m"6708"d"65E5"`,
  117. 30: "m-d-yy",
  118. 31: `yyyy"5E74"m"6708"d"65E5"`,
  119. 32: `h"65F6"mm"5206"`,
  120. 33: `h"65F6"mm"5206"ss"79D2"`,
  121. 34: `4E0A5348/4E0B5348h"65F6"mm"5206"`,
  122. 35: `4E0A5348/4E0B5348h"65F6"mm"5206"ss"79D2"`,
  123. 36: `yyyy"5E74"m"6708"`,
  124. 50: `yyyy"5E74"m"6708"`,
  125. 51: `m"6708"d"65E5"`,
  126. 52: `yyyy"5E74"m"6708"`,
  127. 53: `m"6708"d"65E5"`,
  128. 54: `m"6708"d"65E5"`,
  129. 55: `4E0A5348/4E0B5348h"65F6"mm"5206"`,
  130. 56: `4E0A5348/4E0B5348h"65F6"mm"5206"ss"79D2"`,
  131. 57: `yyyy"5E74"m"6708"`,
  132. 58: `m"6708"d"65E5"`,
  133. },
  134. "ja-jp": map[int]string{
  135. 27: "[$-411]ge.m.d",
  136. 28: `[$-411]ggge"年"m"月"d"日"`,
  137. 29: `[$-411]ggge"年"m"月"d"日"`,
  138. 30: "m/d/yy",
  139. 31: `yyyy"年"m"月"d"日"`,
  140. 32: `h"時"mm"分"`,
  141. 33: `h"時"mm"分"ss"秒"`,
  142. 34: `yyyy"年"m"月"`,
  143. 35: `m"月"d"日"`,
  144. 36: "[$-411]ge.m.d",
  145. 50: "[$-411]ge.m.d",
  146. 51: `[$-411]ggge"年"m"月"d"日"`,
  147. 52: `yyyy"年"m"月"`,
  148. 53: `m"月"d"日"`,
  149. 54: `[$-411]ggge"年"m"月"d"日"`,
  150. 55: `yyyy"年"m"月"`,
  151. 56: `m"月"d"日"`,
  152. 57: "[$-411]ge.m.d",
  153. 58: `[$-411]ggge"年"m"月"d"日"`,
  154. },
  155. "ko-kr": map[int]string{
  156. 27: `yyyy"年" mm"月" dd"日"`,
  157. 28: "mm-dd",
  158. 29: "mm-dd",
  159. 30: "mm-dd-yy",
  160. 31: `yyyy"년" mm"월" dd"일"`,
  161. 32: `h"시" mm"분"`,
  162. 33: `h"시" mm"분" ss"초"`,
  163. 34: `yyyy-mm-dd`,
  164. 35: `yyyy-mm-dd`,
  165. 36: `yyyy"年" mm"月" dd"日"`,
  166. 50: `yyyy"年" mm"月" dd"日"`,
  167. 51: "mm-dd",
  168. 52: "yyyy-mm-dd",
  169. 53: "yyyy-mm-dd",
  170. 54: "mm-dd",
  171. 55: "yyyy-mm-dd",
  172. 56: "yyyy-mm-dd",
  173. 57: `yyyy"年" mm"月" dd"日"`,
  174. 58: "mm-dd",
  175. },
  176. "ja-jp_unicode": map[int]string{
  177. 27: "[$-411]ge.m.d",
  178. 28: `[$-411]ggge"5E74"m"6708"d"65E5"`,
  179. 29: `[$-411]ggge"5E74"m"6708"d"65E5"`,
  180. 30: "m/d/yy",
  181. 31: `yyyy"5E74"m"6708"d"65E5"`,
  182. 32: `h"6642"mm"5206"`,
  183. 33: `h"6642"mm"5206"ss"79D2"`,
  184. 34: `yyyy"5E74"m"6708"`,
  185. 35: `m"6708"d"65E5"`,
  186. 36: "[$-411]ge.m.d",
  187. 50: "[$-411]ge.m.d",
  188. 51: `[$-411]ggge"5E74"m"6708"d"65E5"`,
  189. 52: `yyyy"5E74"m"6708"`,
  190. 53: `m"6708"d"65E5"`,
  191. 54: `[$-411]ggge"5E74"m"6708"d"65E5"`,
  192. 55: `yyyy"5E74"m"6708"`,
  193. 56: `m"6708"d"65E5"`,
  194. 57: "[$-411]ge.m.d",
  195. 58: `[$-411]ggge"5E74"m"6708"d"65E5"`,
  196. },
  197. "ko-kr_unicode": map[int]string{
  198. 27: `yyyy"5E74" mm"6708" dd"65E5"`,
  199. 28: "mm-dd",
  200. 29: "mm-dd",
  201. 30: "mm-dd-yy",
  202. 31: `yyyy"B144" mm"C6D4" dd"C77C"`,
  203. 32: `h"C2DC" mm"BD84"`,
  204. 33: `h"C2DC" mm"BD84" ss"CD08"`,
  205. 34: "yyyy-mm-dd",
  206. 35: "yyyy-mm-dd",
  207. 36: `yyyy"5E74" mm"6708" dd"65E5"`,
  208. 50: `yyyy"5E74" mm"6708" dd"65E5"`,
  209. 51: "mm-dd",
  210. 52: "yyyy-mm-dd",
  211. 53: "yyyy-mm-dd",
  212. 54: "mm-dd",
  213. 55: "yyyy-mm-dd",
  214. 56: "yyyy-mm-dd",
  215. 57: `yyyy"5E74" mm"6708" dd"65E5"`,
  216. 58: "mm-dd",
  217. },
  218. "th-th": map[int]string{
  219. 59: "t0",
  220. 60: "t0.00",
  221. 61: "t#,##0",
  222. 62: "t#,##0.00",
  223. 67: "t0%",
  224. 68: "t0.00%",
  225. 69: "t# ?/?",
  226. 70: "t# ??/??",
  227. 71: "ว/ด/ปปปป",
  228. 72: "ว-ดดด-ปป",
  229. 73: "ว-ดดด",
  230. 74: "ดดด-ปป",
  231. 75: "ช:นน",
  232. 76: "ช:นน:ทท",
  233. 77: "ว/ด/ปปปป ช:นน",
  234. 78: "นน:ทท",
  235. 79: "[ช]:นน:ทท",
  236. 80: "นน:ทท.0",
  237. 81: "d/m/bb",
  238. },
  239. "th-th_unicode": map[int]string{
  240. 59: "t0",
  241. 60: "t0.00",
  242. 61: "t#,##0",
  243. 62: "t#,##0.00",
  244. 67: "t0%",
  245. 68: "t0.00%",
  246. 69: "t# ?/?",
  247. 70: "t# ??/??",
  248. 71: "0E27/0E14/0E1B0E1B0E1B0E1B",
  249. 72: "0E27-0E140E140E14-0E1B0E1B",
  250. 73: "0E27-0E140E140E14",
  251. 74: "0E140E140E14-0E1B0E1B",
  252. 75: "0E0A:0E190E19",
  253. 76: "0E0A:0E190E19:0E170E17",
  254. 77: "0E27/0E14/0E1B0E1B0E1B0E1B 0E0A:0E190E19",
  255. 78: "0E190E19:0E170E17",
  256. 79: "[0E0A]:0E190E19:0E170E17",
  257. 80: "0E190E19:0E170E17.0",
  258. 81: "d/m/bb",
  259. },
  260. }
  261. // currencyNumFmt defined the currency number format map.
  262. var currencyNumFmt = map[int]string{
  263. 164: `"CN¥",##0.00`,
  264. 165: "[$$-409]#,##0.00",
  265. 166: "[$$-45C]#,##0.00",
  266. 167: "[$$-1004]#,##0.00",
  267. 168: "[$$-404]#,##0.00",
  268. 169: "[$$-C09]#,##0.00",
  269. 170: "[$$-2809]#,##0.00",
  270. 171: "[$$-1009]#,##0.00",
  271. 172: "[$$-2009]#,##0.00",
  272. 173: "[$$-1409]#,##0.00",
  273. 174: "[$$-4809]#,##0.00",
  274. 175: "[$$-2C09]#,##0.00",
  275. 176: "[$$-2409]#,##0.00",
  276. 177: "[$$-1000]#,##0.00",
  277. 178: `#,##0.00\ [$$-C0C]`,
  278. 179: "[$$-475]#,##0.00",
  279. 180: "[$$-83E]#,##0.00",
  280. 181: `[$$-86B]\ #,##0.00`,
  281. 182: `[$$-340A]\ #,##0.00`,
  282. 183: "[$$-240A]#,##0.00",
  283. 184: `[$$-300A]\ #,##0.00`,
  284. 185: "[$$-440A]#,##0.00",
  285. 186: "[$$-80A]#,##0.00",
  286. 187: "[$$-500A]#,##0.00",
  287. 188: "[$$-540A]#,##0.00",
  288. 189: `[$$-380A]\ #,##0.00`,
  289. 190: "[$£-809]#,##0.00",
  290. 191: "[$£-491]#,##0.00",
  291. 192: "[$£-452]#,##0.00",
  292. 193: "[$¥-804]#,##0.00",
  293. 194: "[$¥-411]#,##0.00",
  294. 195: "[$¥-478]#,##0.00",
  295. 196: "[$¥-451]#,##0.00",
  296. 197: "[$¥-480]#,##0.00",
  297. 198: "#,##0.00\\ [$\u058F-42B]",
  298. 199: "[$\u060B-463]#,##0.00",
  299. 200: "[$\u060B-48C]#,##0.00",
  300. 201: "[$\u09F3-845]\\ #,##0.00",
  301. 202: "#,##0.00[$\u17DB-453]",
  302. 203: "[$\u20A1-140A]#,##0.00",
  303. 204: "[$\u20A6-468]\\ #,##0.00",
  304. 205: "[$\u20A6-470]\\ #,##0.00",
  305. 206: "[$\u20A9-412]#,##0.00",
  306. 207: "[$\u20AA-40D]\\ #,##0.00",
  307. 208: "#,##0.00\\ [$\u20AB-42A]",
  308. 209: "#,##0.00\\ [$\u20AC-42D]",
  309. 210: "#,##0.00\\ [$\u20AC-47E]",
  310. 211: "#,##0.00\\ [$\u20AC-403]",
  311. 212: "#,##0.00\\ [$\u20AC-483]",
  312. 213: "[$\u20AC-813]\\ #,##0.00",
  313. 214: "[$\u20AC-413]\\ #,##0.00",
  314. 215: "[$\u20AC-1809]#,##0.00",
  315. 216: "#,##0.00\\ [$\u20AC-425]",
  316. 217: "[$\u20AC-2]\\ #,##0.00",
  317. 218: "#,##0.00\\ [$\u20AC-1]",
  318. 219: "#,##0.00\\ [$\u20AC-40B]",
  319. 220: "#,##0.00\\ [$\u20AC-80C]",
  320. 221: "#,##0.00\\ [$\u20AC-40C]",
  321. 222: "#,##0.00\\ [$\u20AC-140C]",
  322. 223: "#,##0.00\\ [$\u20AC-180C]",
  323. 224: "[$\u20AC-200C]#,##0.00",
  324. 225: "#,##0.00\\ [$\u20AC-456]",
  325. 226: "#,##0.00\\ [$\u20AC-C07]",
  326. 227: "#,##0.00\\ [$\u20AC-407]",
  327. 228: "#,##0.00\\ [$\u20AC-1007]",
  328. 229: "#,##0.00\\ [$\u20AC-408]",
  329. 230: "#,##0.00\\ [$\u20AC-243B]",
  330. 231: "[$\u20AC-83C]#,##0.00",
  331. 232: "[$\u20AC-410]\\ #,##0.00",
  332. 233: "[$\u20AC-476]#,##0.00",
  333. 234: "#,##0.00\\ [$\u20AC-2C1A]",
  334. 235: "[$\u20AC-426]\\ #,##0.00",
  335. 236: "#,##0.00\\ [$\u20AC-427]",
  336. 237: "#,##0.00\\ [$\u20AC-82E]",
  337. 238: "#,##0.00\\ [$\u20AC-46E]",
  338. 239: "[$\u20AC-43A]#,##0.00",
  339. 240: "#,##0.00\\ [$\u20AC-C3B]",
  340. 241: "#,##0.00\\ [$\u20AC-482]",
  341. 242: "#,##0.00\\ [$\u20AC-816]",
  342. 243: "#,##0.00\\ [$\u20AC-301A]",
  343. 244: "#,##0.00\\ [$\u20AC-203B]",
  344. 245: "#,##0.00\\ [$\u20AC-41B]",
  345. 246: "#,##0.00\\ [$\u20AC-424]",
  346. 247: "#,##0.00\\ [$\u20AC-C0A]",
  347. 248: "#,##0.00\\ [$\u20AC-81D]",
  348. 249: "#,##0.00\\ [$\u20AC-484]",
  349. 250: "#,##0.00\\ [$\u20AC-42E]",
  350. 251: "[$\u20AC-462]\\ #,##0.00",
  351. 252: "#,##0.00\\ [$₭-454]",
  352. 253: "#,##0.00\\ [$₮-450]",
  353. 254: "[$\u20AE-C50]#,##0.00",
  354. 255: "[$\u20B1-3409]#,##0.00",
  355. 256: "[$\u20B1-464]#,##0.00",
  356. 257: "#,##0.00[$\u20B4-422]",
  357. 258: "[$\u20B8-43F]#,##0.00",
  358. 259: "[$\u20B9-460]#,##0.00",
  359. 260: "[$\u20B9-4009]\\ #,##0.00",
  360. 261: "[$\u20B9-447]\\ #,##0.00",
  361. 262: "[$\u20B9-439]\\ #,##0.00",
  362. 263: "[$\u20B9-44B]\\ #,##0.00",
  363. 264: "[$\u20B9-860]#,##0.00",
  364. 265: "[$\u20B9-457]\\ #,##0.00",
  365. 266: "[$\u20B9-458]#,##0.00",
  366. 267: "[$\u20B9-44E]\\ #,##0.00",
  367. 268: "[$\u20B9-861]#,##0.00",
  368. 269: "[$\u20B9-448]\\ #,##0.00",
  369. 270: "[$\u20B9-446]\\ #,##0.00",
  370. 271: "[$\u20B9-44F]\\ #,##0.00",
  371. 272: "[$\u20B9-459]#,##0.00",
  372. 273: "[$\u20B9-449]\\ #,##0.00",
  373. 274: "[$\u20B9-820]#,##0.00",
  374. 275: "#,##0.00\\ [$\u20BA-41F]",
  375. 276: "#,##0.00\\ [$\u20BC-42C]",
  376. 277: "#,##0.00\\ [$\u20BC-82C]",
  377. 278: "#,##0.00\\ [$\u20BD-419]",
  378. 279: "#,##0.00[$\u20BD-485]",
  379. 280: "#,##0.00\\ [$\u20BE-437]",
  380. 281: "[$B/.-180A]\\ #,##0.00",
  381. 282: "[$Br-472]#,##0.00",
  382. 283: "[$Br-477]#,##0.00",
  383. 284: "#,##0.00[$Br-473]",
  384. 285: "[$Bs-46B]\\ #,##0.00",
  385. 286: "[$Bs-400A]\\ #,##0.00",
  386. 287: "[$Bs.-200A]\\ #,##0.00",
  387. 288: "[$BWP-832]\\ #,##0.00",
  388. 289: "[$C$-4C0A]#,##0.00",
  389. 290: "[$CA$-85D]#,##0.00",
  390. 291: "[$CA$-47C]#,##0.00",
  391. 292: "[$CA$-45D]#,##0.00",
  392. 293: "[$CFA-340C]#,##0.00",
  393. 294: "[$CFA-280C]#,##0.00",
  394. 295: "#,##0.00\\ [$CFA-867]",
  395. 296: "#,##0.00\\ [$CFA-488]",
  396. 297: "#,##0.00\\ [$CHF-100C]",
  397. 298: "[$CHF-1407]\\ #,##0.00",
  398. 299: "[$CHF-807]\\ #,##0.00",
  399. 300: "[$CHF-810]\\ #,##0.00",
  400. 301: "[$CHF-417]\\ #,##0.00",
  401. 302: "[$CLP-47A]\\ #,##0.00",
  402. 303: "[$CN¥-850]#,##0.00",
  403. 304: "#,##0.00\\ [$DZD-85F]",
  404. 305: "[$FCFA-2C0C]#,##0.00",
  405. 306: "#,##0.00\\ [$Ft-40E]",
  406. 307: "[$G-3C0C]#,##0.00",
  407. 308: "[$Gs.-3C0A]\\ #,##0.00",
  408. 309: "[$GTQ-486]#,##0.00",
  409. 310: "[$HK$-C04]#,##0.00",
  410. 311: "[$HK$-3C09]#,##0.00",
  411. 312: "#,##0.00\\ [$HRK-41A]",
  412. 313: "[$IDR-3809]#,##0.00",
  413. 314: "[$IQD-492]#,##0.00",
  414. 315: "#,##0.00\\ [$ISK-40F]",
  415. 316: "[$K-455]#,##0.00",
  416. 317: "#,##0.00\\ [$K\u010D-405]",
  417. 318: "#,##0.00\\ [$KM-141A]",
  418. 319: "#,##0.00\\ [$KM-101A]",
  419. 320: "#,##0.00\\ [$KM-181A]",
  420. 321: "[$kr-438]\\ #,##0.00",
  421. 322: "[$kr-43B]\\ #,##0.00",
  422. 323: "#,##0.00\\ [$kr-83B]",
  423. 324: "[$kr-414]\\ #,##0.00",
  424. 325: "[$kr-814]\\ #,##0.00",
  425. 326: "#,##0.00\\ [$kr-41D]",
  426. 327: "[$kr.-406]\\ #,##0.00",
  427. 328: "[$kr.-46F]\\ #,##0.00",
  428. 329: "[$Ksh-441]#,##0.00",
  429. 330: "[$L-818]#,##0.00",
  430. 331: "[$L-819]#,##0.00",
  431. 332: "[$L-480A]\\ #,##0.00",
  432. 333: "#,##0.00\\ [$Lek\u00EB-41C]",
  433. 334: "[$MAD-45F]#,##0.00",
  434. 335: "[$MAD-380C]#,##0.00",
  435. 336: "#,##0.00\\ [$MAD-105F]",
  436. 337: "[$MOP$-1404]#,##0.00",
  437. 338: "#,##0.00\\ [$MVR-465]_-",
  438. 339: "#,##0.00[$Nfk-873]",
  439. 340: "[$NGN-466]#,##0.00",
  440. 341: "[$NGN-467]#,##0.00",
  441. 342: "[$NGN-469]#,##0.00",
  442. 343: "[$NGN-471]#,##0.00",
  443. 344: "[$NOK-103B]\\ #,##0.00",
  444. 345: "[$NOK-183B]\\ #,##0.00",
  445. 346: "[$NZ$-481]#,##0.00",
  446. 347: "[$PKR-859]\\ #,##0.00",
  447. 348: "[$PYG-474]#,##0.00",
  448. 349: "[$Q-100A]#,##0.00",
  449. 350: "[$R-436]\\ #,##0.00",
  450. 351: "[$R-1C09]\\ #,##0.00",
  451. 352: "[$R-435]\\ #,##0.00",
  452. 353: "[$R$-416]\\ #,##0.00",
  453. 354: "[$RD$-1C0A]#,##0.00",
  454. 355: "#,##0.00\\ [$RF-487]",
  455. 356: "[$RM-4409]#,##0.00",
  456. 357: "[$RM-43E]#,##0.00",
  457. 358: "#,##0.00\\ [$RON-418]",
  458. }
  459. // builtInNumFmtFunc defined the format conversion functions map. Partial format
  460. // code doesn't support currently and will return original string.
  461. var builtInNumFmtFunc = map[int]func(i int, v string) string{
  462. 0: formatToString,
  463. 1: formatToInt,
  464. 2: formatToFloat,
  465. 3: formatToInt,
  466. 4: formatToFloat,
  467. 9: formatToC,
  468. 10: formatToD,
  469. 11: formatToE,
  470. 12: formatToString, // Doesn't support currently
  471. 13: formatToString, // Doesn't support currently
  472. 14: parseTime,
  473. 15: parseTime,
  474. 16: parseTime,
  475. 17: parseTime,
  476. 18: parseTime,
  477. 19: parseTime,
  478. 20: parseTime,
  479. 21: parseTime,
  480. 22: parseTime,
  481. 37: formatToA,
  482. 38: formatToA,
  483. 39: formatToB,
  484. 40: formatToB,
  485. 41: formatToString, // Doesn't support currently
  486. 42: formatToString, // Doesn't support currently
  487. 43: formatToString, // Doesn't support currently
  488. 44: formatToString, // Doesn't support currently
  489. 45: parseTime,
  490. 46: parseTime,
  491. 47: parseTime,
  492. 48: formatToE,
  493. 49: formatToString,
  494. }
  495. // formatToString provides function to return original string by given built-in
  496. // number formats code and cell string.
  497. func formatToString(i int, v string) string {
  498. return v
  499. }
  500. // formatToInt provides function to convert original string to integer format as
  501. // string type by given built-in number formats code and cell string.
  502. func formatToInt(i int, v string) string {
  503. f, err := strconv.ParseFloat(v, 64)
  504. if err != nil {
  505. return v
  506. }
  507. return fmt.Sprintf("%d", int(f))
  508. }
  509. // formatToFloat provides function to convert original string to float format as
  510. // string type by given built-in number formats code and cell string.
  511. func formatToFloat(i int, v string) string {
  512. f, err := strconv.ParseFloat(v, 64)
  513. if err != nil {
  514. return v
  515. }
  516. return fmt.Sprintf("%.2f", f)
  517. }
  518. // formatToA provides function to convert original string to special format as
  519. // string type by given built-in number formats code and cell string.
  520. func formatToA(i int, v string) string {
  521. f, err := strconv.ParseFloat(v, 64)
  522. if err != nil {
  523. return v
  524. }
  525. if f < 0 {
  526. t := int(math.Abs(f))
  527. return fmt.Sprintf("(%d)", t)
  528. }
  529. t := int(f)
  530. return fmt.Sprintf("%d", t)
  531. }
  532. // formatToB provides function to convert original string to special format as
  533. // string type by given built-in number formats code and cell string.
  534. func formatToB(i int, v string) string {
  535. f, err := strconv.ParseFloat(v, 64)
  536. if err != nil {
  537. return v
  538. }
  539. if f < 0 {
  540. return fmt.Sprintf("(%.2f)", f)
  541. }
  542. return fmt.Sprintf("%.2f", f)
  543. }
  544. // formatToC provides function to convert original string to special format as
  545. // string type by given built-in number formats code and cell string.
  546. func formatToC(i int, v string) string {
  547. f, err := strconv.ParseFloat(v, 64)
  548. if err != nil {
  549. return v
  550. }
  551. f = f * 100
  552. return fmt.Sprintf("%d%%", int(f))
  553. }
  554. // formatToD provides function to convert original string to special format as
  555. // string type by given built-in number formats code and cell string.
  556. func formatToD(i int, v string) string {
  557. f, err := strconv.ParseFloat(v, 64)
  558. if err != nil {
  559. return v
  560. }
  561. f = f * 100
  562. return fmt.Sprintf("%.2f%%", f)
  563. }
  564. // formatToE provides function to convert original string to special format as
  565. // string type by given built-in number formats code and cell string.
  566. func formatToE(i int, v string) string {
  567. f, err := strconv.ParseFloat(v, 64)
  568. if err != nil {
  569. return v
  570. }
  571. return fmt.Sprintf("%.e", f)
  572. }
  573. // parseTime provides function to returns a string parsed using time.Time.
  574. // Replace Excel placeholders with Go time placeholders. For example, replace
  575. // yyyy with 2006. These are in a specific order, due to the fact that m is used
  576. // in month, minute, and am/pm. It would be easier to fix that with regular
  577. // expressions, but if it's possible to keep this simple it would be easier to
  578. // maintain. Full-length month and days (e.g. March, Tuesday) have letters in
  579. // them that would be replaced by other characters below (such as the 'h' in
  580. // March, or the 'd' in Tuesday) below. First we convert them to arbitrary
  581. // characters unused in Excel Date formats, and then at the end, turn them to
  582. // what they should actually be.
  583. func parseTime(i int, v string) string {
  584. f, err := strconv.ParseFloat(v, 64)
  585. if err != nil {
  586. return v
  587. }
  588. val := timeFromExcelTime(f, false)
  589. format := builtInNumFmt[i]
  590. replacements := []struct{ xltime, gotime string }{
  591. {"yyyy", "2006"},
  592. {"yy", "06"},
  593. {"mmmm", "%%%%"},
  594. {"dddd", "&&&&"},
  595. {"dd", "02"},
  596. {"d", "2"},
  597. {"mmm", "Jan"},
  598. {"mmss", "0405"},
  599. {"ss", "05"},
  600. {"hh", "15"},
  601. {"h", "3"},
  602. {"mm:", "04:"},
  603. {":mm", ":04"},
  604. {"mm", "01"},
  605. {"am/pm", "pm"},
  606. {"m/", "1/"},
  607. {"%%%%", "January"},
  608. {"&&&&", "Monday"},
  609. }
  610. for _, repl := range replacements {
  611. format = strings.Replace(format, repl.xltime, repl.gotime, 1)
  612. }
  613. // If the hour is optional, strip it out, along with the possible dangling
  614. // colon that would remain.
  615. if val.Hour() < 1 {
  616. format = strings.Replace(format, "]:", "]", 1)
  617. format = strings.Replace(format, "[3]", "", 1)
  618. format = strings.Replace(format, "[15]", "", 1)
  619. } else {
  620. format = strings.Replace(format, "[3]", "3", 1)
  621. format = strings.Replace(format, "[15]", "15", 1)
  622. }
  623. return val.Format(format)
  624. }
  625. // stylesReader provides function to get the pointer to the structure after
  626. // deserialization of xl/styles.xml.
  627. func (f *File) stylesReader() *xlsxStyleSheet {
  628. if f.Styles == nil {
  629. var styleSheet xlsxStyleSheet
  630. xml.Unmarshal([]byte(f.readXML("xl/styles.xml")), &styleSheet)
  631. f.Styles = &styleSheet
  632. }
  633. return f.Styles
  634. }
  635. // styleSheetWriter provides function to save xl/styles.xml after serialize
  636. // structure.
  637. func (f *File) styleSheetWriter() {
  638. if f.Styles != nil {
  639. output, _ := xml.Marshal(f.Styles)
  640. f.saveFileList("xl/styles.xml", replaceWorkSheetsRelationshipsNameSpace(string(output)))
  641. }
  642. }
  643. // parseFormatStyleSet provides function to parse the format settings of the
  644. // borders.
  645. func parseFormatStyleSet(style string) (*formatCellStyle, error) {
  646. format := formatCellStyle{
  647. DecimalPlaces: 2,
  648. }
  649. err := json.Unmarshal([]byte(style), &format)
  650. return &format, err
  651. }
  652. // NewStyle provides function to create style for cells by given style format.
  653. // Note that the color field uses RGB color code.
  654. //
  655. // The following shows the border styles sorted by excelize index number:
  656. //
  657. // | Index | Name | Weight | Style |
  658. // +-------+---------------+--------+-------------+
  659. // | 0 | None | 0 | |
  660. // | 1 | Continuous | 1 | ----------- |
  661. // | 2 | Continuous | 2 | ----------- |
  662. // | 3 | Dash | 1 | - - - - - - |
  663. // | 4 | Dot | 1 | . . . . . . |
  664. // | 5 | Continuous | 3 | ----------- |
  665. // | 6 | Double | 3 | =========== |
  666. // | 7 | Continuous | 0 | ----------- |
  667. // | 8 | Dash | 2 | - - - - - - |
  668. // | 9 | Dash Dot | 1 | - . - . - . |
  669. // | 10 | Dash Dot | 2 | - . - . - . |
  670. // | 11 | Dash Dot Dot | 1 | - . . - . . |
  671. // | 12 | Dash Dot Dot | 2 | - . . - . . |
  672. // | 13 | SlantDash Dot | 2 | / - . / - . |
  673. //
  674. // The following shows the borders in the order shown in the Excel dialog:
  675. //
  676. // | Index | Style | Index | Style |
  677. // +-------+-------------+-------+-------------+
  678. // | 0 | None | 12 | - . . - . . |
  679. // | 7 | ----------- | 13 | / - . / - . |
  680. // | 4 | . . . . . . | 10 | - . - . - . |
  681. // | 11 | - . . - . . | 8 | - - - - - - |
  682. // | 9 | - . - . - . | 2 | ----------- |
  683. // | 3 | - - - - - - | 5 | ----------- |
  684. // | 1 | ----------- | 6 | =========== |
  685. //
  686. // The following shows the shading styles sorted by excelize index number:
  687. //
  688. // | Index | Style | Index | Style |
  689. // +-------+-----------------+-------+-----------------+
  690. // | 0 | Horizontal | 3 | Diagonal down |
  691. // | 1 | Vertical | 4 | From corner |
  692. // | 2 | Diagonal Up | 5 | From center |
  693. //
  694. // The following shows the patterns styles sorted by excelize index number:
  695. //
  696. // | Index | Style | Index | Style |
  697. // +-------+-----------------+-------+-----------------+
  698. // | 0 | None | 10 | darkTrellis |
  699. // | 1 | solid | 11 | lightHorizontal |
  700. // | 2 | mediumGray | 12 | lightVertical |
  701. // | 3 | darkGray | 13 | lightDown |
  702. // | 4 | lightGray | 14 | lightUp |
  703. // | 5 | darkHorizontal | 15 | lightGrid |
  704. // | 6 | darkVertical | 16 | lightTrellis |
  705. // | 7 | darkDown | 17 | gray125 |
  706. // | 8 | darkUp | 18 | gray0625 |
  707. // | 9 | darkGrid | | |
  708. //
  709. // The following the type of horizontal alignment in cells:
  710. //
  711. // | Style |
  712. // +------------------+
  713. // | left |
  714. // | center |
  715. // | right |
  716. // | fill |
  717. // | justify |
  718. // | centerContinuous |
  719. // | distributed |
  720. //
  721. // The following the type of vertical alignment in cells:
  722. //
  723. // | Style |
  724. // +------------------+
  725. // | top |
  726. // | center |
  727. // | justify |
  728. // | distributed |
  729. //
  730. // The following the type of font underline style:
  731. //
  732. // | Style |
  733. // +------------------+
  734. // | single |
  735. // | double |
  736. //
  737. // Excel's built-in all languages formats are shown in the following table:
  738. //
  739. // | Index | Format String |
  740. // +-------+----------------------------------------------------+
  741. // | 0 | General |
  742. // | 1 | 0 |
  743. // | 2 | 0.00 |
  744. // | 3 | #,##0 |
  745. // | 4 | #,##0.00 |
  746. // | 5 | ($#,##0_);($#,##0) |
  747. // | 6 | ($#,##0_);[Red]($#,##0) |
  748. // | 7 | ($#,##0.00_);($#,##0.00) |
  749. // | 8 | ($#,##0.00_);[Red]($#,##0.00) |
  750. // | 9 | 0% |
  751. // | 10 | 0.00% |
  752. // | 11 | 0.00E+00 |
  753. // | 12 | # ?/? |
  754. // | 13 | # ??/?? |
  755. // | 14 | m/d/yy |
  756. // | 15 | d-mmm-yy |
  757. // | 16 | d-mmm |
  758. // | 17 | mmm-yy |
  759. // | 18 | h:mm AM/PM |
  760. // | 19 | h:mm:ss AM/PM |
  761. // | 20 | h:mm |
  762. // | 21 | h:mm:ss |
  763. // | 22 | m/d/yy h:mm |
  764. // | ... | ... |
  765. // | 37 | (#,##0_);(#,##0) |
  766. // | 38 | (#,##0_);[Red](#,##0) |
  767. // | 39 | (#,##0.00_);(#,##0.00) |
  768. // | 40 | (#,##0.00_);[Red](#,##0.00) |
  769. // | 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
  770. // | 42 | _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_) |
  771. // | 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
  772. // | 44 | _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_) |
  773. // | 45 | mm:ss |
  774. // | 46 | [h]:mm:ss |
  775. // | 47 | mm:ss.0 |
  776. // | 48 | ##0.0E+0 |
  777. // | 49 | @ |
  778. //
  779. // Excelize built-in currency formats are shown in the following table, only
  780. // support these types in the following table (Index number is used only for
  781. // markup and is not used inside an Excel file and you can't get formatted value
  782. // by the function GetCellValue) currently:
  783. //
  784. // | Index | Symbol |
  785. // +-------+---------------------------------------------------------------+
  786. // | 164 | CN¥ |
  787. // | 165 | $ English (China) |
  788. // | 166 | $ Cherokee (United States) |
  789. // | 167 | $ Chinese (Singapore) |
  790. // | 168 | $ Chinese (Taiwan) |
  791. // | 169 | $ English (Australia) |
  792. // | 170 | $ English (Belize) |
  793. // | 171 | $ English (Canada) |
  794. // | 172 | $ English (Jamaica) |
  795. // | 173 | $ English (New Zealand) |
  796. // | 174 | $ English (Singapore) |
  797. // | 175 | $ English (Trinidad & Tobago) |
  798. // | 176 | $ English (U.S. Vigin Islands) |
  799. // | 177 | $ English (United States) |
  800. // | 178 | $ French (Canada) |
  801. // | 179 | $ Hawaiian (United States) |
  802. // | 180 | $ Malay (Brunei) |
  803. // | 181 | $ Quechua (Ecuador) |
  804. // | 182 | $ Spanish (Chile) |
  805. // | 183 | $ Spanish (Colombia) |
  806. // | 184 | $ Spanish (Ecuador) |
  807. // | 185 | $ Spanish (El Salvador) |
  808. // | 186 | $ Spanish (Mexico) |
  809. // | 187 | $ Spanish (Puerto Rico) |
  810. // | 188 | $ Spanish (United States) |
  811. // | 189 | $ Spanish (Uruguay) |
  812. // | 190 | £ English (United Kingdom) |
  813. // | 191 | £ Scottish Gaelic (United Kingdom) |
  814. // | 192 | £ Welsh (United Kindom) |
  815. // | 193 | ¥ Chinese (China) |
  816. // | 194 | ¥ Japanese (Japan) |
  817. // | 195 | ¥ Sichuan Yi (China) |
  818. // | 196 | ¥ Tibetan (China) |
  819. // | 197 | ¥ Uyghur (China) |
  820. // | 198 | ֏ Armenian (Armenia) |
  821. // | 199 | ؋ Pashto (Afghanistan) |
  822. // | 200 | ؋ Persian (Afghanistan) |
  823. // | 201 | ৳ Bengali (Bangladesh) |
  824. // | 202 | ៛ Khmer (Cambodia) |
  825. // | 203 | ₡ Spanish (Costa Rica) |
  826. // | 204 | ₦ Hausa (Nigeria) |
  827. // | 205 | ₦ Igbo (Nigeria) |
  828. // | 206 | ₦ Yoruba (Nigeria) |
  829. // | 207 | ₩ Korean (South Korea) |
  830. // | 208 | ₪ Hebrew (Israel) |
  831. // | 209 | ₫ Vietnamese (Vietnam) |
  832. // | 210 | € Basque (Spain) |
  833. // | 211 | € Breton (France) |
  834. // | 212 | € Catalan (Spain) |
  835. // | 213 | € Corsican (France) |
  836. // | 214 | € Dutch (Belgium) |
  837. // | 215 | € Dutch (Netherlands) |
  838. // | 216 | € English (Ireland) |
  839. // | 217 | € Estonian (Estonia) |
  840. // | 218 | € Euro (€ 123) |
  841. // | 219 | € Euro (123 €) |
  842. // | 220 | € Finnish (Finland) |
  843. // | 221 | € French (Belgium) |
  844. // | 222 | € French (France) |
  845. // | 223 | € French (Luxembourg) |
  846. // | 224 | € French (Monaco) |
  847. // | 225 | € French (Réunion) |
  848. // | 226 | € Galician (Spain) |
  849. // | 227 | € German (Austria) |
  850. // | 228 | € German (Luxembourg) |
  851. // | 229 | € Greek (Greece) |
  852. // | 230 | € Inari Sami (Finland) |
  853. // | 231 | € Irish (Ireland) |
  854. // | 232 | € Italian (Italy) |
  855. // | 233 | € Latin (Italy) |
  856. // | 234 | € Latin, Serbian (Montenegro) |
  857. // | 235 | € Larvian (Latvia) |
  858. // | 236 | € Lithuanian (Lithuania) |
  859. // | 237 | € Lower Sorbian (Germany) |
  860. // | 238 | € Luxembourgish (Luxembourg) |
  861. // | 239 | € Maltese (Malta) |
  862. // | 240 | € Northern Sami (Finland) |
  863. // | 241 | € Occitan (France) |
  864. // | 242 | € Portuguese (Portugal) |
  865. // | 243 | € Serbian (Montenegro) |
  866. // | 244 | € Skolt Sami (Finland) |
  867. // | 245 | € Slovak (Slovakia) |
  868. // | 246 | € Slovenian (Slovenia) |
  869. // | 247 | € Spanish (Spain) |
  870. // | 248 | € Swedish (Finland) |
  871. // | 249 | € Swiss German (France) |
  872. // | 250 | € Upper Sorbian (Germany) |
  873. // | 251 | € Western Frisian (Netherlands) |
  874. // | 252 | ₭ Lao (Laos) |
  875. // | 253 | ₮ Mongolian (Mongolia) |
  876. // | 254 | ₮ Mongolian, Mongolian (Mongolia) |
  877. // | 255 | ₱ English (Philippines) |
  878. // | 256 | ₱ Filipino (Philippines) |
  879. // | 257 | ₴ Ukrainian (Ukraine) |
  880. // | 258 | ₸ Kazakh (Kazakhstan) |
  881. // | 259 | ₹ Arabic, Kashmiri (India) |
  882. // | 260 | ₹ English (India) |
  883. // | 261 | ₹ Gujarati (India) |
  884. // | 262 | ₹ Hindi (India) |
  885. // | 263 | ₹ Kannada (India) |
  886. // | 264 | ₹ Kashmiri (India) |
  887. // | 265 | ₹ Konkani (India) |
  888. // | 266 | ₹ Manipuri (India) |
  889. // | 267 | ₹ Marathi (India) |
  890. // | 268 | ₹ Nepali (India) |
  891. // | 269 | ₹ Oriya (India) |
  892. // | 270 | ₹ Punjabi (India) |
  893. // | 271 | ₹ Sanskrit (India) |
  894. // | 272 | ₹ Sindhi (India) |
  895. // | 273 | ₹ Tamil (India) |
  896. // | 274 | ₹ Urdu (India) |
  897. // | 275 | ₺ Turkish (Turkey) |
  898. // | 276 | ₼ Azerbaijani (Azerbaijan) |
  899. // | 277 | ₼ Cyrillic, Azerbaijani (Azerbaijan) |
  900. // | 278 | ₽ Russian (Russia) |
  901. // | 279 | ₽ Sakha (Russia) |
  902. // | 280 | ₾ Georgian (Georgia) |
  903. // | 281 | B/. Spanish (Panama) |
  904. // | 282 | Br Oromo (Ethiopia) |
  905. // | 283 | Br Somali (Ethiopia) |
  906. // | 284 | Br Tigrinya (Ethiopia) |
  907. // | 285 | Bs Quechua (Bolivia) |
  908. // | 286 | Bs Spanish (Bolivia) |
  909. // | 287 | BS. Spanish (Venezuela) |
  910. // | 288 | BWP Tswana (Botswana) |
  911. // | 289 | C$ Spanish (Nicaragua) |
  912. // | 290 | CA$ Latin, Inuktitut (Canada) |
  913. // | 291 | CA$ Mohawk (Canada) |
  914. // | 292 | CA$ Unified Canadian Aboriginal Syllabics, Inuktitut (Canada) |
  915. // | 293 | CFA French (Mali) |
  916. // | 294 | CFA French (Senegal) |
  917. // | 295 | CFA Fulah (Senegal) |
  918. // | 296 | CFA Wolof (Senegal) |
  919. // | 297 | CHF French (Switzerland) |
  920. // | 298 | CHF German (Liechtenstein) |
  921. // | 299 | CHF German (Switzerland) |
  922. // | 300 | CHF Italian (Switzerland) |
  923. // | 301 | CHF Romansh (Switzerland) |
  924. // | 302 | CLP Mapuche (Chile) |
  925. // | 303 | CN¥ Mongolian, Mongolian (China) |
  926. // | 304 | DZD Central Atlas Tamazight (Algeria) |
  927. // | 305 | FCFA French (Cameroon) |
  928. // | 306 | Ft Hungarian (Hungary) |
  929. // | 307 | G French (Haiti) |
  930. // | 308 | Gs. Spanish (Paraguay) |
  931. // | 309 | GTQ K'iche' (Guatemala) |
  932. // | 310 | HK$ Chinese (Hong Kong (China)) |
  933. // | 311 | HK$ English (Hong Kong (China)) |
  934. // | 312 | HRK Croatian (Croatia) |
  935. // | 313 | IDR English (Indonesia) |
  936. // | 314 | IQD Arbic, Central Kurdish (Iraq) |
  937. // | 315 | ISK Icelandic (Iceland) |
  938. // | 316 | K Burmese (Myanmar (Burma)) |
  939. // | 317 | Kč Czech (Czech Republic) |
  940. // | 318 | KM Bosnian (Bosnia & Herzegovina) |
  941. // | 319 | KM Croatian (Bosnia & Herzegovina) |
  942. // | 320 | KM Latin, Serbian (Bosnia & Herzegovina) |
  943. // | 321 | kr Faroese (Faroe Islands) |
  944. // | 322 | kr Northern Sami (Norway) |
  945. // | 323 | kr Northern Sami (Sweden) |
  946. // | 324 | kr Norwegian Bokmål (Norway) |
  947. // | 325 | kr Norwegian Nynorsk (Norway) |
  948. // | 326 | kr Swedish (Sweden) |
  949. // | 327 | kr. Danish (Denmark) |
  950. // | 328 | kr. Kalaallisut (Greenland) |
  951. // | 329 | Ksh Swahili (kenya) |
  952. // | 330 | L Romanian (Moldova) |
  953. // | 331 | L Russian (Moldova) |
  954. // | 332 | L Spanish (Honduras) |
  955. // | 333 | Lekë Albanian (Albania) |
  956. // | 334 | MAD Arabic, Central Atlas Tamazight (Morocco) |
  957. // | 335 | MAD French (Morocco) |
  958. // | 336 | MAD Tifinagh, Central Atlas Tamazight (Morocco) |
  959. // | 337 | MOP$ Chinese (Macau (China)) |
  960. // | 338 | MVR Divehi (Maldives) |
  961. // | 339 | Nfk Tigrinya (Eritrea) |
  962. // | 340 | NGN Bini (Nigeria) |
  963. // | 341 | NGN Fulah (Nigeria) |
  964. // | 342 | NGN Ibibio (Nigeria) |
  965. // | 343 | NGN Kanuri (Nigeria) |
  966. // | 344 | NOK Lule Sami (Norway) |
  967. // | 345 | NOK Southern Sami (Norway) |
  968. // | 346 | NZ$ Maori (New Zealand) |
  969. // | 347 | PKR Sindhi (Pakistan) |
  970. // | 348 | PYG Guarani (Paraguay) |
  971. // | 349 | Q Spanish (Guatemala) |
  972. // | 350 | R Afrikaans (South Africa) |
  973. // | 351 | R English (South Africa) |
  974. // | 352 | R Zulu (South Africa) |
  975. // | 353 | R$ Portuguese (Brazil) |
  976. // | 354 | RD$ Spanish (Dominican Republic) |
  977. // | 355 | RF Kinyarwanda (Rwanda) |
  978. // | 356 | RM English (Malaysia) |
  979. // | 357 | RM Malay (Malaysia) |
  980. // | 358 | RON Romanian (Romania) |
  981. // | ... | ... |
  982. //
  983. func (f *File) NewStyle(style string) (int, error) {
  984. var cellXfsID int
  985. styleSheet := f.stylesReader()
  986. formatCellStyle, err := parseFormatStyleSet(style)
  987. if err != nil {
  988. return cellXfsID, err
  989. }
  990. numFmtID := setNumFmt(styleSheet, formatCellStyle)
  991. fontID := setFont(styleSheet, formatCellStyle)
  992. borderID := setBorders(styleSheet, formatCellStyle)
  993. fillID := setFills(styleSheet, formatCellStyle)
  994. applyAlignment, alignment := setAlignment(styleSheet, formatCellStyle)
  995. cellXfsID = setCellXfs(styleSheet, fontID, numFmtID, fillID, borderID, applyAlignment, alignment)
  996. return cellXfsID, nil
  997. }
  998. // setFont provides function to add font style by given cell format settings.
  999. func setFont(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  1000. if formatCellStyle.Font == nil {
  1001. return 0
  1002. }
  1003. fontUnderlineType := map[string]string{"single": "single", "double": "double"}
  1004. if formatCellStyle.Font.Family == "" {
  1005. formatCellStyle.Font.Family = "Calibri"
  1006. }
  1007. if formatCellStyle.Font.Size < 1 {
  1008. formatCellStyle.Font.Size = 11
  1009. }
  1010. if formatCellStyle.Font.Color == "" {
  1011. formatCellStyle.Font.Color = "#000000"
  1012. }
  1013. f := font{
  1014. B: formatCellStyle.Font.Bold,
  1015. I: formatCellStyle.Font.Italic,
  1016. Sz: &attrValInt{Val: formatCellStyle.Font.Size},
  1017. Color: &xlsxColor{RGB: getPaletteColor(formatCellStyle.Font.Color)},
  1018. Name: &attrValString{Val: formatCellStyle.Font.Family},
  1019. Family: &attrValInt{Val: 2},
  1020. Scheme: &attrValString{Val: "minor"},
  1021. }
  1022. val, ok := fontUnderlineType[formatCellStyle.Font.Underline]
  1023. if ok {
  1024. f.U = &attrValString{Val: val}
  1025. }
  1026. font, _ := xml.Marshal(f)
  1027. style.Fonts.Count++
  1028. style.Fonts.Font = append(style.Fonts.Font, &xlsxFont{
  1029. Font: string(font[6 : len(font)-7]),
  1030. })
  1031. return style.Fonts.Count - 1
  1032. }
  1033. // setNumFmt provides function to check if number format code in the range of
  1034. // built-in values.
  1035. func setNumFmt(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  1036. dp := "0."
  1037. numFmtID := 164 // Default custom number format code from 164.
  1038. if formatCellStyle.DecimalPlaces < 0 || formatCellStyle.DecimalPlaces > 30 {
  1039. formatCellStyle.DecimalPlaces = 2
  1040. }
  1041. for i := 0; i < formatCellStyle.DecimalPlaces; i++ {
  1042. dp += "0"
  1043. }
  1044. _, ok := builtInNumFmt[formatCellStyle.NumFmt]
  1045. if !ok {
  1046. fc, currency := currencyNumFmt[formatCellStyle.NumFmt]
  1047. if !currency {
  1048. return setLangNumFmt(style, formatCellStyle)
  1049. }
  1050. fc = strings.Replace(fc, "0.00", dp, -1)
  1051. if formatCellStyle.NegRed {
  1052. fc = fc + ";[Red]" + fc
  1053. }
  1054. if style.NumFmts != nil {
  1055. numFmtID = style.NumFmts.NumFmt[len(style.NumFmts.NumFmt)-1].NumFmtID + 1
  1056. nf := xlsxNumFmt{
  1057. FormatCode: fc,
  1058. NumFmtID: numFmtID,
  1059. }
  1060. style.NumFmts.NumFmt = append(style.NumFmts.NumFmt, &nf)
  1061. style.NumFmts.Count++
  1062. } else {
  1063. nf := xlsxNumFmt{
  1064. FormatCode: fc,
  1065. NumFmtID: numFmtID,
  1066. }
  1067. numFmts := xlsxNumFmts{
  1068. NumFmt: []*xlsxNumFmt{&nf},
  1069. Count: 1,
  1070. }
  1071. style.NumFmts = &numFmts
  1072. }
  1073. return numFmtID
  1074. }
  1075. return formatCellStyle.NumFmt
  1076. }
  1077. // setLangNumFmt provides function to set number format code with language.
  1078. func setLangNumFmt(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  1079. numFmts, ok := langNumFmt[formatCellStyle.Lang]
  1080. if !ok {
  1081. return 0
  1082. }
  1083. var fc string
  1084. fc, ok = numFmts[formatCellStyle.NumFmt]
  1085. if !ok {
  1086. return 0
  1087. }
  1088. nf := xlsxNumFmt{
  1089. FormatCode: fc,
  1090. NumFmtID: formatCellStyle.NumFmt,
  1091. }
  1092. if style.NumFmts != nil {
  1093. style.NumFmts.NumFmt = append(style.NumFmts.NumFmt, &nf)
  1094. style.NumFmts.Count++
  1095. } else {
  1096. numFmts := xlsxNumFmts{
  1097. NumFmt: []*xlsxNumFmt{&nf},
  1098. Count: 1,
  1099. }
  1100. style.NumFmts = &numFmts
  1101. }
  1102. return formatCellStyle.NumFmt
  1103. }
  1104. // setFills provides function to add fill elements in the styles.xml by given
  1105. // cell format settings.
  1106. func setFills(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  1107. var patterns = []string{
  1108. "none",
  1109. "solid",
  1110. "mediumGray",
  1111. "darkGray",
  1112. "lightGray",
  1113. "darkHorizontal",
  1114. "darkVertical",
  1115. "darkDown",
  1116. "darkUp",
  1117. "darkGrid",
  1118. "darkTrellis",
  1119. "lightHorizontal",
  1120. "lightVertical",
  1121. "lightDown",
  1122. "lightUp",
  1123. "lightGrid",
  1124. "lightTrellis",
  1125. "gray125",
  1126. "gray0625",
  1127. }
  1128. var variants = []float64{
  1129. 90,
  1130. 0,
  1131. 45,
  1132. 135,
  1133. }
  1134. var fill xlsxFill
  1135. switch formatCellStyle.Fill.Type {
  1136. case "gradient":
  1137. if len(formatCellStyle.Fill.Color) != 2 {
  1138. break
  1139. }
  1140. var gradient xlsxGradientFill
  1141. switch formatCellStyle.Fill.Shading {
  1142. case 0, 1, 2, 3:
  1143. gradient.Degree = variants[formatCellStyle.Fill.Shading]
  1144. case 4:
  1145. gradient.Type = "path"
  1146. case 5:
  1147. gradient.Type = "path"
  1148. gradient.Bottom = 0.5
  1149. gradient.Left = 0.5
  1150. gradient.Right = 0.5
  1151. gradient.Top = 0.5
  1152. default:
  1153. break
  1154. }
  1155. var stops []*xlsxGradientFillStop
  1156. for index, color := range formatCellStyle.Fill.Color {
  1157. var stop xlsxGradientFillStop
  1158. stop.Position = float64(index)
  1159. stop.Color.RGB = getPaletteColor(color)
  1160. stops = append(stops, &stop)
  1161. }
  1162. gradient.Stop = stops
  1163. fill.GradientFill = &gradient
  1164. case "pattern":
  1165. if formatCellStyle.Fill.Pattern > 18 || formatCellStyle.Fill.Pattern < 0 {
  1166. break
  1167. }
  1168. if len(formatCellStyle.Fill.Color) < 1 {
  1169. break
  1170. }
  1171. var pattern xlsxPatternFill
  1172. pattern.PatternType = patterns[formatCellStyle.Fill.Pattern]
  1173. pattern.FgColor.RGB = getPaletteColor(formatCellStyle.Fill.Color[0])
  1174. fill.PatternFill = &pattern
  1175. }
  1176. style.Fills.Count++
  1177. style.Fills.Fill = append(style.Fills.Fill, &fill)
  1178. return style.Fills.Count - 1
  1179. }
  1180. // setAlignment provides function to formatting information pertaining to text
  1181. // alignment in cells. There are a variety of choices for how text is aligned
  1182. // both horizontally and vertically, as well as indentation settings, and so on.
  1183. func setAlignment(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) (bool, *xlsxAlignment) {
  1184. if formatCellStyle.Alignment == nil {
  1185. return false, &xlsxAlignment{}
  1186. }
  1187. var alignment = xlsxAlignment{
  1188. Horizontal: formatCellStyle.Alignment.Horizontal,
  1189. Indent: formatCellStyle.Alignment.Indent,
  1190. JustifyLastLine: formatCellStyle.Alignment.JustifyLastLine,
  1191. ReadingOrder: formatCellStyle.Alignment.ReadingOrder,
  1192. RelativeIndent: formatCellStyle.Alignment.RelativeIndent,
  1193. ShrinkToFit: formatCellStyle.Alignment.ShrinkToFit,
  1194. TextRotation: formatCellStyle.Alignment.TextRotation,
  1195. Vertical: formatCellStyle.Alignment.Vertical,
  1196. WrapText: formatCellStyle.Alignment.WrapText,
  1197. }
  1198. return true, &alignment
  1199. }
  1200. // setBorders provides function to add border elements in the styles.xml by
  1201. // given borders format settings.
  1202. func setBorders(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  1203. var styles = []string{
  1204. "none",
  1205. "thin",
  1206. "medium",
  1207. "dashed",
  1208. "dotted",
  1209. "thick",
  1210. "double",
  1211. "hair",
  1212. "mediumDashed",
  1213. "dashDot",
  1214. "mediumDashDot",
  1215. "dashDotDot",
  1216. "mediumDashDotDot",
  1217. "slantDashDot",
  1218. }
  1219. var border xlsxBorder
  1220. for _, v := range formatCellStyle.Border {
  1221. if v.Style > 13 || v.Style < 0 {
  1222. continue
  1223. }
  1224. var color xlsxColor
  1225. color.RGB = getPaletteColor(v.Color)
  1226. switch v.Type {
  1227. case "left":
  1228. border.Left.Style = styles[v.Style]
  1229. border.Left.Color = &color
  1230. case "right":
  1231. border.Right.Style = styles[v.Style]
  1232. border.Right.Color = &color
  1233. case "top":
  1234. border.Top.Style = styles[v.Style]
  1235. border.Top.Color = &color
  1236. case "bottom":
  1237. border.Bottom.Style = styles[v.Style]
  1238. border.Bottom.Color = &color
  1239. case "diagonalUp":
  1240. border.Diagonal.Style = styles[v.Style]
  1241. border.Diagonal.Color = &color
  1242. border.DiagonalUp = true
  1243. case "diagonalDown":
  1244. border.Diagonal.Style = styles[v.Style]
  1245. border.Diagonal.Color = &color
  1246. border.DiagonalDown = true
  1247. }
  1248. }
  1249. style.Borders.Count++
  1250. style.Borders.Border = append(style.Borders.Border, &border)
  1251. return style.Borders.Count - 1
  1252. }
  1253. // setCellXfs provides function to set describes all of the formatting for a
  1254. // cell.
  1255. func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment bool, alignment *xlsxAlignment) int {
  1256. var xf xlsxXf
  1257. xf.FontID = fontID
  1258. if fontID != 0 {
  1259. xf.ApplyFont = true
  1260. }
  1261. xf.NumFmtID = numFmtID
  1262. if numFmtID != 0 {
  1263. xf.ApplyNumberFormat = true
  1264. }
  1265. xf.FillID = fillID
  1266. xf.BorderID = borderID
  1267. style.CellXfs.Count++
  1268. xf.Alignment = alignment
  1269. xf.ApplyAlignment = applyAlignment
  1270. style.CellXfs.Xf = append(style.CellXfs.Xf, xf)
  1271. return style.CellXfs.Count - 1
  1272. }
  1273. // SetCellStyle provides function to add style attribute for cells by given
  1274. // worksheet sheet index, coordinate area and style ID. Note that diagonalDown
  1275. // and diagonalUp type border should be use same color in the same coordinate
  1276. // area.
  1277. //
  1278. // For example create a borders of cell H9 on Sheet1:
  1279. //
  1280. // style, err := xlsx.NewStyle(`{"border":[{"type":"left","color":"0000FF","style":3},{"type":"top","color":"00FF00","style":4},{"type":"bottom","color":"FFFF00","style":5},{"type":"right","color":"FF0000","style":6},{"type":"diagonalDown","color":"A020F0","style":7},{"type":"diagonalUp","color":"A020F0","style":8}]}`)
  1281. // if err != nil {
  1282. // fmt.Println(err)
  1283. // }
  1284. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1285. //
  1286. // Set gradient fill with vertical variants shading styles for cell H9 on
  1287. // Sheet1:
  1288. //
  1289. // style, err := xlsx.NewStyle(`{"fill":{"type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":1}}`)
  1290. // if err != nil {
  1291. // fmt.Println(err)
  1292. // }
  1293. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1294. //
  1295. // Set solid style pattern fill for cell H9 on Sheet1:
  1296. //
  1297. // style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["#E0EBF5"],"pattern":1}}`)
  1298. // if err != nil {
  1299. // fmt.Println(err)
  1300. // }
  1301. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1302. //
  1303. // Set alignment style for cell H9 on Sheet1:
  1304. //
  1305. // style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center","ident":1,"justify_last_line":true,"reading_order":0,"relative_indent":1,"shrink_to_fit":true,"text_rotation":45,"vertical":"","wrap_text":true}}`)
  1306. // if err != nil {
  1307. // fmt.Println(err)
  1308. // }
  1309. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1310. //
  1311. // Dates and times in Excel are represented by real numbers, for example "Apr 7
  1312. // 2017 12:00 PM" is represented by the number 42920.5. Set date and time format
  1313. // for cell H9 on Sheet1:
  1314. //
  1315. // xlsx.SetCellValue("Sheet1", "H9", 42920.5)
  1316. // style, err := xlsx.NewStyle(`{"number_format": 22}`)
  1317. // if err != nil {
  1318. // fmt.Println(err)
  1319. // }
  1320. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1321. //
  1322. // Set font style for cell H9 on Sheet1:
  1323. //
  1324. // style, err := xlsx.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`)
  1325. // if err != nil {
  1326. // fmt.Println(err)
  1327. // }
  1328. // xlsx.SetCellStyle("Sheet1", "H9", "H9", style)
  1329. //
  1330. func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
  1331. hcell = strings.ToUpper(hcell)
  1332. vcell = strings.ToUpper(vcell)
  1333. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  1334. hcol := string(strings.Map(letterOnlyMapF, hcell))
  1335. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  1336. hyAxis := hrow - 1
  1337. hxAxis := TitleToNumber(hcol)
  1338. vcol := string(strings.Map(letterOnlyMapF, vcell))
  1339. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  1340. vyAxis := vrow - 1
  1341. vxAxis := TitleToNumber(vcol)
  1342. if vxAxis < hxAxis {
  1343. hcell, vcell = vcell, hcell
  1344. vxAxis, hxAxis = hxAxis, vxAxis
  1345. }
  1346. if vyAxis < hyAxis {
  1347. hcell, vcell = vcell, hcell
  1348. vyAxis, hyAxis = hyAxis, vyAxis
  1349. }
  1350. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  1351. hcell = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1)
  1352. vcell = ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
  1353. xlsx := f.workSheetReader(sheet)
  1354. completeRow(xlsx, vyAxis+1, vxAxis+1)
  1355. completeCol(xlsx, vyAxis+1, vxAxis+1)
  1356. for r, row := range xlsx.SheetData.Row {
  1357. for k, c := range row.C {
  1358. if checkCellInArea(c.R, hcell+":"+vcell) {
  1359. xlsx.SheetData.Row[r].C[k].S = styleID
  1360. }
  1361. }
  1362. }
  1363. }
  1364. // getPaletteColor provides function to convert the RBG color by given string.
  1365. func getPaletteColor(color string) string {
  1366. return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1)
  1367. }