PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.3k stars 3.44k forks source link

Uncaught PhpOffice\PhpSpreadsheet\Exception: Spreadsheet objects cannot be json encoded in \app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php:1519 #4079

Open konsoft1 opened 3 months ago

konsoft1 commented 3 months ago

This is:

- [v] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

I want no error execution.

What is the current behavior?


Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Spreadsheet objects cannot be json encoded in E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php:1519 Stack trace:

0 [internal function]: PhpOffice\PhpSpreadsheet\Spreadsheet->jsonSerialize()

1 E:\Work\sanarena\project\invctrlpan\system\Format\JSONFormatter.php(42): json_encode(Array, 960, 512)

2 E:\Work\sanarena\project\invctrlpan\system\API\ResponseTrait.php(357): CodeIgniter\Format\JSONFormatter->format(Array)

3 E:\Work\sanarena\project\invctrlpan\system\API\ResponseTrait.php(103): CodeIgniter\Debug\ExceptionHandler->format(Array)

4 E:\Work\sanarena\project\invctrlpan\system\Debug\ExceptionHandler.php(83): CodeIgniter\Debug\ExceptionHandler->respond(Array, 500)

5 E:\Work\sanarena\project\invctrlpan\system\Debug\Exceptions.php(163): CodeIgniter\Debug\ExceptionHandler->handle(Object(ReflectionException), Object(CodeIgniter\HTTP\IncomingRequest), Object(CodeIgniter\HTTP\Response), 500, 1)

6 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ReflectionException))

7 {main}

thrown in E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.phpon line1519
{ "title": "ErrorException", "type": "ErrorException", "code": 500, "message": "Uncaught PhpOffice\PhpSpreadsheet\Exception: Spreadsheet objects cannot be json encoded in E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php:1519\nStack trace:\n#0 [internal function]: PhpOffice\PhpSpreadsheet\Spreadsheet->jsonSerialize()\n#1 E:\Work\sanarena\project\invctrlpan\system\Format\JSONFormatter.php(42): json_encode(Array, 960, 512)\n#2 E:\Work\sanarena\project\invctrlpan\system\API\ResponseTrait.php(357): CodeIgniter\Format\JSONFormatter->format(Array)\n#3 E:\Work\sanarena\project\invctrlpan\system\API\ResponseTrait.php(103): CodeIgniter\Debug\ExceptionHandler->format(Array)\n#4 E:\Work\sanarena\project\invctrlpan\system\Debug\ExceptionHandler.php(83): CodeIgniter\Debug\ExceptionHandler->respond(Array, 500)\n#5 E:\Work\sanarena\project\invctrlpan\system\Debug\Exceptions.php(163): CodeIgniter\Debug\ExceptionHandler->handle(Object(ReflectionException), Object(CodeIgniter\HTTP\IncomingRequest), Object(CodeIgniter\HTTP\Response), 500, 1)\n#6 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ReflectionException))\n#7 {main}\n thrown\n【Previous Exception】\nReflectionException\nClass \"Psr\SimpleCache\CacheInterface\" does not exist\n#0 E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Settings.php(147): ReflectionClass->construct('Psr\\SimpleCache...')\n#1 E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Settings.php(139): PhpOffice\PhpSpreadsheet\Settings::useSimpleCacheVersion3()\n#2 E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Collection\CellsFactory.php(18): PhpOffice\PhpSpreadsheet\Settings::getCache()\n#3 E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Worksheet\Worksheet.php(335): PhpOffice\PhpSpreadsheet\Collection\CellsFactory::getInstance(Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet))\n#4 E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php(409): PhpOffice\PhpSpreadsheet\Worksheet\Worksheet->construct(Object(PhpOffice\PhpSpreadsheet\Spreadsheet))\n#5 E:\Work\sanarena\project\invctrlpan\app\Controllers\Document\Download.php(151): PhpOffice\PhpSpreadsheet\Spreadsheet->__construct()\n#6 E:\Work\sanarena\project\invctrlpan\system\CodeIgniter.php(933): App\Controllers\Document\Download->getExportXml('8')\n#7 E:\Work\sanarena\project\invctrlpan\system\CodeIgniter.php(509): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Document\Download))\n#8 E:\Work\sanarena\project\invctrlpan\system\CodeIgniter.php(355): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)\n#9 E:\Work\sanarena\project\invctrlpan\system\Boot.php(325): CodeIgniter\CodeIgniter->run()\n#10 E:\Work\sanarena\project\invctrlpan\system\Boot.php(67): CodeIgniter\Boot::runCodeIgniter(Object(CodeIgniter\CodeIgniter))\n#11 E:\Work\sanarena\project\invctrlpan\public\index.php(56): CodeIgniter\Boot::bootWeb(Object(Config\Paths))\n#12 {main}", "file": "E:\Work\sanarena\project\invctrlpan\app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php", "line": 1519, "trace": [ { "function": "shutdownHandler", "class": "CodeIgniter\Debug\Exceptions", "type": "->", "args": [] } ] }

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:


<?php

// CodeIgniter code
namespace App\Controllers\Document;

use App\Controllers\BaseController;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Download extends BaseController
{
    public function getExportXml($doc_id) {

// add code that show the issue here...
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// rest of the file
        return 'success';
    }
}

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

### What features do you think are causing the issue

PhpSpreadsheet.php file itself

### Does an issue affect all spreadsheet file formats? If not, which formats are affected?
Yes, I cannot even create an PhpSpreadsheet Object.

### Which versions of PhpSpreadsheet and PHP are affected?
PhpSpreadsheet 2.1.0
PHP 8.2.12
oleibman commented 3 months ago

Behavior is deliberate - Spreadsheet objects cannot be serialized or json encoded. See PR #3199.

konsoft1 commented 3 months ago

But I never serialized or json encoded the Spreadsheet object. I just created the object.

oleibman commented 3 months ago

Something is calling json_encode. But it is not called anywhere inside PhpSpreadsheet. I see the following external JSON calls in your stack trace.

#1 E:\Work\sanarena\project\invctrlpan\system\Format\JSONFormatter.php(42): json_encode(Array, 960, 512)
#2 E:\Work\sanarena\project\invctrlpan\system\API\ResponseTrait.php(357): CodeIgniter\Format\JSONFormatter->format(Array)

Is it possible that you have a Spreadsheet object in your array when this code is invoked?