WorksheetNamedRangesTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Exception;
  4. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PHPUnit\Framework\TestCase;
  7. class WorksheetNamedRangesTest extends TestCase
  8. {
  9. /**
  10. * @var Spreadsheet
  11. */
  12. private $spreadsheet;
  13. protected function setUp(): void
  14. {
  15. $reader = new Xlsx();
  16. $this->spreadsheet = $reader->load('tests/data/Worksheet/namedRangeTest.xlsx');
  17. }
  18. public function testCellExists(): void
  19. {
  20. $namedCell = 'GREETING';
  21. $worksheet = $this->spreadsheet->getActiveSheet();
  22. $cellExists = $worksheet->cellExists($namedCell);
  23. self::assertTrue($cellExists);
  24. }
  25. public function testCellExistsUtf8(): void
  26. {
  27. $namedCell = 'Χαιρετισμός';
  28. $worksheet = $this->spreadsheet->getActiveSheet();
  29. $cellExists = $worksheet->cellExists($namedCell);
  30. self::assertTrue($cellExists);
  31. }
  32. public function testCellNotExists(): void
  33. {
  34. $namedCell = 'GOODBYE';
  35. $worksheet = $this->spreadsheet->getActiveSheet();
  36. $cellExists = $worksheet->cellExists($namedCell);
  37. self::assertFalse($cellExists);
  38. }
  39. public function testCellExistsInvalidScope(): void
  40. {
  41. $namedCell = 'Result';
  42. $worksheet = $this->spreadsheet->getActiveSheet();
  43. $cellExists = $worksheet->cellExists($namedCell);
  44. self::assertFalse($cellExists);
  45. }
  46. public function testCellExistsRange(): void
  47. {
  48. $namedRange = 'Range1';
  49. $this->expectException(Exception::class);
  50. $this->expectExceptionMessage('Cell coordinate string can not be a range of cells');
  51. $worksheet = $this->spreadsheet->getActiveSheet();
  52. $worksheet->cellExists($namedRange);
  53. }
  54. public function testGetCell(): void
  55. {
  56. $namedCell = 'GREETING';
  57. $worksheet = $this->spreadsheet->getActiveSheet();
  58. $cell = $worksheet->getCell($namedCell);
  59. self::assertSame('Hello', $cell->getValue());
  60. }
  61. public function testGetCellUtf8(): void
  62. {
  63. $namedCell = 'Χαιρετισμός';
  64. $worksheet = $this->spreadsheet->getActiveSheet();
  65. $cell = $worksheet->getCell($namedCell);
  66. self::assertSame('नमस्ते', $cell->getValue());
  67. }
  68. public function testGetCellNotExists(): void
  69. {
  70. $namedCell = 'GOODBYE';
  71. $this->expectException(Exception::class);
  72. $this->expectExceptionMessage("Invalid cell coordinate {$namedCell}");
  73. $worksheet = $this->spreadsheet->getActiveSheet();
  74. $worksheet->getCell($namedCell);
  75. }
  76. public function testGetCellInvalidScope(): void
  77. {
  78. $namedCell = 'Result';
  79. $ucNamedCell = strtoupper($namedCell);
  80. $this->expectException(Exception::class);
  81. $this->expectExceptionMessage("Invalid cell coordinate {$ucNamedCell}");
  82. $worksheet = $this->spreadsheet->getActiveSheet();
  83. $worksheet->getCell($namedCell);
  84. }
  85. public function testGetCellLocalScoped(): void
  86. {
  87. $namedCell = 'Result';
  88. $this->spreadsheet->setActiveSheetIndexByName('Sheet2');
  89. $worksheet = $this->spreadsheet->getActiveSheet();
  90. $cell = $worksheet->getCell($namedCell);
  91. self::assertSame(8, $cell->getCalculatedValue());
  92. }
  93. public function testGetCellNamedFormula(): void
  94. {
  95. $namedCell = 'Result';
  96. $this->spreadsheet->setActiveSheetIndexByName('Sheet2');
  97. $worksheet = $this->spreadsheet->getActiveSheet();
  98. $cell = $worksheet->getCell($namedCell);
  99. self::assertSame(8, $cell->getCalculatedValue());
  100. }
  101. public function testGetCellWithNamedRange(): void
  102. {
  103. $namedCell = 'Range1';
  104. $this->expectException(Exception::class);
  105. $this->expectExceptionMessage('Cell coordinate string can not be a range of cells');
  106. $worksheet = $this->spreadsheet->getActiveSheet();
  107. $worksheet->getCell($namedCell);
  108. }
  109. public function testNamedRangeToArray(): void
  110. {
  111. $namedRange = 'Range1';
  112. $worksheet = $this->spreadsheet->getActiveSheet();
  113. $rangeData = $worksheet->namedRangeToArray($namedRange);
  114. self::assertSame([[1, 2, 3]], $rangeData);
  115. }
  116. public function testInvalidNamedRangeToArray(): void
  117. {
  118. $namedRange = 'Range2';
  119. $this->expectException(Exception::class);
  120. $this->expectExceptionMessage("Named Range {$namedRange} does not exist");
  121. $worksheet = $this->spreadsheet->getActiveSheet();
  122. $rangeData = $worksheet->namedRangeToArray($namedRange);
  123. self::assertSame([[1, 2, 3]], $rangeData);
  124. }
  125. }