Laravel-excel: [질문] 빈 행을 κ±΄λ„ˆ λ›°λŠ” 방법 (3.1)

에 λ§Œλ“  2018λ…„ 10μ›” 29일  Β·  12μ½”λ©˜νŠΈ  Β·  좜처: Maatwebsite/Laravel-Excel

  • νŒ¨ν‚€μ§€ 버전: 3.1

μ„€λͺ…

λ¬Έμ„œμ—μ„œ 빈 행을 κ±΄λ„ˆλ›°λŠ” 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

more information needed

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

μ•ˆλ…•ν•˜μ„Έμš”,
$row->filter()->isNotEmpty()λ₯Ό μ‚¬μš©ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ WithValidation을 μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— μ½”λ“œκ°€ μœ νš¨μ„± 검사 였λ₯˜μ™€ ν•¨κ»˜ μ€‘μ§€λ©λ‹ˆλ‹€. λ‚΄ κ·œμΉ™μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

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

λΆˆν–‰νžˆλ„, Excel 파일이 μžˆλŠ” 경우 Excel 파일 끝에 μžˆλŠ” 일뢀 ν–‰μ˜ λ‚΄μš©μ„ μ‚­μ œν•˜λ©΄ Laravel-Excel은 μ—¬μ „νžˆ ​​해당 행을 λΉ„μ–΄ μžˆμ§€ μ•Šμ€ κ²ƒμœΌλ‘œ μΈμ‹ν•©λ‹ˆλ‹€. 그런 λ‹€μŒ μœ νš¨μ„± 검사가 μ‹€νŒ¨ν•©λ‹ˆλ‹€. μœ νš¨μ„± 검사 전에 μ΄λŸ¬ν•œ 행을 κ±΄λ„ˆλ›°κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“  12 λŒ“κΈ€

티켓을 μ œμΆœν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. λΆˆν–‰νžˆλ„ κ·€ν•˜κ°€ μ œκ³΅ν•œ μ •λ³΄λŠ” λΆˆμ™„μ „ν•©λ‹ˆλ‹€. μš°λ¦¬λŠ” 당신이 μ‚¬μš©ν•˜λŠ” 버전과 그것을 μž¬μƒμ‚°ν•˜λŠ” 방법을 μ•Œμ•„μ•Ό ν•©λ‹ˆλ‹€. μ½”λ“œ 예제λ₯Ό ν¬ν•¨ν•˜μ‹­μ‹œμ˜€. μˆ˜λ Ήν•˜κΈ° 전에 (https://github.com/Maatwebsite/Laravel-Excel/blob/3.0/.github/ISSUE_TEMPLATE.md) λˆ„λ½λœ 정보λ₯Ό ν™•μΈν•˜κ³  μΆ”κ°€ν•˜μ„Έμš”. 이 티켓을 훨씬 μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆλ„λ‘ (https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/contributing) 문제 ν…œν”Œλ¦Ώμ„ μž‘μ„±ν–ˆλŠ”μ§€ λ‹€μ‹œ ν™•μΈν•˜μ‹­μ‹œμ˜€. λ°”λ₯΄κ²Œ. μ΄λ ‡κ²Œ ν•˜λ©΄ 보닀 효율적으둜 티켓을 μˆ˜λ Ήν•  수 μžˆμŠ΅λ‹ˆλ‹€. 지침을 μ˜¬λ°”λ₯΄κ²Œ λ”°λ₯΄λŠ” λ¬Έμ œλŠ” λ‹€λ₯Έ λ¬Έμ œλ³΄λ‹€ μš°μ„ ν•©λ‹ˆλ‹€.

#1834의 볡제

ToCollection λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ if($row->filter()->isNotEmpty()) λ‚΄μ˜ λͺ¨λ“  ν•­λͺ©μ„ λž˜ν•‘ν•©λ‹ˆλ‹€.
```
곡개 ν•¨μˆ˜ μ»¬λ ‰μ…˜(μ»¬λ ‰μ…˜ $rows)
{
foreach($rowλ₯Ό $row둜) {
if($row->filter()->isNotEmpty()){
// λ‘œμ§μ€ μ—¬κΈ°λ‘œ 갈 수 μžˆμŠ΅λ‹ˆλ‹€.

            $user = User::create([
                'name' => ucwords($row['name']),
                'class' => $row['class'],
                ...
            ]);
        }
    }   
}

```

toModel λ°©μ‹μ—μ„œλŠ” μ–΄λ–»κ²Œ μž‘λ™ν•©λ‹ˆκΉŒ?

toModelμ—μ„œ null을 λ°˜ν™˜ν•˜λ©΄ ν•΄λ‹Ή 행을 κ±΄λ„ˆλ›Έ 수 μžˆμŠ΅λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”,
$row->filter()->isNotEmpty()λ₯Ό μ‚¬μš©ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ WithValidation을 μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— μ½”λ“œκ°€ μœ νš¨μ„± 검사 였λ₯˜μ™€ ν•¨κ»˜ μ€‘μ§€λ©λ‹ˆλ‹€. λ‚΄ κ·œμΉ™μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

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

λΆˆν–‰νžˆλ„, Excel 파일이 μžˆλŠ” 경우 Excel 파일 끝에 μžˆλŠ” 일뢀 ν–‰μ˜ λ‚΄μš©μ„ μ‚­μ œν•˜λ©΄ Laravel-Excel은 μ—¬μ „νžˆ ​​해당 행을 λΉ„μ–΄ μžˆμ§€ μ•Šμ€ κ²ƒμœΌλ‘œ μΈμ‹ν•©λ‹ˆλ‹€. 그런 λ‹€μŒ μœ νš¨μ„± 검사가 μ‹€νŒ¨ν•©λ‹ˆλ‹€. μœ νš¨μ„± 검사 전에 μ΄λŸ¬ν•œ 행을 κ±΄λ„ˆλ›°κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

@seven21 , 이에 λŒ€ν•œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆκΉŒ? λˆ„κ΅¬λ“ μ§€μ΄ 문제λ₯Ό ν•΄κ²° ν–ˆμŠ΅λ‹ˆκΉŒ?

제 κ²½μš°μ— 도움이 λ˜λŠ” ν•΄κ²° 방법을 μ‚¬μš©ν•©λ‹ˆλ‹€. μœ νš¨μ„± κ²€μ‚¬λŠ” 였λ₯˜λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ λͺ¨λ“  ν–‰μ—μ„œ nullable둜 ν™•μž₯된 λ‹€μŒ λ‚΄κ°€ μΆ”κ°€ν•œ λ°©λ²•μ—μ„œ
public function model(array $row) {if(!array_filter($row)) { return null;} return new Customer([..... 및 이 μ‹œμ μ—μ„œ 이 행을 κ±΄λ„ˆλœλ‹ˆλ‹€. μœ μš©ν•œ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό λ°˜ν™˜ν•  μˆ˜λŠ” μ—†μ§€λ§Œ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” κ°€μž₯ 쒋은 방법을 찾지 λͺ»ν–ˆκΈ° λ•Œλ¬Έμ— 이에 λ§Œμ‘±ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@seven21 μΈμŠ€ν„΄μŠ€μ™€ μœ μš©ν•œ λ©”μ‹œμ§€μ— λŒ€ν•΄ λŒ€λ‹¨νžˆ κ°μ‚¬ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ‹€μ œλ‘œ μœ νš¨μ„± 검사λ₯Ό μ‚¬μš©ν•˜λŠ” κ²½μš°μ—λ„ 이것이 쒋은 방법이 μ•„λ‹ˆλΌκ³  μƒκ°ν•©λ‹ˆλ‹€.

@mohamednizar 이 문제λ₯Ό μ–΄λ–»κ²Œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆκΉŒ?

이 문제λ₯Ό μ™„μ „νžˆ ν•΄κ²°ν•˜μ§€λŠ” λͺ»ν–ˆμ§€λ§Œ λ‚΄ μ†”λ£¨μ…˜μ„ μœ„ν•΄ λ‹€μŒκ³Ό 같은 μ‚¬μš©μž 지정 ν•¨μˆ˜λ₯Ό μΆ”κ°€ν•˜μ—¬ 배치 크기와 ν–‰ μ œν•œμ„ λ°˜ν™˜ν–ˆμŠ΅λ‹ˆλ‹€. 열에 데이터가 μžˆλŠ” κ²½μš°μ—λ§Œ ν–‰ μ œν•œμ΄ κ³„μ‚°λ©λ‹ˆλ‹€.

 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;
        }
    }

λ•Œλ•Œλ‘œ 이것은 μΌμ’…μ˜ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 도움이 될 수 μžˆμŠ΅λ‹ˆλ‹€.

config/excel.php νŒŒμΌμ„ μ—…λ°μ΄νŠΈν•˜μ—¬ 빈 행을 λ¬΄μ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ—μ„œ:

        /*
        |--------------------------------------------------------------------------
        | 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,

μ—κ²Œ:

        /*
        |--------------------------------------------------------------------------
        | 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,

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰