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

Sheet index being changed when multiple sheets are used in formula #1495

Closed
yobokkie opened this issue May 28, 2020 · 3 comments
Closed

Sheet index being changed when multiple sheets are used in formula #1495

yobokkie opened this issue May 28, 2020 · 3 comments

Comments

@yobokkie
Copy link

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 calculating the value of a cell that uses a cell on another sheet in the workbook, the next cell lookup should stay on the original sheet, so that an implicitly set request to set the active sheet remains current.

What is the current behavior?

When running a formula that looks up a value on another sheet in the workbook, and that value in turn looks up a value on the original sheet, it seems that the active sheet is being set to that sheet, instead of returning to the original sheet that was active.

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';

// Create new Spreadsheet object
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('testsheet.xlsx');
$cell = 'A1';
$spreadsheet->setActiveSheetIndex(0);
echo 'sheetindex ' . $spreadsheet->getActiveSheetIndex() . '<br/>';
echo 'value ' . $spreadsheet->getActiveSheet()->getCell($cell)->getCalculatedValue() . '<br/>';
echo 'sheetindex ' . $spreadsheet->getActiveSheetIndex();

In order to replicate the issue, a spreadsheet called 'testsheet' should be created, with 2 sheets. In the first sheet, there should be three cells filled in with cell A1 looking up a value on the other sheet. The second sheet should have a formula that looks up and adds the other two values from the first sheet.

The expected output after this should be that the sheet index is still zero, but it will instead echo out the value of the other sheets index, showing that it has changed internally.
testsheet.xlsx

sheetindex 0
value 246
sheetindex 1

Which versions of PhpSpreadsheet and PHP are affected?

Tested on phpspreadsheet 1.12 and php 7.4.6

@yobokkie
Copy link
Author

I am reasonably certain this issue was not present on older versions of phpspreadsheet, as I discovered it after upgrading and suddenly formulas and lookups that used to work, stopped working. I eventually resorted to explicitly setting the sheet before each command, something I never needed to do before.

@oleibman oleibman mentioned this issue May 31, 2020
5 tasks
MarkBaker pushed a commit that referenced this issue Jun 19, 2020
#1495 reports that ActiveSheet can change when calculation
involves jumping around between sheets.
Save index before calculation, restore after, add test.
@stale
Copy link

stale bot commented Jul 27, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Jul 27, 2020
@stale stale bot closed this as completed Aug 8, 2020
@oleibman oleibman removed the stale label Jul 19, 2024
@oleibman
Copy link
Collaborator

Fixed by PR #1500.

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

2 participants