xmlStyle.go 24 KB

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