sheet.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package xlsx
  2. import (
  3. // "bytes"
  4. // "encoding/xml"
  5. "fmt"
  6. "strconv"
  7. )
  8. // Sheet is a high level structure intended to provide user access to
  9. // the contents of a particular sheet within an XLSX file.
  10. type Sheet struct {
  11. Rows []*Row
  12. MaxRow int
  13. MaxCol int
  14. }
  15. // Add a new Row to a Sheet
  16. func (s *Sheet) AddRow() *Row {
  17. row := &Row{}
  18. s.Rows = append(s.Rows, row)
  19. if len(s.Rows) > s.MaxRow {
  20. s.MaxRow = len(s.Rows)
  21. }
  22. return row
  23. }
  24. // Dump sheet to it's XML representation
  25. func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
  26. worksheet := &xlsxWorksheet{}
  27. xSheet := xlsxSheetData{}
  28. maxRow := 0
  29. maxCell := 0
  30. for r, row := range s.Rows {
  31. if r > maxRow {
  32. maxRow = r
  33. }
  34. xRow := xlsxRow{}
  35. xRow.R = r + 1
  36. for c, cell := range row.Cells {
  37. if c > maxCell {
  38. maxCell = c
  39. }
  40. xC := xlsxC{}
  41. xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r + 1)
  42. xC.V = strconv.Itoa(refTable.AddString(cell.Value))
  43. xC.T = "s" // Hardcode string type, for now.
  44. xRow.C = append(xRow.C, xC)
  45. }
  46. xSheet.Row = append(xSheet.Row, xRow)
  47. }
  48. worksheet.SheetData = xSheet
  49. dimension := xlsxDimension{}
  50. dimension.Ref = fmt.Sprintf("A1:%s%d",
  51. numericToLetters(maxCell), maxRow + 1)
  52. worksheet.Dimension = dimension
  53. return worksheet
  54. }