xmlStyle.go 24 KB

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