PHPExcel不评估公式

J-S*_*arp 5 php phpexcel

我正在尝试使用PHPExcel 1.7.9在我的XSLX工作簿中评估以下公式: =IF($H$7=1,$F$13,$H$227*$J$227)

在我必须更新工作簿中的某些单元格然后我使用此行来计算值之前:

$sheet->getCell('L228')->getCalculatedValue();
Run Code Online (Sandbox Code Playgroud)

但是,PHPExcel似乎不会正确更新包含公式的单元格.它会抛出一些通知消息,如下所示但不更新单元格的计算值:

注意:未定义的偏移量:第2852行的D:\ my-path\hkc\Classes\PHPExcel\Calculation.php中的2

没有例外,似乎getCalculatedValue()返回由MS Excel计算的旧缓存值.如下所述,我已经激活了计算引擎的调试日志记录,但它似乎是空的:

PHPExcel_CalcEngine_Logger Object
(
    [_writeDebugLog:PHPExcel_CalcEngine_Logger:private] => 
    [_echoDebugLog:PHPExcel_CalcEngine_Logger:private] => 
    [_debugLog:PHPExcel_CalcEngine_Logger:private] => Array
        (
        )

    [_cellStack:PHPExcel_CalcEngine_Logger:private] => PHPExcel_CalcEngine_CyclicReferenceStack Object
        (
            [_stack:PHPExcel_CalcEngine_CyclicReferenceStack:private] => Array
                (
                )

        )

)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下步骤来解决问题:

  1. 停用计算缓存: PHPExcel_Calculation::getInstance()->setCalculationCacheEnabled(FALSE);
  2. 激活计算引擎的调试模式:如何使用PhpExcel处理异常中描述了这一点.特别是http://phpexcel.codeplex.com/discussions/233047
    但是,我不得不稍微修改引用页面的代码 - 也许在PHPExcel 1.7.9中有些变化?
    激活调试:PHPExcel_Calculation::getInstance()->writeDebugLog = true;
    获取调试日志:print_r(PHPExcel_Calculation::getInstance()->getDebugLog());

我不明白为什么引擎的实际日志是空的?引擎在写出任何条目之前是否停止工作或者我的调试配置有问题?

但是,我试图用Java和Apache POI评估相同的公式 - 它有效!不幸的是我要将PHP用于当前项目.

请帮我!也许有人知道公式评估有什么问题,或者至少可以给我一些提示如何正确激活调试?

谢谢!

Mar*_*ker 1

还没有答案,但评论太长了:

PHPExcel 1.7.9 将计算引擎从单例修改为多例(需要避免同时处理多个工作簿时发生冲突)。因此,每个工作簿都运行自己的计算引擎实例,并且在调用时

PHPExcel_Calculation::getInstance()
Run Code Online (Sandbox Code Playgroud)

you now have to specify which instance you need to access. Each PHPExcel instance has an ID value, maintained in the PHPExcel object that it is used for this purpose, and you need to pass the PHPExcel object itself to the getInstance() method. So, if your PHPExcel workbook is $objPHPExcel, you'd need to use

PHPExcel_Calculation::getInstance($objPHPExcel)
Run Code Online (Sandbox Code Playgroud)

to reference the correct calc engine instance when flushing the cache, or enabling the logger, or reading the log.

re. the specifics of your error: Do any of the cells referenced by your IF formula cell reference other formulae containing CUBE functions or the SUMIFS function? Those are the only functions that I'm aware of that might trigger this particular error

EDIT

Example of refactoring a SUMIFS() function

=SUMIFS(A2:A3, B2:B3, E1, C2:C3, F1)
Run Code Online (Sandbox Code Playgroud)

which sums A2:A3 if B2:B3 meets the E1 criteria, and C2:C3 meets the F1 criteria.

This can be refactored as

=SUMPRODUCT(A2:A3 * (B2:B3=E1) * (C2:C3=F1))
Run Code Online (Sandbox Code Playgroud)