xmlStyle.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // xslx is a package designed to help with reading data from
  2. // spreadsheets stored in the XLSX format used in recent versions of
  3. // Microsoft's Excel spreadsheet.
  4. //
  5. // For a concise example of how to use this library why not check out
  6. // the source for xlsx2csv here: https://github.com/tealeg/xlsx2csv
  7. package xlsx
  8. import (
  9. "encoding/xml"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. )
  15. // Excel styles can reference number formats that are built-in, all of which
  16. // have an id less than 164.
  17. const builtinNumFmtsCount = 163
  18. // Excel styles can reference number formats that are built-in, all of which
  19. // have an id less than 164. This is a possibly incomplete list comprised of as
  20. // many of them as I could find.
  21. var builtInNumFmt = map[int]string{
  22. 0: "general",
  23. 1: "0",
  24. 2: "0.00",
  25. 3: "#,##0",
  26. 4: "#,##0.00",
  27. 9: "0%",
  28. 10: "0.00%",
  29. 11: "0.00e+00",
  30. 12: "# ?/?",
  31. 13: "# ??/??",
  32. 14: "mm-dd-yy",
  33. 15: "d-mmm-yy",
  34. 16: "d-mmm",
  35. 17: "mmm-yy",
  36. 18: "h:mm am/pm",
  37. 19: "h:mm:ss am/pm",
  38. 20: "h:mm",
  39. 21: "h:mm:ss",
  40. 22: "m/d/yy h:mm",
  41. 37: "#,##0 ;(#,##0)",
  42. 38: "#,##0 ;[red](#,##0)",
  43. 39: "#,##0.00;(#,##0.00)",
  44. 40: "#,##0.00;[red](#,##0.00)",
  45. 41: `_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)`,
  46. 42: `_("$"* #,##0_);_("$* \(#,##0\);_("$"* "-"_);_(@_)`,
  47. 43: `_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)`,
  48. 44: `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`,
  49. 45: "mm:ss",
  50. 46: "[h]:mm:ss",
  51. 47: "mmss.0",
  52. 48: "##0.0e+0",
  53. 49: "@",
  54. }
  55. // xlsxStyle directly maps the styleSheet element in the namespace
  56. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  57. // currently I have not checked it for completeness - it does as much
  58. // as I need.
  59. type xlsxStyleSheet struct {
  60. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
  61. Fonts xlsxFonts `xml:"fonts,omitempty"`
  62. Fills xlsxFills `xml:"fills,omitempty"`
  63. Borders xlsxBorders `xml:"borders,omitempty"`
  64. CellStyleXfs xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
  65. CellXfs xlsxCellXfs `xml:"cellXfs,omitempty"`
  66. NumFmts xlsxNumFmts `xml:"numFmts,omitempty"`
  67. theme *theme
  68. styleCache map[int]*Style
  69. numFmtRefTable map[int]xlsxNumFmt
  70. lock *sync.RWMutex
  71. }
  72. func newXlsxStyleSheet(t *theme) *xlsxStyleSheet {
  73. stylesheet := new(xlsxStyleSheet)
  74. stylesheet.theme = t
  75. stylesheet.styleCache = make(map[int]*Style)
  76. stylesheet.lock = new(sync.RWMutex)
  77. return stylesheet
  78. }
  79. func (styles *xlsxStyleSheet) reset() {
  80. styles.Fonts = xlsxFonts{}
  81. styles.Fills = xlsxFills{}
  82. styles.Borders = xlsxBorders{}
  83. styles.CellStyleXfs = xlsxCellStyleXfs{}
  84. // add default xf
  85. styles.CellXfs = xlsxCellXfs{Count: 1, Xf: []xlsxXf{xlsxXf{}}}
  86. styles.NumFmts = xlsxNumFmts{}
  87. }
  88. func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
  89. styles.lock.RLock()
  90. style, ok := styles.styleCache[styleIndex]
  91. styles.lock.RUnlock()
  92. if ok {
  93. return
  94. }
  95. var styleXf xlsxXf
  96. style = &Style{}
  97. style.Border = Border{}
  98. style.Fill = Fill{}
  99. style.Font = Font{}
  100. xfCount := styles.CellXfs.Count
  101. if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
  102. xf := styles.CellXfs.Xf[styleIndex]
  103. // Google docs can produce output that has fewer
  104. // CellStyleXfs than CellXfs - this copes with that.
  105. if styleIndex < styles.CellStyleXfs.Count {
  106. styleXf = styles.CellStyleXfs.Xf[styleIndex]
  107. } else {
  108. styleXf = xlsxXf{}
  109. }
  110. style.ApplyBorder = xf.ApplyBorder || styleXf.ApplyBorder
  111. style.ApplyFill = xf.ApplyFill || styleXf.ApplyFill
  112. style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
  113. style.ApplyAlignment = xf.ApplyAlignment || styleXf.ApplyAlignment
  114. if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
  115. var border xlsxBorder
  116. border = styles.Borders.Border[xf.BorderId]
  117. style.Border.Left = border.Left.Style
  118. style.Border.Right = border.Right.Style
  119. style.Border.Top = border.Top.Style
  120. style.Border.Bottom = border.Bottom.Style
  121. }
  122. if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
  123. xFill := styles.Fills.Fill[xf.FillId]
  124. style.Fill.PatternType = xFill.PatternFill.PatternType
  125. style.Fill.FgColor = styles.argbValue(xFill.PatternFill.FgColor)
  126. style.Fill.BgColor = styles.argbValue(xFill.PatternFill.BgColor)
  127. }
  128. if xf.FontId > -1 && xf.FontId < styles.Fonts.Count {
  129. xfont := styles.Fonts.Font[xf.FontId]
  130. style.Font.Size, _ = strconv.Atoi(xfont.Sz.Val)
  131. style.Font.Name = xfont.Name.Val
  132. style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
  133. style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
  134. style.Font.Color = styles.argbValue(xfont.Color)
  135. if bold := xfont.B; bold != nil && bold.Val != "0" {
  136. style.Font.Bold = true
  137. }
  138. if italic := xfont.I; italic != nil && italic.Val != "0" {
  139. style.Font.Italic = true
  140. }
  141. if underline := xfont.U; underline != nil && underline.Val != "0" {
  142. style.Font.Underline = true
  143. }
  144. }
  145. if xf.Alignment.Horizontal != "" {
  146. style.Alignment.Horizontal = xf.Alignment.Horizontal
  147. }
  148. if xf.Alignment.Vertical != "" {
  149. style.Alignment.Vertical = xf.Alignment.Vertical
  150. }
  151. styles.lock.Lock()
  152. styles.styleCache[styleIndex] = style
  153. styles.lock.Unlock()
  154. }
  155. return style
  156. }
  157. func (styles *xlsxStyleSheet) argbValue(color xlsxColor) string {
  158. if color.Theme != nil && styles.theme != nil {
  159. return styles.theme.themeColor(int64(*color.Theme), color.Tint)
  160. } else {
  161. return color.RGB
  162. }
  163. }
  164. // Excel styles can reference number formats that are built-in, all of which
  165. // have an id less than 164. This is a possibly incomplete list comprised of as
  166. // many of them as I could find.
  167. func getBuiltinNumberFormat(numFmtId int) string {
  168. return builtInNumFmt[numFmtId]
  169. }
  170. func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
  171. if styles.CellXfs.Xf == nil {
  172. return ""
  173. }
  174. var numberFormat string = ""
  175. if styleIndex > -1 && styleIndex <= styles.CellXfs.Count {
  176. xf := styles.CellXfs.Xf[styleIndex]
  177. if builtin := getBuiltinNumberFormat(xf.NumFmtId); builtin != "" {
  178. return builtin
  179. }
  180. if styles.numFmtRefTable != nil {
  181. numFmt := styles.numFmtRefTable[xf.NumFmtId]
  182. numberFormat = numFmt.FormatCode
  183. }
  184. }
  185. return strings.ToLower(numberFormat)
  186. }
  187. func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
  188. var font xlsxFont
  189. if xFont.Name.Val == "" {
  190. return 0
  191. }
  192. for index, font = range styles.Fonts.Font {
  193. if font.Equals(xFont) {
  194. return index
  195. }
  196. }
  197. styles.Fonts.Font = append(styles.Fonts.Font, xFont)
  198. index = styles.Fonts.Count
  199. styles.Fonts.Count += 1
  200. return
  201. }
  202. func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
  203. var fill xlsxFill
  204. for index, fill = range styles.Fills.Fill {
  205. if fill.Equals(xFill) {
  206. return index
  207. }
  208. }
  209. styles.Fills.Fill = append(styles.Fills.Fill, xFill)
  210. index = styles.Fills.Count
  211. styles.Fills.Count += 1
  212. return
  213. }
  214. func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
  215. var border xlsxBorder
  216. for index, border = range styles.Borders.Border {
  217. if border.Equals(xBorder) {
  218. return index
  219. }
  220. }
  221. styles.Borders.Border = append(styles.Borders.Border, xBorder)
  222. index = styles.Borders.Count
  223. styles.Borders.Count += 1
  224. return
  225. }
  226. func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
  227. var cellStyleXf xlsxXf
  228. for index, cellStyleXf = range styles.CellStyleXfs.Xf {
  229. if cellStyleXf.Equals(xCellStyleXf) {
  230. return index
  231. }
  232. }
  233. styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
  234. index = styles.CellStyleXfs.Count
  235. styles.CellStyleXfs.Count += 1
  236. return
  237. }
  238. func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
  239. var cellXf xlsxXf
  240. for index, cellXf = range styles.CellXfs.Xf {
  241. if cellXf.Equals(xCellXf) {
  242. return index
  243. }
  244. }
  245. styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
  246. index = styles.CellXfs.Count
  247. styles.CellXfs.Count += 1
  248. return
  249. }
  250. // newNumFmt generate a xlsxNumFmt according the format code. When the FormatCode is built in, it will return a xlsxNumFmt with the NumFmtId defined in ECMA document, otherwise it will generate a new NumFmtId greater than 164.
  251. func (styles *xlsxStyleSheet) newNumFmt(formatCode string) xlsxNumFmt {
  252. if formatCode == "" {
  253. return xlsxNumFmt{NumFmtId: 0, FormatCode: "general"}
  254. }
  255. // built in NumFmts in xmlStyle.go, traverse from the const.
  256. numFmts := make(map[string]int)
  257. for k, v := range builtInNumFmt {
  258. numFmts[v] = k
  259. }
  260. numFmtId, ok := numFmts[formatCode]
  261. if ok {
  262. return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
  263. }
  264. // find the exist xlsxNumFmt
  265. for _, numFmt := range styles.NumFmts.NumFmt {
  266. if formatCode == numFmt.FormatCode {
  267. return numFmt
  268. }
  269. }
  270. // The user define NumFmtId. The one less than 164 in built in.
  271. numFmtId = builtinNumFmtsCount + 1
  272. styles.lock.Lock()
  273. defer styles.lock.Unlock()
  274. for {
  275. // get a unused NumFmtId
  276. if _, ok = styles.numFmtRefTable[numFmtId]; ok {
  277. numFmtId += 1
  278. } else {
  279. styles.addNumFmt(xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode})
  280. break
  281. }
  282. }
  283. return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
  284. }
  285. // addNumFmt add xlsxNumFmt if its not exist.
  286. func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) {
  287. // don't add built in NumFmt
  288. if xNumFmt.NumFmtId <= builtinNumFmtsCount {
  289. return
  290. }
  291. _, ok := styles.numFmtRefTable[xNumFmt.NumFmtId]
  292. if !ok {
  293. if styles.numFmtRefTable == nil {
  294. styles.numFmtRefTable = make(map[int]xlsxNumFmt)
  295. }
  296. styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
  297. styles.numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
  298. styles.NumFmts.Count += 1
  299. }
  300. }
  301. func (styles *xlsxStyleSheet) Marshal() (result string, err error) {
  302. var xNumFmts string
  303. var xfonts string
  304. var xfills string
  305. var xborders string
  306. var xcellStyleXfs string
  307. var xcellXfs string
  308. var outputFontMap map[int]int = make(map[int]int)
  309. var outputFillMap map[int]int = make(map[int]int)
  310. var outputBorderMap map[int]int = make(map[int]int)
  311. result = xml.Header
  312. result += `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
  313. xNumFmts, err = styles.NumFmts.Marshal()
  314. if err != nil {
  315. return
  316. }
  317. result += xNumFmts
  318. xfonts, err = styles.Fonts.Marshal(outputFontMap)
  319. if err != nil {
  320. return
  321. }
  322. result += xfonts
  323. xfills, err = styles.Fills.Marshal(outputFillMap)
  324. if err != nil {
  325. return
  326. }
  327. result += xfills
  328. xborders, err = styles.Borders.Marshal(outputBorderMap)
  329. if err != nil {
  330. return
  331. }
  332. result += xborders
  333. xcellStyleXfs, err = styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  334. if err != nil {
  335. return
  336. }
  337. result += xcellStyleXfs
  338. xcellXfs, err = styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  339. if err != nil {
  340. return
  341. }
  342. result += xcellXfs
  343. result += `</styleSheet>`
  344. return
  345. }
  346. // xlsxNumFmts directly maps the numFmts element in the namespace
  347. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  348. // currently I have not checked it for completeness - it does as much
  349. // as I need.
  350. type xlsxNumFmts struct {
  351. Count int `xml:"count,attr"`
  352. NumFmt []xlsxNumFmt `xml:"numFmt,omitempty"`
  353. }
  354. func (numFmts *xlsxNumFmts) Marshal() (result string, err error) {
  355. if numFmts.Count > 0 {
  356. result = fmt.Sprintf(`<numFmts count="%d">`, numFmts.Count)
  357. for _, numFmt := range numFmts.NumFmt {
  358. var xNumFmt string
  359. xNumFmt, err = numFmt.Marshal()
  360. if err != nil {
  361. return
  362. }
  363. result += xNumFmt
  364. }
  365. result += `</numFmts>`
  366. }
  367. return
  368. }
  369. // xlsxNumFmt directly maps the numFmt element in the namespace
  370. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  371. // currently I have not checked it for completeness - it does as much
  372. // as I need.
  373. type xlsxNumFmt struct {
  374. NumFmtId int `xml:"numFmtId,attr,omitempty"`
  375. FormatCode string `xml:"formatCode,attr,omitempty"`
  376. }
  377. func (numFmt *xlsxNumFmt) Marshal() (result string, err error) {
  378. return fmt.Sprintf(`<numFmt numFmtId="%d" formatCode="%s"/>`, numFmt.NumFmtId, numFmt.FormatCode), nil
  379. }
  380. // xlsxFonts directly maps the fonts element in the namespace
  381. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  382. // currently I have not checked it for completeness - it does as much
  383. // as I need.
  384. type xlsxFonts struct {
  385. XMLName xml.Name `xml:"fonts"`
  386. Count int `xml:"count,attr"`
  387. Font []xlsxFont `xml:"font,omitempty"`
  388. }
  389. func (fonts *xlsxFonts) Marshal(outputFontMap map[int]int) (result string, err error) {
  390. emittedCount := 0
  391. subparts := ""
  392. for i, font := range fonts.Font {
  393. var xfont string
  394. xfont, err = font.Marshal()
  395. if err != nil {
  396. return
  397. }
  398. if xfont != "" {
  399. outputFontMap[i] = emittedCount
  400. emittedCount += 1
  401. subparts += xfont
  402. }
  403. }
  404. if emittedCount > 0 {
  405. result = fmt.Sprintf(`<fonts count="%d">`, fonts.Count)
  406. result += subparts
  407. result += `</fonts>`
  408. }
  409. return
  410. }
  411. // xlsxFont directly maps the font element in the namespace
  412. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  413. // currently I have not checked it for completeness - it does as much
  414. // as I need.
  415. type xlsxFont struct {
  416. Sz xlsxVal `xml:"sz,omitempty"`
  417. Name xlsxVal `xml:"name,omitempty"`
  418. Family xlsxVal `xml:"family,omitempty"`
  419. Charset xlsxVal `xml:"charset,omitempty"`
  420. Color xlsxColor `xml:"color,omitempty"`
  421. B *xlsxVal `xml:"b,omitempty"`
  422. I *xlsxVal `xml:"i,omitempty"`
  423. U *xlsxVal `xml:"u,omitempty"`
  424. }
  425. func (font *xlsxFont) Equals(other xlsxFont) bool {
  426. if (font.B == nil && other.B != nil) || (font.B != nil && other.B == nil) {
  427. return false
  428. }
  429. if (font.I == nil && other.I != nil) || (font.I != nil && other.I == nil) {
  430. return false
  431. }
  432. if (font.U == nil && other.U != nil) || (font.U != nil && other.U == nil) {
  433. return false
  434. }
  435. return font.Sz.Equals(other.Sz) && font.Name.Equals(other.Name) && font.Family.Equals(other.Family) && font.Charset.Equals(other.Charset) && font.Color.Equals(other.Color)
  436. }
  437. func (font *xlsxFont) Marshal() (result string, err error) {
  438. result = `<font>`
  439. if font.Sz.Val != "" {
  440. result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
  441. }
  442. if font.Name.Val != "" {
  443. result += fmt.Sprintf(`<name val="%s"/>`, font.Name.Val)
  444. }
  445. if font.Family.Val != "" {
  446. result += fmt.Sprintf(`<family val="%s"/>`, font.Family.Val)
  447. }
  448. if font.Charset.Val != "" {
  449. result += fmt.Sprintf(`<charset val="%s"/>`, font.Charset.Val)
  450. }
  451. if font.Color.RGB != "" {
  452. result += fmt.Sprintf(`<color rgb="%s"/>`, font.Color.RGB)
  453. }
  454. if font.B != nil {
  455. result += "<b/>"
  456. }
  457. if font.I != nil {
  458. result += "<i/>"
  459. }
  460. if font.U != nil {
  461. result += "<u/>"
  462. }
  463. result += `</font>`
  464. return
  465. }
  466. // xlsxVal directly maps the val element in the namespace
  467. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  468. // currently I have not checked it for completeness - it does as much
  469. // as I need.
  470. type xlsxVal struct {
  471. Val string `xml:"val,attr,omitempty"`
  472. }
  473. func (val *xlsxVal) Equals(other xlsxVal) bool {
  474. return val.Val == other.Val
  475. }
  476. // xlsxFills directly maps the fills element in the namespace
  477. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  478. // currently I have not checked it for completeness - it does as much
  479. // as I need.
  480. type xlsxFills struct {
  481. Count int `xml:"count,attr"`
  482. Fill []xlsxFill `xml:"fill,omitempty"`
  483. }
  484. func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (result string, err error) {
  485. emittedCount := 0
  486. subparts := ""
  487. for i, fill := range fills.Fill {
  488. var xfill string
  489. xfill, err = fill.Marshal()
  490. if err != nil {
  491. return
  492. }
  493. if xfill != "" {
  494. outputFillMap[i] = emittedCount
  495. emittedCount += 1
  496. subparts += xfill
  497. }
  498. }
  499. if emittedCount > 0 {
  500. result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
  501. result += subparts
  502. result += `</fills>`
  503. }
  504. return
  505. }
  506. // xlsxFill directly maps the fill element in the namespace
  507. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  508. // currently I have not checked it for completeness - it does as much
  509. // as I need.
  510. type xlsxFill struct {
  511. PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
  512. }
  513. func (fill *xlsxFill) Equals(other xlsxFill) bool {
  514. return fill.PatternFill.Equals(other.PatternFill)
  515. }
  516. func (fill *xlsxFill) Marshal() (result string, err error) {
  517. if fill.PatternFill.PatternType != "" {
  518. var xpatternFill string
  519. result = `<fill>`
  520. xpatternFill, err = fill.PatternFill.Marshal()
  521. if err != nil {
  522. return
  523. }
  524. result += xpatternFill
  525. result += `</fill>`
  526. }
  527. return
  528. }
  529. // xlsxPatternFill directly maps the patternFill element in the namespace
  530. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  531. // currently I have not checked it for completeness - it does as much
  532. // as I need.
  533. type xlsxPatternFill struct {
  534. PatternType string `xml:"patternType,attr,omitempty"`
  535. FgColor xlsxColor `xml:"fgColor,omitempty"`
  536. BgColor xlsxColor `xml:"bgColor,omitempty"`
  537. }
  538. func (patternFill *xlsxPatternFill) Equals(other xlsxPatternFill) bool {
  539. return patternFill.PatternType == other.PatternType && patternFill.FgColor.Equals(other.FgColor) && patternFill.BgColor.Equals(other.BgColor)
  540. }
  541. func (patternFill *xlsxPatternFill) Marshal() (result string, err error) {
  542. result = fmt.Sprintf(`<patternFill patternType="%s"`, patternFill.PatternType)
  543. ending := `/>`
  544. terminator := ""
  545. subparts := ""
  546. if patternFill.FgColor.RGB != "" {
  547. ending = `>`
  548. terminator = "</patternFill>"
  549. subparts += fmt.Sprintf(`<fgColor rgb="%s"/>`, patternFill.FgColor.RGB)
  550. }
  551. if patternFill.BgColor.RGB != "" {
  552. ending = `>`
  553. terminator = "</patternFill>"
  554. subparts += fmt.Sprintf(`<bgColor rgb="%s"/>`, patternFill.BgColor.RGB)
  555. }
  556. result += ending
  557. result += subparts
  558. result += terminator
  559. return
  560. }
  561. // xlsxColor is a common mapping used for both the fgColor and bgColor
  562. // elements in the namespace
  563. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  564. // currently I have not checked it for completeness - it does as much
  565. // as I need.
  566. type xlsxColor struct {
  567. RGB string `xml:"rgb,attr,omitempty"`
  568. Theme *int `xml:"theme,attr,omitempty"`
  569. Tint float64 `xml:"tint,attr,omitempty"`
  570. }
  571. func (color *xlsxColor) Equals(other xlsxColor) bool {
  572. return color.RGB == other.RGB
  573. }
  574. // xlsxBorders directly maps the borders element in the namespace
  575. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  576. // currently I have not checked it for completeness - it does as much
  577. // as I need.
  578. type xlsxBorders struct {
  579. Count int `xml:"count,attr"`
  580. Border []xlsxBorder `xml:"border,omitempty"`
  581. }
  582. func (borders *xlsxBorders) Marshal(outputBorderMap map[int]int) (result string, err error) {
  583. result = ""
  584. emittedCount := 0
  585. subparts := ""
  586. for i, border := range borders.Border {
  587. var xborder string
  588. xborder, err = border.Marshal()
  589. if err != nil {
  590. return
  591. }
  592. if xborder != "" {
  593. outputBorderMap[i] = emittedCount
  594. emittedCount += 1
  595. subparts += xborder
  596. }
  597. }
  598. if emittedCount > 0 {
  599. result += fmt.Sprintf(`<borders count="%d">`, emittedCount)
  600. result += subparts
  601. result += `</borders>`
  602. }
  603. return
  604. }
  605. // xlsxBorder directly maps the border element in the namespace
  606. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  607. // currently I have not checked it for completeness - it does as much
  608. // as I need.
  609. type xlsxBorder struct {
  610. Left xlsxLine `xml:"left,omitempty"`
  611. Right xlsxLine `xml:"right,omitempty"`
  612. Top xlsxLine `xml:"top,omitempty"`
  613. Bottom xlsxLine `xml:"bottom,omitempty"`
  614. }
  615. func (border *xlsxBorder) Equals(other xlsxBorder) bool {
  616. return border.Left.Equals(other.Left) && border.Right.Equals(other.Right) && border.Top.Equals(other.Top) && border.Bottom.Equals(other.Bottom)
  617. }
  618. func (border *xlsxBorder) Marshal() (result string, err error) {
  619. emit := false
  620. subparts := ""
  621. if border.Left.Style != "" {
  622. emit = true
  623. subparts += fmt.Sprintf(`<left style="%s"/>`, border.Left.Style)
  624. }
  625. if border.Right.Style != "" {
  626. emit = true
  627. subparts += fmt.Sprintf(`<right style="%s"/>`, border.Right.Style)
  628. }
  629. if border.Top.Style != "" {
  630. emit = true
  631. subparts += fmt.Sprintf(`<top style="%s"/>`, border.Top.Style)
  632. }
  633. if border.Bottom.Style != "" {
  634. emit = true
  635. subparts += fmt.Sprintf(`<bottom style="%s"/>`, border.Bottom.Style)
  636. }
  637. if emit {
  638. result += `<border>`
  639. result += subparts
  640. result += `</border>`
  641. }
  642. return
  643. }
  644. // xlsxLine directly maps the line style element in the namespace
  645. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  646. // currently I have not checked it for completeness - it does as much
  647. // as I need.
  648. type xlsxLine struct {
  649. Style string `xml:"style,attr,omitempty"`
  650. }
  651. func (line *xlsxLine) Equals(other xlsxLine) bool {
  652. return line.Style == other.Style
  653. }
  654. // xlsxCellStyleXfs directly maps the cellStyleXfs element in the
  655. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  656. // - currently I have not checked it for completeness - it does as
  657. // much as I need.
  658. type xlsxCellStyleXfs struct {
  659. Count int `xml:"count,attr"`
  660. Xf []xlsxXf `xml:"xf,omitempty"`
  661. }
  662. func (cellStyleXfs *xlsxCellStyleXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  663. if cellStyleXfs.Count > 0 {
  664. result = fmt.Sprintf(`<cellStyleXfs count="%d">`, cellStyleXfs.Count)
  665. for _, xf := range cellStyleXfs.Xf {
  666. var xxf string
  667. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  668. if err != nil {
  669. return
  670. }
  671. result += xxf
  672. }
  673. result += `</cellStyleXfs>`
  674. }
  675. return
  676. }
  677. // xlsxCellXfs directly maps the cellXfs element in the namespace
  678. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  679. // currently I have not checked it for completeness - it does as much
  680. // as I need.
  681. type xlsxCellXfs struct {
  682. Count int `xml:"count,attr"`
  683. Xf []xlsxXf `xml:"xf,omitempty"`
  684. }
  685. func (cellXfs *xlsxCellXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  686. if cellXfs.Count > 0 {
  687. result = fmt.Sprintf(`<cellXfs count="%d">`, cellXfs.Count)
  688. for _, xf := range cellXfs.Xf {
  689. var xxf string
  690. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  691. if err != nil {
  692. return
  693. }
  694. result += xxf
  695. }
  696. result += `</cellXfs>`
  697. }
  698. return
  699. }
  700. // xlsxXf directly maps the xf element in the namespace
  701. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  702. // currently I have not checked it for completeness - it does as much
  703. // as I need.
  704. type xlsxXf struct {
  705. ApplyAlignment bool `xml:"applyAlignment,attr"`
  706. ApplyBorder bool `xml:"applyBorder,attr"`
  707. ApplyFont bool `xml:"applyFont,attr"`
  708. ApplyFill bool `xml:"applyFill,attr"`
  709. ApplyNumberFormat bool `xml:"applyNumberFormat,attr"`
  710. ApplyProtection bool `xml:"applyProtection,attr"`
  711. BorderId int `xml:"borderId,attr"`
  712. FillId int `xml:"fillId,attr"`
  713. FontId int `xml:"fontId,attr"`
  714. NumFmtId int `xml:"numFmtId,attr"`
  715. Alignment xlsxAlignment `xml:"alignment"`
  716. }
  717. func (xf *xlsxXf) Equals(other xlsxXf) bool {
  718. return xf.ApplyAlignment == other.ApplyAlignment &&
  719. xf.ApplyBorder == other.ApplyBorder &&
  720. xf.ApplyFont == other.ApplyFont &&
  721. xf.ApplyFill == other.ApplyFill &&
  722. xf.ApplyProtection == other.ApplyProtection &&
  723. xf.BorderId == other.BorderId &&
  724. xf.FillId == other.FillId &&
  725. xf.FontId == other.FontId &&
  726. xf.NumFmtId == other.NumFmtId &&
  727. xf.Alignment.Equals(other.Alignment)
  728. }
  729. func (xf *xlsxXf) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  730. var xAlignment string
  731. result = fmt.Sprintf(`<xf applyAlignment="%b" applyBorder="%b" applyFont="%b" applyFill="%b" applyNumberFormat="%b" applyProtection="%b" borderId="%d" fillId="%d" fontId="%d" numFmtId="%d">`, bool2Int(xf.ApplyAlignment), bool2Int(xf.ApplyBorder), bool2Int(xf.ApplyFont), bool2Int(xf.ApplyFill), bool2Int(xf.ApplyNumberFormat), bool2Int(xf.ApplyProtection), outputBorderMap[xf.BorderId], outputFillMap[xf.FillId], outputFontMap[xf.FontId], xf.NumFmtId)
  732. xAlignment, err = xf.Alignment.Marshal()
  733. if err != nil {
  734. return
  735. }
  736. result += xAlignment
  737. result += `</xf>`
  738. return
  739. }
  740. type xlsxAlignment struct {
  741. Horizontal string `xml:"horizontal,attr"`
  742. Indent int `xml:"indent,attr"`
  743. ShrinkToFit bool `xml:"shrinkToFit,attr"`
  744. TextRotation int `xml:"textRotation,attr"`
  745. Vertical string `xml:"vertical,attr"`
  746. WrapText bool `xml:"wrapText,attr"`
  747. }
  748. func (alignment *xlsxAlignment) Equals(other xlsxAlignment) bool {
  749. return alignment.Horizontal == other.Horizontal &&
  750. alignment.Indent == other.Indent &&
  751. alignment.ShrinkToFit == other.ShrinkToFit &&
  752. alignment.TextRotation == other.TextRotation &&
  753. alignment.Vertical == other.Vertical &&
  754. alignment.WrapText == other.WrapText
  755. }
  756. func (alignment *xlsxAlignment) Marshal() (result string, err error) {
  757. result = fmt.Sprintf(`<alignment horizontal="%s" indent="%d" shrinkToFit="%b" textRotation="%d" vertical="%s" wrapText="%b"/>`, alignment.Horizontal, alignment.Indent, bool2Int(alignment.ShrinkToFit), alignment.TextRotation, alignment.Vertical, bool2Int(alignment.WrapText))
  758. return
  759. }
  760. func bool2Int(b bool) int {
  761. if b {
  762. return 1
  763. }
  764. return 0
  765. }