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

INDIRECT function not working with Defined names in excel #1993

Closed
Lzolcsi opened this issue Apr 8, 2021 · 4 comments
Closed

INDIRECT function not working with Defined names in excel #1993

Lzolcsi opened this issue Apr 8, 2021 · 4 comments

Comments

@Lzolcsi
Copy link

Lzolcsi commented Apr 8, 2021

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)

I have an excel file that uses defined names and I use the INDIRECT function to reference them, but this throws an error when trying to access the calculated value of the cell.

What is the expected behavior?

-return the correct value (300 in this case)

What is the current behavior?

PhpSpreadsheet throws the following Exceptions:

PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception: Worksheet!B3 -> Invalid cell coordinate =B1 in /Users/zolcsi/www/szamlazo/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php:272
Stack trace:
#0 /Users/zolcsi/www/szamlazo/bin/inc/modules/Project.php(5014): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#1 /Users/zolcsi/www/szamlazo/bin/inc/modules/Project.php(155): Project->XlsGyartoSzallitoMegfeleltetes()
#2 /Users/zolcsi/www/szamlazo/bin/inc/common.php(48): Project->Execute()
#3 /Users/zolcsi/www/szamlazo/html/index.php(19): require_once('/Users/zolcsi/w...')
#4 {main}
thrown in /Users/zolcsi/www/szamlazo/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php on line 272

Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception: Worksheet!B3 -> Invalid cell coordinate =B1 in /Users/zolcsi/www/szamlazo/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php:272
Stack trace:
#0 /Users/zolcsi/www/szamlazo/bin/inc/modules/Project.php(5014): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#1 /Users/zolcsi/www/szamlazo/bin/inc/modules/Project.php(155): Project->XlsGyartoSzallitoMegfeleltetes()
#2 /Users/zolcsi/www/szamlazo/bin/inc/common.php(48): Project->Execute()
#3 /Users/zolcsi/www/szamlazo/html/index.php(19): require_once('/Users/zolcsi/w...')
#4 {main}
thrown in /Users/zolcsi/www/szamlazo/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php on line 272

What are the steps to reproduce?

<?php

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


$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'EUR');
$sheet->setCellValue('A2', 'USD');
$sheet->setCellValue('A3', 'EUR');
$sheet->setCellValue('B1', 360);
$sheet->setCellValue('B2', 300);

$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('EUR', $sheet, '=$B$1') );
$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('USD', $sheet, '=$B$2') );
$sheet->setCellValue('B3', '=INDIRECT("USD")');
var_dump($sheet -> getCell('B3') -> getCalculatedValue());


### Which versions of PhpSpreadsheet and PHP are affected?
PhpSpreadsheet 1.17.1
PHP 7.4.16
@oleibman
Copy link
Collaborator

oleibman commented Apr 8, 2021

Not the same problem as #1913, but same function is involved.

@MarkBaker
Copy link
Member

Issue here is that the implementation of INDIRECT() only works with actual cell references, and isn't defined name-aware. Need to look at making it so

MarkBaker pushed a commit that referenced this issue Apr 20, 2021
* Improved Support for INDIRECT, ROW, and COLUMN Functions

This should address issues #1913 and #1993. INDIRECT had heretofore not supported an optional parameter intended to support addresses in R1C1 format which was introduced with Excel 2010. It also had not supported the use of defined names as an argument. This PR is a replacement for #1995, which is currently in draft status and which I will close in a day or two.

The ROW and COLUMN functions also should support defined names. I have added that, and test cases, with the latest push. ROWS and COLUMNS already supported it correctly, but there had been no test cases. Because ROW and COLUMN can return arrays, and PhpSpreadsheet does not support dynamic arrays, I left the existing direct-call tests unchanged to demonstrate those capabilities.

The unit tests for INDIRECT had used mocking, and were sorely lacking (tested only error conditions). They have been replaced with normal, and hopefully adequate, tests. This includes testing globally defined names, as well as locally defined names, both in and out of scope.

The test case in 1913 was too complicated for me to add as a unit test. The main impediments to it are now removed, and its complex situation will, I hope, be corrected with this fix.

INDIRECT can also support a reference of the form Sheetname!localName when localName on its own would be out of scope. That functionality is added. It is also added, in theory, for ROW and COLUMN, however such a construction is rejected by the Calculation engine before passing control to ROW or COLUMN. It might be possible to change the engine to allow this, and I may want to look into that later, but it seems much too risky, and not nearly useful enough, to attempt to address that as part of this change.

Several unusual test cases (leading equals sign, not-quite-as-expected name definition in file, complex indirection involving concatenation and a dropdown list) were suggested by @MarkBaker and are included in this request.
@stale
Copy link

stale bot commented Jun 26, 2021

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 Jun 26, 2021
@oleibman
Copy link
Collaborator

Fixed by PR 2004.

@oleibman oleibman removed the stale label Jul 3, 2024
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

3 participants