Skip to content

Novis\Spreadsheet\Sheet::addLink

$sheet->addLink(
    string $cell,
    string $target,
    ?{ text?: string, tooltip?: string } $options = null,
): Sheet

Makes the cell $cell a link (a hyperlink) to $target, and returns the sheet.

The target is an http: or https: address, a mailto: address, or a cell of the workbook, such as "Totals!A1". text is the text the cell shows. tooltip is the text a person sees when the mouse is over the link.

Good to know: any other target, such as a file: or javascript: address, throws a LogicError. So a link built from user data cannot open a local file or run a script. Put the name of a sheet with a space in quotes, such as "'Sheet 2'!A1". A CSV file has no links, so addLink does nothing there.

The examples below link to a website, link to a cell on another sheet, and show the error for a target that is not allowed.

Parameters

  • $cellstring
  • $targetstring
  • $options?{ text?: string, tooltip?: string }default null

Return value

Returns Sheet.

Changelog

VersionChange
0.0.6Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart.

Examples

A link to a website

<?nvs
// Shows how to link each product to its page on a website.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Products');
$sheet->addRows([['product' => 'Widget', 'page' => 'Open'], ['product' => 'Gadget', 'page' => 'Open']]);
// `text` is the text the cell shows. `tooltip` is the text over the link.
$sheet->addLink('B2', 'https://example.com/widget', { text: 'Widget page', tooltip: 'Opens the website' });
$sheet->addLink('B3', 'https://example.com/gadget');
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->links('Products') as var $link) {
echo $link->anchor, ' -> ', $link->target, ', text: ', $link->text ?? 'none', "\n";
}
Output
B2 -> https://example.com/widget, text: Widget page
B3 -> https://example.com/gadget, text: none

A link to another sheet

<?nvs
// Shows how a summary sheet links to the sheet with the details.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $summary = $book->addSheet('Summary');
$summary->addRow(['Orders', 'See the list']);
// A target without `http:` is a cell of the workbook. `external` is `false` for it.
$summary->addLink('B1', 'Orders!A1');
$book->addSheet('Orders')->addRows([['order' => 1042], ['order' => 1043]]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->links('Summary') as var $link) {
echo $link->anchor, ' -> ', $link->target, ', external: ', $link->external ? 'yes' : 'no', "\n";
}
Output
B1 -> Orders!A1, external: no

A target that is not allowed

<?nvs
// Shows the error for a link to a local file, such as one from a form a user filled in.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Shops');
$sheet->addRow(['North', 'Website']);
// A `file:` address is not allowed, so `addLink` throws a `LogicError`.
try {
$sheet->addLink('B1', 'file:///C:/Windows/notepad.exe');
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The link target `file:///C:/Windows/notepad.exe` is not allowed. A link goes to an `http:`, `https:` or `mailto:` address, or to a cell such as `'Sheet 2'!A1`.