sheet.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, s.File.numFmtRefTable)
  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. styleXfId := styles.addCellStyleXf(xCellStyleXf)
  83. XfId := styles.addCellXf(xCellXf)
  84. if styleXfId != XfId {
  85. panic("StyleXFId != XfId, this should never happen.")
  86. }
  87. if c > maxCell {
  88. maxCell = c
  89. }
  90. xC := xlsxC{}
  91. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  92. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  93. xC.T = "s" // Hardcode string type, for now.
  94. xC.S = XfId
  95. xRow.C = append(xRow.C, xC)
  96. }
  97. xSheet.Row = append(xSheet.Row, xRow)
  98. }
  99. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  100. for _, col := range s.Cols {
  101. if col.Width == 0 {
  102. col.Width = ColWidth
  103. }
  104. worksheet.Cols.Col = append(worksheet.Cols.Col,
  105. xlsxCol{Min: col.Min,
  106. Max: col.Max,
  107. Hidden: col.Hidden,
  108. Width: col.Width,
  109. Collapsed: col.Collapsed,
  110. Style: col.Style})
  111. }
  112. worksheet.SheetData = xSheet
  113. dimension := xlsxDimension{}
  114. dimension.Ref = fmt.Sprintf("A1:%s%d",
  115. numericToLetters(maxCell), maxRow+1)
  116. if dimension.Ref == "A1:A1" {
  117. dimension.Ref = "A1"
  118. }
  119. worksheet.Dimension = dimension
  120. return worksheet
  121. }