xmlStyle.go 22 KB

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