xmlStyle.go 20 KB

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