xmlStyle.go 27 KB

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