sheet.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. SheetViews []SheetView
  17. }
  18. type SheetView struct {
  19. Pane *Pane
  20. }
  21. type Pane struct {
  22. XSplit int
  23. YSplit int
  24. TopLeftCell string
  25. ActivePane string
  26. State string // Either "split" or "frozen"
  27. }
  28. // Add a new Row to a Sheet
  29. func (s *Sheet) AddRow() *Row {
  30. row := &Row{Sheet: s}
  31. s.Rows = append(s.Rows, row)
  32. if len(s.Rows) > s.MaxRow {
  33. s.MaxRow = len(s.Rows)
  34. }
  35. return row
  36. }
  37. // Make sure we always have as many Cols as we do cells.
  38. func (s *Sheet) maybeAddCol(cellCount int) {
  39. if cellCount > s.MaxCol {
  40. col := &Col{
  41. Min: cellCount,
  42. Max: cellCount,
  43. Hidden: false,
  44. Collapsed: false,
  45. // Style: 0,
  46. Width: ColWidth}
  47. s.Cols = append(s.Cols, col)
  48. s.MaxCol = cellCount
  49. }
  50. }
  51. // Get a Cell by passing it's cartesian coordinates (zero based) as
  52. // row and column integer indexes.
  53. //
  54. // For example:
  55. //
  56. // cell := sheet.Cell(0,0)
  57. //
  58. // ... would set the variable "cell" to contain a Cell struct
  59. // containing the data from the field "A1" on the spreadsheet.
  60. func (sh *Sheet) Cell(row, col int) *Cell {
  61. if len(sh.Rows) > row && sh.Rows[row] != nil && len(sh.Rows[row].Cells) > col {
  62. return sh.Rows[row].Cells[col]
  63. }
  64. return new(Cell)
  65. }
  66. //Set the width of a single column or multipel columns.
  67. func (s *Sheet) SetColWidth(startcol, endcol int, width float64) error {
  68. if startcol > endcol {
  69. return fmt.Errorf("Could not set width for range %g-%g: startcol must be less than endcol.", startcol, endcol)
  70. }
  71. col := &Col{
  72. Min: startcol + 1,
  73. Max: endcol + 1,
  74. Hidden: false,
  75. Collapsed: false,
  76. // Style: 0,
  77. Width: width}
  78. s.Cols = append(s.Cols, col)
  79. if endcol+1 > s.MaxCol {
  80. s.MaxCol = endcol + 1
  81. }
  82. return nil
  83. }
  84. // Dump sheet to it's XML representation, intended for internal use only
  85. func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxWorksheet {
  86. worksheet := newXlsxWorksheet()
  87. xSheet := xlsxSheetData{}
  88. maxRow := 0
  89. maxCell := 0
  90. XfId := 0
  91. for r, row := range s.Rows {
  92. if r > maxRow {
  93. maxRow = r
  94. }
  95. xRow := xlsxRow{}
  96. xRow.R = r + 1
  97. for c, cell := range row.Cells {
  98. style := cell.GetStyle()
  99. if style != nil {
  100. xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
  101. fontId := styles.addFont(xFont)
  102. fillId := styles.addFill(xFill)
  103. borderId := styles.addBorder(xBorder)
  104. xCellStyleXf.FontId = fontId
  105. xCellStyleXf.FillId = fillId
  106. xCellStyleXf.BorderId = borderId
  107. xCellStyleXf.NumFmtId = 0 // General
  108. xCellXf.FontId = fontId
  109. xCellXf.FillId = fillId
  110. xCellXf.BorderId = borderId
  111. xCellXf.NumFmtId = 0 // General
  112. styles.addCellStyleXf(xCellStyleXf)
  113. XfId = styles.addCellXf(xCellXf)
  114. }
  115. if c > maxCell {
  116. maxCell = c
  117. }
  118. xC := xlsxC{}
  119. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  120. switch cell.cellType {
  121. case CellTypeString:
  122. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  123. xC.T = "s"
  124. xC.S = XfId
  125. case CellTypeBool:
  126. xC.V = cell.Value
  127. xC.T = "b"
  128. xC.S = XfId
  129. case CellTypeNumeric:
  130. xC.V = cell.Value
  131. xC.S = XfId
  132. case CellTypeFormula:
  133. xC.V = cell.Value
  134. xC.F = &xlsxF{Content: cell.formula}
  135. xC.S = XfId
  136. case CellTypeError:
  137. xC.V = cell.Value
  138. xC.F = &xlsxF{Content: cell.formula}
  139. xC.T = "e"
  140. xC.S = XfId
  141. }
  142. xRow.C = append(xRow.C, xC)
  143. }
  144. xSheet.Row = append(xSheet.Row, xRow)
  145. }
  146. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  147. for _, col := range s.Cols {
  148. if col.Width == 0 {
  149. col.Width = ColWidth
  150. }
  151. worksheet.Cols.Col = append(worksheet.Cols.Col,
  152. xlsxCol{Min: col.Min,
  153. Max: col.Max,
  154. Hidden: col.Hidden,
  155. Width: col.Width,
  156. Collapsed: col.Collapsed,
  157. // Style: col.Style
  158. })
  159. }
  160. worksheet.SheetData = xSheet
  161. dimension := xlsxDimension{}
  162. dimension.Ref = fmt.Sprintf("A1:%s%d",
  163. numericToLetters(maxCell), maxRow+1)
  164. if dimension.Ref == "A1:A1" {
  165. dimension.Ref = "A1"
  166. }
  167. worksheet.Dimension = dimension
  168. return worksheet
  169. }