xmlStyle.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. )
  14. type NumFmtRefTable map[int]xlsxNumFmt
  15. // xlsxStyle directly maps the styleSheet element in the namespace
  16. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  17. // currently I have not checked it for completeness - it does as much
  18. // as I need.
  19. type xlsxStyleSheet struct {
  20. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
  21. Fonts xlsxFonts `xml:"fonts,omitempty"`
  22. Fills xlsxFills `xml:"fills,omitempty"`
  23. Borders xlsxBorders `xml:"borders,omitempty"`
  24. CellStyleXfs xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
  25. CellXfs xlsxCellXfs `xml:"cellXfs,omitempty"`
  26. NumFmts xlsxNumFmts `xml:"numFmts,omitempty"`
  27. }
  28. func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style Style) {
  29. var styleXf xlsxXf
  30. style = Style{}
  31. style.Border = Border{}
  32. style.Fill = Fill{}
  33. style.Font = Font{}
  34. xfCount := styles.CellXfs.Count
  35. if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
  36. xf := styles.CellXfs.Xf[styleIndex]
  37. // Google docs can produce output that has fewer
  38. // CellStyleXfs than CellXfs - this copes with that.
  39. if styleIndex < styles.CellStyleXfs.Count {
  40. styleXf = styles.CellStyleXfs.Xf[styleIndex]
  41. } else {
  42. styleXf = xlsxXf{}
  43. }
  44. style.ApplyBorder = xf.ApplyBorder || styleXf.ApplyBorder
  45. style.ApplyFill = xf.ApplyFill || styleXf.ApplyFill
  46. style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
  47. if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
  48. style.Border.Left = styles.Borders.Border[xf.BorderId].Left.Style
  49. style.Border.Right = styles.Borders.Border[xf.BorderId].Right.Style
  50. style.Border.Top = styles.Borders.Border[xf.BorderId].Top.Style
  51. style.Border.Bottom = styles.Borders.Border[xf.BorderId].Bottom.Style
  52. }
  53. if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
  54. xFill := styles.Fills.Fill[xf.FillId]
  55. style.Fill.PatternType = xFill.PatternFill.PatternType
  56. style.Fill.FgColor = xFill.PatternFill.FgColor.RGB
  57. style.Fill.BgColor = xFill.PatternFill.BgColor.RGB
  58. }
  59. if xf.FontId > -1 && xf.FontId < styles.Fonts.Count {
  60. xfont := styles.Fonts.Font[xf.FontId]
  61. style.Font.Size, _ = strconv.Atoi(xfont.Sz.Val)
  62. style.Font.Name = xfont.Name.Val
  63. style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
  64. style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
  65. }
  66. }
  67. return style
  68. }
  69. func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int, numFmtRefTable map[int]xlsxNumFmt) string {
  70. if styles.CellXfs.Xf == nil {
  71. return ""
  72. }
  73. var numberFormat string = ""
  74. if styleIndex > -1 && styleIndex <= styles.CellXfs.Count {
  75. xf := styles.CellXfs.Xf[styleIndex]
  76. numFmt := numFmtRefTable[xf.NumFmtId]
  77. numberFormat = numFmt.FormatCode
  78. }
  79. return strings.ToLower(numberFormat)
  80. }
  81. func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
  82. styles.Fonts.Font = append(styles.Fonts.Font, xFont)
  83. index = styles.Fonts.Count
  84. styles.Fonts.Count += 1
  85. return
  86. }
  87. func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
  88. styles.Fills.Fill = append(styles.Fills.Fill, xFill)
  89. index = styles.Fills.Count
  90. styles.Fills.Count += 1
  91. return
  92. }
  93. func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
  94. styles.Borders.Border = append(styles.Borders.Border, xBorder)
  95. index = styles.Borders.Count
  96. styles.Borders.Count += 1
  97. return
  98. }
  99. func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
  100. styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
  101. index = styles.CellStyleXfs.Count
  102. styles.CellStyleXfs.Count += 1
  103. return
  104. }
  105. func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
  106. styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
  107. index = styles.CellXfs.Count
  108. styles.CellXfs.Count += 1
  109. return
  110. }
  111. func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt, numFmtRefTable NumFmtRefTable) (index int) {
  112. numFmt, ok := numFmtRefTable[xNumFmt.NumFmtId]
  113. if !ok {
  114. styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
  115. numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
  116. index = styles.NumFmts.Count
  117. styles.NumFmts.Count += 1
  118. return
  119. }
  120. numFmt.FormatCode = xNumFmt.FormatCode
  121. return
  122. }
  123. func (styles *xlsxStyleSheet) Marshal() (result string, err error) {
  124. var xNumFmts string
  125. var xfonts string
  126. var xfills string
  127. var xborders string
  128. var xcellStyleXfs string
  129. var xcellXfs string
  130. var outputFontMap map[int]int = make(map[int]int)
  131. var outputFillMap map[int]int = make(map[int]int)
  132. var outputBorderMap map[int]int = make(map[int]int)
  133. result = xml.Header
  134. result += `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
  135. xNumFmts, err = styles.NumFmts.Marshal()
  136. if err != nil {
  137. return
  138. }
  139. result += xNumFmts
  140. xfonts, err = styles.Fonts.Marshal(outputFontMap)
  141. if err != nil {
  142. return
  143. }
  144. result += xfonts
  145. xfills, err = styles.Fills.Marshal(outputFillMap)
  146. if err != nil {
  147. return
  148. }
  149. result += xfills
  150. xborders, err = styles.Borders.Marshal(outputBorderMap)
  151. if err != nil {
  152. return
  153. }
  154. result += xborders
  155. xcellStyleXfs, err = styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  156. if err != nil {
  157. return
  158. }
  159. result += xcellStyleXfs
  160. xcellXfs, err = styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  161. if err != nil {
  162. return
  163. }
  164. result += xcellXfs
  165. result += `</styleSheet>`
  166. return
  167. }
  168. // xlsxNumFmts directly maps the numFmts element in the namespace
  169. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  170. // currently I have not checked it for completeness - it does as much
  171. // as I need.
  172. type xlsxNumFmts struct {
  173. Count int `xml:"count,attr"`
  174. NumFmt []xlsxNumFmt `xml:"numFmt,omitempty"`
  175. }
  176. func (numFmts *xlsxNumFmts) Marshal() (result string, err error) {
  177. if numFmts.Count > 0 {
  178. result = fmt.Sprintf(`<numFmts count="%d">`, numFmts.Count)
  179. for _, numFmt := range numFmts.NumFmt {
  180. var xNumFmt string
  181. xNumFmt, err = numFmt.Marshal()
  182. if err != nil {
  183. return
  184. }
  185. result += xNumFmt
  186. }
  187. result += `</numFmts>`
  188. }
  189. return
  190. }
  191. // xlsxNumFmt directly maps the numFmt element in the namespace
  192. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  193. // currently I have not checked it for completeness - it does as much
  194. // as I need.
  195. type xlsxNumFmt struct {
  196. NumFmtId int `xml:"numFmtId,omitempty"`
  197. FormatCode string `xml:"formatCode,omitempty"`
  198. }
  199. func (numFmt *xlsxNumFmt) Marshal() (result string, err error) {
  200. return fmt.Sprintf(`<numFmt numFmtId="%d" formatCode="%s"/>`, numFmt.NumFmtId, numFmt.FormatCode), nil
  201. }
  202. // xlsxFonts directly maps the fonts element in the namespace
  203. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  204. // currently I have not checked it for completeness - it does as much
  205. // as I need.
  206. type xlsxFonts struct {
  207. XMLName xml.Name `xml:"fonts"`
  208. Count int `xml:"count,attr"`
  209. Font []xlsxFont `xml:"font,omitempty"`
  210. }
  211. func (fonts *xlsxFonts) Marshal(outputFontMap map[int]int) (result string, err error) {
  212. emittedCount := 0
  213. subparts := ""
  214. for i, font := range fonts.Font {
  215. var xfont string
  216. xfont, err = font.Marshal()
  217. if err != nil {
  218. return
  219. }
  220. if xfont != "" {
  221. outputFontMap[i] = emittedCount
  222. emittedCount += 1
  223. subparts += xfont
  224. }
  225. }
  226. if emittedCount > 0 {
  227. result = fmt.Sprintf(`<fonts count="%d">`, fonts.Count)
  228. result += subparts
  229. result += `</fonts>`
  230. }
  231. return
  232. }
  233. // xlsxFont directly maps the font element in the namespace
  234. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  235. // currently I have not checked it for completeness - it does as much
  236. // as I need.
  237. type xlsxFont struct {
  238. Sz xlsxVal `xml:"sz,omitempty"`
  239. Name xlsxVal `xml:"name,omitempty"`
  240. Family xlsxVal `xml:"family,omitempty"`
  241. Charset xlsxVal `xml:"charset,omitempty"`
  242. Color xlsxColor `xml:"color,omitempty"`
  243. }
  244. func (font *xlsxFont) Marshal() (result string, err error) {
  245. result = `<font>`
  246. if font.Sz.Val != "" {
  247. result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
  248. }
  249. if font.Name.Val != "" {
  250. result += fmt.Sprintf(`<name val="%s"/>`, font.Name.Val)
  251. }
  252. if font.Family.Val != "" {
  253. result += fmt.Sprintf(`<family val="%s"/>`, font.Family.Val)
  254. }
  255. if font.Charset.Val != "" {
  256. result += fmt.Sprintf(`<charset val="%s"/>`, font.Charset.Val)
  257. }
  258. if font.Color.RGB != "" {
  259. result += fmt.Sprintf(`<color rgb="%s"/>`, font.Color.RGB)
  260. }
  261. result += `</font>`
  262. return
  263. }
  264. // xlsxVal directly maps the val element in the namespace
  265. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  266. // currently I have not checked it for completeness - it does as much
  267. // as I need.
  268. type xlsxVal struct {
  269. Val string `xml:"val,attr,omitempty"`
  270. }
  271. // xlsxFills directly maps the fills element in the namespace
  272. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  273. // currently I have not checked it for completeness - it does as much
  274. // as I need.
  275. type xlsxFills struct {
  276. Count int `xml:"count,attr"`
  277. Fill []xlsxFill `xml:"fill,omitempty"`
  278. }
  279. func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (result string, err error) {
  280. emittedCount := 0
  281. subparts := ""
  282. for i, fill := range fills.Fill {
  283. var xfill string
  284. xfill, err = fill.Marshal()
  285. if err != nil {
  286. return
  287. }
  288. if xfill != "" {
  289. outputFillMap[i] = emittedCount
  290. emittedCount += 1
  291. subparts += xfill
  292. }
  293. }
  294. if emittedCount > 0 {
  295. result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
  296. result += subparts
  297. result += `</fills>`
  298. }
  299. return
  300. }
  301. // xlsxFill directly maps the fill element in the namespace
  302. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  303. // currently I have not checked it for completeness - it does as much
  304. // as I need.
  305. type xlsxFill struct {
  306. PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
  307. }
  308. func (fill *xlsxFill) Marshal() (result string, err error) {
  309. if fill.PatternFill.PatternType != "" {
  310. var xpatternFill string
  311. result = `<fill>`
  312. xpatternFill, err = fill.PatternFill.Marshal()
  313. if err != nil {
  314. return
  315. }
  316. result += xpatternFill
  317. result += `</fill>`
  318. }
  319. return
  320. }
  321. // xlsxPatternFill directly maps the patternFill element in the namespace
  322. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  323. // currently I have not checked it for completeness - it does as much
  324. // as I need.
  325. type xlsxPatternFill struct {
  326. PatternType string `xml:"patternType,attr,omitempty"`
  327. FgColor xlsxColor `xml:"fgColor,omitempty"`
  328. BgColor xlsxColor `xml:"bgColor,omitempty"`
  329. }
  330. func (patternFill *xlsxPatternFill) Marshal() (result string, err error) {
  331. result = fmt.Sprintf(`<patternFill patternType="%s"`, patternFill.PatternType)
  332. ending := `/>`
  333. subparts := ""
  334. if patternFill.FgColor.RGB != "" {
  335. ending = `>`
  336. subparts += fmt.Sprintf(`<fgColor rgb="%s"/>`, patternFill.FgColor.RGB)
  337. }
  338. if patternFill.BgColor.RGB != "" {
  339. ending = `>`
  340. subparts += fmt.Sprintf(`<bgColor rgb="%s"/>`, patternFill.BgColor.RGB)
  341. }
  342. result += ending
  343. result += subparts
  344. result += `</patternFill>`
  345. return
  346. }
  347. // xlsxColor is a common mapping used for both the fgColor and bgColor
  348. // elements in the namespace
  349. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  350. // currently I have not checked it for completeness - it does as much
  351. // as I need.
  352. type xlsxColor struct {
  353. RGB string `xml:"rgb,attr,omitempty"`
  354. }
  355. // xlsxBorders directly maps the borders element in the namespace
  356. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  357. // currently I have not checked it for completeness - it does as much
  358. // as I need.
  359. type xlsxBorders struct {
  360. Count int `xml:"count,attr"`
  361. Border []xlsxBorder `xml:"border,omitempty"`
  362. }
  363. func (borders *xlsxBorders) Marshal(outputBorderMap map[int]int) (result string, err error) {
  364. result = ""
  365. emittedCount := 0
  366. subparts := ""
  367. for i, border := range borders.Border {
  368. var xborder string
  369. xborder, err = border.Marshal()
  370. if err != nil {
  371. return
  372. }
  373. if xborder != "" {
  374. outputBorderMap[i] = emittedCount
  375. emittedCount += 1
  376. subparts += xborder
  377. }
  378. }
  379. if emittedCount > 0 {
  380. result += fmt.Sprintf(`<borders count="%d">`, emittedCount)
  381. result += subparts
  382. result += `</borders>`
  383. }
  384. return
  385. }
  386. // xlsxBorder directly maps the border element in the namespace
  387. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  388. // currently I have not checked it for completeness - it does as much
  389. // as I need.
  390. type xlsxBorder struct {
  391. Left xlsxLine `xml:"left,omitempty"`
  392. Right xlsxLine `xml:"right,omitempty"`
  393. Top xlsxLine `xml:"top,omitempty"`
  394. Bottom xlsxLine `xml:"bottom,omitempty"`
  395. }
  396. func (border *xlsxBorder) Marshal() (result string, err error) {
  397. emit := false
  398. subparts := ""
  399. if border.Left.Style != "" {
  400. emit = true
  401. subparts += fmt.Sprintf(`<left style="%s"/>`, border.Left.Style)
  402. }
  403. if border.Right.Style != "" {
  404. emit = true
  405. subparts += fmt.Sprintf(`<right style="%s"/>`, border.Right.Style)
  406. }
  407. if border.Top.Style != "" {
  408. emit = true
  409. subparts += fmt.Sprintf(`<top style="%s"/>`, border.Top.Style)
  410. }
  411. if border.Bottom.Style != "" {
  412. emit = true
  413. subparts += fmt.Sprintf(`<bottom style="%s"/>`, border.Bottom.Style)
  414. }
  415. if emit {
  416. result += `<border>`
  417. result += subparts
  418. result += `</border>`
  419. }
  420. return
  421. }
  422. // xlsxLine directly maps the line style 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 xlsxLine struct {
  427. Style string `xml:"style,attr,omitempty"`
  428. }
  429. // xlsxCellStyleXfs directly maps the cellStyleXfs element in the
  430. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  431. // - currently I have not checked it for completeness - it does as
  432. // much as I need.
  433. type xlsxCellStyleXfs struct {
  434. Count int `xml:"count,attr"`
  435. Xf []xlsxXf `xml:"xf,omitempty"`
  436. }
  437. func (cellStyleXfs *xlsxCellStyleXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  438. if cellStyleXfs.Count > 0 {
  439. result = fmt.Sprintf(`<cellStyleXfs count="%d">`, cellStyleXfs.Count)
  440. for _, xf := range cellStyleXfs.Xf {
  441. var xxf string
  442. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  443. if err != nil {
  444. return
  445. }
  446. result += xxf
  447. }
  448. result += `</cellStyleXfs>`
  449. }
  450. return
  451. }
  452. // xlsxCellXfs directly maps the cellXfs element in the namespace
  453. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  454. // currently I have not checked it for completeness - it does as much
  455. // as I need.
  456. type xlsxCellXfs struct {
  457. Count int `xml:"count,attr"`
  458. Xf []xlsxXf `xml:"xf,omitempty"`
  459. }
  460. func (cellXfs *xlsxCellXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  461. if cellXfs.Count > 0 {
  462. result = fmt.Sprintf(`<cellXfs count="%d">`, cellXfs.Count)
  463. for _, xf := range cellXfs.Xf {
  464. var xxf string
  465. xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
  466. if err != nil {
  467. return
  468. }
  469. result += xxf
  470. }
  471. result += `</cellXfs>`
  472. }
  473. return
  474. }
  475. // xlsxXf directly maps the xf element in the namespace
  476. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  477. // currently I have not checked it for completeness - it does as much
  478. // as I need.
  479. type xlsxXf struct {
  480. ApplyAlignment bool `xml:"applyAlignment,attr"`
  481. ApplyBorder bool `xml:"applyBorder,attr"`
  482. ApplyFont bool `xml:"applyFont,attr"`
  483. ApplyFill bool `xml:"applyFill,attr"`
  484. ApplyProtection bool `xml:"applyProtection,attr"`
  485. BorderId int `xml:"borderId,attr"`
  486. FillId int `xml:"fillId,attr"`
  487. FontId int `xml:"fontId,attr"`
  488. NumFmtId int `xml:"numFmtId,attr"`
  489. alignment xlsxAlignment `xml:"alignment"`
  490. }
  491. func (xf *xlsxXf) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
  492. var xalignment string
  493. result = fmt.Sprintf(`<xf applyAlignment="%t" applyBorder="%t" applyFont="%t" applyFill="%t" applyProtection="%t" borderId="%d" fillId="%d" fontId="%d" numFmtId="%d">`, xf.ApplyAlignment, xf.ApplyBorder, xf.ApplyFont, xf.ApplyFill, xf.ApplyProtection, outputBorderMap[xf.BorderId], outputFillMap[xf.FillId], outputFontMap[xf.FontId], xf.NumFmtId)
  494. xalignment, err = xf.alignment.Marshal()
  495. if err != nil {
  496. return
  497. }
  498. result += xalignment
  499. result += `</xf>`
  500. return
  501. }
  502. type xlsxAlignment struct {
  503. Horizontal string `xml:"horizontal,attr"`
  504. Indent int `xml:"indent,attr"`
  505. ShrinkToFit bool `xml:"shrinkToFit,attr"`
  506. TextRotation int `xml:"textRotation,attr"`
  507. Vertical string `xml:"vertical,attr"`
  508. WrapText bool `xml:"wrapText,attr"`
  509. }
  510. func (alignment *xlsxAlignment) Marshal() (result string, err error) {
  511. result = fmt.Sprintf(`<alignment horizontal="%s" indent="%d" shrinkToFit="%t" textRotation="%d" vertical="%s" wrapText="%t"/>`, alignment.Horizontal, alignment.Indent, alignment.ShrinkToFit, alignment.TextRotation, alignment.Vertical, alignment.WrapText)
  512. return
  513. }