xmlStyle.go 28 KB

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