Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]

   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX\Helper;
   4  
   5  use Box\Spout\Common\Exception\InvalidArgumentException;
   6  
   7  /**
   8   * Class CellHelper
   9   * This class provides helper functions when working with cells
  10   */
  11  class CellHelper
  12  {
  13      // Using ord() is super slow... Using a pre-computed hash table instead.
  14      private static $columnLetterToIndexMapping = [
  15          'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
  16          'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
  17          'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
  18          'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25,
  19      ];
  20  
  21      /**
  22       * Returns the base 10 column index associated to the cell index (base 26).
  23       * Excel uses A to Z letters for column indexing, where A is the 1st column,
  24       * Z is the 26th and AA is the 27th.
  25       * The mapping is zero based, so that A1 maps to 0, B2 maps to 1, Z13 to 25 and AA4 to 26.
  26       *
  27       * @param string $cellIndex The Excel cell index ('A1', 'BC13', ...)
  28       * @throws \Box\Spout\Common\Exception\InvalidArgumentException When the given cell index is invalid
  29       * @return int
  30       */
  31      public static function getColumnIndexFromCellIndex($cellIndex)
  32      {
  33          if (!self::isValidCellIndex($cellIndex)) {
  34              throw new InvalidArgumentException('Cannot get column index from an invalid cell index.');
  35          }
  36  
  37          $columnIndex = 0;
  38  
  39          // Remove row information
  40          $columnLetters = preg_replace('/\d/', '', $cellIndex);
  41  
  42          // strlen() is super slow too... Using isset() is way faster and not too unreadable,
  43          // since we checked before that there are between 1 and 3 letters.
  44          $columnLength = isset($columnLetters[1]) ? (isset($columnLetters[2]) ? 3 : 2) : 1;
  45  
  46          // Looping over the different letters of the column is slower than this method.
  47          // Also, not using the pow() function because it's slooooow...
  48          switch ($columnLength) {
  49              case 1:
  50                  $columnIndex = (self::$columnLetterToIndexMapping[$columnLetters]);
  51                  break;
  52              case 2:
  53                  $firstLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[0]] + 1) * 26;
  54                  $secondLetterIndex = self::$columnLetterToIndexMapping[$columnLetters[1]];
  55                  $columnIndex = $firstLetterIndex + $secondLetterIndex;
  56                  break;
  57              case 3:
  58                  $firstLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[0]] + 1) * 676;
  59                  $secondLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[1]] + 1) * 26;
  60                  $thirdLetterIndex = self::$columnLetterToIndexMapping[$columnLetters[2]];
  61                  $columnIndex = $firstLetterIndex + $secondLetterIndex + $thirdLetterIndex;
  62                  break;
  63          }
  64  
  65          return $columnIndex;
  66      }
  67  
  68      /**
  69       * Returns whether a cell index is valid, in an Excel world.
  70       * To be valid, the cell index should start with capital letters and be followed by numbers.
  71       * There can only be 3 letters, as there can only be 16,384 rows, which is equivalent to 'XFE'.
  72       *
  73       * @param string $cellIndex The Excel cell index ('A1', 'BC13', ...)
  74       * @return bool
  75       */
  76      protected static function isValidCellIndex($cellIndex)
  77      {
  78          return (preg_match('/^[A-Z]{1,3}\d+$/', $cellIndex) === 1);
  79      }
  80  }