xmlStyle.go 28 KB

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