微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel Excel - 如何在不存储到模型的情况下访问映射的单元格

如何解决Laravel Excel - 如何在不存储到模型的情况下访问映射的单元格

我正在尝试从 Excel 文件获取工作表中某些单元格的值。根据文档 (https://docs.laravel-excel.com/3.1/imports/mapped-cells.html) 我需要使用 WithMappedCells 并且他们的示例代码是:

public function mapping(): array
    {
        return [
            'name'  => 'B1','email' => 'B2',];
    }
    
    public function model(array $row)
    {
        return new User([
            'name' => $row['name'],'email' => $row['email'],]);
    }

这是我的电子表格:

enter image description here

在我的导入文件中,我有这个映射

public function mapping(): array
    {
        return [
            'type_key'  => 'A1','isDraft_key' => 'A2','isPublished_key' => 'A3','type'  => 'B1','isDraft' => 'B2','isPublished' => 'B3',];
    }

在他们的 github 上我找到了这个解决方案:https://github.com/Maatwebsite/Laravel-Excel/issues/1836#issuecomment-431808465

public function map($row)
    {
        return new Veiling([
            'name' => $row['name'],'nummer' => $row['nummer'],'info' => $row['info'],]);
    }

然而,这仍然试图将其添加到模型中。我注意到它使用了 $row 所以我试过是这样的:

public function map($row): array {
        Log::info($row['type_key']);
        return $row;
    }

但我收到此错误

未定义索引:type_key

如何简单地从这些特定单元格中获取这些值,以便对它们进行一些检查?

编辑:当我退出 $row 时,我得到:

array (
  0 =>
  array (
    'type' => 'isDraft','report.' => 'TRUE',),0 =>
  array (
    'type' => 'isPublished','report.' => 'FALSE',)

如您所见,数据在那里,但映射不正确。我已经检查过,我正在使用:

use Maatwebsite\Excel\Concerns\WithMappedCells;
use Maatwebsite\Excel\Concerns\WithMapping;

在我的文件顶部,并确保我的类实现这些:

Import Implements WithMapping,WithMappedCells {

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。