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\Calculation\LookupRef;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
use PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
8
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9
 
10
class Address
11
{
12
    use ArrayEnabled;
13
 
14
    public const ADDRESS_ABSOLUTE = 1;
15
    public const ADDRESS_COLUMN_RELATIVE = 2;
16
    public const ADDRESS_ROW_RELATIVE = 3;
17
    public const ADDRESS_RELATIVE = 4;
18
 
19
    public const REFERENCE_STYLE_A1 = true;
20
    public const REFERENCE_STYLE_R1C1 = false;
21
 
22
    /**
23
     * ADDRESS.
24
     *
25
     * Creates a cell address as text, given specified row and column numbers.
26
     *
27
     * Excel Function:
28
     *        =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText])
29
     *
30
     * @param mixed $row Row number (integer) to use in the cell reference
31
     *                      Or can be an array of values
32
     * @param mixed $column Column number (integer) to use in the cell reference
33
     *                      Or can be an array of values
34
     * @param mixed $relativity Integer flag indicating the type of reference to return
35
     *                             1 or omitted    Absolute
36
     *                             2               Absolute row; relative column
37
     *                             3               Relative row; absolute column
38
     *                             4               Relative
39
     *                      Or can be an array of values
40
     * @param mixed $referenceStyle A logical (boolean) value that specifies the A1 or R1C1 reference style.
41
     *                                TRUE or omitted    ADDRESS returns an A1-style reference
42
     *                                FALSE              ADDRESS returns an R1C1-style reference
43
     *                      Or can be an array of values
44
     * @param mixed $sheetName Optional Name of worksheet to use
45
     *                      Or can be an array of values
46
     *
47
     * @return array|string If an array of values is passed as the $testValue argument, then the returned result will also be
48
     *            an array with the same dimensions
49
     */
50
    public static function cell(mixed $row, mixed $column, mixed $relativity = 1, mixed $referenceStyle = true, mixed $sheetName = ''): array|string
51
    {
52
        if (
53
            is_array($row) || is_array($column)
54
            || is_array($relativity) || is_array($referenceStyle) || is_array($sheetName)
55
        ) {
56
            return self::evaluateArrayArguments(
57
                [self::class, __FUNCTION__],
58
                $row,
59
                $column,
60
                $relativity,
61
                $referenceStyle,
62
                $sheetName
63
            );
64
        }
65
 
66
        $relativity = $relativity ?? 1;
67
        $referenceStyle = $referenceStyle ?? true;
68
 
69
        if (($row < 1) || ($column < 1)) {
70
            return ExcelError::VALUE();
71
        }
72
 
73
        $sheetName = self::sheetName($sheetName);
74
 
75
        if (is_int($referenceStyle)) {
76
            $referenceStyle = (bool) $referenceStyle;
77
        }
78
        if ((!is_bool($referenceStyle)) || $referenceStyle === self::REFERENCE_STYLE_A1) {
79
            return self::formatAsA1($row, $column, $relativity, $sheetName);
80
        }
81
 
82
        return self::formatAsR1C1($row, $column, $relativity, $sheetName);
83
    }
84
 
85
    private static function sheetName(string $sheetName): string
86
    {
87
        if ($sheetName > '') {
88
            if (str_contains($sheetName, ' ') || str_contains($sheetName, '[')) {
89
                $sheetName = "'{$sheetName}'";
90
            }
91
            $sheetName .= '!';
92
        }
93
 
94
        return $sheetName;
95
    }
96
 
97
    private static function formatAsA1(int $row, int $column, int $relativity, string $sheetName): string
98
    {
99
        $rowRelative = $columnRelative = '$';
100
        if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
101
            $columnRelative = '';
102
        }
103
        if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
104
            $rowRelative = '';
105
        }
106
        $column = Coordinate::stringFromColumnIndex($column);
107
 
108
        return "{$sheetName}{$columnRelative}{$column}{$rowRelative}{$row}";
109
    }
110
 
111
    private static function formatAsR1C1(int $row, int $column, int $relativity, string $sheetName): string
112
    {
113
        if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
114
            $column = "[{$column}]";
115
        }
116
        if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) {
117
            $row = "[{$row}]";
118
        }
119
        [$rowChar, $colChar] = AddressHelper::getRowAndColumnChars();
120
 
121
        return "{$sheetName}$rowChar{$row}$colChar{$column}";
122
    }
123
}