sheet.go 5.3 KB

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