sheet.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. File *File
  11. Rows []*Row
  12. Cols []*Col
  13. MaxRow int
  14. MaxCol int
  15. Hidden bool
  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. col := &Col{
  30. Min: cellCount,
  31. Max: cellCount,
  32. Hidden: false,
  33. Collapsed: false,
  34. // Style: 0,
  35. Width: ColWidth}
  36. s.Cols = append(s.Cols, col)
  37. s.MaxCol = cellCount
  38. }
  39. }
  40. // Get a Cell by passing it's cartesian coordinates (zero based) as
  41. // row and column integer indexes.
  42. //
  43. // For example:
  44. //
  45. // cell := sheet.Cell(0,0)
  46. //
  47. // ... would set the variable "cell" to contain a Cell struct
  48. // containing the data from the field "A1" on the spreadsheet.
  49. func (sh *Sheet) Cell(row, col int) *Cell {
  50. if len(sh.Rows) > row && sh.Rows[row] != nil && len(sh.Rows[row].Cells) > col {
  51. return sh.Rows[row].Cells[col]
  52. }
  53. return new(Cell)
  54. }
  55. // Dump sheet to it's XML representation, intended for internal use only
  56. func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxWorksheet {
  57. worksheet := newXlsxWorksheet()
  58. xSheet := xlsxSheetData{}
  59. maxRow := 0
  60. maxCell := 0
  61. for r, row := range s.Rows {
  62. if r > maxRow {
  63. maxRow = r
  64. }
  65. xRow := xlsxRow{}
  66. xRow.R = r + 1
  67. for c, cell := range row.Cells {
  68. style := cell.GetStyle()
  69. xNumFmt, xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
  70. fontId := styles.addFont(xFont)
  71. fillId := styles.addFill(xFill)
  72. borderId := styles.addBorder(xBorder)
  73. styles.addNumFmt(xNumFmt)
  74. xCellStyleXf.FontId = fontId
  75. xCellStyleXf.FillId = fillId
  76. xCellStyleXf.BorderId = borderId
  77. xCellStyleXf.NumFmtId = xNumFmt.NumFmtId
  78. xCellXf.FontId = fontId
  79. xCellXf.FillId = fillId
  80. xCellXf.BorderId = borderId
  81. xCellXf.NumFmtId = xNumFmt.NumFmtId
  82. styles.addCellStyleXf(xCellStyleXf)
  83. XfId := styles.addCellXf(xCellXf)
  84. if c > maxCell {
  85. maxCell = c
  86. }
  87. xC := xlsxC{}
  88. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  89. switch cell.cellType {
  90. case CellTypeString:
  91. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  92. xC.T = "s"
  93. xC.S = XfId
  94. case CellTypeBool:
  95. xC.V = cell.Value
  96. xC.T = "b"
  97. xC.S = XfId
  98. case CellTypeNumeric:
  99. xC.V = cell.Value
  100. xC.S = XfId
  101. case CellTypeFormula:
  102. xC.V = cell.Value
  103. xC.F = cell.formula
  104. xC.S = XfId
  105. case CellTypeError:
  106. xC.V = cell.Value
  107. xC.F = cell.formula
  108. xC.T = "e"
  109. xC.S = XfId
  110. }
  111. xRow.C = append(xRow.C, xC)
  112. }
  113. xSheet.Row = append(xSheet.Row, xRow)
  114. }
  115. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  116. for _, col := range s.Cols {
  117. if col.Width == 0 {
  118. col.Width = ColWidth
  119. }
  120. worksheet.Cols.Col = append(worksheet.Cols.Col,
  121. xlsxCol{Min: col.Min,
  122. Max: col.Max,
  123. Hidden: col.Hidden,
  124. Width: col.Width,
  125. Collapsed: col.Collapsed,
  126. // Style: col.Style
  127. })
  128. }
  129. worksheet.SheetData = xSheet
  130. dimension := xlsxDimension{}
  131. dimension.Ref = fmt.Sprintf("A1:%s%d",
  132. numericToLetters(maxCell), maxRow+1)
  133. if dimension.Ref == "A1:A1" {
  134. dimension.Ref = "A1"
  135. }
  136. worksheet.Dimension = dimension
  137. return worksheet
  138. }