Skip to content

Commit

Permalink
Merge pull request #1 from rentiansheng/sheet.row
Browse files Browse the repository at this point in the history
sheet.row get row by index from sheet issue #388
  • Loading branch information
rentiansheng committed May 10, 2018
2 parents 0d24a1d + 9053893 commit f5f5fbc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ func (s *Sheet) AddRow() *Row {
return row
}

// Make sure we always have as many Rows as we do cells.
func (s *Sheet) maybeAddRow(rowCount int) {
if rowCount > s.MaxRow {
loopCnt := rowCount - s.MaxRow
for i := 0; i < loopCnt; i++ {

row := &Row{Sheet: s}
s.Rows = append(s.Rows, row)
}
s.MaxRow = rowCount
}
}

// Make sure we always have as many Rows as we do cells.
func (s *Sheet) Row(idx int) *Row {
s.maybeAddRow(idx + 1)
return s.Rows[idx]
}

// Make sure we always have as many Cols as we do cells.
func (s *Sheet) maybeAddCol(cellCount int) {
if cellCount > s.MaxCol {
Expand Down
13 changes: 13 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ func (s *SheetSuite) TestAddRow(c *C) {
c.Assert(len(sheet.Rows), Equals, 1)
}

// Test we can get row by index from Sheet
func (s *SheetSuite) TestGetRowByIndex(c *C) {
var f *File
f = NewFile()
sheet, _ := f.AddSheet("MySheet")
row := sheet.Row(10)
c.Assert(row, NotNil)
c.Assert(len(sheet.Rows), Equals, 10)
row = sheet.Row(2)
c.Assert(row, NotNil)
c.Assert(len(sheet.Rows), Equals, 10)
}

func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
Expand Down

0 comments on commit f5f5fbc

Please sign in to comment.