0

I'm trying to read some cell values from a .xlsx file using PhpSpreadSheet, but I don't know why this isn't working. The file opens but when I read the values of the cell it returns nothing. I have checked that the filename is correct and that cells have data. I want to read the SECOND sheet of the file, that why I have used getSheet(1).

This is my code:

use PhpOffice\PhpSpreadsheet\IOFactory;   

$inputFileType = 'Xlsx';
$inputFileName = 'Test.xlsx';


$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheet = $spreadsheet->getSheet(1);


$filterHours = $sheet->getCell('D309')->getOldCalculatedValue();
$otherHour = $sheet->getCell('D295')->getOldCalculatedValue();
$title = $sheet->getCell('B297')->getOldCalculatedValue();

printf($title);
printf($filterHours);
printf($otherHour);

EDIT: If I use getCalculatedValue() instead of getOldCalculatedValue() I get this error:

Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception:
Configurador!D309 -> Configurador!D745 -> Configurador!D755 -> Configurador!C776 -> Formula Error: An unexpected error occured in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php:274
Stack trace: #0 C:\xampp\htdocs\OfferConfigurator\php\generateDocumentation\prueba.php(26): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#1 {main} thrown in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php on line 274

Can that be because Configurador!C776 has a formula that references to another sheet?

nngeek
  • 1,912
  • 16
  • 24
d1649356
  • 91
  • 1
  • 9
  • Are you sure you're reading the correct sheet? The first sheet will be 0. – user2924019 Mar 18 '20 at 09:32
  • I know, but I want to read the second sheet. That´s why the index is 1 – d1649356 Mar 18 '20 at 09:35
  • Just checking as it's a simple mistake I would make. – user2924019 Mar 18 '20 at 09:37
  • I would suggest testing "Looping through cells" on this page: https://phpspreadsheet.readthedocs.io/en/latest/topics/accessing-cells/#looping-through-cells – user2924019 Mar 18 '20 at 09:40
  • it will be helpful to provide the content of your excel file and also the output of your code. – nngeek Mar 18 '20 at 09:59
  • @nngeek Sorry, but I can´t provide the content of the excel due to confidential issues. But I can tell you that the cells that I wanted to read are related with cells from another sheet. Can that be the problem? – d1649356 Mar 18 '20 at 10:06
  • Sometimes by multible redirection of the Cell errors occur can you provide the content of dthe cell D309 and all there called formulas and values? – Collie-IT Anne K. Frey Mar 19 '20 at 18:08

1 Answers1

-1

Try degrading your Phpspreadsheet version to 1.8.2.

double-beep
  • 5,031
  • 17
  • 33
  • 41