sheet.go 3.0 KB

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