xmlStyle.go 22 KB

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