13_CalculationCyclicFormulae.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. require __DIR__ . '/../Header.php';
  5. // Create new Spreadsheet object
  6. $helper->log('Create new Spreadsheet object');
  7. $spreadsheet = new Spreadsheet();
  8. // Add some data, we will use some formulas here
  9. $helper->log('Add some data and formulas');
  10. $spreadsheet->getActiveSheet()->setCellValue('A1', '=B1')
  11. ->setCellValue('A2', '=B2+1')
  12. ->setCellValue('B1', '=A1+1')
  13. ->setCellValue('B2', '=A2');
  14. Calculation::getInstance($spreadsheet)->cyclicFormulaCount = 100;
  15. // Calculated data
  16. $helper->log('Calculated data');
  17. for ($row = 1; $row <= 2; ++$row) {
  18. for ($col = 'A'; $col != 'C'; ++$col) {
  19. if ((($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null) &&
  20. ($formula[0] == '=')) {
  21. $helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());
  22. }
  23. }
  24. }
  25. // Save
  26. $helper->write($spreadsheet, __FILE__);