Skip to content

Commit

Permalink
Add row and column offset
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Nov 5, 2020
1 parent 328dc39 commit a343002
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cellwalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type CellWalker struct {
func newCellWalker(col int, row int) *CellWalker {
if col > ColumnsLimit {
col = ColumnsLimit
} else {
if col < 1 {
col = 1
}
}
if row < 1 {
row = 1
Expand Down Expand Up @@ -152,3 +156,13 @@ func (c *CellWalker) Column(colName string) *CellWalker {
func (c *CellWalker) Row(row int) *CellWalker {
return newCellWalker(c.column, row)
}

// ColumnOffset return a cell with a given offset distance to column
func (c *CellWalker) ColumnOffset(offset int) *CellWalker {
return newCellWalker(c.column+offset, c.row)
}

// RowOffset return a cell with a given offset distance to row
func (c *CellWalker) RowOffset(offset int) *CellWalker {
return newCellWalker(c.column, c.row+offset)
}
48 changes: 48 additions & 0 deletions cellwalker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,51 @@ func TestJumpToAnyRowInTheSameColumn(t *testing.T) {

assert.Equal(t, "A778", result.String())
}

func TestPositiveColumnOffset(t *testing.T) {
result := At("A1").ColumnOffset(4)

assert.Equal(t, "E1", result.String())
}

func TestNegativeColumnOffset(t *testing.T) {
result := At("AB1").ColumnOffset(-4)

assert.Equal(t, "X1", result.String())
}

func TestVeryLargePositiveColumnOffset(t *testing.T) {
result := At("A1").ColumnOffset(40000000)

assert.Equal(t, "XFD1", result.String())
}

func TestVeryLargeNegativeColumnOffset(t *testing.T) {
result := At("XFD1").ColumnOffset(-40000000)

assert.Equal(t, "A1", result.String())
}

func TestPositiveRowOffset(t *testing.T) {
result := At("A1").RowOffset(4)

assert.Equal(t, "A5", result.String())
}

func TestNegativeRowOffset(t *testing.T) {
result := At("AB10").RowOffset(-4)

assert.Equal(t, "AB6", result.String())
}

func TestVeryLargePositiveRowOffset(t *testing.T) {
result := At("A1").RowOffset(40000000)

assert.Equal(t, "A1048576", result.String())
}

func TestVeryLargeNegativeRowOffset(t *testing.T) {
result := At("A1048576").RowOffset(-40000000)

assert.Equal(t, "A1", result.String())
}

0 comments on commit a343002

Please sign in to comment.