xmlStyle.go 27 KB

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