xmlStyle.go 20 KB

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