PhpSpreadsheet - upgrade to PHP 7.4.6 - now setting formula causes 'Trying to access array offset on value of type null' exception

Viewed 622

I know there are many posts with this same error but none of them seem to address this particular issue.

I am upgrading an app using PHPSpreadsheet from PHP 7.3.22 to 7.4.6. PHP 7.4.6 is throwing a null exception error when setting a formula in a cell. The following snippet illustrates the problem:

    $file_name = './uploads/helloworld.xlsx';  
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

    // these lines work with PHP Versions 7.3.22 and 7.4.6
    $spreadsheet->getActiveSheet()->setCellValue('A1', '1'); 
    $spreadsheet->getActiveSheet()->setCellValue('A2', '2'); 

    // this line causes the exception only with PHP Version 7.4.6
    $spreadsheet->getActiveSheet()->setCellValue('A3', "=A1+A2");

    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    
    // the exception gets thrown here
    $writer->save($file_name);

    $spreadsheet->disconnectWorksheets();
    unset($spreadsheet);

Any advice appreciated - thanks - Jon

1 Answers

Here's how I solved this in case it helps someone else who is new to PHP as I am.

I couldn't find this documented anywhere - but there is apparently a compatibility problem with PHP 7.3 and PHPSpreadsheet 1.6.0. So the solution was to just upgrade PHPSpreadsheet. Here are the steps I went through to solve this:

To determine the version I had installed I did this:

composer show 

This showed my installed version as 1.6.0. The current version (as I write this) is 1.8.0.

To upgrade to the newest version I did this:

composer require phpoffice/phpspreadsheet -w --prefer-source

This upgraded PHPSpreadhseet from 1.6.0 to 1.8.0 and this solves the problem.

"...it's times like these that I miss C#!"

Related