Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Check if the row is empty #561

Open
arogachev opened this issue May 9, 2015 · 13 comments
Open

Check if the row is empty #561

arogachev opened this issue May 9, 2015 · 13 comments

Comments

@arogachev
Copy link

arogachev commented May 9, 2015

I didn't find built-in functionality for check if row is completely empty (all cells are without any values).
I solved it with additional helper class and following method:

public static function isRowEmpty($row)
{
    foreach ($row->getCellIterator() as $cell) {
        if ($cell->getValue()) {
            return false;
        }
    }

    return true;
}

Not sure if it's 100% right, but it works. I think it should be included in PHPExcel_Worksheet_Row something like $row->isEmpty().

@kimegede
Copy link

kimegede commented Sep 8, 2015

👍

3 similar comments
@gael-wogenstahl
Copy link

👍

@arecaps
Copy link

arecaps commented Dec 24, 2015

👍

@obojdi
Copy link

obojdi commented Jun 22, 2016

👍

@kimegede
Copy link

@dbonsch, @gmsantos, @maartenba, @MarkBaker, @Progi1984, @RomanSyroeshko : Any feedback from you guys or don't you take any issues or PR's???

@stavros-liaskos
Copy link

that could be waaaay useful

@gmsantos
Copy link
Contributor

gmsantos commented Dec 1, 2016

@kimegede ??

@Stefanescul
Copy link

Is working or not?

@Dfred
Copy link

Dfred commented Feb 23, 2017

What about that discrepancy?
$hr = $sheet->getHighestDataRow($column_id); // 0 if cell at $column_id.'1' is empty, or 1 if not

However this doesn't work!
$hc = $sheet->getHighestDataColumn($row_id); // 'A' if cell at 'A'.$row_id is empty or not

@CJDennis
Copy link

CJDennis commented Aug 25, 2017

Beware that PHP's truthy/falsy values are not always what you'd expect! String "0" is false, even though it is not empty, as are numerically zero values. PHPExcel_Cell::getValue() can also return false as a boolean value, which means that the cell has FALSE entered into it, so it is not empty.

I believe something like this would work better:

class PHPExcel_Worksheet_Row {
    //...

    public function isRowEmpty() {
        $is_row_empty = true;
        foreach ($this->getCellIterator() as $cell) {
            if ($cell->getValue() !== null && $cell->getValue() !== '') {
                $is_row_empty = false;
                break;
            }
        }

        return $is_row_empty;
    }

    public function getCellIterator() {
        //....
    }
}

@brian-lamb-software-engineer
Copy link

@CJDennis You might want to add trim to that empty string check so that its trim($cell->getValue() !== ''), I have had empty cells, that contains a space due to some weird excel formatting.

How would I implement this into a laravel app where i pull this package in via composer as a repository, then use PHPExcel on my controller. Im thinking i can write a separate class, extend PHPExcel, and then use that class instead.

Well, in the end i set something into rangeToArray to extract empty rows

          $rowData =  $sheet->rangeToArray(
            'A' . $row . ':' . $highestColumn . $row,   NULL, TRUE, TRUE, TRUE
          );
          $filteredRowData = array_filter( array_map('trim', $rowData[$row]));
          if(!empty($filteredRowData))
          {
            $cleanedData[$row] = $rowData[$row];
          }

Note, without the array_map trim, it will not remove empty rows with weird PHPExcel columns, such as those that have "date" type, in some circumstances, because the date type somehow sets them to a space (" ")

@CJDennis
Copy link

@brian-lamb-software-engineer Using trim() mightn't suit everyone, e.g. imagine a sentence split by character, with one character per row. All the spaces would be skipped. If it works for you, great! Do it!

Yes, dates are weird in Excel. It might be best to stick with the "post-processing" solution you've come up with.

I was merely trying to show some flaws in the suggested improvements, and make them better, not fix everything.

@JulioCVaz
Copy link

Thank you, that helped us.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests