title: How to improve formula calculation with PHPExcel?
body: <p>I'm new to PHPExcel, and I've scoured the Googles, but not finding much for my specific problem here. I'm finding that updating/evaluating the formulas in my sheet is being <em>really</em> slow.</p>
  
  <p>I've got a pretty small excel file on which I'm doing some very basic goal-seeking (goal seeking done in PHP, end result calculation done in excel sheet). I've got it working accurately, but the speed is absolutely killing me. It appears that the formula calculation is to blame -- <strong>how can formula calculations/updates be sped up?</strong></p>
  
  <p>Unfortunately, I can't post a copy of the excel file as the contents are a trade secret for my company, but it's nothing out of the ordinary. Very simple arithmetic in the formulas. The only thing I can think of that might have an effect here is that some of the cell-dependency chains can be somewhat long (15-ish dependencies).</p>
  
  <p>As you can see from the output below, we're only executing 11 iterations for the goal seeking, and taking a total of 4-5 seconds. Since this will be an AJAX service, I really need it to be faster than that.</p>
  
  <h2>Code</h2>
  
  <p>This is very quick and dirty proof-of-concept code, please bear with me:</p>
  
  
  
  <pre><code>&lt;?php
  
  Stopwatch::start();
  
  $inputFileType = PHPExcel_IOFactory::identify( './example.xlsx' );
  var_dump( 'FileType: '.$inputFileType );
  Stopwatch::rel( 'identify filetype' );
  
  $objReader = PHPExcel_IOFactory::createReader( $inputFileType );
  $objReader-&gt;setReadDataOnly( true );
  $filterSubset = new ReadFilter( 1, 35, range( 'A', 'J' ));
  $objReader-&gt;setReadFilter( $filterSubset );
  Stopwatch::rel( 'create reader' );
  
  $objPHPExcel = $objReader-&gt;load( $inputFileName );
  Stopwatch::rel( 'load file' );
  
  $data = $objPHPExcel-&gt;getSheetByName( 'Data' );
  $inputCell  = $data-&gt;getCell( 'B9' );
  $outputCell = $data-&gt;getCell( 'B35' );
  Stopwatch::rel( 'get cells' );
  
  goalSeek( $inputCell, $outputCell, '0.10', 1, 5 );
  
  
  
  
  
  
  
  function goalSeek( $inputCell, $outputCell, $targetValue ) {
      $cellValue = function() use ( &amp;$outputCell, $precision ) {
          return round( $outputCell-&gt;getCalculatedValue(), $precision );
      };
  
      $setValue = function( $value ) use ( &amp;$inputCell, &amp;$objPHPExcel, $cellValue ) {
          $inputCell-&gt;setValue( $value );
          PHPExcel_Calculation::getInstance( $objPHPExcel )-&gt;clearCalculationCache(); // -- clear cache so updates are calculated
          Stopwatch::rel( 'goal-seek' );
      };
  
      // -- very basic goal seeking psuedo-code
      while( $stillHunting ) { // -- outside tolerance
          $setValue( $newInputValue );
      }
  };
  
  class ReadFilter implements PHPExcel_Reader_IReadFilter {
      private $_startRow = 0;
      private $_endRow   = 0;
      private $_columns  = [];
  
      public function __construct( $startRow, $endRow, $columns ) {
          $this-&gt;_startRow = $startRow;
          $this-&gt;_endRow   = $endRow;
          $this-&gt;_columns  = $columns;
      }
  
      public function readCell( $column, $row, $worksheetName = '' ) {
          if( $row &gt;= $this-&gt;_startRow &amp;&amp; $row &lt;= $this-&gt;_endRow ) { // -- valid row
              if( in_array( $column, $this-&gt;_columns )) { // -- valid column
                  return true;
              }
          }
          // else (implicit)
  
          return false;
      }
  }
  </code></pre>
  
  <h2>Output</h2>
  
  <pre><code>string 'FileType: Excel2007' (length=19)
  
  array (size=2)
    'rel' =&gt; 
      array (size=17)
        'identify' =&gt; float 0.008597135543823242
        'create reader' =&gt; float 0.0001199245452880859
        'load file' =&gt; float 0.387645959854126
        'get cells' =&gt; float 5.292892456054688E-5
        'goal-seek' =&gt; float 0.4194750785827637
        'goal-seek2' =&gt; float 0.3829901218414307
        'goal-seek3' =&gt; float 0.3478608131408691
        'goal-seek4' =&gt; float 0.3471150398254395
        'goal-seek5' =&gt; float 0.3569440841674805
        'goal-seek6' =&gt; float 0.378180980682373
        'goal-seek7' =&gt; float 0.3683559894561768
        'goal-seek8' =&gt; float 0.3778479099273682
        'goal-seek9' =&gt; float 0.3664979934692383
        'goal-seek10' =&gt; float 0.4503841400146484
        '_avg' =&gt; float 0.2794940630594889
        '_untilStop' =&gt; float 0.5339441299438477
    'total' =&gt; float 4.726345062255859
  </code></pre>