xmlStyle.go 26 KB

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