Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel get column name relative to given column

Tags:

php

phpexcel

Using PHPExcel, is it possible to get the name of a column located X number of columns to the left or right?

Example, given column BZ, I'd like to return column name CB or BX. (2 to the right or left)

Thanks

like image 755
raidzero Avatar asked Feb 28 '13 22:02

raidzero


1 Answers

There are functions already built into PHPExcel to help you do this

$adjustment = -2;
$currentColumn = 'BZ';

$columnIndex = PHPExcel_Cell::columnIndexFromString($currentColumn);
$adjustedColumnIndex = $columnIndex + $adjustment;
$adjustedColumn = PHPExcel_Cell::stringFromColumnIndex($adjustedColumnIndex - 1);

Note the (historic) discrepancy that columnIndexFromString() will return a 1 for column A, but that stringFromColumnIndex expects a 0 to correspond to column A

like image 81
Mark Baker Avatar answered Nov 06 '22 07:11

Mark Baker