Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument 1 passed to PhpOffice\PhpSpreadsheet\Shared\Font::getTextWidthPixelsExact() must be of the type string, null given #2165

Closed
tbl0605 opened this issue Jun 16, 2021 · 2 comments

Comments

@tbl0605
Copy link

tbl0605 commented Jun 16, 2021

Hi,
this is a bug report.

What is the expected behavior?

There should be no exception thrown on $activeSheet->setCellValueByColumnAndRow(1, 1, null); when used with PhpOffice\PhpSpreadsheet\Shared\Font::setAutoSizeMethod(PhpOffice\PhpSpreadsheet\Shared\Font::AUTOSIZE_METHOD_EXACT); and $activeSheet->getColumnDimension(...)->setAutoSize(true);

Replacing null value by "" will solve the problem, but I have too many old programs using null values and it would be painful to modify them all to wokaround this issue.
So I think (for backward compatibility) that PhpSpreadsheet should add the missing $value ?? "" check here in its code.

Thank you!

What is the current behavior?

An exception is thrown (the output comes from version 1.18.0):

Argument 1 passed to PhpOffice\PhpSpreadsheet\Shared\Font::getTextWidthPixelsExact() must be of the type string, null given, called in src\PhpSpreadsheet\Shared\Font.php on line 254
at src\PhpSpreadsheet\Shared\Font.php(277)
#0 src\PhpSpreadsheet\Shared\Font.php(254): PhpOffice\PhpSpreadsheet\Shared\Font::getTextWidthPixelsExact(NULL, Object(PhpOffice\PhpSpreadsheet\Style\Font), 0)
#1 src\PhpSpreadsheet\Worksheet\Worksheet.php(766): PhpOffice\PhpSpreadsheet\Shared\Font::calculateColumnWidth(Object(PhpOffice\PhpSpreadsheet\Style\Font), NULL, 0, Object(PhpOffice\PhpSpreadsheet\Style\Font))
#2 src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(369): PhpOffice\PhpSpreadsheet\Worksheet\Worksheet->calculateColumnWidths()
#3 src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(67): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCols(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet))
#4 src\PhpSpreadsheet\Writer\Xlsx.php(377): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeWorksheet(Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), Array, false)

What are the steps to reproduce?

Here is a minimal PHP script to reproduce the problem:

<?php
require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath('/path/to/some/fonts/');
PhpOffice\PhpSpreadsheet\Shared\Font::setAutoSizeMethod(PhpOffice\PhpSpreadsheet\Shared\Font::AUTOSIZE_METHOD_EXACT);

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$activeSheet = $spreadsheet->createSheet();
$activeSheet->setTitle('Feuil1');
$spreadsheet->setActiveSheetIndexByName('Feuil1');

// Replacing null by '' will solve the problem:
$activeSheet->setCellValueByColumnAndRow(1, 1, null);
$activeSheet->getColumnDimension(\PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(1))->setAutoSize(true);

$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$objWriter->save('C:\\temp\\output.xlsx');
?>

Which versions of PhpSpreadsheet and PHP are affected?

version 1.18.0 and master branch

@cwolcott
Copy link

I recently noticed the same thing. I made a quick change to Shared/Font.php and added a line at 254

if ($cellText == null) $cellText = '';

Please consider making an appropriate change to just bypass the try/catch block at line 251 or making the addition described above.

@PowerKiKi
Copy link
Member

Fixed in 1.19.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants