xmlStyle.go 23 KB

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