sheet.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 multiple 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 %d-%d: 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 its 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. // generate NumFmtId and add new NumFmt
  104. xNumFmt := styles.newNumFmt(cell.numFmt)
  105. // HACK - adding light grey fill, as in OO and Google
  106. greyfill := xlsxFill{}
  107. greyfill.PatternFill.PatternType = "lightGrey"
  108. styles.addFill(greyfill)
  109. borderId := styles.addBorder(xBorder)
  110. xCellStyleXf.FontId = fontId
  111. xCellStyleXf.FillId = fillId
  112. xCellStyleXf.BorderId = borderId
  113. xCellStyleXf.NumFmtId = 0 // General
  114. xCellXf.FontId = fontId
  115. xCellXf.FillId = fillId
  116. xCellXf.BorderId = borderId
  117. xCellXf.NumFmtId = xNumFmt.NumFmtId
  118. // apply the numFmtId when it is not the default cellxf
  119. if xCellXf.NumFmtId > 0 {
  120. xCellXf.ApplyNumberFormat = true
  121. }
  122. styles.addCellStyleXf(xCellStyleXf)
  123. XfId = styles.addCellXf(xCellXf)
  124. }
  125. if c > maxCell {
  126. maxCell = c
  127. }
  128. xC := xlsxC{}
  129. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  130. switch cell.cellType {
  131. case CellTypeString:
  132. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  133. xC.T = "s"
  134. xC.S = XfId
  135. case CellTypeBool:
  136. xC.V = cell.Value
  137. xC.T = "b"
  138. xC.S = XfId
  139. case CellTypeNumeric:
  140. xC.V = cell.Value
  141. xC.S = XfId
  142. case CellTypeFormula:
  143. xC.V = cell.Value
  144. xC.F = &xlsxF{Content: cell.formula}
  145. xC.S = XfId
  146. case CellTypeError:
  147. xC.V = cell.Value
  148. xC.F = &xlsxF{Content: cell.formula}
  149. xC.T = "e"
  150. xC.S = XfId
  151. }
  152. xRow.C = append(xRow.C, xC)
  153. if cell.HMerge > 0 || cell.VMerge > 0 {
  154. // r == rownum, c == colnum
  155. mc := xlsxMergeCell{}
  156. start := fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  157. endcol := c + cell.HMerge
  158. endrow := r + cell.VMerge + 1
  159. end := fmt.Sprintf("%s%d", numericToLetters(endcol), endrow)
  160. mc.Ref = start + ":" + end
  161. if worksheet.MergeCells == nil {
  162. worksheet.MergeCells = &xlsxMergeCells{}
  163. }
  164. worksheet.MergeCells.Cells = append(worksheet.MergeCells.Cells, mc)
  165. }
  166. }
  167. xSheet.Row = append(xSheet.Row, xRow)
  168. }
  169. if worksheet.MergeCells != nil {
  170. worksheet.MergeCells.Count = len(worksheet.MergeCells.Cells)
  171. }
  172. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  173. for _, col := range s.Cols {
  174. if col.Width == 0 {
  175. col.Width = ColWidth
  176. }
  177. worksheet.Cols.Col = append(worksheet.Cols.Col,
  178. xlsxCol{Min: col.Min,
  179. Max: col.Max,
  180. Hidden: col.Hidden,
  181. Width: col.Width,
  182. Collapsed: col.Collapsed,
  183. // Style: col.Style
  184. })
  185. }
  186. worksheet.SheetData = xSheet
  187. dimension := xlsxDimension{}
  188. dimension.Ref = fmt.Sprintf("A1:%s%d",
  189. numericToLetters(maxCell), maxRow+1)
  190. if dimension.Ref == "A1:A1" {
  191. dimension.Ref = "A1"
  192. }
  193. worksheet.Dimension = dimension
  194. return worksheet
  195. }