如何从单元格值(VLOOKUP)中获取链接表?
单击该单元格后,我得到了“ Sheet2”,因此具有单元格“ A2”。如何从单元格中获取工作表ID或工作表标题?
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load('/home/test.xlsx');
$cell = $spreadsheet->getActiveSheet()->getCell('A2');
# next? $cell->linkedSheet ? $cell->VLOOKUP ?
好吧,在阅读了文档和源代码之后,我明白了问题出在哪里。
$reader->setReadDataOnly(False); # Must be False
之后,我们可以从单元格值中获取工作表链接。
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
$url = $spreadsheet->getActiveSheet()->getCell('A2')->getHyperlink()->getUrl();
# sheet://'some title'!A1
$url = str_replace(
'sheet://', '', $url
); # Result: 'some title'!A1
$sheet_name = Worksheet::extractSheetTitle($url, true) # ['some title', 'A1']
# Get new sheet instange
$sheet = $spreadsheet->getSheetByName($sheet_name[0]);
echo $sheet->getTitle(); # some title