Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   7  use SimpleXMLElement;
   8  
   9  class DataValidations
  10  {
  11      /** @var Worksheet */
  12      private $worksheet;
  13  
  14      /** @var SimpleXMLElement */
  15      private $worksheetXml;
  16  
  17      public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml)
  18      {
  19          $this->worksheet = $workSheet;
  20          $this->worksheetXml = $worksheetXml;
  21      }
  22  
  23      public function load(): void
  24      {
  25          foreach ($this->worksheetXml->dataValidations->dataValidation as $dataValidation) {
  26              // Uppercase coordinate
  27              $range = strtoupper((string) $dataValidation['sqref']);
  28              $rangeSet = explode(' ', $range);
  29              foreach ($rangeSet as $range) {
  30                  $stRange = $this->worksheet->shrinkRangeToFit($range);
  31  
  32                  // Extract all cell references in $range
  33                  foreach (Coordinate::extractAllCellReferencesInRange($stRange) as $reference) {
  34                      // Create validation
  35                      $docValidation = $this->worksheet->getCell($reference)->getDataValidation();
  36                      $docValidation->setType((string) $dataValidation['type']);
  37                      $docValidation->setErrorStyle((string) $dataValidation['errorStyle']);
  38                      $docValidation->setOperator((string) $dataValidation['operator']);
  39                      $docValidation->setAllowBlank(filter_var($dataValidation['allowBlank'], FILTER_VALIDATE_BOOLEAN));
  40                      // showDropDown is inverted (works as hideDropDown if true)
  41                      $docValidation->setShowDropDown(!filter_var($dataValidation['showDropDown'], FILTER_VALIDATE_BOOLEAN));
  42                      $docValidation->setShowInputMessage(filter_var($dataValidation['showInputMessage'], FILTER_VALIDATE_BOOLEAN));
  43                      $docValidation->setShowErrorMessage(filter_var($dataValidation['showErrorMessage'], FILTER_VALIDATE_BOOLEAN));
  44                      $docValidation->setErrorTitle((string) $dataValidation['errorTitle']);
  45                      $docValidation->setError((string) $dataValidation['error']);
  46                      $docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
  47                      $docValidation->setPrompt((string) $dataValidation['prompt']);
  48                      $docValidation->setFormula1((string) $dataValidation->formula1);
  49                      $docValidation->setFormula2((string) $dataValidation->formula2);
  50                      $docValidation->setSqref($range);
  51                  }
  52              }
  53          }
  54      }
  55  }