xmlStyle.go 27 KB

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