'How to get result of the formula on import xlsx with maatwebsite using Laravel? I get formula and not value of the formula

Please advise! During import of xlsx file I get formula and not result of the Formula.

Changed config file excel.php to set 'calculate'=> true, on imports and still doesn't work.

MyModel

namespace App\Imports;

use App\ImpData;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;

class ImpDataImport implements ToModel
{
    public function model(array $row)
    {
        return new ImpData([
            'f1' => $row[1],
            'f2' => $row[2],
            'f3' => $row[3],
            'f4' => $row[4],
            'f5' => $row[5],
            'f6' => $row[6],
            'f7' => $row[7],
            'f8' => $row[8],
            'f9' => $row[9],
            'f10' => $row[10],
            'f11' => $row[11],
            'f12' => $row[12],
            'f13' => $row[13],
            'f14' => $row[14],
            'f15' => $row[15],
            'f16' => $row[16],
            'f17' => $row[17],
            'f18' => $row[18]
        ]);
    }
}

In My Controller

$data = Excel::toArray(new ImpDataImport(), $file);

Source file:

A1 = '1'
A2 = '2'
A3 = '=A1+B1'

After import I get:

array:1 [▼
  0 => array:1 [▼
    0 => array:3 [▼
      0 => 1.0
      1 => 2.0
      2 => "=A1+B1"
    ]
  ]
]


Solution 1:[1]

You need to define your class as

class ImpDataImport implements ToModel, WithCalculatedFormulas

for it to work. Just added this for any future viewers of this question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karl Edwall