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

"!" characters in Worksheet title will make reference formulas crash #325

Closed
hampusiggstrom opened this issue Jan 9, 2018 · 8 comments
Closed

Comments

@hampusiggstrom
Copy link

hampusiggstrom commented Jan 9, 2018

This is:

- [X] a bug report
- [ ] a feature request
- [X] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

The calculation should be valid. The "!" needs to be added to $invalidCharacters or calculations needs to be updated to support exclamation mark characters in sheet titles.

What is the current behavior?

Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception: Title!!C1 -> Cell coordinate can not be zero-length string

What are the steps to reproduce?

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Title!');
$sheet->fromArray([1, 2,'=SUM(A1:A2)']);

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

Which versions of PhpSpreadsheet and PHP are affected?

all

@hampusiggstrom
Copy link
Author

hampusiggstrom commented Jan 9, 2018

Relevant code lines seem to be:

if (strpos($operand1Data['reference'], '!') !== false) {
list($sheet1, $operand1Data['reference']) = explode('!', $operand1Data['reference']);
} else {
$sheet1 = ($pCellParent !== null) ? $pCellWorksheet->getTitle() : '';
}
if (strpos($operand2Data['reference'], '!') !== false) {
list($sheet2, $operand2Data['reference']) = explode('!', $operand2Data['reference']);
} else {
$sheet2 = $sheet1;
}

$operand1Data['reference'] and $operand2Data['reference'] will be calculated to empty strings. Given the sheet title Title!

@PowerKiKi
Copy link
Member

Can confirm it will crash even wihtout saving like so:

<?php

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

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Title!a');
$sheet->fromArray([1, 2,'=SUM(A1:A2)']);
$sheet->getCell('C1')->getCalculatedValue();

@stale
Copy link

stale bot commented Mar 22, 2018

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 Mar 22, 2018
@hampusiggstrom
Copy link
Author

Bug still present in latest release

@stale stale bot removed the stale label Mar 23, 2018
@stale
Copy link

stale bot commented May 22, 2018

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 May 22, 2018
@catseyes250
Copy link

I'm having this issue as well.
When trying to reference another worksheet within a formula, I get an exception thrown.

$spreadsheet->getActiveSheet() ->setCellValue('A2', '=SUM("Handset Activity"!C:C)*"Handset Activity"!D:D');

Exception:
Fatal error: Uncaught exception 'PhpOffice\PhpSpreadsheet\Calculation\Exception' with message 'Totals!A2 -> Totals!C2 -> undefined variable '!''

@stale stale bot removed the stale label May 25, 2018
@stale
Copy link

stale bot commented Jul 24, 2018

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 24, 2018
@stale stale bot closed this as completed Jul 31, 2018
@PowerKiKi
Copy link
Member

Would still be nice if somebody can come up with a PR for this issue

@PowerKiKi PowerKiKi reopened this Aug 7, 2018
@stale stale bot removed the stale label Aug 7, 2018
gurrumpalad pushed a commit to gurrumpalad/PhpSpreadsheet that referenced this issue Sep 6, 2018
…HPOffice#325

 Bugfix: replace explode by "!" with special function, that extracts sheet name from reference
gurrumpalad pushed a commit to gurrumpalad/PhpSpreadsheet that referenced this issue Sep 6, 2018
…s crash PHPOffice#325  Bugfix: replace explode by "!" with special function, that extracts sheet name from reference"

This reverts commit b349ea3
gurrumpalad pushed a commit to gurrumpalad/PhpSpreadsheet that referenced this issue Sep 6, 2018
…HPOffice#325

 Bugfix: replace explode by "!" with special function, that extracts sheet name from reference
gurrumpalad pushed a commit to gurrumpalad/PhpSpreadsheet that referenced this issue Sep 6, 2018
gurrumpalad pushed a commit to gurrumpalad/PhpSpreadsheet that referenced this issue Oct 2, 2018
…as crash PHPOffice#325

 adding unit test for Worksheet::extractSheetTitle
Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this issue Nov 20, 2018
When extracting sheet title from string reference (like `"Work!sheet1!A1"`), PHP function `explode()` divide this string into three parts: `['Work', 'sheet1', 'A1']`. And then these wrong values are used in formulas, ranges, etc.

This change fix that problem by using special function `Worksheet::extractSheetTitle()`. This function also has been changed to make sure that worksheet title can contain "!" character. So, that function search last position of "!" in reference string and divide it to 2 parts correctly: `['Work!sheet1', 'A1']`.

Fixes PHPOffice#325
Fixes PHPOffice#662
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants