Skip to content

Commit 54bbeb5

Browse files
committed
Xlsx Reader Better Namespace Handling Phase 1 First Bugfix
See issue PHPOffice#2203. An undotted i uncrossed t. When using namespaces, need to call attributes() to access the attributes before trying to access them directly. Failure to do so in castToFormula caused problem for shared formulas. Surprisingly, this didn't show up in unit tests. Perhaps sharing the same formula between two cells isn't common. It did show up in Chart Samples. I've added a test. I was really inclined to merge this right away. Not to worry - I can control myself. It should be moved fairly quickly nevertheless.
1 parent eb0cda1 commit 54bbeb5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,16 @@ private static function castToString($c)
312312

313313
private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType): void
314314
{
315+
$attr = $c->f->attributes();
315316
$cellDataType = 'f';
316317
$value = "={$c->f}";
317318
$calculatedValue = self::$castBaseType($c);
318319

319320
// Shared formula?
320-
if (isset($c->f['t']) && strtolower((string) $c->f['t']) == 'shared') {
321-
$instance = (string) $c->f['si'];
321+
if (isset($attr['t']) && strtolower((string) $attr['t']) == 'shared') {
322+
$instance = (string) $attr['si'];
322323

323-
if (!isset($sharedFormulas[(string) $c->f['si']])) {
324+
if (!isset($sharedFormulas[(string) $attr['si']])) {
324325
$sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
325326
} else {
326327
$master = Coordinate::indexesFromString($sharedFormulas[$instance]['master']);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\IOFactory;
6+
use PhpOffice\PhpSpreadsheet\Reader\IReader;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class SharedFormulaTest extends TestCase
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private static $testbook = 'samples/templates/32readwriteAreaChart1.xlsx';
15+
16+
public function testPreliminaries(): void
17+
{
18+
$file = 'zip://';
19+
$file .= self::$testbook;
20+
$file .= '#xl/worksheets/sheet1.xml';
21+
$data = file_get_contents($file);
22+
// confirm that file contains shared formula
23+
if ($data === false) {
24+
self::fail('Unable to read file');
25+
} else {
26+
self::assertStringContainsString('<c r="D6"><f t="shared" ca="1" si="0"/>', $data);
27+
self::assertStringContainsString('<c r="E6"><f t="shared" ca="1" si="0"/>', $data);
28+
}
29+
}
30+
31+
public function testLoadSheetsXlsxChart(): void
32+
{
33+
$filename = self::$testbook;
34+
$reader = IOFactory::createReader('Xlsx');
35+
$spreadsheet = $reader->load($filename, IReader::LOAD_WITH_CHARTS);
36+
$sheet = $spreadsheet->getActiveSheet();
37+
self::assertSame('=(RANDBETWEEN(-50,250)+100)*10', $sheet->getCell('D6')->getValue());
38+
self::assertSame('=(RANDBETWEEN(-50,250)+100)*10', $sheet->getCell('E6')->getValue());
39+
$spreadsheet->disconnectWorksheets();
40+
}
41+
}

0 commit comments

Comments
 (0)