2012-03-21 33 views
0

私のアプリにPhpExcelを使用していますが、エラーが表示されます。私は try{}catch(){}で例外を処理しようとしましたが、動作しません。 PhpExcelで例外を処理する方法は?ここに私のコードです:PhpExcelで例外を処理する方法は?

function import($excelObj) { 
    $sheet=$excelObj->getActiveSheet(); 
    $cell = $sheet->getCellByColumnAndRow(1, 10);//assume we need calculate at col 1, row 10 

    try { 
     //This line seen error, but cannot echo in catch. 
     $val = $cell->getCalculatedValue(); // $cell contain a formula, example: `=A1+A6-A8` 
              // with A1 is constant, A6 is formula `=A2*A5` 
              // and A8 is another `=A1/(A4*100)-A7` 
     return $val; 
    } catch (Exception $e) { 
     echo $e->getTraceAsTring(); 
    } 
} 

ありがとうございます!

+0

'$ val = $ cell-> getCalculatedValue();'は例外を投げる行ですか? '$ sheet = $ excelObj-> getActiveSheet();をラップしてみてください。 $ cell = $ sheet-> getCellByColumnAndRow(1,10); 'もtryブロック内にあります。 – F21

+0

*正確なエラーメッセージ*とは何ですか? – Phil

+0

'echo $ e-> getTraceAsTring()'は実行されません。エラーメッセージの表示: 'bbjectをintに変換できません ':| 私は 'Yiiフレームワーク'を使用しています – Davuz

答えて

2

計算エンジンは、catcheableである通常のPHP例外をスローする必要があります。私は計算エンジンエラーをデバッグするために使用するフロント・エンド・ロジックは次のとおりです。

// enable debugging 
PHPExcel_Calculation::getInstance()->writeDebugLog = true; 

$formulaValue = $sheet->getCell($cell)->getValue(); 
echo '<b>'.$cell.' Value is </b>'.$formulaValue."<br />\n"; 

$calculate = false; 
try { 
    $tokens = PHPExcel_Calculation::getInstance()->parseFormula($formulaValue,$sheet->getCell($cell)); 
    echo '<b>Parser Stack :-</b><pre>'; 
    print_r($tokens); 
    echo '</pre>'; 
    $calculate = true; 
} catch (Exception $e) { 
    echo "PARSER ERROR: ".$e->getMessage()."<br />\n"; 

    echo '<b>Parser Stack :-</b><pre>'; 
    print_r($tokens); 
    echo '</pre>'; 
} 

if ($calculate) { 
    // calculate 
    try { 
     $cellValue = $sheet->getCell($cell)->getCalculatedValue(); 
    } catch (Exception $e) { 
     echo "CALCULATION ENGINE ERROR: ".$e->getMessage()."<br />\n"; 

     echo '<h3>Evaluation Log:</h3><pre>'; 
     print_r(PHPExcel_Calculation::getInstance()->debugLog); 
     echo '</pre>'; 
    } 
} 

これは、デバッグ時に非常に役立ちます計算エンジンがどのように機能するかについての追加情報の多くを提供します。

+0

ありがとうございます!おそらく、これは私が必要とする解決策です。 – Davuz

関連する問題