xmlStyle.go 26 KB

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