Skip to content

Commit

Permalink
Two Problems with Sample19
Browse files Browse the repository at this point in the history
19_NamedRange.php was not changed to use absolute addressing when that was introduced to Named Ranges. Consequently, the output from this sample has been wrong ever since, for both Xls and Xlsx.

There was an additional problem with Xls. It appears that the Xls Writer Parser does not parse multiple concatenations using the ampersand operator correctly. So, `=B1+" "+B2` was parsed as `=B1+" "`. I believe that this is due to ampersand being treated as a condition rather than an operator; `A1>A2>A3` isn't valid, but `A1&A2&A3` is. My original PR (PHPOffice#1992, which I will now close) only partially resolved this, but I think moving ampersand handling from `condition` to `expression` is fully successful.

There are already more than ample tests for Named Ranges, so I did not add a new one for that purpose. However, I did add a new test for the Xls parser problem.
  • Loading branch information
oleibman committed May 9, 2021
1 parent b05dc31 commit 9fed8d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions samples/Basic/19_Namedrange.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

// Define named ranges
$helper->log('Define named ranges');
$spreadsheet->addNamedRange(new NamedRange('PersonName', $spreadsheet->getActiveSheet(), 'B1'));
$spreadsheet->addNamedRange(new NamedRange('PersonLN', $spreadsheet->getActiveSheet(), 'B2'));
$spreadsheet->addNamedRange(new NamedRange('PersonName', $spreadsheet->getActiveSheet(), '$B$1'));
$spreadsheet->addNamedRange(new NamedRange('PersonLN', $spreadsheet->getActiveSheet(), '$B$2'));

// Rename named ranges
$helper->log('Rename named ranges');
Expand Down
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Writer/Xls/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,10 +1149,6 @@ private function condition()
$this->advance();
$result2 = $this->expression();
$result = $this->createTree('ptgNE', $result, $result2);
} elseif ($this->currentToken == '&') {
$this->advance();
$result2 = $this->expression();
$result = $this->createTree('ptgConcat', $result, $result2);
}

return $result;
Expand Down Expand Up @@ -1203,6 +1199,11 @@ private function expression()
return $this->createTree('ptgUplus', $result2, '');
}
$result = $this->term();
while ($this->currentToken === '&') {
$this->advance();
$result2 = $this->expression();
$result = $this->createTree('ptgConcat', $result, $result2);
}
while (
($this->currentToken == '+') ||
($this->currentToken == '-') ||
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xls/Sample19Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;

use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Sample19Test extends AbstractFunctional
{
public function testSample19Xls(): void
{
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Firstname:')
->setCellValue('A2', 'Lastname:')
->setCellValue('A3', 'Fullname:')
->setCellValue('B1', 'Maarten')
->setCellValue('B2', 'Balliauw')
->setCellValue('B3', '=B1 & " " & B2')
->setCellValue('C1', '=A2&A3&A3&A2&B1');

$robj = $this->writeAndReload($spreadsheet, 'Xls');
$sheet0 = $robj->setActiveSheetIndex(0);
// Xls parser eliminates unneeded whitespace
self::assertEquals('=B1&" "&B2', $sheet0->getCell('B3')->getValue());
self::assertEquals('Maarten Balliauw', $sheet0->getCell('B3')->getCalculatedValue());
self::assertEquals('Lastname:Fullname:Fullname:Lastname:Maarten', $sheet0->getCell('C1')->getCalculatedValue());
}
}

0 comments on commit 9fed8d8

Please sign in to comment.