Skip to content
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

SUMIFS error with multiple conditions #704

Closed
marcusblevin opened this issue Oct 4, 2018 · 2 comments
Closed

SUMIFS error with multiple conditions #704

marcusblevin opened this issue Oct 4, 2018 · 2 comments

Comments

@marcusblevin
Copy link
Contributor

This is:

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

What is the expected behavior?

The range is summed if the conditions are met

What is the current behavior?

If only one condition is provided, the range is summed correctly. If multiple conditions are provided the sum is inaccurate. After the XL spreadsheet is loaded, the formula can be refreshed and is then accurate

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();

// add code that show the issue here...
$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'Y')
	  ->setCellValue('B1', 'High')
	  ->setCellValue('C1', 55327)
	  ->setCellValue('A2', 'Y')
	  ->setCellValue('B2', 'High')
	  ->setCellValue('C2', 12345)
	  ->setCellValue('A3', 'N')
	  ->setCellValue('B3', 'High')
	  ->setCellValue('C3', 98786)
	  ->setCellValue('A4', 'Y')
	  ->setCellValue('B4', 'Mid')
	  ->setCellValue('C4', 56466)
	  ->setCellValue('A5', 'Y')
	  ->setCellValue('B5', 'Mid')
	  ->setCellValue('C5', 79843)
	  ->setCellValue('A6', 'N')
	  ->setCellValue('B6', 'Mid')
	  ->setCellValue('C6', 12654)
	  ->setCellValue('A7', 'Y')
	  ->setCellValue('B7', 'Long')
	  ->setCellValue('C7', 65132)
	  ->setCellValue('A8', 'Y')
	  ->setCellValue('B8', 'Long')
	  ->setCellValue('C8', 75312)
	  ->setCellValue('A9', 'N')
	  ->setCellValue('B9', 'Long')
	  ->setCellValue('C9', 96315);

	  
$sheet->setCellValue('E1', 'High')
	  ->setCellValue('F1', '=IFERROR(SUMIFS(C1:C9, A1:A9,"=Y", B1:B9,"=High"),0)')
	  ->setCellValue('G1', '=IFERROR(SUMIFS(C1:C9, B1:B9,"=High"),0)')
	  ->setCellValue('E2', 'Mid')
	  ->setCellValue('F2', '=IFERROR(SUMIFS(C1:C9, A1:A9,"=Y", B1:B9,"=Mid"),0)')
	  ->setCellValue('G2', '=IFERROR(SUMIFS(C1:C9, B1:B9,"=Mid"),0)')
	  ->setCellValue('E3', 'Long')
	  ->setCellValue('F3', '=IFERROR(SUMIFS(C1:C9, A1:A9,"=Y", B1:B9,"=Long"),0)')
	  ->setCellValue('G3', '=IFERROR(SUMIFS(C1:C9, B1:B9,"=Long"),0)');

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet: 1.4.1
PHP: 7.2.10

@marcusblevin marcusblevin changed the title SUMIFS only evaluating first condition SUMIFS error with multiple conditions Oct 4, 2018
@PowerKiKi
Copy link
Member

PowerKiKi commented Oct 7, 2018

Can reproduce the issue with the following simplified code. Expecting 2, but got 5. It seems that a cell is summed multiple times if it match the several conditions whereas we should only sum it once, even if matching several times.

PR are welcome to fix this.

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

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'Y')
    ->setCellValue('B1', 'High')
    ->setCellValue('C1', 1)
    ->setCellValue('A2', 'Y')
    ->setCellValue('B2', 'High')
    ->setCellValue('C2', 1)
    ->setCellValue('A3', 'N')
    ->setCellValue('B3', 'High')
    ->setCellValue('C3', 1);

$sheet->setCellValue('F1', '=SUMIFS(C1:C9, A1:A9,"=Y", B1:B9,"=High")');

$actual = $sheet->getCell('F1')->getCalculatedValue();

var_dump([$actual, $actual == 2]);

@marcusblevin
Copy link
Contributor Author

marcusblevin commented Oct 8, 2018

Submitted a PR #710

My first one so apologies if it isn't to standard

guillaume-ro-fr pushed a commit to guillaume-ro-fr/PhpSpreadsheet that referenced this issue Jun 12, 2019
Values were summed multiple times if it matched several conditions
whereas it should only be summed once.

Fixes PHPOffice#704
Fixes PHPOffice#710
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants