xmlStyle.go 27 KB

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