xmlStyle.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 `xml:"-"`
  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. 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)
  410. }
  411. func (font *xlsxFont) Marshal() (result string, err error) {
  412. result = `<font>`
  413. if font.Sz.Val != "" {
  414. result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
  415. }
  416. if font.Name.Val != "" {
  417. result += fmt.Sprintf(`<name val="%s"/>`, font.Name.Val)
  418. }
  419. if font.Family.Val != "" {
  420. result += fmt.Sprintf(`<family val="%s"/>`, font.Family.Val)
  421. }
  422. if font.Charset.Val != "" {
  423. result += fmt.Sprintf(`<charset val="%s"/>`, font.Charset.Val)
  424. }
  425. if font.Color.RGB != "" {
  426. result += fmt.Sprintf(`<color rgb="%s"/>`, font.Color.RGB)
  427. }
  428. result += `</font>`
  429. return
  430. }
  431. // xlsxVal directly maps the val element in the namespace
  432. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  433. // currently I have not checked it for completeness - it does as much
  434. // as I need.
  435. type xlsxVal struct {
  436. Val string `xml:"val,attr,omitempty"`
  437. }
  438. func (val *xlsxVal) Equals(other xlsxVal) bool {
  439. return val.Val == other.Val
  440. }
  441. // xlsxFills directly maps the fills element in the namespace
  442. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  443. // currently I have not checked it for completeness - it does as much
  444. // as I need.
  445. type xlsxFills struct {
  446. Count int `xml:"count,attr"`
  447. Fill []xlsxFill `xml:"fill,omitempty"`
  448. }
  449. func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (result string, err error) {
  450. emittedCount := 0
  451. subparts := ""
  452. for i, fill := range fills.Fill {
  453. var xfill string
  454. xfill, err = fill.Marshal()
  455. if err != nil {
  456. return
  457. }
  458. if xfill != "" {
  459. outputFillMap[i] = emittedCount
  460. emittedCount += 1
  461. subparts += xfill
  462. }
  463. }
  464. if emittedCount > 0 {
  465. result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
  466. result += subparts
  467. result += `</fills>`
  468. }
  469. return
  470. }
  471. // xlsxFill directly maps the fill element in the namespace
  472. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  473. // currently I have not checked it for completeness - it does as much
  474. // as I need.
  475. type xlsxFill struct {
  476. PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
  477. }
  478. func (fill *xlsxFill) Equals(other xlsxFill) bool {
  479. return fill.PatternFill.Equals(other.PatternFill)
  480. }
  481. func (fill *xlsxFill) Marshal() (result string, err error) {
  482. if fill.PatternFill.PatternType != "" {
  483. var xpatternFill string
  484. result = `<fill>`
  485. xpatternFill, err = fill.PatternFill.Marshal()
  486. if err != nil {
  487. return
  488. }
  489. result += xpatternFill
  490. result += `</fill>`
  491. }
  492. return
  493. }
  494. // xlsxPatternFill directly maps the patternFill element in the namespace
  495. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  496. // currently I have not checked it for completeness - it does as much
  497. // as I need.
  498. type xlsxPatternFill struct {
  499. PatternType string `xml:"patternType,attr,omitempty"`
  500. FgColor xlsxColor `xml:"fgColor,omitempty"`
  501. BgColor xlsxColor `xml:"bgColor,omitempty"`
  502. }
  503. func (patternFill *xlsxPatternFill) Equals(other xlsxPatternFill) bool {
  504. return patternFill.PatternType == other.PatternType && patternFill.FgColor.Equals(other.FgColor) && patternFill.BgColor.Equals(other.BgColor)
  505. }
  506. func (patternFill *xlsxPatternFill) Marshal() (result string, err error) {
  507. result = fmt.Sprintf(`<patternFill patternType="%s"`, patternFill.PatternType)
  508. ending := `/>`
  509. terminator := ""
  510. subparts := ""
  511. if patternFill.FgColor.RGB != "" {
  512. ending = `>`
  513. terminator = "</patternFill>"
  514. subparts += fmt.Sprintf(`<fgColor rgb="%s"/>`, patternFill.FgColor.RGB)
  515. }
  516. if patternFill.BgColor.RGB != "" {
  517. ending = `>`
  518. terminator = "</patternFill>"
  519. subparts += fmt.Sprintf(`<bgColor rgb="%s"/>`, patternFill.BgColor.RGB)
  520. }
  521. result += ending
  522. result += subparts
  523. result += terminator
  524. return
  525. }
  526. // xlsxColor is a common mapping used for both the fgColor and bgColor
  527. // elements in the namespace
  528. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  529. // currently I have not checked it for completeness - it does as much
  530. // as I need.
  531. type xlsxColor struct {
  532. RGB string `xml:"rgb,attr,omitempty"`
  533. Theme *int `xml:"theme,attr,omitempty"`
  534. Tint float64 `xml:"tint,attr,omitempty"`
  535. }
  536. func (color *xlsxColor) Equals(other xlsxColor) bool {
  537. return color.RGB == other.RGB
  538. }
  539. // xlsxBorders directly maps the borders element in the namespace
  540. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  541. // currently I have not checked it for completeness - it does as much
  542. // as I need.
  543. type xlsxBorders struct {
  544. Count int `xml:"count,attr"`
  545. Border []xlsxBorder `xml:"border,omitempty"`
  546. }
  547. func (borders *xlsxBorders) Marshal(outputBorderMap map[int]int) (result string, err error) {
  548. result = ""
  549. emittedCount := 0
  550. subparts := ""
  551. for i, border := range borders.Border {
  552. var xborder string
  553. xborder, err = border.Marshal()
  554. if err != nil {
  555. return
  556. }
  557. if xborder != "" {
  558. outputBorderMap[i] = emittedCount
  559. emittedCount += 1
  560. subparts += xborder
  561. }
  562. }
  563. if emittedCount > 0 {
  564. result += fmt.Sprintf(`<borders count="%d">`, emittedCount)
  565. result += subparts
  566. result += `</borders>`
  567. }
  568. return
  569. }
  570. // xlsxBorder directly maps the border element in the namespace
  571. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  572. // currently I have not checked it for completeness - it does as much
  573. // as I need.
  574. type xlsxBorder struct {
  575. Left xlsxLine `xml:"left,omitempty"`
  576. Right xlsxLine `xml:"right,omitempty"`
  577. Top xlsxLine `xml:"top,omitempty"`
  578. Bottom xlsxLine `xml:"bottom,omitempty"`
  579. }
  580. func (border *xlsxBorder) Equals(other xlsxBorder) bool {
  581. return border.Left.Equals(other.Left) && border.Right.Equals(other.Right) && border.Top.Equals(other.Top) && border.Bottom.Equals(other.Bottom)
  582. }
  583. func (border *xlsxBorder) Marshal() (result string, err error) {
  584. emit := false
  585. subparts := ""
  586. if border.Left.Style != "" {
  587. emit = true
  588. subparts += fmt.Sprintf(`<left style="%s"/>`, border.Left.Style)
  589. }
  590. if border.Right.Style != "" {
  591. emit = true
  592. subparts += fmt.Sprintf(`<right style="%s"/>`, border.Right.Style)
  593. }
  594. if border.Top.Style != "" {
  595. emit = true
  596. subparts += fmt.Sprintf(`<top style="%s"/>`, border.Top.Style)
  597. }
  598. if border.Bottom.Style != "" {
  599. emit = true
  600. subparts += fmt.Sprintf(`<bottom style="%s"/>`, border.Bottom.Style)
  601. }
  602. if emit {
  603. result += `<border>`
  604. result += subparts
  605. result += `</border>`
  606. }
  607. return
  608. }
  609. // xlsxLine directly maps the line style element in the namespace
  610. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  611. // currently I have not checked it for completeness - it does as much
  612. // as I need.
  613. type xlsxLine struct {
  614. Style string `xml:"style,attr,omitempty"`
  615. }
  616. func (line *xlsxLine) Equals(other xlsxLine) bool {
  617. return line.Style == other.Style
  618. }
  619. // xlsxCellStyleXfs directly maps the cellStyleXfs element in the
  620. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  621. // - currently I have not checked it for completeness - it does as
  622. // much as I need.
  623. type xlsxCellStyleXfs struct {
  624. Count int `xml:"count,attr"`
  625. Xf []xlsxXf `xml:"xf,omitempty"`
  626. }
  627. func (cellStyleXfs *xlsxCellStyleXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  628. if cellStyleXfs.Count > 0 {
  629. result = fmt.Sprintf(`<cellStyleXfs count="%d">`, cellStyleXfs.Count)
  630. for _, xf := range cellStyleXfs.Xf {
  631. var xxf string
  632. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  633. if err != nil {
  634. return
  635. }
  636. result += xxf
  637. }
  638. result += `</cellStyleXfs>`
  639. }
  640. return
  641. }
  642. // xlsxCellXfs directly maps the cellXfs element in the namespace
  643. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  644. // currently I have not checked it for completeness - it does as much
  645. // as I need.
  646. type xlsxCellXfs struct {
  647. Count int `xml:"count,attr"`
  648. Xf []xlsxXf `xml:"xf,omitempty"`
  649. }
  650. func (cellXfs *xlsxCellXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  651. if cellXfs.Count > 0 {
  652. result = fmt.Sprintf(`<cellXfs count="%d">`, cellXfs.Count)
  653. for _, xf := range cellXfs.Xf {
  654. var xxf string
  655. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  656. if err != nil {
  657. return
  658. }
  659. result += xxf
  660. }
  661. result += `</cellXfs>`
  662. }
  663. return
  664. }
  665. // xlsxXf directly maps the xf element in the namespace
  666. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  667. // currently I have not checked it for completeness - it does as much
  668. // as I need.
  669. type xlsxXf struct {
  670. ApplyAlignment bool `xml:"applyAlignment,attr"`
  671. ApplyBorder bool `xml:"applyBorder,attr"`
  672. ApplyFont bool `xml:"applyFont,attr"`
  673. ApplyFill bool `xml:"applyFill,attr"`
  674. ApplyProtection bool `xml:"applyProtection,attr"`
  675. BorderId int `xml:"borderId,attr"`
  676. FillId int `xml:"fillId,attr"`
  677. FontId int `xml:"fontId,attr"`
  678. NumFmtId int `xml:"numFmtId,attr"`
  679. Alignment xlsxAlignment `xml:"alignment"`
  680. }
  681. func (xf *xlsxXf) Equals(other xlsxXf) bool {
  682. return xf.ApplyAlignment == other.ApplyAlignment &&
  683. xf.ApplyBorder == other.ApplyBorder &&
  684. xf.ApplyFont == other.ApplyFont &&
  685. xf.ApplyFill == other.ApplyFill &&
  686. xf.ApplyProtection == other.ApplyProtection &&
  687. xf.BorderId == other.BorderId &&
  688. xf.FillId == other.FillId &&
  689. xf.FontId == other.FontId &&
  690. xf.NumFmtId == other.NumFmtId &&
  691. xf.Alignment.Equals(other.Alignment)
  692. }
  693. func (xf *xlsxXf) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  694. var xAlignment string
  695. 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)
  696. xAlignment, err = xf.Alignment.Marshal()
  697. if err != nil {
  698. return
  699. }
  700. result += xAlignment
  701. result += `</xf>`
  702. return
  703. }
  704. type xlsxAlignment struct {
  705. Horizontal string `xml:"horizontal,attr"`
  706. Indent int `xml:"indent,attr"`
  707. ShrinkToFit bool `xml:"shrinkToFit,attr"`
  708. TextRotation int `xml:"textRotation,attr"`
  709. Vertical string `xml:"vertical,attr"`
  710. WrapText bool `xml:"wrapText,attr"`
  711. }
  712. func (alignment *xlsxAlignment) Equals(other xlsxAlignment) bool {
  713. return alignment.Horizontal == other.Horizontal &&
  714. alignment.Indent == other.Indent &&
  715. alignment.ShrinkToFit == other.ShrinkToFit &&
  716. alignment.TextRotation == other.TextRotation &&
  717. alignment.Vertical == other.Vertical &&
  718. alignment.WrapText == other.WrapText
  719. }
  720. func (alignment *xlsxAlignment) Marshal() (result string, err error) {
  721. 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))
  722. return
  723. }
  724. func bool2Int(b bool) int {
  725. if b {
  726. return 1
  727. }
  728. return 0
  729. }