From 4abfe6d4eea7a9b7514d15e4758c8ffc4ba8f15f Mon Sep 17 00:00:00 2001 From: "Geoffrey J. Teale" Date: Sat, 20 Jun 2020 07:10:46 +0200 Subject: [PATCH] Rows and Cells can report their coordinates --- cell.go | 10 ++++++++++ cell_test.go | 20 ++++++++++++++++++++ row.go | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/cell.go b/cell.go index 102f3d55..48fcff25 100644 --- a/cell.go +++ b/cell.go @@ -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 +} diff --git a/cell_test.go b/cell_test.go index 93dc82fd..078899d9 100644 --- a/cell_test.go +++ b/cell_test.go @@ -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 diff --git a/row.go b/row.go index 456db91e..071d9e17 100644 --- a/row.go +++ b/row.go @@ -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