Skip to content

Commit 7c8d73f

Browse files
authored
Merge pull request #4286 from oleibman/choosecols
CHOOSECOLS, CHOOSEROWS, DROP, TAKE, and EXPAND
2 parents c06d59e + 7bb8b00 commit 7c8d73f

File tree

17 files changed

+1271
-21
lines changed

17 files changed

+1271
-21
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12-
- Nothing yet.
12+
- CHOOSECOLS, CHOOSEROWS, DROP, TAKE, and EXPAND. [PR #4286](https://github.com/PHPOffice/PhpSpreadsheet/pull/4286)
1313

1414
### Changed
1515

docs/references/function-list-by-category.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ Excel Function | PhpSpreadsheet Function
240240
ADDRESS | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Address::cell
241241
AREAS | **Not yet Implemented**
242242
CHOOSE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Selection::CHOOSE
243+
CHOOSECOLS | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::chooseCols
244+
CHOOSEROWS | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::chooseRows
243245
COLUMN | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\RowColumnInformation::COLUMN
244246
COLUMNS | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\RowColumnInformation::COLUMNS
247+
DROP | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::drop
248+
EXPAND | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::expand
245249
FILTER | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Filter::filter
246250
FORMULATEXT | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Formula::text
247251
GETPIVOTDATA | **Not yet Implemented**
@@ -257,6 +261,7 @@ ROWS | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\RowCo
257261
RTD | **Not yet Implemented**
258262
SORT | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Sort::sort
259263
SORTBY | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Sort::sortBy
264+
TAKE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::take
260265
TRANSPOSE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Matrix::transpose
261266
UNIQUE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Unique::unique
262267
VLOOKUP | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\VLookup::lookup
@@ -283,8 +288,6 @@ BASE | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Base::
283288
CEILING | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Ceiling::ceiling
284289
CEILING.MATH | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Ceiling::math
285290
CEILING.PRECISE | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Ceiling::precise
286-
CHOOSECOLS | **Not yet Implemented**
287-
CHOOSEROWS | **Not yet Implemented**
288291
COMBIN | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Combinations::withoutRepetition
289292
COMBINA | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Combinations::withRepetition
290293
COS | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Cosine::cos
@@ -295,11 +298,9 @@ CSC | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\C
295298
CSCH | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Cosecant::csch
296299
DECIMAL | **Not yet Implemented**
297300
DEGREES | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Angle::toDegrees
298-
DROP | **Not yet Implemented**
299301
ECMA.CEILING | **Not yet Implemented**
300302
EVEN | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Round::even
301303
EXP | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp::evaluate
302-
EXPAND | **Not yet Implemented**
303304
FACT | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Factorial::fact
304305
FACTDOUBLE | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Factorial::factDouble
305306
FLOOR | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Floor::floor
@@ -353,7 +354,6 @@ SUMSQ | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\SumSqu
353354
SUMX2MY2 | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\SumSquares::sumXSquaredMinusYSquared
354355
SUMX2PY2 | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\SumSquares::sumXSquaredPlusYSquared
355356
SUMXMY2 | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\SumSquares::sumXMinusYSquared
356-
TAKE | **Not yet Implemented**
357357
TAN | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Tangent::tan
358358
TANH | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Tangent::tanh
359359
TOCOL | **Not yet Implemented**

docs/references/function-list-by-name.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ CHISQ.INV.RT | CATEGORY_STATISTICAL | \PhpOffice\PhpSpread
7979
CHISQ.TEST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\ChiSquared::test
8080
CHITEST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\ChiSquared::test
8181
CHOOSE | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Selection::CHOOSE
82-
CHOOSECOLS | CATEGORY_MATH_AND_TRIG | **Not yet Implemented**
83-
CHOOSEROWS | CATEGORY_MATH_AND_TRIG | **Not yet Implemented**
82+
CHOOSECOLS | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::chooseCols
83+
CHOOSEROWS | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::chooseRows
8484
CLEAN | CATEGORY_TEXT_AND_DATA | \PhpOffice\PhpSpreadsheet\Calculation\TextData\Trim::nonPrintable
8585
CODE | CATEGORY_TEXT_AND_DATA | \PhpOffice\PhpSpreadsheet\Calculation\TextData\CharacterConvert::code
8686
COLUMN | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\RowColumnInformation::COLUMN
@@ -158,7 +158,7 @@ DOLLAR | CATEGORY_TEXT_AND_DATA | \PhpOffice\PhpSpread
158158
DOLLARDE | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial\Dollar::decimal
159159
DOLLARFR | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial\Dollar::fractional
160160
DPRODUCT | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database\DProduct::evaluate
161-
DROP | CATEGORY_MATH_AND_TRIG | **Not yet Implemented**
161+
DROP | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::drop
162162
DSTDEV | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database\DStDev::evaluate
163163
DSTDEVP | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database\DStDevP::evaluate
164164
DSUM | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database\DSum::evaluate
@@ -183,7 +183,7 @@ ERROR.TYPE | CATEGORY_INFORMATION | \PhpOffice\PhpSpread
183183
EVEN | CATEGORY_MATH_AND_TRIG | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Round::even
184184
EXACT | CATEGORY_TEXT_AND_DATA | \PhpOffice\PhpSpreadsheet\Calculation\TextData\Text::exact
185185
EXP | CATEGORY_MATH_AND_TRIG | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp::evaluate
186-
EXPAND | CATEGORY_MATH_AND_TRIG | **Not yet Implemented**
186+
EXPAND | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::expand
187187
EXPON.DIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\Exponential::distribution
188188
EXPONDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\Exponential::distribution
189189

@@ -553,7 +553,7 @@ T.DIST.RT | CATEGORY_STATISTICAL | **Not yet Implemente
553553
T.INV | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\StudentT::inverse
554554
T.INV.2T | CATEGORY_STATISTICAL | **Not yet Implemented**
555555
T.TEST | CATEGORY_STATISTICAL | **Not yet Implemented**
556-
TAKE | CATEGORY_MATH_AND_TRIG | **Not yet Implemented**
556+
TAKE | CATEGORY_LOOKUP_AND_REFERENCE | \PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ChooseRowsEtc::take
557557
TAN | CATEGORY_MATH_AND_TRIG | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Tangent::tan
558558
TANH | CATEGORY_MATH_AND_TRIG | \PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Trig\Tangent::tanh
559559
TBILLEQ | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial\TreasuryBill::bondEquivalentYield

src/PhpSpreadsheet/Calculation/Calculation.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,13 @@ public static function getExcelConstants(string $key): bool|null
553553
'argumentCount' => '2+',
554554
],
555555
'CHOOSECOLS' => [
556-
'category' => Category::CATEGORY_MATH_AND_TRIG,
557-
'functionCall' => [Functions::class, 'DUMMY'],
556+
'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
557+
'functionCall' => [LookupRef\ChooseRowsEtc::class, 'chooseCols'],
558558
'argumentCount' => '2+',
559559
],
560560
'CHOOSEROWS' => [
561-
'category' => Category::CATEGORY_MATH_AND_TRIG,
562-
'functionCall' => [Functions::class, 'DUMMY'],
561+
'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
562+
'functionCall' => [LookupRef\ChooseRowsEtc::class, 'chooseRows'],
563563
'argumentCount' => '2+',
564564
],
565565
'CLEAN' => [
@@ -925,8 +925,8 @@ public static function getExcelConstants(string $key): bool|null
925925
'argumentCount' => '3',
926926
],
927927
'DROP' => [
928-
'category' => Category::CATEGORY_MATH_AND_TRIG,
929-
'functionCall' => [Functions::class, 'DUMMY'],
928+
'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
929+
'functionCall' => [LookupRef\ChooseRowsEtc::class, 'drop'],
930930
'argumentCount' => '2-3',
931931
],
932932
'DSTDEV' => [
@@ -1025,8 +1025,8 @@ public static function getExcelConstants(string $key): bool|null
10251025
'argumentCount' => '1',
10261026
],
10271027
'EXPAND' => [
1028-
'category' => Category::CATEGORY_MATH_AND_TRIG,
1029-
'functionCall' => [Functions::class, 'DUMMY'],
1028+
'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
1029+
'functionCall' => [LookupRef\ChooseRowsEtc::class, 'expand'],
10301030
'argumentCount' => '2-4',
10311031
],
10321032
'EXPONDIST' => [
@@ -2485,8 +2485,8 @@ public static function getExcelConstants(string $key): bool|null
24852485
'argumentCount' => '1',
24862486
],
24872487
'TAKE' => [
2488-
'category' => Category::CATEGORY_MATH_AND_TRIG,
2489-
'functionCall' => [Functions::class, 'DUMMY'],
2488+
'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE,
2489+
'functionCall' => [LookupRef\ChooseRowsEtc::class, 'take'],
24902490
'argumentCount' => '2-3',
24912491
],
24922492
'TAN' => [

src/PhpSpreadsheet/Calculation/Functions.php

+26
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,32 @@ public static function flattenArray(mixed $array): array
230230
return $flattened;
231231
}
232232

233+
/**
234+
* Convert a multi-dimensional array to a simple 1-dimensional array.
235+
* Same as above but argument is specified in ... format.
236+
*
237+
* @param mixed $array Array to be flattened
238+
*
239+
* @return array Flattened array
240+
*/
241+
public static function flattenArray2(mixed ...$array): array
242+
{
243+
$flattened = [];
244+
$stack = array_values($array);
245+
246+
while (!empty($stack)) {
247+
$value = array_shift($stack);
248+
249+
if (is_array($value)) {
250+
array_unshift($stack, ...array_values($value));
251+
} else {
252+
$flattened[] = $value;
253+
}
254+
}
255+
256+
return $flattened;
257+
}
258+
233259
public static function scalar(mixed $value): mixed
234260
{
235261
if (!is_array($value)) {

0 commit comments

Comments
 (0)