Change input value but not change result in PhpExcel

I have a problem with PHPExcel as below function

function Test($a, $b) {

    //  Create a new PHPExcel object with a single sheet
    $objPHPExcel = new PHPExcel();
    $activeSheet = $objPHPExcel->getActiveSheet();

    $activeSheet->SetCellValue('B2',$a);
    $activeSheet->SetCellValue('B3',$b);
    $activeSheet->SetCellValue('C4',"=B2+B3");
    $C4 = $activeSheet ->getCell('C4')->getCalculatedValue();

    echo "C4:$C4<br/>";
}

      

Finally, I call this function

Test(10, 20);
Test(40, 70);
Test(30, 80);

      

but the result

C4:30
C4:30
C4:30 

      

Why does getCalculatedValue () NOT change the result? This function seems to only get the first value.

+3


source to share


2 answers


You can change the result, but for performance reasons, the calculator caches the result of evaluating the formula after it has been evaluated. If you want to change the underlying data, you must clear this cache before requesting the computed value again:

PHPExcel_Calculation::getInstance()->clearCalculationCache();

      

or



PHPExcel_Calculation::flushInstance();

      

You can also change this default behavior - so that results are not cached at all - before issuing other requests to the compute engine:

PHPExcel_Calculation::getInstance()->setCalculationCacheEnabled(FALSE);

      

+2


source


If you are using phpExcel you don't need this. You can only do this:

set the title once.

$objPHPExcel = new PHPExcel(); $activeSheet = $objPHPExcel->getActiveSheet();

Then use it

`function Test ($ a, $ b) {

$activeSheet->SetCellValue('B2',$a);
$activeSheet->SetCellValue('B3',$b);
$activeSheet->SetCellValue('C4',"=B2+B3");

      



} `

declare value Test(10, 30);

if you set coll and don't change cell then overwrite. If you want a list

function Test($a, $b) {
$i++;
    $activeSheet->SetCellValue('B2',$a);
    $activeSheet->SetCellValue('B3',$b);
    $activeSheet->SetCellValue('C'.$i,"=B2+B3");

}

      

and set the value Test(10, 30);

then output 40

0


source







All Articles