sheet.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. Rows []*Row
  10. MaxRow int
  11. MaxCol int
  12. }
  13. // Add a new Row to a Sheet
  14. func (s *Sheet) AddRow() *Row {
  15. row := &Row{}
  16. s.Rows = append(s.Rows, row)
  17. if len(s.Rows) > s.MaxRow {
  18. s.MaxRow = len(s.Rows)
  19. }
  20. return row
  21. }
  22. // Dump sheet to it's XML representation
  23. func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
  24. worksheet := &xlsxWorksheet{}
  25. xSheet := xlsxSheetData{}
  26. maxRow := 0
  27. maxCell := 0
  28. for r, row := range s.Rows {
  29. if r > maxRow {
  30. maxRow = r
  31. }
  32. xRow := xlsxRow{}
  33. xRow.R = r + 1
  34. for c, cell := range row.Cells {
  35. if c > maxCell {
  36. maxCell = c
  37. }
  38. xC := xlsxC{}
  39. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r + 1)
  40. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  41. xC.T = "s" // Hardcode string type, for now.
  42. xRow.C = append(xRow.C, xC)
  43. }
  44. xSheet.Row = append(xSheet.Row, xRow)
  45. }
  46. worksheet.SheetData = xSheet
  47. dimension := xlsxDimension{}
  48. dimension.Ref = fmt.Sprintf("A1:%s%d",
  49. numericToLetters(maxCell), maxRow + 1)
  50. worksheet.Dimension = dimension
  51. return worksheet
  52. }