Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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      private $worksheet;
  12  
  13      private $worksheetXml;
  14  
  15      public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml)
  16      {
  17          $this->worksheet = $workSheet;
  18          $this->worksheetXml = $worksheetXml;
  19      }
  20  
  21      public function load(): void
  22      {
  23          foreach ($this->worksheetXml->dataValidations->dataValidation as $dataValidation) {
  24              // Uppercase coordinate
  25              $range = strtoupper($dataValidation['sqref']);
  26              $rangeSet = explode(' ', $range);
  27              foreach ($rangeSet as $range) {
  28                  $stRange = $this->worksheet->shrinkRangeToFit($range);
  29  
  30                  // Extract all cell references in $range
  31                  foreach (Coordinate::extractAllCellReferencesInRange($stRange) as $reference) {
  32                      // Create validation
  33                      $docValidation = $this->worksheet->getCell($reference)->getDataValidation();
  34                      $docValidation->setType((string) $dataValidation['type']);
  35                      $docValidation->setErrorStyle((string) $dataValidation['errorStyle']);
  36                      $docValidation->setOperator((string) $dataValidation['operator']);
  37                      $docValidation->setAllowBlank(filter_var($dataValidation['allowBlank'], FILTER_VALIDATE_BOOLEAN));
  38                      // showDropDown is inverted (works as hideDropDown if true)
  39                      $docValidation->setShowDropDown(!filter_var($dataValidation['showDropDown'], FILTER_VALIDATE_BOOLEAN));
  40                      $docValidation->setShowInputMessage(filter_var($dataValidation['showInputMessage'], FILTER_VALIDATE_BOOLEAN));
  41                      $docValidation->setShowErrorMessage(filter_var($dataValidation['showErrorMessage'], FILTER_VALIDATE_BOOLEAN));
  42                      $docValidation->setErrorTitle((string) $dataValidation['errorTitle']);
  43                      $docValidation->setError((string) $dataValidation['error']);
  44                      $docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
  45                      $docValidation->setPrompt((string) $dataValidation['prompt']);
  46                      $docValidation->setFormula1((string) $dataValidation->formula1);
  47                      $docValidation->setFormula2((string) $dataValidation->formula2);
  48                      $docValidation->setSqref($range);
  49                  }
  50              }
  51          }
  52      }
  53  }