From b42c4a19ea64ef935d0c5e8ee53543f67061856a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Kov=C3=A1cs?= Date: Wed, 13 Jun 2018 01:33:08 +0200 Subject: [PATCH] support columns above Z --- src/Sheet/TranslationsSheetCoordinates.php | 34 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Sheet/TranslationsSheetCoordinates.php b/src/Sheet/TranslationsSheetCoordinates.php index ca8d029..75b9064 100644 --- a/src/Sheet/TranslationsSheetCoordinates.php +++ b/src/Sheet/TranslationsSheetCoordinates.php @@ -60,9 +60,7 @@ public static function sheetWithData($dataRowsCount, $columnsCount, $localesCoun public function headerShortRange() { - $alphabet = range('A', 'Z'); - - return $this->sheetTitle.'!A1:'.$alphabet[$this->getColumnsCount() - 1].'1'; + return $this->sheetTitle.'!A1:'.self::stringFromColumnIndex($this->getColumnsCount()).'1'; } public function headerRange() @@ -100,9 +98,8 @@ public function metaColumnsRange() public function dataShortRange($firstRow = 2, $noLastRow = false) { - $alphabet = range('A', 'Z'); $firstColumn = 'A'; - $lastColumn = $alphabet[$this->getColumnsCount() - 1]; + $lastColumn = self::stringFromColumnIndex($this->getColumnsCount()); $lastRow = $this->getRowsCount(); return $this->sheetTitle.'!'.$firstColumn.$firstRow.':'.$lastColumn.($noLastRow ? '' : $lastRow); @@ -164,4 +161,31 @@ public function sourceFileColumnIndex() { return $this->getLocalesCount() + 4; } + + /** + * String from column index. + * + * @see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Cell/Coordinate.php Source of implementation. + * + * @license https://raw.githubusercontent.com/PHPOffice/PhpSpreadsheet/master/LICENSE LGPL (GNU LESSER GENERAL PUBLIC LICENSE) + * + * @param int $columnIndex Column index (A = 1) + * + * @return string + */ + public static function stringFromColumnIndex($columnIndex) + { + static $indexCache = []; + if (!isset($indexCache[$columnIndex])) { + $indexValue = $columnIndex; + $base26 = null; + do { + $characterValue = ($indexValue % 26) ?: 26; + $indexValue = ($indexValue - $characterValue) / 26; + $base26 = chr($characterValue + 64) . ($base26 ?: ''); + } while ($indexValue > 0); + $indexCache[$columnIndex] = $base26; + } + return $indexCache[$columnIndex]; + } }