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