Skip to content

sumif() not calculating when comparing numerical values #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DuckDensity opened this issue Sep 20, 2018 · 3 comments
Closed

sumif() not calculating when comparing numerical values #683

DuckDensity opened this issue Sep 20, 2018 · 3 comments

Comments

@DuckDensity
Copy link

This is:

- [x] a bug report
- [ ] a feature request
- [x] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Calculation of the resultant value in the saved spreadsheet

What is the current behavior?

The value is calculated correctly if the criteria for sumif() is a string.
The value is zero if the criteria is numeric.

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// Some sample data
$spreadsheet->getActiveSheet()->setCellValue('A1', "Text-Test")
                              ->setCellValue('A2', "Text-Test")
                              ->setCellValue('A3', "Text-Testing")
                              ->setCellValue('A4', "Text-Testing")
                              ->setCellValue('B1', "5")
                              ->setCellValue('B2', "8")
                              ->setCellValue('B3', "12")
                              ->setCellValue('B4', "15")
                              ->setCellValue('C1', 0)
                              ->setCellValue('C2', 0.2)
                              ->setCellValue('C3', 0)
                              ->setCellValue('C4', 0.2);

// This sumif() works when comparing text, and gives result of 13 (5+8) (Cells B1 + B2)
$spreadsheet->getActiveSheet()->setCellValue('A5', "=SUMIF(A1:A4,\"Text-Test\",B1:B4)");
$spreadsheet->getActiveSheet()->setCellValue('A6', "A5 Should be 13");

// This sumif() does not calculate and zero is saved in the spread sheet
// Selecting the cell for editing in Excel and then accepting forces Excel
// to calculate the cell value of 17 (5+12) (Cells B1 + B3)
$spreadsheet->getActiveSheet()->setCellValue('C5', "=SUMIF(C1:C4,0,B1:B4)");
$spreadsheet->getActiveSheet()->setCellValue('C6', "C5 Should be 17");

// Write spreadsheet
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$objWriter->save("test.xlsx");

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet [1.4.0] - 2018-08-06
PHP 7.1.19

ilicmsreten pushed a commit to ilicmsreten/PhpSpreadsheet that referenced this issue Oct 2, 2018
@PowerKiKi
Copy link
Member

I can reproduce with this slightly simplified code:

<?php
require 'vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// Some sample data
$spreadsheet->getActiveSheet()
    ->setCellValue('B1', "5")
    ->setCellValue('B2', "8")
    ->setCellValue('B3', "12")
    ->setCellValue('B4', "15")
    ->setCellValue('C1', 0)
    ->setCellValue('C2', 0.2)
    ->setCellValue('C3', 0)
    ->setCellValue('C4', 0.2);

// This sumif() does not calculate and zero is saved in the spread sheet
// Selecting the cell for editing in Excel and then accepting forces Excel
// to calculate the cell value of 17 (5+12) (Cells B1 + B3)
$spreadsheet->getActiveSheet()->setCellValue('C5', "=SUMIF(C1:C4,0,B1:B4)");
$spreadsheet->getActiveSheet()->setCellValue('C6', "C5 Should be 17");

$actual = $spreadsheet->getActiveSheet()->getCell('C5')->getCalculatedValue();
var_dump([$actual, $actual == 17]);

PR would be very welcome

@DuckDensity
Copy link
Author

After submitting the report I did investigate this further, and found that it affected CountIf and AverageIf in the same way. I also discovered a flaw in the AverageIf function which I will report separately.

This morning I have spent a bit more time on it, and found the code that relates to the bug I originally described, which is on line 270 of Functions.php. This lines replaces numerical conditions with a test for an empty string.

My proposal was to add in an additional if test for numeric conditions, Although I then looked at the pull requests, and see that someone else has proposed the same fix a few days ago #701.

@ilicmsreten
Copy link
Contributor

PR #701

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants