RowColumnReferenceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  5. use PHPUnit\Framework\TestCase;
  6. class RowColumnReferenceTest extends TestCase
  7. {
  8. /**
  9. * @var Spreadsheet
  10. */
  11. protected $spreadSheet;
  12. protected function setUp(): void
  13. {
  14. $this->spreadSheet = new Spreadsheet();
  15. $dataSheet = new Worksheet($this->spreadSheet, 'data sheet');
  16. $this->spreadSheet->addSheet($dataSheet, 0);
  17. $dataSheet->setCellValue('B1', 1.1);
  18. $dataSheet->setCellValue('B2', 2.2);
  19. $dataSheet->setCellValue('B3', 4.4);
  20. $dataSheet->setCellValue('C3', 8.8);
  21. $dataSheet->setCellValue('D3', 16.16);
  22. $calcSheet = new Worksheet($this->spreadSheet, 'summary sheet');
  23. $this->spreadSheet->addSheet($calcSheet, 1);
  24. $calcSheet->setCellValue('B1', 2.2);
  25. $calcSheet->setCellValue('B2', 4.4);
  26. $calcSheet->setCellValue('B3', 8.8);
  27. $calcSheet->setCellValue('C3', 16.16);
  28. $calcSheet->setCellValue('D3', 32.32);
  29. $this->spreadSheet->setActiveSheetIndexByName('summary sheet');
  30. }
  31. /**
  32. * @dataProvider providerCurrentWorksheetFormulae
  33. */
  34. public function testCurrentWorksheet(string $formula, float $expectedResult): void
  35. {
  36. $worksheet = $this->spreadSheet->getActiveSheet();
  37. $worksheet->setCellValue('A1', $formula);
  38. $result = $worksheet->getCell('A1')->getCalculatedValue();
  39. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
  40. }
  41. public function providerCurrentWorksheetFormulae(): array
  42. {
  43. return [
  44. 'relative range in active worksheet' => ['=SUM(B1:B3)', 15.4],
  45. 'range with absolute columns in active worksheet' => ['=SUM($B1:$B3)', 15.4],
  46. 'range with absolute rows in active worksheet' => ['=SUM(B$1:B$3)', 15.4],
  47. 'range with absolute columns and rows in active worksheet' => ['=SUM($B$1:$B$3)', 15.4],
  48. 'another relative range in active worksheet' => ['=SUM(B3:D3)', 57.28],
  49. 'relative column range in active worksheet' => ['=SUM(B:B)', 15.4],
  50. 'absolute column range in active worksheet' => ['=SUM($B:$B)', 15.4],
  51. 'relative row range in active worksheet' => ['=SUM(3:3)', 57.28],
  52. 'absolute row range in active worksheet' => ['=SUM($3:$3)', 57.28],
  53. 'relative range in specified active worksheet' => ['=SUM(\'summary sheet\'!B1:B3)', 15.4],
  54. 'range with absolute columns in specified active worksheet' => ['=SUM(\'summary sheet\'!$B1:$B3)', 15.4],
  55. 'range with absolute rows in specified active worksheet' => ['=SUM(\'summary sheet\'!B$1:B$3)', 15.4],
  56. 'range with absolute columns and rows in specified active worksheet' => ['=SUM(\'summary sheet\'!$B$1:$B$3)', 15.4],
  57. 'another relative range in specified active worksheet' => ['=SUM(\'summary sheet\'!B3:D3)', 57.28],
  58. 'relative column range in specified active worksheet' => ['=SUM(\'summary sheet\'!B:B)', 15.4],
  59. 'absolute column range in specified active worksheet' => ['=SUM(\'summary sheet\'!$B:$B)', 15.4],
  60. 'relative row range in specified active worksheet' => ['=SUM(\'summary sheet\'!3:3)', 57.28],
  61. 'absolute row range in specified active worksheet' => ['=SUM(\'summary sheet\'!$3:$3)', 57.28],
  62. 'relative range in specified other worksheet' => ['=SUM(\'data sheet\'!B1:B3)', 7.7],
  63. 'range with absolute columns in specified other worksheet' => ['=SUM(\'data sheet\'!$B1:$B3)', 7.7],
  64. 'range with absolute rows in specified other worksheet' => ['=SUM(\'data sheet\'!B$1:B$3)', 7.7],
  65. 'range with absolute columns and rows in specified other worksheet' => ['=SUM(\'data sheet\'!$B$1:$B$3)', 7.7],
  66. 'another relative range in specified other worksheet' => ['=SUM(\'data sheet\'!B3:D3)', 29.36],
  67. 'relative column range in specified other worksheet' => ['=SUM(\'data sheet\'!B:B)', 7.7],
  68. 'absolute column range in specified other worksheet' => ['=SUM(\'data sheet\'!$B:$B)', 7.7],
  69. 'relative row range in specified other worksheet' => ['=SUM(\'data sheet\'!3:3)', 29.36],
  70. 'absolute row range in specified other worksheet' => ['=SUM(\'data sheet\'!$3:$3)', 29.36],
  71. ];
  72. }
  73. }