xmlStyle.go 28 KB

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