PHPExcel get column name relative to given column

后端 未结 1 640
执念已碎
执念已碎 2021-02-07 11:51

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

相关标签:
1条回答
  • 2021-02-07 12:14

    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

    0 讨论(0)
提交回复
热议问题