xmlStyle.go 25 KB

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