douyong5476 2016-04-14 08:11
浏览 82

PHPExcel SUMIF返回错误/未计算的公式

I have a cell C13 with a simple test formula =SUMIF(D8:D18,2,E8:E18). D8->D18 Holds 2 2's and the values of D8:D18 are hardcoded, not retrieved from another formula. The range of E8->E18 are all 1's. In excel the correct value is 2. But when i call PHPExcel with the following simple code.

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
PHPExcel_Calculation::getInstance($objPHPExcel)->clearCalculationCache();
$objReader  = $objPHPExcel->setActiveSheetIndexByName("TestSumIf");
$value = $objPHPExcel->getActiveSheet()->getCell('C13')->getCalculatedValue();

I get $value = 0.

Formula Value is=SUMIF(D8:D18,2,E8:E18)

Expected Value is 0

Parser Stack :- Array ( [0] => Array ( [type] => Cell Reference [value] => D8 [reference] => D8 ) [1] => Array ( [type] => Cell Reference [value] => D18 [reference] => D18 ) [2] => Array ( [type] => Binary Operator [value] => : [reference] => ) [3] => Array ( [type] => Value [value] => 2 [reference] => ) [4] => Array ( [type] => Cell Reference [value] => E8 [reference] => E8 ) [5] => Array ( [type] => Cell Reference [value] => E18 [reference] => E18 ) [6] => Array ( [type] => Binary Operator [value] => : [reference] => ) [7] => Array ( [type] => Operand Count for Function SUMIF() [value] => 3 [reference] => ) [8] => Array ( [type] => Function [value] => SUMIF( [reference] => ) )

Calculated Value is 0

Evaluation Log:

Anyone have an insight as to why this might be happening. Ive seen the documentation saying there should be no error with SUMIF but only with SUMIFS that can be rewritten as SUMPRODUCT, but in my case SUMIF is causing the error.

  • 写回答

1条回答 默认 最新

  • dongnanke4106 2016-04-14 11:27
    关注

    Well, if this causes a problem for anyone. What i personally did was changed the SUMIF function in MathTrig.php to the following. In my case i want to exclude values that are '---' in my $array and $sum_array. It also counts the new array $ar count if the values are equal to those in $checkVars. Change the if statement to your own specifications.

    public static function SUMIF($array,$criteria,$sum_array)
    { 
     $array = PHPExcel_Calculation_Functions::flattenArray($array);
     $sum_array = PHPExcel_Calculation_Functions::flattenArray($sum_array);
     $arraySize = sizeof($array);
     $arraySizeCount = 0;
     $checkVars = array(18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75);
        for($i = 0; $i < $arraySize;$i++)
        {
            if($array[$i] === '---')
            {
                unset($array[$i]);
                unset($sum_array[$i]);
            }
            else if(in_array($array[$i], $checkVars))
            {
                $arraySizeCount++;
            }
        }
     if(is_array($array) && is_array($sum_array) && trim($criteria)!="")
     { 
        $result = 0 ;
            for($i=0;$i<$arraySizeCount;$i++)
            {
                if(preg_match("/^</",$criteria))
                {
                    $value = preg_replace("/^</","",$criteria);
                    $result += $array[$i] < $value ? $sum_array[$i]:0; 
                 }
                elseif(preg_match("/^>/",$criteria))
                { 
                    $value = preg_replace("/^>/","",$criteria);
                    $result += $array[$i] > $value ? $sum_array[$i]:0;
                }
                else
                {
                    $value = $criteria;
                    $result += $array[$i] == $value ? $sum_array[$i]:0;
                    echo $result; 
                } 
            }
        return $result ? $result:0;
     }
    }
    

    It works perfectly. If anyone has a mean of writing this better, please let me know.

    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染