2014-10-08 133 views
0

我推斷getOldCalculatedValue()返回已經計算的公式,如果單元格包含公式,或者如果單元格不包含公式,則返回值。一個單元格A1PHPExcel getOldCalculatedValue返回NULL

$value = $objPHPExcel->setActiveSheetIndex(0) 
        ->getCell('G'.$i) 
        ->getValue(); 

Will return the actual formula if the cell contains a formula, or the 
value if the cell contains a value. 

$value = $objPHPExcel->setActiveSheetIndex(0) 
        ->getCell('G'.$i) 
        ->getCalculatedValue(); 

Will return the calculated value if the cell contains a formula, or the value 
if the cell contains a value. 

$value = $objPHPExcel->setActiveSheetIndex(0) 
        ->getCell('G'.$i) 
        ->getOldCalculatedValue(); 

Will return the previous result of a calculation if the cell contains a formula, 
or the value if the cell contains a value. 

我創建XLSX(Excel 2007中):我的假設是由Mark Baker's post操作(雖然這個職位是從2011年年)證實。 A1的內容是字符串'foo'。當我打電話

$reader = new PHPExcel_Reader_Excel2007(); 
$excel = $reader->load(Input::file("pricelist")->getRealPath()); 
$worksheet = $excel->setActiveSheetIndex(0); 
var_dump($worksheet->getCell('A1')->getOldCalculatedValue()); 

它打印NULL。如果我將函數更改爲getCalculatedValue()或getValue(),則它可以正常工作。我正在使用PHPExcel 1.8.0。

回答

2

這並不嚴格,即使對於包含公式的單元格,getOldCalculatedValue()可能會返回NULL也有幾個原因。

的主要原因是:

  • 並非所有的電子表格格式支持維持最後的計算值
  • 即使那些支持維持最後的計算值格式,創建該文件的應用程序可能不會寫值,以該文件
  • 自動計算可能在Excel中被停用(假設Excel中用於創建文件

如果在公式輸入後的某個時間點自動計算被禁用,但最後可能更成爲一個問題,但後續數據更改會影響公式,因爲它們不會反映在舊計算值中