From e087e2b03ed9ad76582e15ac78c1db2f1f3e4b2a Mon Sep 17 00:00:00 2001 From: 97WaterPolo <97WaterPolo@users.noreply.github.com> Date: Fri, 28 Jun 2019 12:41:09 -0400 Subject: [PATCH] Added Hyperlink Support Adaptation of a previous version PR https://github.com/box/spout/pull/352 --- .../Writer/XLSX/Manager/WorksheetManager.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index b0e458f..1ee4fbc 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -213,7 +213,20 @@ EOD; $cellXML .= ' s="' . $styleId . '"'; if ($cell->isString()) { - $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); + $matches = array(); + if (preg_match('/=HYPERLINK\([\'"](.*)[\'"],\s*[\'"](.*)[\'"]\)/', $cell->getValue(), $matches)) { + if ($this->stringHelper->getStringLength($cell->getValue()) > self::MAX_CHARACTERS_PER_CELL) { + throw new InvalidArgumentException('Trying to add a value that exceeds the maximum number of characters allowed in a cell (32,767)'); + } + // Special case to add HYPERLINK Formula + $url = $this->stringsEscaper->escape($matches[1]); + $text = $this->stringsEscaper->escape($matches[2]); + $formula = sprintf('HYPERLINK("%s","%s")', $url, $text); + $cellXML = sprintf( + '%s%s', + $columnIndex, $rowIndex, $formula, $text); + }else + $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); } elseif ($cell->isBoolean()) { $cellXML .= ' t="b">' . (int) ($cell->getValue()) . ''; } elseif ($cell->isNumeric()) {