styles.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. // builtInNumFmtFunc defined the format conversion functions map. Partial format
  48. // code doesn't support currently and will return original string.
  49. var builtInNumFmtFunc = map[int]func(i int, v string) string{
  50. 0: formatToString,
  51. 1: formatToInt,
  52. 2: formatToFloat,
  53. 3: formatToInt,
  54. 4: formatToFloat,
  55. 9: formatToC,
  56. 10: formatToD,
  57. 11: formatToE,
  58. 12: formatToString, // Doesn't support currently
  59. 13: formatToString, // Doesn't support currently
  60. 14: parseTime,
  61. 15: parseTime,
  62. 16: parseTime,
  63. 17: parseTime,
  64. 18: parseTime,
  65. 19: parseTime,
  66. 20: parseTime,
  67. 21: parseTime,
  68. 22: parseTime,
  69. 37: formatToA,
  70. 38: formatToA,
  71. 39: formatToB,
  72. 40: formatToB,
  73. 41: formatToString, // Doesn't support currently
  74. 42: formatToString, // Doesn't support currently
  75. 43: formatToString, // Doesn't support currently
  76. 44: formatToString, // Doesn't support currently
  77. 45: parseTime,
  78. 46: parseTime,
  79. 47: parseTime,
  80. 48: formatToE,
  81. 49: formatToString,
  82. }
  83. // formatToString provides function to return original string by given built-in
  84. // number formats code and cell string.
  85. func formatToString(i int, v string) string {
  86. return v
  87. }
  88. // formatToInt provides function to convert original string to integer format as
  89. // string type by given built-in number formats code and cell string.
  90. func formatToInt(i int, v string) string {
  91. f, err := strconv.ParseFloat(v, 64)
  92. if err != nil {
  93. return v
  94. }
  95. return fmt.Sprintf("%d", int(f))
  96. }
  97. // formatToFloat provides function to convert original string to float format as
  98. // string type by given built-in number formats code and cell string.
  99. func formatToFloat(i int, v string) string {
  100. f, err := strconv.ParseFloat(v, 64)
  101. if err != nil {
  102. return v
  103. }
  104. return fmt.Sprintf("%.2f", f)
  105. }
  106. // formatToA provides function to convert original string to special format as
  107. // string type by given built-in number formats code and cell string.
  108. func formatToA(i int, v string) string {
  109. f, err := strconv.ParseFloat(v, 64)
  110. if err != nil {
  111. return v
  112. }
  113. if f < 0 {
  114. t := int(math.Abs(f))
  115. return fmt.Sprintf("(%d)", t)
  116. }
  117. t := int(f)
  118. return fmt.Sprintf("%d", t)
  119. }
  120. // formatToB provides function to convert original string to special format as
  121. // string type by given built-in number formats code and cell string.
  122. func formatToB(i int, v string) string {
  123. f, err := strconv.ParseFloat(v, 64)
  124. if err != nil {
  125. return v
  126. }
  127. if f < 0 {
  128. return fmt.Sprintf("(%.2f)", f)
  129. }
  130. return fmt.Sprintf("%.2f", f)
  131. }
  132. // formatToC provides function to convert original string to special format as
  133. // string type by given built-in number formats code and cell string.
  134. func formatToC(i int, v string) string {
  135. f, err := strconv.ParseFloat(v, 64)
  136. if err != nil {
  137. return v
  138. }
  139. f = f * 100
  140. return fmt.Sprintf("%d%%", int(f))
  141. }
  142. // formatToD provides function to convert original string to special format as
  143. // string type by given built-in number formats code and cell string.
  144. func formatToD(i int, v string) string {
  145. f, err := strconv.ParseFloat(v, 64)
  146. if err != nil {
  147. return v
  148. }
  149. f = f * 100
  150. return fmt.Sprintf("%.2f%%", f)
  151. }
  152. // formatToE provides function to convert original string to special format as
  153. // string type by given built-in number formats code and cell string.
  154. func formatToE(i int, v string) string {
  155. f, err := strconv.ParseFloat(v, 64)
  156. if err != nil {
  157. return v
  158. }
  159. return fmt.Sprintf("%.e", f)
  160. }
  161. // parseTime provides function to returns a string parsed using time.Time.
  162. // Replace Excel placeholders with Go time placeholders. For example, replace
  163. // yyyy with 2006. These are in a specific order, due to the fact that m is used
  164. // in month, minute, and am/pm. It would be easier to fix that with regular
  165. // expressions, but if it's possible to keep this simple it would be easier to
  166. // maintain. Full-length month and days (e.g. March, Tuesday) have letters in
  167. // them that would be replaced by other characters below (such as the 'h' in
  168. // March, or the 'd' in Tuesday) below. First we convert them to arbitrary
  169. // characters unused in Excel Date formats, and then at the end, turn them to
  170. // what they should actually be.
  171. func parseTime(i int, v string) string {
  172. f, err := strconv.ParseFloat(v, 64)
  173. if err != nil {
  174. return v
  175. }
  176. val := timeFromExcelTime(f, false)
  177. format := builtInNumFmt[i]
  178. replacements := []struct{ xltime, gotime string }{
  179. {"yyyy", "2006"},
  180. {"yy", "06"},
  181. {"mmmm", "%%%%"},
  182. {"dddd", "&&&&"},
  183. {"dd", "02"},
  184. {"d", "2"},
  185. {"mmm", "Jan"},
  186. {"mmss", "0405"},
  187. {"ss", "05"},
  188. {"hh", "15"},
  189. {"h", "3"},
  190. {"mm:", "04:"},
  191. {":mm", ":04"},
  192. {"mm", "01"},
  193. {"am/pm", "pm"},
  194. {"m/", "1/"},
  195. {"%%%%", "January"},
  196. {"&&&&", "Monday"},
  197. }
  198. for _, repl := range replacements {
  199. format = strings.Replace(format, repl.xltime, repl.gotime, 1)
  200. }
  201. // If the hour is optional, strip it out, along with the possible dangling
  202. // colon that would remain.
  203. if val.Hour() < 1 {
  204. format = strings.Replace(format, "]:", "]", 1)
  205. format = strings.Replace(format, "[3]", "", 1)
  206. format = strings.Replace(format, "[15]", "", 1)
  207. } else {
  208. format = strings.Replace(format, "[3]", "3", 1)
  209. format = strings.Replace(format, "[15]", "15", 1)
  210. }
  211. return val.Format(format)
  212. }
  213. // parseFormatStyleSet provides function to parse the format settings of the
  214. // borders.
  215. func parseFormatStyleSet(style string) (*formatCellStyle, error) {
  216. var format formatCellStyle
  217. err := json.Unmarshal([]byte(style), &format)
  218. return &format, err
  219. }
  220. // SetCellStyle provides function to set style for cells by given sheet index
  221. // and coordinate area in XLSX file. Note that the color field uses RGB color
  222. // code and diagonalDown and diagonalUp type border should be use same color in
  223. // the same coordinate area.
  224. //
  225. // For example create a borders of cell H9 on Sheet1:
  226. //
  227. // err := xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"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}]}`)
  228. // if err != nil {
  229. // fmt.Println(err)
  230. // }
  231. //
  232. // Set gradient fill with vertical variants shading styles for cell H9 on
  233. // Sheet1:
  234. //
  235. // err := xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"fill":{"type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":1}}`)
  236. // if err != nil {
  237. // fmt.Println(err)
  238. // }
  239. //
  240. // Set solid style pattern fill for cell H9 on Sheet1:
  241. //
  242. // err := xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"fill":{"type":"pattern","color":["#E0EBF5"],"pattern":1}}`)
  243. // if err != nil {
  244. // fmt.Println(err)
  245. // }
  246. //
  247. // Set alignment style for cell H9 on Sheet1:
  248. //
  249. // err = xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"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}}`)
  250. // if err != nil {
  251. // fmt.Println(err)
  252. // }
  253. //
  254. // Dates and times in Excel are represented by real numbers, for example "Apr 7
  255. // 2017 12:00 PM" is represented by the number 42920.5. Set date and time format
  256. // for cell H9 on Sheet1:
  257. //
  258. // xlsx.SetCellValue("Sheet2", "H9", 42920.5)
  259. // err = xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"number_format": 22}`)
  260. // if err != nil {
  261. // fmt.Println(err)
  262. // }
  263. //
  264. // Set font style for cell H9 on Sheet1:
  265. //
  266. // err = xlsx.SetCellStyle("Sheet1", "H9", "H9", `{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`)
  267. // if err != nil {
  268. // fmt.Println(err)
  269. // }
  270. //
  271. // The following shows the border styles sorted by excelize index number:
  272. //
  273. // +-------+---------------+--------+-----------------+
  274. // | Index | Name | Weight | Style |
  275. // +=======+===============+========+=================+
  276. // | 0 | None | 0 | |
  277. // +-------+---------------+--------+-----------------+
  278. // | 1 | Continuous | 1 | ``-----------`` |
  279. // +-------+---------------+--------+-----------------+
  280. // | 2 | Continuous | 2 | ``-----------`` |
  281. // +-------+---------------+--------+-----------------+
  282. // | 3 | Dash | 1 | ``- - - - - -`` |
  283. // +-------+---------------+--------+-----------------+
  284. // | 4 | Dot | 1 | ``. . . . . .`` |
  285. // +-------+---------------+--------+-----------------+
  286. // | 5 | Continuous | 3 | ``-----------`` |
  287. // +-------+---------------+--------+-----------------+
  288. // | 6 | Double | 3 | ``===========`` |
  289. // +-------+---------------+--------+-----------------+
  290. // | 7 | Continuous | 0 | ``-----------`` |
  291. // +-------+---------------+--------+-----------------+
  292. // | 8 | Dash | 2 | ``- - - - - -`` |
  293. // +-------+---------------+--------+-----------------+
  294. // | 9 | Dash Dot | 1 | ``- . - . - .`` |
  295. // +-------+---------------+--------+-----------------+
  296. // | 10 | Dash Dot | 2 | ``- . - . - .`` |
  297. // +-------+---------------+--------+-----------------+
  298. // | 11 | Dash Dot Dot | 1 | ``- . . - . .`` |
  299. // +-------+---------------+--------+-----------------+
  300. // | 12 | Dash Dot Dot | 2 | ``- . . - . .`` |
  301. // +-------+---------------+--------+-----------------+
  302. // | 13 | SlantDash Dot | 2 | ``/ - . / - .`` |
  303. // +-------+---------------+--------+-----------------+
  304. //
  305. // The following shows the borders in the order shown in the Excel dialog:
  306. //
  307. // +-------+-----------------+-------+-----------------+
  308. // | Index | Style | Index | Style |
  309. // +=======+=================+=======+=================+
  310. // | 0 | None | 12 | ``- . . - . .`` |
  311. // +-------+-----------------+-------+-----------------+
  312. // | 7 | ``-----------`` | 13 | ``/ - . / - .`` |
  313. // +-------+-----------------+-------+-----------------+
  314. // | 4 | ``. . . . . .`` | 10 | ``- . - . - .`` |
  315. // +-------+-----------------+-------+-----------------+
  316. // | 11 | ``- . . - . .`` | 8 | ``- - - - - -`` |
  317. // +-------+-----------------+-------+-----------------+
  318. // | 9 | ``- . - . - .`` | 2 | ``-----------`` |
  319. // +-------+-----------------+-------+-----------------+
  320. // | 3 | ``- - - - - -`` | 5 | ``-----------`` |
  321. // +-------+-----------------+-------+-----------------+
  322. // | 1 | ``-----------`` | 6 | ``===========`` |
  323. // +-------+-----------------+-------+-----------------+
  324. //
  325. // The following shows the shading styles sorted by excelize index number:
  326. //
  327. // +-------+-----------------+-------+-----------------+
  328. // | Index | Style | Index | Style |
  329. // +=======+=================+=======+=================+
  330. // | 0 | Horizontal | 3 | Diagonal down |
  331. // +-------+-----------------+-------+-----------------+
  332. // | 1 | Vertical | 4 | From corner |
  333. // +-------+-----------------+-------+-----------------+
  334. // | 2 | Diagonal Up | 5 | From center |
  335. // +-------+-----------------+-------+-----------------+
  336. //
  337. // The following shows the patterns styles sorted by excelize index number:
  338. //
  339. // +-------+-----------------+-------+-----------------+
  340. // | Index | Style | Index | Style |
  341. // +=======+=================+=======+=================+
  342. // | 0 | None | 10 | darkTrellis |
  343. // +-------+-----------------+-------+-----------------+
  344. // | 1 | solid | 11 | lightHorizontal |
  345. // +-------+-----------------+-------+-----------------+
  346. // | 2 | mediumGray | 12 | lightVertical |
  347. // +-------+-----------------+-------+-----------------+
  348. // | 3 | darkGray | 13 | lightDown |
  349. // +-------+-----------------+-------+-----------------+
  350. // | 4 | lightGray | 14 | lightUp |
  351. // +-------+-----------------+-------+-----------------+
  352. // | 5 | darkHorizontal | 15 | lightGrid |
  353. // +-------+-----------------+-------+-----------------+
  354. // | 6 | darkVertical | 16 | lightTrellis |
  355. // +-------+-----------------+-------+-----------------+
  356. // | 7 | darkDown | 17 | gray125 |
  357. // +-------+-----------------+-------+-----------------+
  358. // | 8 | darkUp | 18 | gray0625 |
  359. // +-------+-----------------+-------+-----------------+
  360. // | 9 | darkGrid | | |
  361. // +-------+-----------------+-------+-----------------+
  362. //
  363. // The following the type of horizontal alignment in cells:
  364. //
  365. // +------------------+
  366. // | Style |
  367. // +==================+
  368. // | left |
  369. // +------------------+
  370. // | center |
  371. // +------------------+
  372. // | right |
  373. // +------------------+
  374. // | fill |
  375. // +------------------+
  376. // | justify |
  377. // +------------------+
  378. // | centerContinuous |
  379. // +------------------+
  380. // | distributed |
  381. // +------------------+
  382. //
  383. // The following the type of vertical alignment in cells:
  384. //
  385. // +------------------+
  386. // | Style |
  387. // +==================+
  388. // | top |
  389. // +------------------+
  390. // | center |
  391. // +------------------+
  392. // | justify |
  393. // +------------------+
  394. // | distributed |
  395. // +------------------+
  396. //
  397. // The following the type of font underline style:
  398. //
  399. // +------------------+
  400. // | Style |
  401. // +==================+
  402. // | single |
  403. // +------------------+
  404. // | double |
  405. // +------------------+
  406. //
  407. // Excel's built-in formats are shown in the following table:
  408. //
  409. // +-------+----------------------------------------------------+
  410. // | Index | Format String |
  411. // +=======+====================================================+
  412. // | 0 | General |
  413. // +-------+----------------------------------------------------+
  414. // | 1 | 0 |
  415. // +-------+----------------------------------------------------+
  416. // | 2 | 0.00 |
  417. // +-------+----------------------------------------------------+
  418. // | 3 | #,##0 |
  419. // +-------+----------------------------------------------------+
  420. // | 4 | #,##0.00 |
  421. // +-------+----------------------------------------------------+
  422. // | 5 | ($#,##0_);($#,##0) |
  423. // +-------+----------------------------------------------------+
  424. // | 6 | ($#,##0_);[Red]($#,##0) |
  425. // +-------+----------------------------------------------------+
  426. // | 7 | ($#,##0.00_);($#,##0.00) |
  427. // +-------+----------------------------------------------------+
  428. // | 8 | ($#,##0.00_);[Red]($#,##0.00) |
  429. // +-------+----------------------------------------------------+
  430. // | 9 | 0% |
  431. // +-------+----------------------------------------------------+
  432. // | 10 | 0.00% |
  433. // +-------+----------------------------------------------------+
  434. // | 11 | 0.00E+00 |
  435. // +-------+----------------------------------------------------+
  436. // | 12 | # ?/? |
  437. // +-------+----------------------------------------------------+
  438. // | 13 | # ??/?? |
  439. // +-------+----------------------------------------------------+
  440. // | 14 | m/d/yy |
  441. // +-------+----------------------------------------------------+
  442. // | 15 | d-mmm-yy |
  443. // +-------+----------------------------------------------------+
  444. // | 16 | d-mmm |
  445. // +-------+----------------------------------------------------+
  446. // | 17 | mmm-yy |
  447. // +-------+----------------------------------------------------+
  448. // | 18 | h:mm AM/PM |
  449. // +-------+----------------------------------------------------+
  450. // | 19 | h:mm:ss AM/PM |
  451. // +-------+----------------------------------------------------+
  452. // | 20 | h:mm |
  453. // +-------+----------------------------------------------------+
  454. // | 21 | h:mm:ss |
  455. // +-------+----------------------------------------------------+
  456. // | 22 | m/d/yy h:mm |
  457. // +-------+----------------------------------------------------+
  458. // | ... | ... |
  459. // +-------+----------------------------------------------------+
  460. // | 37 | (#,##0_);(#,##0) |
  461. // +-------+----------------------------------------------------+
  462. // | 38 | (#,##0_);[Red](#,##0) |
  463. // +-------+----------------------------------------------------+
  464. // | 39 | (#,##0.00_);(#,##0.00) |
  465. // +-------+----------------------------------------------------+
  466. // | 40 | (#,##0.00_);[Red](#,##0.00) |
  467. // +-------+----------------------------------------------------+
  468. // | 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
  469. // +-------+----------------------------------------------------+
  470. // | 42 | _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_) |
  471. // +-------+----------------------------------------------------+
  472. // | 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
  473. // +-------+----------------------------------------------------+
  474. // | 44 | _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_) |
  475. // +-------+----------------------------------------------------+
  476. // | 45 | mm:ss |
  477. // +-------+----------------------------------------------------+
  478. // | 46 | [h]:mm:ss |
  479. // +-------+----------------------------------------------------+
  480. // | 47 | mm:ss.0 |
  481. // +-------+----------------------------------------------------+
  482. // | 48 | ##0.0E+0 |
  483. // +-------+----------------------------------------------------+
  484. // | 49 | @ |
  485. // +-------+----------------------------------------------------+
  486. //
  487. func (f *File) SetCellStyle(sheet, hcell, vcell, style string) error {
  488. var styleSheet xlsxStyleSheet
  489. xml.Unmarshal([]byte(f.readXML("xl/styles.xml")), &styleSheet)
  490. formatCellStyle, err := parseFormatStyleSet(style)
  491. if err != nil {
  492. return err
  493. }
  494. numFmtID := setNumFmt(&styleSheet, formatCellStyle)
  495. fontID := setFont(&styleSheet, formatCellStyle)
  496. borderID := setBorders(&styleSheet, formatCellStyle)
  497. fillID := setFills(&styleSheet, formatCellStyle)
  498. applyAlignment, alignment := setAlignment(&styleSheet, formatCellStyle)
  499. cellXfsID := setCellXfs(&styleSheet, fontID, numFmtID, fillID, borderID, applyAlignment, alignment)
  500. output, err := xml.Marshal(styleSheet)
  501. if err != nil {
  502. return err
  503. }
  504. f.saveFileList("xl/styles.xml", replaceWorkSheetsRelationshipsNameSpace(string(output)))
  505. f.setCellStyle(sheet, hcell, vcell, cellXfsID)
  506. return err
  507. }
  508. // setFont provides function to add font style by given cell format settings.
  509. func setFont(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  510. if formatCellStyle.Font == nil {
  511. return 0
  512. }
  513. fontUnderlineType := map[string]string{"single": "single", "double": "double"}
  514. if formatCellStyle.Font.Family == "" {
  515. formatCellStyle.Font.Family = "Calibri"
  516. }
  517. if formatCellStyle.Font.Size < 1 {
  518. formatCellStyle.Font.Size = 11
  519. }
  520. if formatCellStyle.Font.Color == "" {
  521. formatCellStyle.Font.Color = "#000000"
  522. }
  523. f := font{
  524. B: formatCellStyle.Font.Bold,
  525. I: formatCellStyle.Font.Italic,
  526. Sz: &attrValInt{Val: formatCellStyle.Font.Size},
  527. Color: &xlsxColor{RGB: getPaletteColor(formatCellStyle.Font.Color)},
  528. Name: &attrValString{Val: formatCellStyle.Font.Family},
  529. Family: &attrValInt{Val: 2},
  530. Scheme: &attrValString{Val: "minor"},
  531. }
  532. val, ok := fontUnderlineType[formatCellStyle.Font.Underline]
  533. if ok {
  534. f.U = &attrValString{Val: val}
  535. }
  536. font, _ := xml.Marshal(f)
  537. style.Fonts.Count++
  538. style.Fonts.Font = append(style.Fonts.Font, &xlsxFont{
  539. Font: string(font[6 : len(font)-7]),
  540. })
  541. return style.Fonts.Count - 1
  542. }
  543. // setNumFmt provides function to check if number format code in the range of
  544. // built-in values.
  545. func setNumFmt(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  546. _, ok := builtInNumFmt[formatCellStyle.NumFmt]
  547. if !ok {
  548. return 0
  549. }
  550. return formatCellStyle.NumFmt
  551. }
  552. // setFills provides function to add fill elements in the styles.xml by given
  553. // cell format settings.
  554. func setFills(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  555. var patterns = []string{
  556. "none",
  557. "solid",
  558. "mediumGray",
  559. "darkGray",
  560. "lightGray",
  561. "darkHorizontal",
  562. "darkVertical",
  563. "darkDown",
  564. "darkUp",
  565. "darkGrid",
  566. "darkTrellis",
  567. "lightHorizontal",
  568. "lightVertical",
  569. "lightDown",
  570. "lightUp",
  571. "lightGrid",
  572. "lightTrellis",
  573. "gray125",
  574. "gray0625",
  575. }
  576. var variants = []float64{
  577. 90,
  578. 0,
  579. 45,
  580. 135,
  581. }
  582. var fill xlsxFill
  583. switch formatCellStyle.Fill.Type {
  584. case "gradient":
  585. if len(formatCellStyle.Fill.Color) != 2 {
  586. break
  587. }
  588. var gradient xlsxGradientFill
  589. switch formatCellStyle.Fill.Shading {
  590. case 0, 1, 2, 3:
  591. gradient.Degree = variants[formatCellStyle.Fill.Shading]
  592. case 4:
  593. gradient.Type = "path"
  594. case 5:
  595. gradient.Type = "path"
  596. gradient.Bottom = 0.5
  597. gradient.Left = 0.5
  598. gradient.Right = 0.5
  599. gradient.Top = 0.5
  600. default:
  601. break
  602. }
  603. var stops []*xlsxGradientFillStop
  604. for index, color := range formatCellStyle.Fill.Color {
  605. var stop xlsxGradientFillStop
  606. stop.Position = float64(index)
  607. stop.Color.RGB = getPaletteColor(color)
  608. stops = append(stops, &stop)
  609. }
  610. gradient.Stop = stops
  611. fill.GradientFill = &gradient
  612. case "pattern":
  613. if formatCellStyle.Fill.Pattern > 18 || formatCellStyle.Fill.Pattern < 0 {
  614. break
  615. }
  616. if len(formatCellStyle.Fill.Color) < 1 {
  617. break
  618. }
  619. var pattern xlsxPatternFill
  620. pattern.PatternType = patterns[formatCellStyle.Fill.Pattern]
  621. pattern.FgColor.RGB = getPaletteColor(formatCellStyle.Fill.Color[0])
  622. fill.PatternFill = &pattern
  623. }
  624. style.Fills.Count++
  625. style.Fills.Fill = append(style.Fills.Fill, &fill)
  626. return style.Fills.Count - 1
  627. }
  628. // setAlignment provides function to formatting information pertaining to text
  629. // alignment in cells. There are a variety of choices for how text is aligned
  630. // both horizontally and vertically, as well as indentation settings, and so on.
  631. func setAlignment(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) (bool, *xlsxAlignment) {
  632. if formatCellStyle.Alignment == nil {
  633. return false, &xlsxAlignment{}
  634. }
  635. var alignment = xlsxAlignment{
  636. Horizontal: formatCellStyle.Alignment.Horizontal,
  637. Indent: formatCellStyle.Alignment.Indent,
  638. JustifyLastLine: formatCellStyle.Alignment.JustifyLastLine,
  639. ReadingOrder: formatCellStyle.Alignment.ReadingOrder,
  640. RelativeIndent: formatCellStyle.Alignment.RelativeIndent,
  641. ShrinkToFit: formatCellStyle.Alignment.ShrinkToFit,
  642. TextRotation: formatCellStyle.Alignment.TextRotation,
  643. Vertical: formatCellStyle.Alignment.Vertical,
  644. WrapText: formatCellStyle.Alignment.WrapText,
  645. }
  646. return true, &alignment
  647. }
  648. // setBorders provides function to add border elements in the styles.xml by
  649. // given borders format settings.
  650. func setBorders(style *xlsxStyleSheet, formatCellStyle *formatCellStyle) int {
  651. var styles = []string{
  652. "none",
  653. "thin",
  654. "medium",
  655. "dashed",
  656. "dotted",
  657. "thick",
  658. "double",
  659. "hair",
  660. "mediumDashed",
  661. "dashDot",
  662. "mediumDashDot",
  663. "dashDotDot",
  664. "mediumDashDotDot",
  665. "slantDashDot",
  666. }
  667. var border xlsxBorder
  668. for _, v := range formatCellStyle.Border {
  669. if v.Style > 13 || v.Style < 0 {
  670. continue
  671. }
  672. var color xlsxColor
  673. color.RGB = getPaletteColor(v.Color)
  674. switch v.Type {
  675. case "left":
  676. border.Left.Style = styles[v.Style]
  677. border.Left.Color = &color
  678. case "right":
  679. border.Right.Style = styles[v.Style]
  680. border.Right.Color = &color
  681. case "top":
  682. border.Top.Style = styles[v.Style]
  683. border.Top.Color = &color
  684. case "bottom":
  685. border.Bottom.Style = styles[v.Style]
  686. border.Bottom.Color = &color
  687. case "diagonalUp":
  688. border.Diagonal.Style = styles[v.Style]
  689. border.Diagonal.Color = &color
  690. border.DiagonalUp = true
  691. case "diagonalDown":
  692. border.Diagonal.Style = styles[v.Style]
  693. border.Diagonal.Color = &color
  694. border.DiagonalDown = true
  695. }
  696. }
  697. style.Borders.Count++
  698. style.Borders.Border = append(style.Borders.Border, &border)
  699. return style.Borders.Count - 1
  700. }
  701. // setCellXfs provides function to set describes all of the formatting for a
  702. // cell.
  703. func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment bool, alignment *xlsxAlignment) int {
  704. var xf xlsxXf
  705. xf.FontID = fontID
  706. if fontID != 0 {
  707. xf.ApplyFont = true
  708. }
  709. xf.NumFmtID = numFmtID
  710. if numFmtID != 0 {
  711. xf.ApplyNumberFormat = true
  712. }
  713. xf.FillID = fillID
  714. xf.BorderID = borderID
  715. style.CellXfs.Count++
  716. xf.Alignment = alignment
  717. xf.ApplyAlignment = applyAlignment
  718. style.CellXfs.Xf = append(style.CellXfs.Xf, xf)
  719. return style.CellXfs.Count - 1
  720. }
  721. // setCellStyle provides function to add style attribute for cells by given
  722. // sheet index, coordinate area and style ID.
  723. func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
  724. hcell = strings.ToUpper(hcell)
  725. vcell = strings.ToUpper(vcell)
  726. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  727. hcol := string(strings.Map(letterOnlyMapF, hcell))
  728. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  729. hyAxis := hrow - 1
  730. hxAxis := titleToNumber(hcol)
  731. vcol := string(strings.Map(letterOnlyMapF, vcell))
  732. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  733. vyAxis := vrow - 1
  734. vxAxis := titleToNumber(vcol)
  735. if vxAxis < hxAxis {
  736. hcell, vcell = vcell, hcell
  737. vxAxis, hxAxis = hxAxis, vxAxis
  738. }
  739. if vyAxis < hyAxis {
  740. hcell, vcell = vcell, hcell
  741. vyAxis, hyAxis = hyAxis, vyAxis
  742. }
  743. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  744. hcell = toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1)
  745. vcell = toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  746. xlsx := f.workSheetReader(sheet)
  747. completeRow(xlsx, vyAxis+1, vxAxis+1)
  748. completeCol(xlsx, vyAxis+1, vxAxis+1)
  749. for r, row := range xlsx.SheetData.Row {
  750. for k, c := range row.C {
  751. if checkCellInArea(c.R, hcell+":"+vcell) {
  752. xlsx.SheetData.Row[r].C[k].S = styleID
  753. }
  754. }
  755. }
  756. }
  757. // getPaletteColor provides function to convert the RBG color by given string.
  758. func getPaletteColor(color string) string {
  759. return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1)
  760. }