Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 402 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
   6  use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
   7  use PhpOffice\PhpSpreadsheet\Cell\CellRange;
   8  use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
   9  
  10  class Validations
  11  {
  12      /**
  13       * Validate a cell address.
  14       *
  15       * @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
  16       *               or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
  17       */
  18      public static function validateCellAddress($cellAddress): string
  19      {
  20          if (is_string($cellAddress)) {
  21              [$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
  22  //            if (!empty($worksheet) && $worksheet !== $this->getTitle()) {
  23  //                throw new Exception('Reference is not for this worksheet');
  24  //            }
  25  
  26              return empty($worksheet) ? strtoupper("$address") : $worksheet . '!' . strtoupper("$address");
  27          }
  28  
  29          if (is_array($cellAddress)) {
  30              $cellAddress = CellAddress::fromColumnRowArray($cellAddress);
  31          }
  32  
  33          return (string) $cellAddress;
  34      }
  35  
  36      /**
  37       * Validate a cell address or cell range.
  38       *
  39       * @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  40       *               or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  41       *               or as a CellAddress or AddressRange object.
  42       */
  43      public static function validateCellOrCellRange($cellRange): string
  44      {
  45          if (is_string($cellRange) || is_numeric($cellRange)) {
  46              // Convert a single column reference like 'A' to 'A:A',
  47              //    a single row reference like '1' to '1:1'
  48              $cellRange = (string) preg_replace('/^([A-Z]+|\d+)$/', '$1}:$1}', (string) $cellRange);
  49          } elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
  50              $cellRange = new CellRange($cellRange, $cellRange);
  51          }
  52  
  53          return self::validateCellRange($cellRange);
  54      }
  55  
  56      /**
  57       * Validate a cell range.
  58       *
  59       * @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
  60       *               or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
  61       *               or as an AddressRange object.
  62       */
  63      public static function validateCellRange($cellRange): string
  64      {
  65          if (is_string($cellRange)) {
  66              [$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
  67  
  68              // Convert Column ranges like 'A:C' to 'A1:C1048576'
  69              //      or Row ranges like '1:3' to 'A1:XFD3'
  70              $addressRange = (string) preg_replace(
  71                  ['/^([A-Z]+):([A-Z]+)$/i', '/^(\\d+):(\\d+)$/'],
  72                  ['$1}1:$2}1048576', 'A$1}:XFD$2}'],
  73                  $addressRange
  74              );
  75  
  76              return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange);
  77          }
  78  
  79          if (is_array($cellRange)) {
  80              switch (count($cellRange)) {
  81                  case 2:
  82                      $from = [$cellRange[0], $cellRange[1]];
  83                      $to = [$cellRange[0], $cellRange[1]];
  84  
  85                      break;
  86                  case 4:
  87                      $from = [$cellRange[0], $cellRange[1]];
  88                      $to = [$cellRange[2], $cellRange[3]];
  89  
  90                      break;
  91                  default:
  92                      throw new SpreadsheetException('CellRange array length must be 2 or 4');
  93              }
  94              $cellRange = new CellRange(CellAddress::fromColumnRowArray($from), CellAddress::fromColumnRowArray($to));
  95          }
  96  
  97          return (string) $cellRange;
  98      }
  99  
 100      public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
 101      {
 102          // Uppercase coordinate
 103          $coordinate = strtoupper($coordinate);
 104          // Eliminate leading equal sign
 105          $testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
 106          $defined = $worksheet->getParentOrThrow()->getDefinedName($testCoordinate, $worksheet);
 107          if ($defined !== null) {
 108              if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
 109                  $coordinate = (string) preg_replace('/^=/', '', $defined->getValue());
 110              }
 111          }
 112  
 113          return $coordinate;
 114      }
 115  }