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($rows 作为 $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 ,你找到解决方案了吗? 有人解决了这个问题吗?

我使用了一种解决方法,这对我的情况有所帮助。 验证在所有行上都可以为空来扩展以避免错误,然后在我添加的方法中
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 等级

相关问题

alejandri picture alejandri  ·  3评论

thearabbit picture thearabbit  ·  3评论

muhghazaliakbar picture muhghazaliakbar  ·  3评论

octoxan picture octoxan  ·  3评论

amine8ghandi8amine picture amine8ghandi8amine  ·  3评论