| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 9 |
|
| 10 |
class Hyperlink |
| 11 |
{ |
| 12 |
/** |
| 13 |
* HYPERLINK. |
| 14 |
* |
| 15 |
* Excel Function: |
| 16 |
* =HYPERLINK(linkURL, [displayName]) |
| 17 |
* |
| 18 |
* @param mixed $linkURL Expect string. Value to check, is also the value returned when no error |
| 19 |
* @param mixed $displayName Expect string. Value to return when testValue is an error condition |
| 20 |
* @param ?Cell $cell The cell to set the hyperlink in |
| 21 |
* |
| 22 |
* @return string The value of $displayName (or $linkURL if $displayName was blank) |
| 23 |
*/ |
| 24 |
public static function set($linkURL = '', $displayName = null, ?Cell $cell = null): string |
| 25 |
{ |
| 26 |
$worksheet = null; |
| 27 |
$coordinate = ''; |
| 28 |
if ($cell !== null) { |
| 29 |
$coordinate = $cell->getCoordinate(); |
| 30 |
$worksheet = $cell->getWorksheetOrNull(); |
| 31 |
} |
| 32 |
|
| 33 |
$linkURL = ($linkURL === null) ? '' : StringHelper::convertToString(Functions::flattenSingleValue($linkURL)); |
| 34 |
$displayName = ($displayName === null) ? '' : Functions::flattenSingleValue($displayName); |
| 35 |
|
| 36 |
if ((!is_object($cell)) || (trim($linkURL) == '')) { |
| 37 |
return ExcelError::REF(); |
| 38 |
} |
| 39 |
|
| 40 |
$displayName = StringHelper::convertToString($displayName, false); |
| 41 |
if (trim($displayName) === '') { |
| 42 |
$displayName = $linkURL; |
| 43 |
} |
| 44 |
|
| 45 |
(($nullsafeVariable1 = $worksheet) ? $nullsafeVariable1->getCell($coordinate)->getHyperlink() : null) |
| 46 |
->setUrl($linkURL) |
| 47 |
->setTooltip($displayName) |
| 48 |
->setDisplay(''); |
| 49 |
|
| 50 |
return $displayName; |
| 51 |
} |
| 52 |
} |
| 53 |
|