xmlStyle.go 20 KB

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