| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
- use PhpOffice\PhpSpreadsheet\Exception;
- use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PHPUnit\Framework\TestCase;
- class WorksheetNamedRangesTest extends TestCase
- {
- /**
- * @var Spreadsheet
- */
- private $spreadsheet;
- protected function setUp(): void
- {
- $reader = new Xlsx();
- $this->spreadsheet = $reader->load('tests/data/Worksheet/namedRangeTest.xlsx');
- }
- public function testCellExists(): void
- {
- $namedCell = 'GREETING';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cellExists = $worksheet->cellExists($namedCell);
- self::assertTrue($cellExists);
- }
- public function testCellExistsUtf8(): void
- {
- $namedCell = 'Χαιρετισμός';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cellExists = $worksheet->cellExists($namedCell);
- self::assertTrue($cellExists);
- }
- public function testCellNotExists(): void
- {
- $namedCell = 'GOODBYE';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cellExists = $worksheet->cellExists($namedCell);
- self::assertFalse($cellExists);
- }
- public function testCellExistsInvalidScope(): void
- {
- $namedCell = 'Result';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cellExists = $worksheet->cellExists($namedCell);
- self::assertFalse($cellExists);
- }
- public function testCellExistsRange(): void
- {
- $namedRange = 'Range1';
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Cell coordinate string can not be a range of cells');
- $worksheet = $this->spreadsheet->getActiveSheet();
- $worksheet->cellExists($namedRange);
- }
- public function testGetCell(): void
- {
- $namedCell = 'GREETING';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cell = $worksheet->getCell($namedCell);
- self::assertSame('Hello', $cell->getValue());
- }
- public function testGetCellUtf8(): void
- {
- $namedCell = 'Χαιρετισμός';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cell = $worksheet->getCell($namedCell);
- self::assertSame('नमस्ते', $cell->getValue());
- }
- public function testGetCellNotExists(): void
- {
- $namedCell = 'GOODBYE';
- $this->expectException(Exception::class);
- $this->expectExceptionMessage("Invalid cell coordinate {$namedCell}");
- $worksheet = $this->spreadsheet->getActiveSheet();
- $worksheet->getCell($namedCell);
- }
- public function testGetCellInvalidScope(): void
- {
- $namedCell = 'Result';
- $ucNamedCell = strtoupper($namedCell);
- $this->expectException(Exception::class);
- $this->expectExceptionMessage("Invalid cell coordinate {$ucNamedCell}");
- $worksheet = $this->spreadsheet->getActiveSheet();
- $worksheet->getCell($namedCell);
- }
- public function testGetCellLocalScoped(): void
- {
- $namedCell = 'Result';
- $this->spreadsheet->setActiveSheetIndexByName('Sheet2');
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cell = $worksheet->getCell($namedCell);
- self::assertSame(8, $cell->getCalculatedValue());
- }
- public function testGetCellNamedFormula(): void
- {
- $namedCell = 'Result';
- $this->spreadsheet->setActiveSheetIndexByName('Sheet2');
- $worksheet = $this->spreadsheet->getActiveSheet();
- $cell = $worksheet->getCell($namedCell);
- self::assertSame(8, $cell->getCalculatedValue());
- }
- public function testGetCellWithNamedRange(): void
- {
- $namedCell = 'Range1';
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Cell coordinate string can not be a range of cells');
- $worksheet = $this->spreadsheet->getActiveSheet();
- $worksheet->getCell($namedCell);
- }
- public function testNamedRangeToArray(): void
- {
- $namedRange = 'Range1';
- $worksheet = $this->spreadsheet->getActiveSheet();
- $rangeData = $worksheet->namedRangeToArray($namedRange);
- self::assertSame([[1, 2, 3]], $rangeData);
- }
- public function testInvalidNamedRangeToArray(): void
- {
- $namedRange = 'Range2';
- $this->expectException(Exception::class);
- $this->expectExceptionMessage("Named Range {$namedRange} does not exist");
- $worksheet = $this->spreadsheet->getActiveSheet();
- $rangeData = $worksheet->namedRangeToArray($namedRange);
- self::assertSame([[1, 2, 3]], $rangeData);
- }
- }
|