xmlStyle.go 22 KB

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