PHPExcel GetCalculatedValue()返回错误的值

时间:2013-08-19 09:21:52

标签: php phpexcel

我有一个带有6张的.xlsx文件,我加载了

$objPHPExcel = PHPExcel_IOFactory::load('./FINAL SEB.xlsx');

当我想从单元格中获取计算值时,我得到一个错误的值。 这个单元格有一个公式

(IF(COMPARATEUR!B6=20,EMPRUNT20ANS!F32,IF(COMPARATEUR!B6=25,EMPRUNT25ANS!F37,IF(COMPARATEUR!B6=15,EMPRUNT15ANS!F27,"Erreur années"))))-B35

在我的PHP代码中我做了

echo $worksheet->getCell('B38')->getCalculatedValue().'<br />';

当我重新点票时,我获得了

Formula Value is=(IF(COMPARATEUR!B6=20,EMPRUNT20ANS!F32,IF(COMPARATEUR!B6=25,EMPRUNTANS!F37,IF(COMPARATEUR!B6=15,EMPRUNT15ANS!F27,"Erreur années"))))-B35
Expected Value is 59045.580877165
Parser Stack :-
array (size=21)
  0 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'COMPARATEUR!B6' (length=14)
      'reference' => string 'COMPARATEUR!B6' (length=14)
  1 => 
    array (size=3)
      'type' => string 'Value' (length=5)
      'value' => int 20
      'reference' => null
  2 => 
    array (size=3)
      'type' => string 'Binary Operator' (length=15)
      'value' => string '=' (length=1)
      'reference' => null
  3 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'EMPRUNT20ANS!F32' (length=16)
      'reference' => string 'EMPRUNT20ANS!F32' (length=16)
  4 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'COMPARATEUR!B6' (length=14)
      'reference' => string 'COMPARATEUR!B6' (length=14)
  5 => 
    array (size=3)
      'type' => string 'Value' (length=5)
      'value' => int 25
      'reference' => null
  6 => 
    array (size=3)
      'type' => string 'Binary Operator' (length=15)
      'value' => string '=' (length=1)
      'reference' => null
  7 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'EMPRUNTANS!F37' (length=14)
      'reference' => string 'EMPRUNTANS!F37' (length=14)
  8 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'COMPARATEUR!B6' (length=14)
      'reference' => string 'COMPARATEUR!B6' (length=14)
  9 => 
    array (size=3)
      'type' => string 'Value' (length=5)
      'value' => int 15
      'reference' => null
  10 => 
    array (size=3)
      'type' => string 'Binary Operator' (length=15)
      'value' => string '=' (length=1)
      'reference' => null
  11 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'EMPRUNT15ANS!F27' (length=16)
      'reference' => string 'EMPRUNT15ANS!F27' (length=16)
  12 => 
    array (size=3)
      'type' => string 'Value' (length=5)
      'value' => string '"Erreur années"' (length=16)
      'reference' => null
  13 => 
    array (size=3)
      'type' => string 'Operand Count for Function IF()' (length=31)
      'value' => int 3
      'reference' => null
  14 => 
    array (size=3)
      'type' => string 'Function' (length=8)
      'value' => string 'IF(' (length=3)
      'reference' => null
  15 => 
    array (size=3)
      'type' => string 'Operand Count for Function IF()' (length=31)
      'value' => int 3
      'reference' => null
  16 => 
    array (size=3)
      'type' => string 'Function' (length=8)
      'value' => string 'IF(' (length=3)
      'reference' => null
  17 => 
    array (size=3)
      'type' => string 'Operand Count for Function IF()' (length=31)
      'value' => int 3
      'reference' => null
  18 => 
    array (size=3)
      'type' => string 'Function' (length=8)
      'value' => string 'IF(' (length=3)
      'reference' => null
  19 => 
    array (size=3)
      'type' => string 'Cell Reference' (length=14)
      'value' => string 'B35' (length=3)
      'reference' => string 'B35' (length=3)
  20 => 
    array (size=3)
      'type' => string 'Binary Operator' (length=15)
      'value' => string '-' (length=1)
      'reference' => null
Calculated Value is 9860500
Evaluation Log:
null

你能帮我吗?

P.S。抱歉我的英文。

1 个答案:

答案 0 :(得分:2)

如果您正在使用我之前在此处发布的标准化testFormula()调试代码来转储您已显示的跟踪,那么当计算引擎从单例修改为一个multiton模式,以避免一次使用多个工作簿文件时出现问题。该代码的更新版本如下所示:

function testFormula($sheet,$cell) {
    $formulaValue = $sheet->getCell($cell)->getValue();
    echo 'Formula Value is' , $formulaValue , PHP_EOL;
    $expectedValue = $sheet->getCell($cell)->getOldCalculatedValue();
    echo 'Expected Value is '  , ((!is_null($expectedValue)) ? $expectedValue : 'UNKNOWN') , PHP_EOL;


    $calculate = false;
    try {
        $tokens = PHPExcel_Calculation::getInstance(
            $sheet->getParent()
        )->parseFormula(
            $formulaValue,
            $sheet->getCell($cell)
        );
        echo 'Parser Stack :-' , PHP_EOL;
        print_r($tokens);
        echo PHP_EOL;
        $calculate = true;
    } catch (Exception $e) {
        echo 'PARSER ERROR: ' , $e->getMessage() , PHP_EOL;

        echo 'Parser Stack :-' , PHP_EOL;
        print_r($tokens);
        echo PHP_EOL;
    }

    if ($calculate) {
        PHPExcel_Calculation::getInstance(
            $sheet->getParent()
        )->getDebugLog()
        ->setWriteDebugLog(true);
        try {
            $cellValue = $sheet->getCell($cell)->getCalculatedValue();
            echo 'Calculated Value is ' , $cellValue , PHP_EOL;

            echo 'Evaluation Log:' , PHP_EOL;
            print_r(
                PHPExcel_Calculation::getInstance(
                    $sheet->getParent()
                )->getDebugLog()
                ->getLog()
            );
            echo PHP_EOL;
        } catch (Exception $e) {
            echo 'CALCULATION ENGINE ERROR: ' , $e->getMessage() , PHP_EOL;

            echo 'Evaluation Log:' , PHP_EOL;
            print_r(
                PHPExcel_Calculation::getInstance(
                    $sheet->getParent()
                )->debugLog
                ->getLog()
            );
            echo PHP_EOL;
        }
    }
}


$sheet = $objPHPExcel->getActiveSheet();
testFormula($sheet,'A1');

请尝试使用上述内容来帮助诊断问题