-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
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?
According to the documentation Worksheet::getActiveSheet() should always return "the one that will be active when the workbook is opened in MS Excel (or other appropriate Spreadsheet program)."
What is the current behavior?
Currenty, the last Sheet is always marked as the Active Sheet after the file was loaded:
Worksheet:676iterates over the file's SheetsWorksheet:713loads conditional styles for the currently processed SheetConditionalStyles:36delegates the loading to thesetConditionalStylesmethodConditionalStyles:174callsWorksheet::getStylemethodWorksheet:1422callssetActiveSheetIndexmethod
Assuming that all the Sheets are always loaded and that they are loaded in the order of the file's Sheets, the last Sheet in the file will always be set as the Active Sheet.
This functionality was working correctly in version 1.7.0 which I'm moving from. I've tried to downgrade to 1.15.0 but the issue is also there, so it's been in the code for quite some time. Versions below 1.15.0 are not compatible with PHP 8.0 so I can't go lower. This is a major blocker for me.
The root cause for me is that get getStyle method changes the Active Sheet Index. Generally methods that start with "get" shouldn't change the state of any objects.
Proposed solutions:
- Change the
getStylemethod to not change the data state - Detect and cache the Active Sheet Index prior to any loading and re-set it once the load has been completed
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
require __DIR__ . '/vendor/autoload.php';
// Load Spreadsheet from file that contains multiple Sheets, and the Active Sheet Index in the file != Last Sheet Index of the file
$spreadsheet = IOFactory::load($filename);
//Load the Active Sheet - it should be the actually Active Sheet, but instead it is the Last Sheet
$worksheet = spreadsheet->getActiveSheet();
### Which versions of PhpSpreadsheet and PHP are affected?
- 8.0
- 8.1