xmlStyle.go 25 KB

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