diff --git a/src/PhpSpreadsheet/Calculation/ArrayEnabled.php b/src/PhpSpreadsheet/Calculation/ArrayEnabled.php index fa90017d..2553c9fd 100644 --- a/src/PhpSpreadsheet/Calculation/ArrayEnabled.php +++ b/src/PhpSpreadsheet/Calculation/ArrayEnabled.php @@ -53,4 +53,25 @@ trait ArrayEnabled return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); } + + /** + * @param mixed ...$arguments + */ + protected static function evaluateArrayArgumentsSubsetFrom(callable $method, int $start, ...$arguments): array + { + $arrayArgumentsSubset = array_combine( + range($start, count($arguments) - $start), + array_slice($arguments, $start) + ); + if ($arrayArgumentsSubset === false) { + return ['#VALUE!']; + } + + self::initialiseHelper($arrayArgumentsSubset); + $leadingArguments = array_slice($arguments, 0, $start); + $arguments = self::$arrayArgumentHelper->arguments(); + $arguments = array_merge($leadingArguments, $arguments); + + return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments); + } } diff --git a/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php b/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php index df413f20..fae9d900 100644 --- a/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php +++ b/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php @@ -6,6 +6,11 @@ use PhpOffice\PhpSpreadsheet\Calculation\Exception; class ArrayArgumentHelper { + /** + * @var int + */ + protected $indexStart = 0; + /** * @var array */ @@ -28,6 +33,8 @@ class ArrayArgumentHelper public function initialise(array $arguments): void { + $keys = array_keys($arguments); + $this->indexStart = (int) array_shift($keys); $this->rows = $this->rows($arguments); $this->columns = $this->columns($arguments); @@ -57,7 +64,7 @@ class ArrayArgumentHelper $rowArrays = $this->filterArray($this->rows); $columnArrays = $this->filterArray($this->columns); - for ($index = 0; $index < $this->argumentCount; ++$index) { + for ($index = $this->indexStart; $index < $this->argumentCount; ++$index) { if (isset($rowArrays[$index]) || isset($columnArrays[$index])) { return ++$index; } @@ -76,7 +83,7 @@ class ArrayArgumentHelper private function getRowVectors(): array { $rowVectors = []; - for ($index = 0; $index < $this->argumentCount; ++$index) { + for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) { if ($this->rows[$index] === 1 && $this->columns[$index] > 1) { $rowVectors[] = $index; } @@ -95,7 +102,7 @@ class ArrayArgumentHelper private function getColumnVectors(): array { $columnVectors = []; - for ($index = 0; $index < $this->argumentCount; ++$index) { + for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) { if ($this->rows[$index] > 1 && $this->columns[$index] === 1) { $columnVectors[] = $index; } @@ -106,7 +113,7 @@ class ArrayArgumentHelper public function getMatrixPair(): array { - for ($i = 0; $i < ($this->argumentCount - 1); ++$i) { + for ($i = $this->indexStart; $i < ($this->indexStart + $this->argumentCount - 1); ++$i) { for ($j = $i + 1; $j < $this->argumentCount; ++$j) { if (isset($this->rows[$i], $this->rows[$j])) { return [$i, $j]; diff --git a/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentProcessor.php b/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentProcessor.php index 4e377fe5..3e69d77f 100644 --- a/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentProcessor.php +++ b/src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentProcessor.php @@ -33,6 +33,7 @@ class ArrayArgumentProcessor $singleRowVectorIndex = self::$arrayArgumentHelper->getSingleRowVector(); $singleColumnVectorIndex = self::$arrayArgumentHelper->getSingleColumnVector(); + if ($singleRowVectorIndex !== null && $singleColumnVectorIndex !== null) { // Basic logic for a single row vector and a single column vector return self::evaluateVectorPair($method, $singleRowVectorIndex, $singleColumnVectorIndex, ...$arguments); diff --git a/src/PhpSpreadsheet/Calculation/Functions.php b/src/PhpSpreadsheet/Calculation/Functions.php index 1165a281..c3b7c257 100644 --- a/src/PhpSpreadsheet/Calculation/Functions.php +++ b/src/PhpSpreadsheet/Calculation/Functions.php @@ -518,7 +518,7 @@ class Functions * * @param null|mixed $value The value you want converted * - * @return number N converts values listed in the following table + * @return number|string N converts values listed in the following table * If value is or refers to N returns * A number That number * A date The serial number of that date diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index 67650480..758c4ef0 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -41,9 +41,9 @@ class LookupRef * @param bool $referenceStyle A logical value that specifies the A1 or R1C1 reference style. * TRUE or omitted CELL_ADDRESS returns an A1-style reference * FALSE CELL_ADDRESS returns an R1C1-style reference - * @param string $sheetText Optional Name of worksheet to use + * @param array|string $sheetText Optional Name of worksheet to use * - * @return string + * @return array|string */ public static function cellAddress($row, $column, $relativity = 1, $referenceStyle = true, $sheetText = '') { @@ -277,7 +277,7 @@ class LookupRef * @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below. * If match_type is 1 or -1, the list has to be ordered. * - * @return int|string The relative position of the found item + * @return array|int|string The relative position of the found item */ public static function MATCH($lookupValue, $lookupArray, $matchType = 1) { diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Address.php b/src/PhpSpreadsheet/Calculation/LookupRef/Address.php index 34215ca6..c8cdf2dd 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/Address.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/Address.php @@ -2,12 +2,14 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; class Address { + use ArrayEnabled; + public const ADDRESS_ABSOLUTE = 1; public const ADDRESS_COLUMN_RELATIVE = 2; public const ADDRESS_ROW_RELATIVE = 3; @@ -25,26 +27,44 @@ class Address * =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText]) * * @param mixed $row Row number (integer) to use in the cell reference + * Or can be an array of values * @param mixed $column Column number (integer) to use in the cell reference + * Or can be an array of values * @param mixed $relativity Integer flag indicating the type of reference to return * 1 or omitted Absolute * 2 Absolute row; relative column * 3 Relative row; absolute column * 4 Relative + * Or can be an array of values * @param mixed $referenceStyle A logical (boolean) value that specifies the A1 or R1C1 reference style. * TRUE or omitted ADDRESS returns an A1-style reference * FALSE ADDRESS returns an R1C1-style reference + * Or can be an array of values * @param mixed $sheetName Optional Name of worksheet to use + * Or can be an array of values * - * @return string + * @return array|string + * If an array of values is passed as the $testValue argument, then the returned result will also be + * an array with the same dimensions */ public static function cell($row, $column, $relativity = 1, $referenceStyle = true, $sheetName = '') { - $row = Functions::flattenSingleValue($row); - $column = Functions::flattenSingleValue($column); - $relativity = ($relativity === null) ? 1 : Functions::flattenSingleValue($relativity); - $referenceStyle = ($referenceStyle === null) ? true : Functions::flattenSingleValue($referenceStyle); - $sheetName = Functions::flattenSingleValue($sheetName); + if ( + is_array($row) || is_array($column) || + is_array($relativity) || is_array($referenceStyle) || is_array($sheetName) + ) { + return self::evaluateArrayArguments( + [self::class, __FUNCTION__], + $row, + $column, + $relativity, + $referenceStyle, + $sheetName + ); + } + + $relativity = $relativity ?? 1; + $referenceStyle = $referenceStyle ?? true; if (($row < 1) || ($column < 1)) { return ExcelError::VALUE(); diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php b/src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php index 4d2361a0..98960283 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/ExcelMatch.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Exception; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; @@ -10,6 +11,8 @@ use PhpOffice\PhpSpreadsheet\Shared\StringHelper; class ExcelMatch { + use ArrayEnabled; + public const MATCHTYPE_SMALLEST_VALUE = -1; public const MATCHTYPE_FIRST_VALUE = 0; public const MATCHTYPE_LARGEST_VALUE = 1; @@ -27,15 +30,16 @@ class ExcelMatch * @param mixed $matchType The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below. * If match_type is 1 or -1, the list has to be ordered. * - * @return int|string The relative position of the found item + * @return array|int|string The relative position of the found item */ public static function MATCH($lookupValue, $lookupArray, $matchType = self::MATCHTYPE_LARGEST_VALUE) { + if (is_array($lookupValue)) { + return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $matchType); + } + $lookupArray = Functions::flattenArray($lookupArray); - $lookupValue = Functions::flattenSingleValue($lookupValue); - $matchType = ($matchType === null) - ? self::MATCHTYPE_LARGEST_VALUE - : (int) Functions::flattenSingleValue($matchType); + $matchType = (int) ($matchType ?? self::MATCHTYPE_LARGEST_VALUE); try { // Input validation diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Lookup.php b/src/PhpSpreadsheet/Calculation/LookupRef/Lookup.php index a3e57a1e..76a360b4 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/Lookup.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/Lookup.php @@ -2,12 +2,14 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; class Lookup { + use ArrayEnabled; + /** * LOOKUP * The LOOKUP function searches for value either from a one-row or one-column range or from an array. @@ -20,7 +22,9 @@ class Lookup */ public static function lookup($lookupValue, $lookupVector, $resultVector = null) { - $lookupValue = Functions::flattenSingleValue($lookupValue); + if (is_array($lookupValue)) { + return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $lookupValue, $lookupVector, $resultVector); + } if (!is_array($lookupVector)) { return ExcelError::NA(); diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php b/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php index e3e5cd98..27e6f8eb 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php @@ -2,12 +2,14 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Exception; -use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; class Matrix { + use ArrayEnabled; + /** * TRANSPOSE. * @@ -46,17 +48,25 @@ class Matrix * @param mixed $matrix A range of cells or an array constant * @param mixed $rowNum The row in the array or range from which to return a value. * If row_num is omitted, column_num is required. + * Or can be an array of values * @param mixed $columnNum The column in the array or range from which to return a value. * If column_num is omitted, row_num is required. + * Or can be an array of values * * TODO Provide support for area_num, currently not supported * * @return mixed the value of a specified cell or array of cells + * If an array of values is passed as the $rowNum and/or $columnNum arguments, then the returned result + * will also be an array with the same dimensions */ public static function index($matrix, $rowNum = 0, $columnNum = 0) { - $rowNum = ($rowNum === null) ? 0 : Functions::flattenSingleValue($rowNum); - $columnNum = ($columnNum === null) ? 0 : Functions::flattenSingleValue($columnNum); + if (is_array($rowNum) || is_array($columnNum)) { + return self::evaluateArrayArgumentsSubsetFrom([self::class, __FUNCTION__], 1, $matrix, $rowNum, $columnNum); + } + + $rowNum = $rowNum ?? 0; + $columnNum = $columnNum ?? 0; try { $rowNum = LookupRefValidations::validatePositiveInt($rowNum); diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Selection.php b/src/PhpSpreadsheet/Calculation/LookupRef/Selection.php index 05df11ed..0ac91777 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/Selection.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/Selection.php @@ -2,11 +2,14 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; class Selection { + use ArrayEnabled; + /** * CHOOSE. * @@ -16,18 +19,19 @@ class Selection * Excel Function: * =CHOOSE(index_num, value1, [value2], ...) * + * @param mixed $chosenEntry The entry to select from the list (indexed from 1) * @param mixed ...$chooseArgs Data values * * @return mixed The selected value */ - public static function choose(...$chooseArgs) + public static function choose($chosenEntry, ...$chooseArgs) { - $chosenEntry = Functions::flattenArray(array_shift($chooseArgs)); + if (is_array($chosenEntry)) { + return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $chosenEntry, ...$chooseArgs); + } + $entryCount = count($chooseArgs) - 1; - if (is_array($chosenEntry)) { - $chosenEntry = array_shift($chosenEntry); - } if (is_numeric($chosenEntry)) { --$chosenEntry; } else { diff --git a/src/PhpSpreadsheet/Cell/DataValidator.php b/src/PhpSpreadsheet/Cell/DataValidator.php index c1991ae0..0e395a7f 100644 --- a/src/PhpSpreadsheet/Cell/DataValidator.php +++ b/src/PhpSpreadsheet/Cell/DataValidator.php @@ -64,6 +64,9 @@ class DataValidator try { $result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell); + while (is_array($result)) { + $result = array_pop($result); + } return $result !== ExcelError::NA(); } catch (Exception $ex) { diff --git a/src/PhpSpreadsheet/Worksheet/AutoFilter.php b/src/PhpSpreadsheet/Worksheet/AutoFilter.php index 893243df..1bc71184 100644 --- a/src/PhpSpreadsheet/Worksheet/AutoFilter.php +++ b/src/PhpSpreadsheet/Worksheet/AutoFilter.php @@ -931,6 +931,9 @@ class AutoFilter $averageFormula = '=AVERAGE(' . $columnID . ($rangeStart[1] + 1) . ':' . $columnID . $rangeEnd[1] . ')'; $spreadsheet = ($this->workSheet === null) ? null : $this->workSheet->getParent(); $average = Calculation::getInstance($spreadsheet)->calculateFormula($averageFormula, null, $this->workSheet->getCell('A1')); + while (is_array($average)) { + $average = array_pop($average); + } // Set above/below rule based on greaterThan or LessTan $operator = ($dynamicRuleType === Rule::AUTOFILTER_RULETYPE_DYNAMIC_ABOVEAVERAGE) ? Rule::AUTOFILTER_COLUMN_RULE_GREATERTHAN diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/AddressTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/AddressTest.php index e6ed234e..2b92030e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/AddressTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/AddressTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,27 @@ class AddressTest extends TestCase { return require 'tests/data/Calculation/LookupRef/ADDRESS.php'; } + + /** + * @dataProvider providerAddressArray + */ + public function testAddressArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=ADDRESS({$argument1}, {$argument2}, 4)"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerAddressArray(): array + { + return [ + 'row/column vectors' => [ + [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']], + '{1; 2; 3}', + '{1, 2, 3}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ChooseTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ChooseTest.php index 857973ff..1a445fd0 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ChooseTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ChooseTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,28 @@ class ChooseTest extends TestCase { return require 'tests/data/Calculation/LookupRef/CHOOSE.php'; } + + /** + * @dataProvider providerChooseArray + */ + public function testChooseArray(array $expectedResult, string $values, array $selections): void + { + $calculation = Calculation::getInstance(); + + $selections = implode(',', $selections); + $formula = "=CHOOSE({$values}, {$selections})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerChooseArray(): array + { + return [ + 'row vector' => [ + [['Orange', 'Blue', 'Yellow']], + '{2, 5, 3}', + ['"Red"', '"Orange"', '"Yellow"', '"Green"', '"Blue"'], + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnsTest.php index e14023cd..71497b01 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnsTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,34 @@ class ColumnsTest extends TestCase { return require 'tests/data/Calculation/LookupRef/COLUMNS.php'; } + + /** + * @dataProvider providerColumnsArray + */ + public function testColumnsArray($expectedResult, string $argument): void + { + $calculation = Calculation::getInstance(); + + $formula = "=COLUMNS({$argument})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerColumnsArray(): array + { + return [ + [ + 3, + '{1,2,3;4,5,6}', + ], + [ + 5, + '{1,2,3,4,5}', + ], + [ + 1, + '{1;2;3;4;5}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexTest.php index 4de661ed..03c87b50 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -21,7 +22,6 @@ class IndexTest extends TestCase public function testINDEX($expectedResult, ...$args): void { $result = LookupRef::INDEX(...$args); -// var_dump($result); self::assertEquals($expectedResult, $result); } @@ -29,4 +29,40 @@ class IndexTest extends TestCase { return require 'tests/data/Calculation/LookupRef/INDEX.php'; } + + /** + * @dataProvider providerIndexArray + */ + public function testIndexArray(array $expectedResult, string $matrix, string $rows, string $columns): void + { + $calculation = Calculation::getInstance(); + + $formula = "=INDEX({$matrix}, {$rows}, {$columns})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerIndexArray(): array + { + return [ + 'row/column vectors' => [ + [[2, 3], [5, 6]], + '{1, 2, 3; 4, 5, 6; 7, 8, 9}', + '{1; 2}', + '{2, 3}', + ], + 'return row' => [ + [1 => [4, 5, 6]], + '{1, 2, 3; 4, 5, 6; 7, 8, 9}', + '2', + '0', + ], + 'return column' => [ + [[2], [5], [8]], + '{1, 2, 3; 4, 5, 6; 7, 8, 9}', + '0', + '2', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/LookupTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/LookupTest.php index 73dffd3d..c2cf4dae 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/LookupTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/LookupTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,28 @@ class LookupTest extends TestCase { return require 'tests/data/Calculation/LookupRef/LOOKUP.php'; } + + /** + * @dataProvider providerLookupArray + */ + public function testLookupArray(array $expectedResult, string $values, string $lookup, string $return): void + { + $calculation = Calculation::getInstance(); + + $formula = "=LOOKUP({$values}, {$lookup}, {$return})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerLookupArray(): array + { + return [ + 'row vector' => [ + [['Orange', 'Green', 'Red']], + '{4.19, 5.77, 4.14}', + '{4.14; 4.19; 5.17; 5.77; 6.39}', + '{"Red"; "Orange"; "Yellow"; "Green"; "Blue"}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/MatchTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/MatchTest.php index ba2dce29..f8e8fa46 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/MatchTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/MatchTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,27 @@ class MatchTest extends TestCase { return require 'tests/data/Calculation/LookupRef/MATCH.php'; } + + /** + * @dataProvider providerMatchArray + */ + public function testMatchArray(array $expectedResult, string $values, string $selections): void + { + $calculation = Calculation::getInstance(); + + $formula = "=MATCH({$values}, {$selections}, 0)"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerMatchArray(): array + { + return [ + 'row vector' => [ + [[2, 5, 3]], + '{"Orange", "Blue", "Yellow"}', + '{"Red", "Orange", "Yellow", "Green", "Blue"}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowsTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowsTest.php index ec9b33d9..52989e19 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/RowsTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PHPUnit\Framework\TestCase; @@ -28,4 +29,34 @@ class RowsTest extends TestCase { return require 'tests/data/Calculation/LookupRef/ROWS.php'; } + + /** + * @dataProvider providerRowsArray + */ + public function testRowsArray($expectedResult, string $argument): void + { + $calculation = Calculation::getInstance(); + + $formula = "=ROWS({$argument})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEquals($expectedResult, $result); + } + + public function providerRowsArray(): array + { + return [ + [ + 2, + '{1,2,3;4,5,6}', + ], + [ + 1, + '{1,2,3,4,5}', + ], + [ + 5, + '{1;2;3;4;5}', + ], + ]; + } } diff --git a/tests/data/Calculation/LookupRef/INDEX.php b/tests/data/Calculation/LookupRef/INDEX.php index a699534e..4d4d1dad 100644 --- a/tests/data/Calculation/LookupRef/INDEX.php +++ b/tests/data/Calculation/LookupRef/INDEX.php @@ -112,7 +112,7 @@ return [ 2, ], [ - 4, // Expected + [4], // Expected // Input [ '20' => ['R' => 1, 'S' => 3],