CellRangeTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Cell;
  3. use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
  4. use PhpOffice\PhpSpreadsheet\Cell\CellRange;
  5. use PhpOffice\PhpSpreadsheet\Exception;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  8. use PHPUnit\Framework\TestCase;
  9. class CellRangeTest extends TestCase
  10. {
  11. public function testCreateCellRange(): void
  12. {
  13. $from = CellAddress::fromCellAddress('B5');
  14. $to = CellAddress::fromCellAddress('E2');
  15. $cellRange = new CellRange($from, $to);
  16. self::assertSame('B2:E5', (string) $cellRange);
  17. }
  18. public function testCreateCellRangeWithWorksheet(): void
  19. {
  20. $spreadsheet = new Spreadsheet();
  21. $worksheet = $spreadsheet->getActiveSheet();
  22. $worksheet->setTitle("Mark's Worksheet");
  23. $from = CellAddress::fromCellAddress('B5', $worksheet);
  24. $to = CellAddress::fromCellAddress('E2');
  25. $cellRange = new CellRange($from, $to);
  26. self::assertSame("'Mark''s Worksheet'!B2:E5", (string) $cellRange);
  27. }
  28. public function testCreateCellRangeWithWorksheets(): void
  29. {
  30. $spreadsheet = new Spreadsheet();
  31. $worksheet = $spreadsheet->getActiveSheet();
  32. $worksheet->setTitle("Mark's Worksheet");
  33. $from = CellAddress::fromCellAddress('B5', $worksheet);
  34. $to = CellAddress::fromCellAddress('E2', $worksheet);
  35. $cellRange = new CellRange($from, $to);
  36. self::assertSame("'Mark''s Worksheet'!B2:E5", (string) $cellRange);
  37. }
  38. public function testSingleCellRange(): void
  39. {
  40. $from = CellAddress::fromCellAddress('C3');
  41. $to = CellAddress::fromCellAddress('C3');
  42. $cellRange = new CellRange($from, $to);
  43. self::assertSame('C3', (string) $cellRange);
  44. }
  45. public function testSingleCellRangeWithWorksheet(): void
  46. {
  47. $spreadsheet = new Spreadsheet();
  48. $worksheet = $spreadsheet->getActiveSheet();
  49. $worksheet->setTitle("Mark's Worksheet");
  50. $from = CellAddress::fromCellAddress('C3', $worksheet);
  51. $to = CellAddress::fromCellAddress('C3');
  52. $cellRange = new CellRange($from, $to);
  53. self::assertSame("'Mark''s Worksheet'!C3", (string) $cellRange);
  54. }
  55. public function testRangeFrom(): void
  56. {
  57. $from = CellAddress::fromCellAddress('B5');
  58. $to = CellAddress::fromCellAddress('E2');
  59. $cellRange = new CellRange($from, $to);
  60. self::assertSame('B2', (string) $cellRange->from());
  61. }
  62. public function testRangeTo(): void
  63. {
  64. $from = CellAddress::fromCellAddress('B5');
  65. $to = CellAddress::fromCellAddress('E2');
  66. $cellRange = new CellRange($from, $to);
  67. self::assertSame('E5', (string) $cellRange->to());
  68. }
  69. public function testCreateCellRangeWithMismatchedWorksheets(): void
  70. {
  71. $spreadsheet = new Spreadsheet();
  72. $worksheet = $spreadsheet->getActiveSheet();
  73. $worksheet->setTitle("Mark's Worksheet");
  74. $secondWorksheet = new Worksheet($spreadsheet, 'A Second Worksheet');
  75. $this->expectException(Exception::class);
  76. $this->expectExceptionMessage('3d Cell Ranges are not supported');
  77. $from = CellAddress::fromCellAddress('B5', $worksheet);
  78. $to = CellAddress::fromCellAddress('E2', $secondWorksheet);
  79. new CellRange($from, $to);
  80. }
  81. public function testCreateCellRangeWithMismatchedSpreadsheets(): void
  82. {
  83. $spreadsheet1 = new Spreadsheet();
  84. $worksheet1 = $spreadsheet1->getActiveSheet();
  85. $worksheet1->setTitle("Mark's Worksheet");
  86. $spreadsheet2 = new Spreadsheet();
  87. $worksheet2 = $spreadsheet2->getActiveSheet();
  88. $worksheet2->setTitle("Mark's Worksheet");
  89. $this->expectException(Exception::class);
  90. $this->expectExceptionMessage('Worksheets must be in the same spreadsheet');
  91. $from = CellAddress::fromCellAddress('B5', $worksheet1);
  92. $to = CellAddress::fromCellAddress('E2', $worksheet2);
  93. new CellRange($from, $to);
  94. }
  95. public function testShiftRangeTo(): void
  96. {
  97. $from = CellAddress::fromCellAddress('B5');
  98. $to = CellAddress::fromCellAddress('E2');
  99. $cellRange = new CellRange($from, $to);
  100. self::assertSame('B2:E5', (string) $cellRange);
  101. $cellRange->to()
  102. ->nextColumn(2)
  103. ->nextRow(2);
  104. self::assertSame('B2', (string) $cellRange->from());
  105. self::assertSame('G7', (string) $cellRange->to());
  106. self::assertSame('B2:G7', (string) $cellRange);
  107. $cellRange->to()
  108. ->previousColumn()
  109. ->previousRow();
  110. self::assertSame('B2', (string) $cellRange->from());
  111. self::assertSame('F6', (string) $cellRange->to());
  112. self::assertSame('B2:F6', (string) $cellRange);
  113. }
  114. public function testShiftRangeFrom(): void
  115. {
  116. $from = CellAddress::fromCellAddress('B5');
  117. $to = CellAddress::fromCellAddress('E2');
  118. $cellRange = new CellRange($from, $to);
  119. self::assertSame('B2:E5', (string) $cellRange);
  120. $cellRange->from()
  121. ->nextColumn(5)
  122. ->nextRow(5);
  123. self::assertSame('E5', (string) $cellRange->from());
  124. self::assertSame('G7', (string) $cellRange->to());
  125. self::assertSame('E5:G7', (string) $cellRange);
  126. }
  127. }