xmlStyle.go 24 KB

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