Skip to content

Sheet does not exist When removing sheet #2266

@Grandpied33

Description

@Grandpied33

This is:

- [x] 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?

When I want to remove sheets from an exsiting file, when saving the following error appear : "Sheet does not exist"

What is the current behavior?

While saving the code stop working and return an error

What are the steps to reproduce?

The code is run inside a Symfony project. I call a PHP file where I can complete my file with data then I want to remove them by using the folowing code

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

public function removeSheets($spreadsheet, $uuid, $path)
    {
        /**
         * @var $worksheet Worksheet
         */

        /**
         * @var $spreadsheet Spreadsheet
         */

        $sheetIndex = $spreadsheet->getIndex(
            $spreadsheet->getSheetByName('Sheet2')
        );
        $spreadsheet->removeSheetByIndex($sheetIndex);
        $sheetIndex = $spreadsheet->getIndex(
            $spreadsheet->getSheetByName('Sheet2')
        );
        $spreadsheet->removeSheetByIndex($sheetIndex);
        $worksheet = $spreadsheet->getActiveSheet();

        try {
            $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
            $writer->save($path.'/'.$uuid.'.xlsx');
        }catch (Exception $e)
        {
            echo $e;
        }
        return $uuid;

    }
<?php

The code stop runnig on saving with no more informations

Which versions of PhpSpreadsheet and PHP are affected?

"phpoffice/phpspreadsheet": "^1.17"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions