sheet.go 5.4 KB

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