sheet.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. style: NewStyle(),
  47. Min: cellCount,
  48. Max: cellCount,
  49. Hidden: false,
  50. Collapsed: false}
  51. s.Cols = append(s.Cols, col)
  52. s.MaxCol = cellCount
  53. }
  54. }
  55. // Make sure we always have as many Cols as we do cells.
  56. func (s *Sheet) Col(idx int) *Col {
  57. s.maybeAddCol(idx + 1)
  58. return s.Cols[idx]
  59. }
  60. // Get a Cell by passing it's cartesian coordinates (zero based) as
  61. // row and column integer indexes.
  62. //
  63. // For example:
  64. //
  65. // cell := sheet.Cell(0,0)
  66. //
  67. // ... would set the variable "cell" to contain a Cell struct
  68. // containing the data from the field "A1" on the spreadsheet.
  69. func (sh *Sheet) Cell(row, col int) *Cell {
  70. if len(sh.Rows) > row && sh.Rows[row] != nil && len(sh.Rows[row].Cells) > col {
  71. return sh.Rows[row].Cells[col]
  72. }
  73. return new(Cell)
  74. }
  75. //Set the width of a single column or multiple columns.
  76. func (s *Sheet) SetColWidth(startcol, endcol int, width float64) error {
  77. if startcol > endcol {
  78. return fmt.Errorf("Could not set width for range %d-%d: startcol must be less than endcol.", startcol, endcol)
  79. }
  80. col := &Col{
  81. style: NewStyle(),
  82. Min: startcol + 1,
  83. Max: endcol + 1,
  84. Hidden: false,
  85. Collapsed: false,
  86. Width: width}
  87. s.Cols = append(s.Cols, col)
  88. if endcol+1 > s.MaxCol {
  89. s.MaxCol = endcol + 1
  90. }
  91. return nil
  92. }
  93. // Dump sheet to its XML representation, intended for internal use only
  94. func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxWorksheet {
  95. worksheet := newXlsxWorksheet()
  96. xSheet := xlsxSheetData{}
  97. maxRow := 0
  98. maxCell := 0
  99. if s.SheetFormat.DefaultRowHeight != 0 {
  100. worksheet.SheetFormatPr.DefaultRowHeight = s.SheetFormat.DefaultRowHeight
  101. }
  102. worksheet.SheetFormatPr.DefaultColWidth = s.SheetFormat.DefaultColWidth
  103. colsXfIdList := make([]int, len(s.Cols))
  104. worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
  105. for c, col := range s.Cols {
  106. XfId := 0
  107. style := col.GetStyle()
  108. //col's style always not nil
  109. if style != nil {
  110. xNumFmt := styles.newNumFmt(col.numFmt)
  111. XfId = handleStyleForXLSX(style, xNumFmt.NumFmtId, styles)
  112. }
  113. colsXfIdList[c] = XfId
  114. var customWidth int
  115. if col.Width == 0 {
  116. col.Width = ColWidth
  117. } else {
  118. customWidth = 1
  119. }
  120. worksheet.Cols.Col = append(worksheet.Cols.Col,
  121. xlsxCol{Min: col.Min,
  122. Max: col.Max,
  123. Hidden: col.Hidden,
  124. Width: col.Width,
  125. CustomWidth: customWidth,
  126. Collapsed: col.Collapsed,
  127. Style: XfId,
  128. })
  129. }
  130. for r, row := range s.Rows {
  131. if r > maxRow {
  132. maxRow = r
  133. }
  134. xRow := xlsxRow{}
  135. xRow.R = r + 1
  136. if row.isCustom {
  137. xRow.CustomHeight = true
  138. xRow.Ht = fmt.Sprintf("%g", row.Height)
  139. }
  140. for c, cell := range row.Cells {
  141. XfId := colsXfIdList[c]
  142. // generate NumFmtId and add new NumFmt
  143. xNumFmt := styles.newNumFmt(cell.numFmt)
  144. style := cell.style
  145. if style != nil {
  146. XfId = handleStyleForXLSX(style, xNumFmt.NumFmtId, styles)
  147. } else if len(cell.numFmt) > 0 && s.Cols[c].numFmt != cell.numFmt {
  148. XfId = handleNumFmtIdForXLSX(xNumFmt.NumFmtId, styles)
  149. }
  150. if c > maxCell {
  151. maxCell = c
  152. }
  153. xC := xlsxC{}
  154. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  155. switch cell.cellType {
  156. case CellTypeString:
  157. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  158. xC.T = "s"
  159. xC.S = XfId
  160. case CellTypeBool:
  161. xC.V = cell.Value
  162. xC.T = "b"
  163. xC.S = XfId
  164. case CellTypeNumeric:
  165. xC.V = cell.Value
  166. xC.S = XfId
  167. case CellTypeDate:
  168. xC.V = cell.Value
  169. xC.S = XfId
  170. case CellTypeFormula:
  171. xC.V = cell.Value
  172. xC.F = &xlsxF{Content: cell.formula}
  173. xC.S = XfId
  174. case CellTypeError:
  175. xC.V = cell.Value
  176. xC.F = &xlsxF{Content: cell.formula}
  177. xC.T = "e"
  178. xC.S = XfId
  179. case CellTypeGeneral:
  180. xC.V = cell.Value
  181. xC.S = XfId
  182. }
  183. xRow.C = append(xRow.C, xC)
  184. if cell.HMerge > 0 || cell.VMerge > 0 {
  185. // r == rownum, c == colnum
  186. mc := xlsxMergeCell{}
  187. start := fmt.Sprintf("%s%d", numericToLetters(c), r+1)
  188. endcol := c + cell.HMerge
  189. endrow := r + cell.VMerge + 1
  190. end := fmt.Sprintf("%s%d", numericToLetters(endcol), endrow)
  191. mc.Ref = start + ":" + end
  192. if worksheet.MergeCells == nil {
  193. worksheet.MergeCells = &xlsxMergeCells{}
  194. }
  195. worksheet.MergeCells.Cells = append(worksheet.MergeCells.Cells, mc)
  196. }
  197. }
  198. xSheet.Row = append(xSheet.Row, xRow)
  199. }
  200. if worksheet.MergeCells != nil {
  201. worksheet.MergeCells.Count = len(worksheet.MergeCells.Cells)
  202. }
  203. worksheet.SheetData = xSheet
  204. dimension := xlsxDimension{}
  205. dimension.Ref = fmt.Sprintf("A1:%s%d",
  206. numericToLetters(maxCell), maxRow+1)
  207. if dimension.Ref == "A1:A1" {
  208. dimension.Ref = "A1"
  209. }
  210. worksheet.Dimension = dimension
  211. return worksheet
  212. }
  213. func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
  214. xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
  215. fontId := styles.addFont(xFont)
  216. fillId := styles.addFill(xFill)
  217. // HACK - adding light grey fill, as in OO and Google
  218. greyfill := xlsxFill{}
  219. greyfill.PatternFill.PatternType = "lightGrey"
  220. styles.addFill(greyfill)
  221. borderId := styles.addBorder(xBorder)
  222. xCellStyleXf.FontId = fontId
  223. xCellStyleXf.FillId = fillId
  224. xCellStyleXf.BorderId = borderId
  225. xCellStyleXf.NumFmtId = builtInNumFmtIndex_GENERAL
  226. xCellXf.FontId = fontId
  227. xCellXf.FillId = fillId
  228. xCellXf.BorderId = borderId
  229. xCellXf.NumFmtId = NumFmtId
  230. // apply the numFmtId when it is not the default cellxf
  231. if xCellXf.NumFmtId > 0 {
  232. xCellXf.ApplyNumberFormat = true
  233. }
  234. xCellStyleXf.Alignment.Horizontal = style.Alignment.Horizontal
  235. xCellStyleXf.Alignment.Vertical = style.Alignment.Vertical
  236. xCellXf.Alignment.Horizontal = style.Alignment.Horizontal
  237. xCellXf.Alignment.Vertical = style.Alignment.Vertical
  238. styles.addCellStyleXf(xCellStyleXf)
  239. XfId = styles.addCellXf(xCellXf)
  240. return
  241. }
  242. func handleNumFmtIdForXLSX(NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
  243. xCellXf := makeXLSXCellElement()
  244. xCellXf.NumFmtId = NumFmtId
  245. XfId = styles.addCellXf(xCellXf)
  246. return
  247. }