Skip to content

Commit 7021ab5

Browse files
author
Kevin Verschaeve
committed
fix: Allow to read data from a table located in a different sheet
1 parent d6e2e24 commit 7021ab5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,31 @@ private function getTableByName(Cell $cell): Table
146146
{
147147
$table = $cell->getWorksheet()->getTableByName($this->tableName);
148148

149+
if (!$table) {
150+
$table = $this->findTableFromOtherSheets($cell);
151+
}
152+
149153
if ($table === null) {
150154
throw new Exception("Table {$this->tableName} for Structured Reference cannot be located");
151155
}
152156

153157
return $table;
154158
}
155159

160+
private function findTableFromOtherSheets(Cell $cell): ?Table
161+
{
162+
$spreadsheet = $cell->getWorksheet()->getParent();
163+
164+
foreach ($spreadsheet->getAllSheets() as $sheet) {
165+
$table = $sheet->getTableByName($this->tableName);
166+
if (null !== $table) {
167+
return $table;
168+
}
169+
}
170+
171+
return null;
172+
}
173+
156174
private function getColumns(Cell $cell, array $tableRange): array
157175
{
158176
$worksheet = $cell->getWorksheet();

0 commit comments

Comments
 (0)