Skip to content

Commit

Permalink
Merge pull request #587 from tealeg/cell-coords
Browse files Browse the repository at this point in the history
Rows and Cells can report their coordinates
  • Loading branch information
tealeg authored Jun 20, 2020
2 parents 4b24be3 + 4abfe6d commit 9dabf76
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,13 @@ func (c *Cell) FormattedValue() (string, error) {
func (c *Cell) SetDataValidation(dd *xlsxDataValidation) {
c.DataValidation = dd
}

// GetCoordinates returns a pair of integers representing the
// cartesian coorindates of the Cell within the Sheet. The
// coordinates are zero based and a returned in order x,y where x is
// the Column number and y is the Row number. If you need to convert
// these numbers to a Excel cellID (i.e. B15) then please see the
// GetCellIDStringFromCoords function.
func (c *Cell) GetCoordinates() (int, int) {
return c.num, c.Row.num
}
20 changes: 20 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,26 @@ func TestCell(t *testing.T) {
}
})

// Test that GetCoordinates returns accurate numbers..
csRunO(c, "GetCoordinates", func(c *qt.C, option FileOption) {
file := NewFile(option)
sheet, _ := file.AddSheet("Test")
row := sheet.AddRow()
cell := row.AddCell()
x, y := cell.GetCoordinates()
c.Assert(x, qt.Equals, 0)
c.Assert(y, qt.Equals, 0)
cell = row.AddCell()
x, y = cell.GetCoordinates()
c.Assert(x, qt.Equals, 1)
c.Assert(y, qt.Equals, 0)
row = sheet.AddRow()
cell = row.AddCell()
x, y = cell.GetCoordinates()
c.Assert(x, qt.Equals, 0)
c.Assert(y, qt.Equals, 1)
})

}

// formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
Expand Down
5 changes: 5 additions & 0 deletions row.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Row struct {
cells []*Cell // the cells
}

// GetCoordinate returns the y coordinate of the row (the row number). This number is zero based, i.e. the Excel CellID "A1" is in Row 0, not Row 1.
func (r *Row) GetCoordinate() int {
return r.num
}

// SetHeight sets the height of the Row in PostScript points
func (r *Row) SetHeight(ht float64) {
r.height = ht
Expand Down

0 comments on commit 9dabf76

Please sign in to comment.