xmlStyle.go 27 KB

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