\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
processRangeSetOperators()
sortCellReferenceArray()
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) : string[]

Coordinate from string.

Parameters

string $pCoordinateString

eg: 'A1'

Returns

string[] —

Array containing column and row (indexes 0 and 1)

coordinateIsRange()

coordinateIsRange(string  $coord) : bool

Checks if a coordinate represents a range of cells.

Parameters

string $coord

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

Returns

bool —

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

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'

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

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) : int

Column index from string.

Parameters

string $pString

eg 'A'

Returns

int —

Column index (A = 1)

stringFromColumnIndex()

stringFromColumnIndex(int  $columnIndex) : string

String from column index.

Parameters

int $columnIndex

Column index (A = 1)

Returns

string —

extractAllCellReferencesInRange()

extractAllCellReferencesInRange(string  $cellRange) : array

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

Parameters

string $cellRange

Range: e.g. 'A1' or 'A1:C10' or 'A1:E10,A20:E25' or 'A1:E5 C3:G7' or 'A1:C1,A3:C3 B1:C3'

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

processRangeSetOperators()

processRangeSetOperators(array  $operators, array  $cells) : array

Parameters

array $operators
array $cells

Returns

array —

sortCellReferenceArray()

sortCellReferenceArray(array  $cellList) : array

Parameters

array $cellList

Returns

array —

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  $rangeString) : array[]

Get the individual cell blocks from a range string, removing any $ characters.

then splitting by operators and returning an array with ranges and operators.

Parameters

string $rangeString

Returns

array[] —

validateRange()

validateRange(string  $cellBlock, int  $startColumnIndex, int  $endColumnIndex, int  $currentRow, int  $endRow) : void

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

int $startColumnIndex
int $endColumnIndex
int $currentRow
int $endRow