Skip to content

Commit cc0c4ed

Browse files
committed
Add Test Case, Correct Calculation
Calculate correct result, at least for calculations with a single answer. For an array of answers, result will be similar to other functions which return an array (dynamic arrays not yet fully supported).
1 parent 60e120f commit cc0c4ed

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/PhpSpreadsheet/Calculation/Engine/Operands/StructuredReference.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ public function parse(Cell $cell): string
8585
{
8686
$this->getTableStructure($cell);
8787
$cellRange = ($this->isRowReference()) ? $this->getRowReference($cell) : $this->getColumnReference();
88+
$sheetName = '';
89+
$worksheet = $this->table->getWorksheet();
90+
if ($worksheet !== null && $worksheet !== $cell->getWorksheet()) {
91+
$sheetName = "'" . $worksheet->getTitle() . "'!";
92+
}
8893

89-
return $cellRange;
94+
return $sheetName . $cellRange;
9095
}
9196

9297
private function isRowReference(): bool
@@ -115,7 +120,12 @@ private function getTableStructure(Cell $cell): void
115120
$this->totalsRow = ($this->table->getShowTotalsRow()) ? (int) $tableRange[1][1] : null;
116121
$this->lastDataRow = ($this->table->getShowTotalsRow()) ? (int) $tableRange[1][1] - 1 : $tableRange[1][1];
117122

118-
$this->columns = $this->getColumns($cell, $tableRange);
123+
$cellParam = $cell;
124+
$worksheet = $this->table->getWorksheet();
125+
if ($worksheet !== null && $worksheet !== $cell->getWorksheet()) {
126+
$cellParam = $worksheet->getCell('A1');
127+
}
128+
$this->columns = $this->getColumns($cellParam, $tableRange);
119129
}
120130

121131
/**
@@ -161,10 +171,12 @@ private function findTableFromOtherSheets(Cell $cell): ?Table
161171
{
162172
$spreadsheet = $cell->getWorksheet()->getParent();
163173

164-
foreach ($spreadsheet->getAllSheets() as $sheet) {
165-
$table = $sheet->getTableByName($this->tableName);
166-
if (null !== $table) {
167-
return $table;
174+
if ($spreadsheet !== null) {
175+
foreach ($spreadsheet->getAllSheets() as $sheet) {
176+
$table = $sheet->getTableByName($this->tableName);
177+
if (null !== $table) {
178+
return $table;
179+
}
168180
}
169181
}
170182

@@ -342,7 +354,7 @@ private function getColumnsForColumnReference(string $reference, int $startRow,
342354
{
343355
$columnsSelected = false;
344356
foreach ($this->columns as $columnId => $columnName) {
345-
$columnName = str_replace("\u{a0}", ' ', $columnName);
357+
$columnName = str_replace("\u{a0}", ' ', $columnName ?? '');
346358
$cellFrom = "{$columnId}{$startRow}";
347359
$cellTo = "{$columnId}{$endRow}";
348360
$cellReference = ($cellFrom === $cellTo) ? $cellFrom : "{$cellFrom}:{$cellTo}";
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
4+
5+
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
6+
7+
class Issue3569Test extends SetupTeardown
8+
{
9+
public function testTableOnOtherSheet(): void
10+
{
11+
$spreadsheet = $this->getSpreadsheet();
12+
$sheet = $this->getSheet();
13+
$sheet->setTitle('Feuil1');
14+
$tableSheet = $spreadsheet->createSheet();
15+
$tableSheet->setTitle('sheet_with_table');
16+
$tableSheet->fromArray(
17+
[
18+
['MyCol', 'Colonne2', 'Colonne3'],
19+
[1],
20+
[2],
21+
[3],
22+
[4],
23+
],
24+
null,
25+
'B1',
26+
true
27+
);
28+
$table = new Table('B1:D5', 'Tableau1');
29+
$tableSheet->addTable($table);
30+
$sheet->setSelectedCells('F7');
31+
$tableSheet->setSelectedCells('F8');
32+
self::assertSame($sheet, $spreadsheet->getActiveSheet());
33+
$sheet->getCell('A1')->setValue('=SUM(Tableau1[MyCol])');
34+
$result = $sheet->getCell('A1')->getCalculatedValue();
35+
self::assertSame(10, $result);
36+
self::assertSame('F7', $sheet->getSelectedCells());
37+
self::assertSame('F8', $tableSheet->getSelectedCells());
38+
self::assertSame($sheet, $spreadsheet->getActiveSheet());
39+
}
40+
}

0 commit comments

Comments
 (0)