sheet.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package xlsx
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // Default column width in excel
  7. const colWidth = 9.5
  8. // Sheet is a high level structure intended to provide user access to
  9. // the contents of a particular sheet within an XLSX file.
  10. type Sheet struct {
  11. Name string
  12. Rows []*Row
  13. Cols []*Col
  14. MaxRow int
  15. MaxCol int
  16. }
  17. // Add a new Row to a Sheet
  18. func (s *Sheet) AddRow() *Row {
  19. row := &Row{sheet: s}
  20. s.Rows = append(s.Rows, row)
  21. if len(s.Rows) > s.MaxRow {
  22. s.MaxRow = len(s.Rows)
  23. }
  24. return row
  25. }
  26. // Make sure we always have as many Cols as we do cells.
  27. func (s *Sheet) maybeAddCol(cellCount int) {
  28. if cellCount > s.MaxCol {
  29. s.Cols = append(s.Cols, &Col{Min: cellCount, Max: cellCount, Hidden: false})
  30. s.MaxCol = cellCount
  31. }
  32. }
  33. // Get a Cell by passing it's cartesian coordinates (zero based) as
  34. // row and column integer indexes.
  35. //
  36. // For example:
  37. //
  38. // cell := sheet.Cell(0,0)
  39. //
  40. // ... would set the variable "cell" to contain a Cell struct
  41. // containing the data from the field "A1" on the spreadsheet.
  42. func (sh *Sheet) Cell(row, col int) *Cell {
  43. if len(sh.Rows) > row && sh.Rows[row] != nil && len(sh.Rows[row].Cells) > col {
  44. return sh.Rows[row].Cells[col]
  45. }
  46. return new(Cell)
  47. }
  48. // Dump sheet to it's XML representation, intended for internal use only
  49. func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyles) *xlsxWorksheet {
  50. worksheet := &xlsxWorksheet{}
  51. xSheet := xlsxSheetData{}
  52. maxRow := 0
  53. maxCell := 0
  54. for r, row := range s.Rows {
  55. if r > maxRow {
  56. maxRow = r
  57. }
  58. xRow := xlsxRow{}
  59. xRow.R = r + 1
  60. for c, cell := range row.Cells {
  61. style := cell.GetStyle()
  62. xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
  63. fontId := styles.addFont(xFont)
  64. fillId := styles.addFill(xFill)
  65. borderId := styles.addBorder(xBorder)
  66. xCellStyleXf.FontId = fontId
  67. xCellStyleXf.FillId = fillId
  68. xCellStyleXf.BorderId = borderId
  69. xCellXf.FontId = fontId
  70. xCellXf.FillId = fillId
  71. xCellXf.BorderId = borderId
  72. styleXfId := styles.addCellStyleXf(xCellStyleXf)
  73. XfId := styles.addCellXf(xCellXf)
  74. if styleXfId != XfId {
  75. panic("StyleXFId != XfId, this should never happen.")
  76. }
  77. if c > maxCell {
  78. maxCell = c
  79. }
  80. xC := xlsxC{}
  81. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  82. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  83. xC.T = "s" // Hardcode string type, for now.
  84. xC.S = XfId
  85. xRow.C = append(xRow.C, xC)
  86. }
  87. xSheet.Row = append(xSheet.Row, xRow)
  88. }
  89. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  90. for _, col := range s.Cols {
  91. worksheet.Cols.Col = append(worksheet.Cols.Col,
  92. xlsxCol{Min: col.Min,
  93. Max: col.Max,
  94. Hidden: col.Hidden,
  95. Width: colWidth})
  96. }
  97. worksheet.SheetData = xSheet
  98. dimension := xlsxDimension{}
  99. dimension.Ref = fmt.Sprintf("A1:%s%d",
  100. numericToLetters(maxCell), maxRow+1)
  101. worksheet.Dimension = dimension
  102. return worksheet
  103. }