Skip to content

Commit

Permalink
1.4.11
Browse files Browse the repository at this point in the history
  • Loading branch information
shuchkin committed Mar 13, 2024
1 parent 016bdc0 commit 8d29ae5
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/SimpleXLSXGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,35 +1181,22 @@ public static function raw($value)
public static function cell2coord($cell, &$x, &$y)
{
$x = $y = 0;
$lettercount = 0;
$cell = str_replace([' ', '\t', '\r', '\n', '\v', '\0'], '', $cell);
if (empty($cell)) {
return;
}
$cell = strtoupper($cell);
for ($i = 0, $len = strlen($cell); $i < $len; $i++) {
if ($cell[$i] >= 'A' && $cell[$i] <= 'Z') {
$lettercount++;
}
}
if ($lettercount > 0) {
$x = ord($cell[$lettercount - 1]) - ord('A');
$e = 1;
for ($i = $lettercount - 2; $i >= 0; $i--) {
$x += (ord($cell[$i]) - ord('A') + 1) * pow(26, $e);
$e++;
if (preg_match('/^([A-Z]+)(\d+)$/', $cell, $m)) {
$len = strlen($m[1]);
for ($i = 0; $i < $len; $i++) {
$int = ord($m[1][$i]) - 65; // A -> 0, B -> 1
$int += ($i === $len - 1) ? 0 : 1;
$x += $int * pow(26, $len-$i-1);
}
}
if ($lettercount < strlen($cell)) {
$y = ((int)substr($cell, $lettercount)) - 1;
$y = ((int)$m[2]) - 1;
}
}

public static function coord2cell($x, $y)
{
$c = '';
for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) {
$c = chr(ord('A') + $i % 26) . $c;
$c = chr(65 + $i % 26) . $c;
}
return $c . ($y + 1);
}
Expand Down

0 comments on commit 8d29ae5

Please sign in to comment.