mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-12 11:06:28 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
|
|
use PhpOffice\PhpSpreadsheet\NamedRange;
|
|
|
|
class OffsetTest extends AllSetupTeardown
|
|
{
|
|
/**
|
|
* @dataProvider providerOFFSET
|
|
*
|
|
* @param mixed $expectedResult
|
|
* @param null|string $cellReference
|
|
*/
|
|
public function testOFFSET($expectedResult, $cellReference = null): void
|
|
{
|
|
$result = LookupRef\Offset::OFFSET($cellReference);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerOFFSET(): array
|
|
{
|
|
return require 'tests/data/Calculation/LookupRef/OFFSET.php';
|
|
}
|
|
|
|
public function testOffsetSpreadsheet(): void
|
|
{
|
|
$sheet = $this->getSheet();
|
|
$sheet->getCell('B6')->setValue(4);
|
|
$sheet->getCell('B7')->setValue(8);
|
|
$sheet->getCell('B8')->setValue(3);
|
|
$sheet->getCell('D6')->setValue(10);
|
|
$sheet->getCell('D7')->setValue(3);
|
|
$sheet->getCell('D8')->setValue(6);
|
|
|
|
$sheet->getCell('A1')->setValue('=OFFSET(D3,3,-2,1,1)');
|
|
self::assertSame(4, $sheet->getCell('A1')->getCalculatedValue());
|
|
$sheet->getCell('A2')->setValue('=SUM(OFFSET(D3:F5,3,-2, 3, 3))');
|
|
self::assertSame(34, $sheet->getCell('A2')->getCalculatedValue());
|
|
$sheet->getCell('A3')->setValue('=OFFSET(D3, -3, -3)');
|
|
self::assertSame('#REF!', $sheet->getCell('A3')->getCalculatedValue());
|
|
|
|
$sheet->getCell('C1')->setValue(5);
|
|
$sheet->getCell('A4')->setValue('=OFFSET(C1, 0, 0, 0, 0)');
|
|
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
|
|
$sheet->getCell('A5')->setValue('=OFFSET(C1, 0, 0)');
|
|
self::assertSame(5, $sheet->getCell('A5')->getCalculatedValue());
|
|
}
|
|
|
|
public function testOffsetNamedRange(): void
|
|
{
|
|
$workSheet = $this->getSheet();
|
|
$workSheet->setCellValue('A1', 1);
|
|
$workSheet->setCellValue('A2', 2);
|
|
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('demo', $workSheet, '=$A$1'));
|
|
|
|
$workSheet->setCellValue('B1', '=demo');
|
|
$workSheet->setCellValue('B2', '=OFFSET(demo, 1, 0)');
|
|
|
|
self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
|
|
}
|
|
}
|