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

[Question] How to skip empty rows (3.1) #1861

Closed
mCassy opened this issue Oct 29, 2018 · 13 comments
Closed

[Question] How to skip empty rows (3.1) #1861

mCassy opened this issue Oct 29, 2018 · 13 comments

Comments

@mCassy
Copy link

mCassy commented Oct 29, 2018

  • Package version: 3.1

Description

How to skip empty rows in the doc?

@ghost
Copy link

ghost commented Oct 29, 2018

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.0/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/contributing) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

@patrickbrouwers
Copy link
Member

Duplicate of #1834

@patrickbrouwers patrickbrouwers marked this as a duplicate of #1834 Oct 30, 2018
@Otienoh
Copy link

Otienoh commented Jan 24, 2019

use ToCollection method the wrap everything within if($row->filter()->isNotEmpty())

public function collection(Collection $rows)
   {
      foreach($rows as $row) {
   	 	if($row->filter()->isNotEmpty()){
   			// you logic can go here
      
       		$user = User::create([
               	'name' => ucwords($row['name']),
               	'class' => $row['class'],
   				...
   			]);
   		}
   	}	
   }

@notflip
Copy link

notflip commented Feb 20, 2019

How does it work for the toModel way?

@patrickbrouwers
Copy link
Member

You can return null in toModel, will skip those rows.

@seven21
Copy link

seven21 commented Apr 26, 2019

Hello,
I have tried to use $row->filter()->isNotEmpty() but due to the fact that I use WithValidation the codes stops with an validation error. My rules look like this:

public function rules(): array { return [ 'name' => 'required', '*.name' => 'required', 'email' => 'required|email', '*.email' => 'required|email', 'discount' => 'nullable|numeric', '*.discount' => 'nullable|numeric', ]; }

Unfortunately, if you have an excel file an delete the content of some rows at the end of the excel file Laravel-Excel still recognizes them as not empty. Then the validation fails. I want to skip such rows before validation.

@mohamednizar
Copy link

@seven21 , did you find a solution for this ? does anyone got resolved this issue ?

@seven21
Copy link

seven21 commented Aug 13, 2019

I use a workaround which helps in my case. Validation is extended by nullable on all rows to avoid errors and then In the method I have added
public function model(array $row) {if(!array_filter($row)) { return null;} return new Customer([..... and skip these rows at this point. Not really happy with this as I cannot return an useful error message but as I found no solution the best way to get the code running.

@mohamednizar
Copy link

@seven21 Thank you very much for your instance and useful message, But I'm also thinking this isn't a good way if we actually using validations.

@siarheipashkevich
Copy link
Contributor

siarheipashkevich commented Oct 7, 2019

@mohamednizar how did you resolve this issue?

@mohamednizar
Copy link

I didn't completely resolve the issue, but for my solution I added following custom functions to return the batch size and row limit. the row limit only will be count if data exists in the column.

 public function limit(): int {
        $highestColumn = $this->worksheet->getHighestDataColumn(3);
        $higestRow = 0;
        for ($row = $this->startRow(); $row <= $this->highestRow; $row++) {
            $rowData = $this->worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            if (isEmptyRow(reset($rowData))) {
                continue;
            } else {
                $higestRow += 1;
            }
        }
        return $higestRow;
    }

   public function batchSize(): int {
        $highestColumn = $this->worksheet->getHighestDataColumn(3);
        $higestRow = 1;
        for ($row = $this->startRow(); $row <= $this->highestRow; $row++) {
            $rowData = $this->worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            if (isEmptyRow(reset($rowData))) {
                continue;
            } else {
                $higestRow += 1;
            }
        }
        if ($higestRow == 0) {
            exit;
        } else {
            return $higestRow;
        }
    }

Some time this may help you to resolve some sort of problems.

@MarJose123
Copy link

you can ignore the empty row by updating the config/excel.php file.

from:

        /*
        |--------------------------------------------------------------------------
        | Ignore Empty
        |--------------------------------------------------------------------------
        |
        | When dealing with imports, you might be interested in ignoring
        | rows that have null values or empty strings. By default rows
        | containing empty strings or empty values are not ignored but can be
        | ignored by enabling the setting ignore_empty to true.
        |
        */
        'ignore_empty' => false,

to:

        /*
        |--------------------------------------------------------------------------
        | Ignore Empty
        |--------------------------------------------------------------------------
        |
        | When dealing with imports, you might be interested in ignoring
        | rows that have null values or empty strings. By default rows
        | containing empty strings or empty values are not ignored but can be
        | ignored by enabling the setting ignore_empty to true.
        |
        */
        'ignore_empty' => true,

@jonjieviduya
Copy link

@whoami213 It works. Thanks a lot man

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

No branches or pull requests

9 participants