Skip to content

Novis\Spreadsheet\Reader::links

$reader->links(tainted string|int $sheet): array<Link>$reader->links(
    tainted string|int $sheet,
): array<Link>

Returns the links (hyperlinks) of one sheet.

$sheet is the sheet name, or its position from 0. Each link has anchor, the cell of the link, such as "B2", and target, where the link goes. text is the text the cell shows, and tooltip is the text a person sees over the link. Both are null when the link does not have them. external is true for a URL or a file, and false for a cell in the same workbook, such as "Totals!A1".

Good to know: reading a file never opens its links. Every target comes from the file, so it is tainted. Check it before your program shows it on a web page or follows it. A CSV file has no links, so the list is empty. A sheet that does not exist throws a LogicError. links reads xlsx, xlsm, xlsb, xls and ods files.

The examples below list the links of a sheet, tell a website link from a link to a cell, and check a link before a program uses it.

Parameters

  • $sheettainted string|int

Return value

Returns array<Link>.

Examples

List the links of a sheet

<?nvs
// Shows how to list the links of a sheet, with the cell of each link and where it goes.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Shops');
$sheet->addLink('B2', 'https://example.com/north')->addLink('B3', 'mailto:south@example.com');
$sheet->addRows([['Shop', 'Contact'], ['North', 'Website'], ['South', 'Email']]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `anchor` is the cell of the link, and `target` is where it goes.
foreach ($reader->links('Shops') as var $link) {
echo $link->anchor, ' -> ', $link->target, "\n";
}
Output
B2 -> https://example.com/north
B3 -> mailto:south@example.com

Links to a website and to a cell

<?nvs
// Shows how `external` tells a link to a website apart from a link to a cell in the same workbook.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Blog');
$sheet->addLink('A1', 'https://example.com/blog')->addLink('A2', 'Totals!A1');
$sheet->addRows([['Read the blog'], ['See the totals']]);
$book->addSheet('Totals')->addRow([42]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `external` is `true` for a URL. It is `false` for a cell such as `Totals!A1`.
foreach ($reader->links(0) as var $link) {
echo $link->anchor, ': ', $link->target, $link->external ? ' (external)' : ' (in the workbook)', "\n";
}
Output
A1: https://example.com/blog (external)
A2: Totals!A1 (in the workbook)

Check a link before you show it

<?nvs
// Shows how to check the target of a link from an upload before your program shows it on a web page.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Products');
$sheet->addLink('A2', 'https://example.com/widget', { text: 'Widget', tooltip: 'Open the product page' });
$sheet->addLink('A3', 'http://example.com/gadget');
$sheet->addRows([['Product'], ['Widget'], ['Gadget']]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// The target is `tainted`. This program accepts only `https:` links.
// `text` and `tooltip` are `null` when the link does not have them.
foreach ($reader->links('Products') as var $link) {
if (Core\Str::startsWith($link->target, 'https://')) {
echo $link->anchor, ': accepted, tooltip ', $link->tooltip ?? 'none', "\n";
} else {
echo $link->anchor, ": not https, skipped\n";
}
}
Output
A2: accepted, tooltip Open the product page
A3: not https, skipped