Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/balping/translation-sheet
Browse files Browse the repository at this point in the history
…into balping-develop
  • Loading branch information
nbourguig committed Sep 6, 2018
2 parents 14b9b23 + b42c4a1 commit cb1a647
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/Sheet/TranslationsSheetCoordinates.php
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
}
}

0 comments on commit cb1a647

Please sign in to comment.