Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
7
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
use SimpleXMLElement;
10
 
11
class DataValidations
12
{
13
    private Worksheet $worksheet;
14
 
15
    private SimpleXMLElement $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
                if (preg_match('/^[A-Z]{1,3}\d{1,7}/', $range, $matches) === 1) {
31
                    // Ensure left/top row of range exists, thereby
32
                    // adjusting high row/column.
33
                    $this->worksheet->getCell($matches[0]);
34
                }
35
            }
36
        }
37
        foreach ($this->worksheetXml->dataValidations->dataValidation as $dataValidation) {
38
            // Uppercase coordinate
39
            $range = strtoupper((string) $dataValidation['sqref']);
40
            $docValidation = new DataValidation();
41
            $docValidation->setType((string) $dataValidation['type']);
42
            $docValidation->setErrorStyle((string) $dataValidation['errorStyle']);
43
            $docValidation->setOperator((string) $dataValidation['operator']);
44
            $docValidation->setAllowBlank(filter_var($dataValidation['allowBlank'], FILTER_VALIDATE_BOOLEAN));
45
            // showDropDown is inverted (works as hideDropDown if true)
46
            $docValidation->setShowDropDown(!filter_var($dataValidation['showDropDown'], FILTER_VALIDATE_BOOLEAN));
47
            $docValidation->setShowInputMessage(filter_var($dataValidation['showInputMessage'], FILTER_VALIDATE_BOOLEAN));
48
            $docValidation->setShowErrorMessage(filter_var($dataValidation['showErrorMessage'], FILTER_VALIDATE_BOOLEAN));
49
            $docValidation->setErrorTitle((string) $dataValidation['errorTitle']);
50
            $docValidation->setError((string) $dataValidation['error']);
51
            $docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
52
            $docValidation->setPrompt((string) $dataValidation['prompt']);
53
            $docValidation->setFormula1(Xlsx::replacePrefixes((string) $dataValidation->formula1));
54
            $docValidation->setFormula2(Xlsx::replacePrefixes((string) $dataValidation->formula2));
55
            $this->worksheet->setDataValidation($range, $docValidation);
56
        }
57
    }
58
}