Browse Source

Merge pull request #389 from rentiansheng/sheet.row

sheet.row get row by index from sheet issue #388
Ryan Hollis 7 years ago
parent
commit
f1917addaa
2 changed files with 32 additions and 0 deletions
  1. 19 0
      sheet.go
  2. 13 0
      sheet_test.go

+ 19 - 0
sheet.go

@@ -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 {

+ 13 - 0
sheet_test.go

@@ -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, 11)
+	row = sheet.Row(2)
+	c.Assert(row, NotNil)
+	c.Assert(len(sheet.Rows), Equals, 11)
+}
+
 func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
 	file := NewFile()
 	sheet, _ := file.AddSheet("Sheet1")