ソースを参照

New AddRowAtIndex func

Ryan Hollis 7 年 前
コミット
115943bfda
2 ファイル変更101 行追加3 行削除
  1. 27 0
      sheet.go
  2. 74 3
      sheet_test.go

+ 27 - 0
sheet.go

@@ -56,6 +56,33 @@ func (s *Sheet) AddRow() *Row {
 	return row
 }
 
+// Add a new Row to a Sheet at a specific index
+func (s *Sheet) AddRowAtIndex(index int) (*Row, error) {
+	if index < 0 || index > len(s.Rows) {
+		return nil, errors.New("AddRowAtIndex: index out of bounds")
+	}
+	row := &Row{Sheet: s}
+	s.Rows = append(s.Rows, nil)
+
+	if index < len(s.Rows) {
+		copy(s.Rows[index+1:], s.Rows[index:])
+	}
+	s.Rows[index] = row
+	if len(s.Rows) > s.MaxRow {
+		s.MaxRow = len(s.Rows)
+	}
+	return row, nil
+}
+
+// Removes a row at a specific index
+func (s *Sheet) RemoveRowAtIndex(index int) error {
+	if index < 0 || index >= len(s.Rows) {
+		return errors.New("RemoveRowAtIndex: index out of bounds")
+	}
+	s.Rows = append(s.Rows[:index], s.Rows[index+1:]...)
+	return nil
+}
+
 // Make sure we always have as many Cols as we do cells.
 func (s *Sheet) maybeAddCol(cellCount int) {
 	if cellCount > s.MaxCol {

+ 74 - 3
sheet_test.go

@@ -13,12 +13,83 @@ var _ = Suite(&SheetSuite{})
 
 // Test we can add a Row to a Sheet
 func (s *SheetSuite) TestAddRow(c *C) {
+	// Create a file with three rows.
 	var f *File
 	f = NewFile()
 	sheet, _ := f.AddSheet("MySheet")
-	row := sheet.AddRow()
-	c.Assert(row, NotNil)
-	c.Assert(len(sheet.Rows), Equals, 1)
+	row0 := sheet.AddRow()
+	cell0 := row0.AddCell()
+	cell0.Value = "Row 0"
+	c.Assert(row0, NotNil)
+	row1 := sheet.AddRow()
+	cell1 := row1.AddCell()
+	cell1.Value = "Row 1"
+	row2 := sheet.AddRow()
+	cell2 := row2.AddCell()
+	cell2.Value = "Row 2"
+	// Check the file
+	expected := []string{"Row 0", "Row 1", "Row 2"}
+	c.Assert(len(sheet.Rows), Equals, len(expected))
+	for i, row := range sheet.Rows {
+		c.Assert(row.Cells[0].Value, Equals, expected[i])
+	}
+
+	// Insert a row in the middle
+	row1pt5, err := sheet.AddRowAtIndex(2)
+	c.Assert(err, IsNil)
+	cell1pt5 := row1pt5.AddCell()
+	cell1pt5.Value = "Row 1.5"
+
+	expected = []string{"Row 0", "Row 1", "Row 1.5", "Row 2"}
+	c.Assert(len(sheet.Rows), Equals, len(expected))
+	for i, row := range sheet.Rows {
+		c.Assert(row.Cells[0].Value, Equals, expected[i])
+	}
+
+	// Insert a row at the beginning
+	rowNewStart, err := sheet.AddRowAtIndex(0)
+	c.Assert(err, IsNil)
+	cellNewStart := rowNewStart.AddCell()
+	cellNewStart.Value = "Row -1"
+	// Insert a row at one index past the end, this is the same as AddRow().
+	row2pt5, err := sheet.AddRowAtIndex(5)
+	c.Assert(err, IsNil)
+	cell2pt5 := row2pt5.AddCell()
+	cell2pt5.Value = "Row 2.5"
+
+	expected = []string{"Row -1", "Row 0", "Row 1", "Row 1.5", "Row 2", "Row 2.5"}
+	c.Assert(len(sheet.Rows), Equals, len(expected))
+	for i, row := range sheet.Rows {
+		c.Assert(row.Cells[0].Value, Equals, expected[i])
+	}
+
+	// Negative and out of range indicies should fail for insert
+	_, err = sheet.AddRowAtIndex(-1)
+	c.Assert(err, NotNil)
+	// Since we allow inserting into the position that does not yet exist, it has to be 1 greater
+	// than you would think in order to fail.
+	_, err = sheet.AddRowAtIndex(7)
+	c.Assert(err, NotNil)
+
+	// Negative and out of range indicies should fail for remove
+	err = sheet.RemoveRowAtIndex(-1)
+	c.Assert(err, NotNil)
+	err = sheet.RemoveRowAtIndex(6)
+	c.Assert(err, NotNil)
+
+	// Remove from the beginning, the end, and the middle.
+	err = sheet.RemoveRowAtIndex(0)
+	c.Assert(err, IsNil)
+	err = sheet.RemoveRowAtIndex(4)
+	c.Assert(err, IsNil)
+	err = sheet.RemoveRowAtIndex(2)
+	c.Assert(err, IsNil)
+
+	expected = []string{"Row 0", "Row 1", "Row 2"}
+	c.Assert(len(sheet.Rows), Equals, len(expected))
+	for i, row := range sheet.Rows {
+		c.Assert(row.Cells[0].Value, Equals, expected[i])
+	}
 }
 
 func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {