xmlStyle.go 27 KB

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