From dbe5daaad3ffa049497c18030ca1f12628fa9057 Mon Sep 17 00:00:00 2001 From: Dieter Adriaenssens Date: Wed, 14 Jul 2010 22:29:26 +0200 Subject: [PATCH] Converting number of Excel column names no longer limited --- ChangeLog | 2 +- libraries/import.lib.php | 68 +++++++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index fe595fd417..2b0c93fbc0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,7 +19,7 @@ $HeadURL: https://phpmyadmin.svn.sourceforge.net/svnroot/phpmyadmin/trunk/phpMyA - bug #3023507 [core] No result set display from stored procedure SELECT - bug [export] CSV for MS Excel (Windows) should have semi-colon as separator - [core] Update library PHPExcel to version 1.7.3c -- bug #2994885 [import] Convert Excel column name correctly +- bug #2994885, bug #3029168 [import] Convert Excel column name correctly 3.3.4.0 (2010-06-28) - bug #2996161 [import] properly escape import value diff --git a/libraries/import.lib.php b/libraries/import.lib.php index 70e5f13852..6c039bd6e7 100644 --- a/libraries/import.lib.php +++ b/libraries/import.lib.php @@ -303,9 +303,19 @@ function PMA_importGetNextChunk($size = 32768) /** * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.) - * This algorithm only works up to ZZ. it fails on AAA (up to 701 columns) * - * @author Derek Schaefer (derek.schaefer@gmail.com) + * This functions uses recursion to build the Excel column name. + * + * The column number (1-26) is converted to the responding ASCII character (A-Z) and returned. + * + * If the column number is bigger than 26 (= num of letters in alfabet), + * an extra character needs to be added. To find this extra character, the number is divided by 26 + * and this value is passed to another instance of the same function (hence recursion). + * In that new instance the number is evaluated again, and if it is still bigger than 26, it is divided again + * and passed to another instance of the same function. This continues until the number is smaller than 26. + * Then the last called function returns the corresponding ASCII character to the function that called it. + * Each time a called function ends an extra character is added to the column name. + * When the first function is reached, the last character is addded and the complete column name is returned. * * @access public * @@ -315,31 +325,35 @@ function PMA_importGetNextChunk($size = 32768) */ function PMA_getColumnAlphaName($num) { - /* ASCII value for capital "A" */ - $A = 65; - $sCol = ""; - $iRemain = 0; - - /* This algorithm only works up to ZZ. it fails on AAA */ - - if ($num > 701) { - return $num; - } elseif ($num <= 26) { - if ($num == 0) { - $sCol = chr(($A + 26) - 1); - } else { - $sCol = chr(($A + $num) - 1); - } - } else { - $iRemain = (($num / 26)) - 1; - if (($num % 26) == 0) { - $sCol = PMA_getColumnAlphaName($iRemain) . PMA_getColumnAlphaName($num % 26); - } else { - $sCol = chr($A + $iRemain) . PMA_getColumnAlphaName($num % 26); - } - } - - return $sCol; + $A = 65; // ASCII value for capital "A" + $col_name = ""; + + if ($num > 26) { + $div = (int)($num / 26); + $remain = (int)($num % 26); + + // subtract 1 of divided value in case the modulus is 0, + // this is necessary because A-Z has no 'zero' + if ($remain == 0) { + $div--; + } + + // recursive function call + $col_name = PMA_getColumnAlphaName($div); + // use modulus as new column number + $num = $remain; + } + + if ($num == 0) { + // use 'Z' if column number is 0, + // this is necessary because A-Z has no 'zero' + $col_name .= chr(($A + 26) - 1); + } else { + // convert column number to ASCII character + $col_name .= chr(($A + $num) - 1); + } + + return $col_name; } /** -- 2.11.4.GIT