\PhpOffice\PhpSpreadsheet\CellCoordinate

Helper class to manipulate cell coordinates.

Columns indexes and rows are always based on 1, not on 0. This match the behavior that Excel users are used to, and also match the Excel functions COLUMN() and ROW().

Summary

Methods
Properties
Constants
coordinateFromString()
coordinateIsRange()
absoluteReference()
absoluteCoordinate()
splitRange()
buildRange()
rangeBoundaries()
rangeDimension()
getRangeBoundaries()
columnIndexFromString()
stringFromColumnIndex()
extractAllCellReferencesInRange()
mergeRangesInCollection()
No public properties found
DEFAULT_RANGE
No protected methods found
No protected properties found
N/A
getReferencesForCellBlock()
getCellBlocksFromRangeString()
validateRange()
No private properties found
N/A

Constants

DEFAULT_RANGE

DEFAULT_RANGE = 'A1:A1' : string

Default range variable constant.

Methods

coordinateFromString()

coordinateFromString(string  $pCoordinateString) : array<mixed,string>

Coordinate from string.

Parameters

string $pCoordinateString

eg: 'A1'

Throws

\PhpOffice\PhpSpreadsheet\Exception

Returns

array<mixed,string> —

Array containing column and row (indexes 0 and 1)

coordinateIsRange()

coordinateIsRange(string  $coord) : boolean

Checks if a coordinate represents a range of cells.

Parameters

string $coord

eg: 'A1' or 'A1:A2' or 'A1:A2,C1:C2'

Returns

boolean —

Whether the coordinate represents a range of cells

absoluteReference()

absoluteReference(string  $pCoordinateString) : string

Make string row, column or cell coordinate absolute.

Parameters

string $pCoordinateString

e.g. 'A' or '1' or 'A1' Note that this value can be a row or column reference as well as a cell reference

Throws

\PhpOffice\PhpSpreadsheet\Exception

Returns

string —

Absolute coordinate e.g. '$A' or '$1' or '$A$1'

absoluteCoordinate()

absoluteCoordinate(string  $pCoordinateString) : string

Make string coordinate absolute.

Parameters

string $pCoordinateString

e.g. 'A1'

Throws

\PhpOffice\PhpSpreadsheet\Exception

Returns

string —

Absolute coordinate e.g. '$A$1'

splitRange()

splitRange(string  $pRange) : array

Split range into coordinate strings.

Parameters

string $pRange

e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'

Returns

array —

Array containing one or more arrays containing one or two coordinate strings e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']] or ['B4']

buildRange()

buildRange(array  $pRange) : string

Build range from coordinate strings.

Parameters

array $pRange

Array containg one or more arrays containing one or two coordinate strings

Throws

\PhpOffice\PhpSpreadsheet\Exception

Returns

string —

String representation of $pRange

rangeBoundaries()

rangeBoundaries(string  $pRange) : array

Calculate range boundaries.

Parameters

string $pRange

Cell range (e.g. A1:A1)

Returns

array —

Range coordinates [Start Cell, End Cell] where Start Cell and End Cell are arrays (Column Number, Row Number)

rangeDimension()

rangeDimension(string  $pRange) : array

Calculate range dimension.

Parameters

string $pRange

Cell range (e.g. A1:A1)

Returns

array —

Range dimension (width, height)

getRangeBoundaries()

getRangeBoundaries(string  $pRange) : array

Calculate range boundaries.

Parameters

string $pRange

Cell range (e.g. A1:A1)

Returns

array —

Range coordinates [Start Cell, End Cell] where Start Cell and End Cell are arrays [Column ID, Row Number]

columnIndexFromString()

columnIndexFromString(string  $pString) : integer

Column index from string.

Parameters

string $pString

eg 'A'

Returns

integer —

Column index (A = 1)

stringFromColumnIndex()

stringFromColumnIndex(integer  $columnIndex) : string

String from column index.

Parameters

integer $columnIndex

Column index (A = 1)

Returns

string

extractAllCellReferencesInRange()

extractAllCellReferencesInRange(string  $pRange) : array

Extract all cell references in range, which may be comprised of multiple cell ranges.

Parameters

string $pRange

Range (e.g. A1 or A1:C10 or A1:E10 A20:E25)

Returns

array —

Array containing single cell references

mergeRangesInCollection()

mergeRangesInCollection(array  $pCoordCollection) : array

Convert an associative array of single cell coordinates to values to an associative array of cell ranges to values. Only adjacent cell coordinates with the same value will be merged. If the value is an object, it must implement the method getHashCode().

For example, this function converts:

[ 'A1' => 'x', 'A2' => 'x', 'A3' => 'x', 'A4' => 'y' ]

to:

[ 'A1:A3' => 'x', 'A4' => 'y' ]

Parameters

array $pCoordCollection

associative array mapping coordinates to values

Returns

array —

associative array mapping coordinate ranges to valuea

getReferencesForCellBlock()

getReferencesForCellBlock(string  $cellBlock) : array

Get all cell references for an individual cell block.

Parameters

string $cellBlock

A cell range e.g. A4:B5

Returns

array —

All individual cells in that range

getCellBlocksFromRangeString()

getCellBlocksFromRangeString(string  $pRange) : array<mixed,string>

Get the individual cell blocks from a range string, splitting by space and removing any $ characters.

Parameters

string $pRange

Returns

array<mixed,string>

validateRange()

validateRange(string  $cellBlock, integer  $startColumnIndex, integer  $endColumnIndex, integer  $currentRow, integer  $endRow) 

Check that the given range is valid, i.e. that the start column and row are not greater than the end column and row.

Parameters

string $cellBlock

The original range, for displaying a meaningful error message

integer $startColumnIndex
integer $endColumnIndex
integer $currentRow
integer $endRow