sheet.go 4.3 KB

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