1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
8
|
|
|
|
9
|
|
|
class TorowTocol |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Excel function TOCOL. |
13
|
|
|
* |
14
|
|
|
* @return mixed[]|string |
15
|
|
|
*/ |
16
|
9 |
|
public static function tocol(mixed $array, mixed $ignore = 0, mixed $byColumn = false): array|string |
17
|
|
|
{ |
18
|
9 |
|
$result = self::torow($array, $ignore, $byColumn); |
19
|
9 |
|
if (is_array($result)) { |
20
|
7 |
|
return array_map((fn ($x) => [$x]), $result); |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
return $result; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Excel function TOROW. |
28
|
|
|
* |
29
|
|
|
* @return mixed[]|string |
30
|
|
|
*/ |
31
|
18 |
|
public static function torow(mixed $array, mixed $ignore = 0, mixed $byColumn = false): array|string |
32
|
|
|
{ |
33
|
18 |
|
if (!is_numeric($ignore)) { |
34
|
2 |
|
return ExcelError::VALUE(); |
35
|
|
|
} |
36
|
16 |
|
$ignore = (int) $ignore; |
37
|
16 |
|
if ($ignore < 0 || $ignore > 3) { |
38
|
2 |
|
return ExcelError::VALUE(); |
39
|
|
|
} |
40
|
14 |
|
if (is_int($byColumn) || is_float($byColumn)) { |
41
|
2 |
|
$byColumn = (bool) $byColumn; |
42
|
|
|
} |
43
|
14 |
|
if (!is_bool($byColumn)) { |
44
|
|
|
return ExcelError::VALUE(); |
45
|
|
|
} |
46
|
14 |
|
if (!is_array($array)) { |
47
|
|
|
$array = [$array]; |
48
|
|
|
} |
49
|
14 |
|
if ($byColumn) { |
50
|
4 |
|
$temp = []; |
51
|
4 |
|
foreach ($array as $row) { |
52
|
4 |
|
if (!is_array($row)) { |
53
|
|
|
$row = [$row]; |
54
|
|
|
} |
55
|
4 |
|
$temp[] = Functions::flattenArray($row); |
56
|
|
|
} |
57
|
4 |
|
$array = ChooseRowsEtc::transpose($temp); |
58
|
|
|
} else { |
59
|
10 |
|
$array = Functions::flattenArray($array); |
60
|
|
|
} |
61
|
|
|
|
62
|
14 |
|
return self::byRow($array, $ignore); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed[] $array |
67
|
|
|
* |
68
|
|
|
* @return mixed[] |
69
|
|
|
*/ |
70
|
14 |
|
private static function byRow(array $array, int $ignore): array |
71
|
|
|
{ |
72
|
14 |
|
$returnMatrix = []; |
73
|
14 |
|
foreach ($array as $row) { |
74
|
14 |
|
if (!is_array($row)) { |
75
|
10 |
|
$row = [$row]; |
76
|
|
|
} |
77
|
14 |
|
foreach ($row as $cell) { |
78
|
14 |
|
if ($cell === null) { |
79
|
14 |
|
if ($ignore === 1 || $ignore === 3) { |
80
|
5 |
|
continue; |
81
|
|
|
} |
82
|
9 |
|
$cell = 0; |
83
|
14 |
|
} elseif (ErrorValue::isError($cell)) { |
84
|
14 |
|
if ($ignore === 2 || $ignore === 3) { |
85
|
5 |
|
continue; |
86
|
|
|
} |
87
|
|
|
} |
88
|
14 |
|
$returnMatrix[] = $cell; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
return $returnMatrix; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|