Search code examples
perlparsingxlsx

Spreadsheet::ParseXLSX and blank cells


I am trying to parse through an XLSX file (getting the value for each cell, row by row,) using Spreadsheet::ParseXLSX. Within my loop that goes through each row, I've tried pulling the values with this code:

$thisCell = $worksheet->get_cell($currentRow, $currentCol)->value();

and also with:

$thisCell = $worksheet->get_cell($currentRow, $currentCol)->unformatted();

However, the script always breaks if it encounters an empty cell. The error method I get is "Can't call method [value|unformatted] on an undefined value..."

To try to anticipate or deal with the empty cells I have tried:

  • putting an "or $thisCell = ''" after the above code
  • wrapping the above code in a if clause like:

    if ($worksheet->get_cell($currentRow, $currentCol)->unformatted()) {
        $thisCell = $worksheet->get_cell($currentRow, $currentCol)->unformatted();
    else {
        $thisCell = "";
    }
    

I'm really stuck on this, and I'm sure it's something basic that I'm mucking up. Any help would be greatly appreciated.


Solution

  • my $cell = $worksheet->get_cell($currentRow, $currentCol);
    
    my $value = $cell ? $cell->value : undef;