Skip to content

No Support For R1C1 Style Address in INDIRECT Function #1913

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

Closed
hubobbb opened this issue Mar 10, 2021 · 5 comments
Closed

No Support For R1C1 Style Address in INDIRECT Function #1913

hubobbb opened this issue Mar 10, 2021 · 5 comments

Comments

@hubobbb
Copy link

hubobbb commented Mar 10, 2021

This is:

- [X ] a bug report


### What is the expected behavior?
fixed

### What is the current behavior?

save file error 
{ "code": 500, "message": "error", "data": { "name": "Exception", "message": "Argument 2 passed to PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef::INDIRECT() must be an instance of PhpOffice\\PhpSpreadsheet\\Cell\\Cell or null, bool given", "code": 0, "type": "TypeError", "file": "/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php", "line": 276, "stack-trace": [ "#0 [internal function]: PhpOffice\\PhpSpreadsheet\\Calculation\\LookupRef::INDIRECT()", "#1 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(4712): call_user_func_array()", "#2 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(3445): PhpOffice\\PhpSpreadsheet\\Calculation\\Calculation->processTokenStack()", "#3 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(3235): PhpOffice\\PhpSpreadsheet\\Calculation\\Calculation->_calculateFormulaValue()", "#4 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(257): PhpOffice\\PhpSpreadsheet\\Calculation\\Calculation->calculateCellValue()", "#5 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1223): PhpOffice\\PhpSpreadsheet\\Cell\\Cell->getCalculatedValue()", "#6 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1293): PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx\\Worksheet->writeCellFormula()", "#7 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(1150): PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx\\Worksheet->writeCell()", "#8 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(70): PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx\\Worksheet->writeSheetData()", "#9 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php(274): PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx\\Worksheet->writeWorksheet()", "#10 /api/modules/tools/Excel.php(576): PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx->save()", ..... ] } }

```php
<?php

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

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$formula2= '=SUM(COUNTIFS(INDIRECT("list!C"&MATCH("teacher_id",INDIRECT("list!R1",FALSE),0),FALSE),">0",INDIRECT("list!C"&MATCH("user_id",INDIRECT("list!R1",FALSE),0),FALSE),">0",INDIRECT("list!C"&MATCH("filed_title",INDIRECT("list!R1",FALSE),0),FALSE),"AAABBB"))';
$spreadsheet ->getActiveSheet()->setCellValueExplicitByColumnAndRow($c, $r, $formula, DataType::TYPE_FORMULA);
$objWrite = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet , 'Xlsx');
$objWrite->save('php://output');

// add code that show the issue here...

Which versions of PhpSpreadsheet and PHP are affected?

PHP7.42, PhpSpreadsheet tag 1.17.1

@hubobbb hubobbb changed the title insert cell with long formula2 is wrong when save file Error: insert cell with long formula when save file Mar 10, 2021
@oleibman
Copy link
Collaborator

oleibman commented Apr 8, 2021

Second argument of INDIRECT is not yet supported "* @todo Support for the optional a1 parameter introduced in Excel 2010". Since it therefore effectively defaults to TRUE where you want FALSE, PhpSpreadsheet could easily be changed to issue a clearer error message, but it will be much harder to get it to support the missing parameter. Would it be difficult for you to use A1 style references rather than R1C1 style, eliminating the need for the optional parameter?

@oleibman oleibman changed the title Error: insert cell with long formula when save file No Support For R1C1 Style Address in INDIRECT Function Apr 8, 2021
@oleibman
Copy link
Collaborator

oleibman commented Apr 8, 2021

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

@oleibman
Copy link
Collaborator

oleibman commented Apr 8, 2021

I believe I have a solution. Your sample code is very complicated and incomplete, in that it contains the formula but not the data. If you were to upload (a subset of) the spreadsheet that is giving you this problem, I could try to incorporate that into my unit tests.

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

Should have been fixed by PR 2004, which was part of Release 18.

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