PHP Excel Calculate Formulas

I am trying to print Excel file data on a page. For this I used PHPExcel lib, everything works well, in addition to printing formulas, I have a simple example with such a formula=SUM(C2:C5)

I print the values ​​this way:

 $val = $cell->getValue();
 echo '<td>' . $val . '</td>';

how can i check if $ val is a formula?

PHPExcel_Cell_DataType::dataTypeForValue($val); told me it was just another line in my $val

Ofc, I can calculate it in a loop, and chek, if this is the last line, insert the necessary information with my hands, but how can I calculate it in a simple way?

We look forward to hearing your advice. Thank.

+5
source share
2 answers

PHPExcel_Cell_DataType::dataTypeForValue($val); , . , . getDataType() 'f' .

getCalculatedValue(), getValue(), PHPExcel , . , , , getValue();. , , - getFormattedValue(), , . . , . , , .

toArray() rangeToArray(), , . , , .

+9

" , $val ?"

excel = :

<?php
$String = 'SUM(B3:B8)';

# See http://www.php.net/manual/en/language.types.string.php
# $String{} is deprecated as of PHP 6.
if($String[0] == '='){
    echo 'Yes';
} else {
    echo 'No';
}
?>

<?php
function IsExcelFormula($String=null){
    if(!$String OR strlen($String) <= 0){
        return false;
    }

    $First = $String[0];

    return ($First == '=' ? true : false);
}

var_dump(IsExcelFormula('=SUM(B8:B9)')); // bool(true)
var_dump(IsExcelFormula('TOTAL(B3:B6)')); // bool(false)
?>
+3

All Articles