xmlStyle.go 28 KB

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