RefErrorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PHPUnit\Framework\TestCase;
  5. class RefErrorTest extends TestCase
  6. {
  7. /**
  8. * @param mixed $expected
  9. *
  10. * @dataProvider providerRefError
  11. */
  12. public function testRefError($expected, string $formula): void
  13. {
  14. $spreadsheet = new Spreadsheet();
  15. $sheet1 = $spreadsheet->getActiveSheet();
  16. $sheet1->setTitle('Sheet1');
  17. $sheet2 = $spreadsheet->createSheet();
  18. $sheet2->setTitle('Sheet2');
  19. $sheet2->getCell('A1')->setValue(5);
  20. $sheet1->getCell('A1')->setValue(9);
  21. $sheet1->getCell('A2')->setValue(2);
  22. $sheet1->getCell('A3')->setValue(4);
  23. $sheet1->getCell('A4')->setValue(6);
  24. $sheet1->getCell('A5')->setValue(7);
  25. $sheet1->getRowDimension(5)->setVisible(false);
  26. $sheet1->getCell('B1')->setValue('=1/0');
  27. $sheet1->getCell('C1')->setValue('=Sheet99!A1');
  28. $sheet1->getCell('C2')->setValue('=Sheet2!A1');
  29. $sheet1->getCell('C3')->setValue('=Sheet2!A2');
  30. $sheet1->getCell('H1')->setValue($formula);
  31. self::assertSame($expected, $sheet1->getCell('H1')->getCalculatedValue());
  32. $spreadsheet->disconnectWorksheets();
  33. }
  34. public function providerRefError(): array
  35. {
  36. return [
  37. 'Subtotal9 Ok' => [12, '=SUBTOTAL(A1,A2:A4)'],
  38. 'Subtotal9 REF' => ['#REF!', '=SUBTOTAL(A1,A2:A4,C1)'],
  39. 'Subtotal9 with literal and cells' => [111, '=SUBTOTAL(A1,A2:A4,99)'],
  40. 'Subtotal9 with literal no rows hidden' => [111, '=SUBTOTAL(109,A2:A4,99)'],
  41. 'Subtotal9 with literal ignoring hidden row' => [111, '=SUBTOTAL(109,A2:A5,99)'],
  42. 'Subtotal9 with literal using hidden row' => [118, '=SUBTOTAL(9,A2:A5,99)'],
  43. 'Subtotal9 with Null same sheet' => [12, '=SUBTOTAL(A1,A2:A4,A99)'],
  44. 'Subtotal9 with Null Different sheet' => [12, '=SUBTOTAL(A1,A2:A4,C3)'],
  45. 'Subtotal9 with NonNull Different sheet' => [17, '=SUBTOTAL(A1,A2:A4,C2)'],
  46. 'Product DIV0' => ['#DIV/0!', '=PRODUCT(2, 3, B1)'],
  47. 'Sqrt REF' => ['#REF!', '=SQRT(C1)'],
  48. 'Sum NUM' => ['#NUM!', '=SUM(SQRT(-1), A2:A4)'],
  49. 'Sum with literal and cells' => [111, '=SUM(A2:A4, 99)'],
  50. 'Sum REF' => ['#REF!', '=SUM(A2:A4, C1)'],
  51. 'Tan DIV0' => ['#DIV/0!', '=TAN(B1)'],
  52. ];
  53. }
  54. }