From 7246fb5e34275cf1741fe418796e7306ef8d177d Mon Sep 17 00:00:00 2001 From: Michael Simpson Date: Wed, 2 Nov 2016 10:33:28 -0400 Subject: [PATCH] Excel export: added a special case to add HYPERLINK Formula when text in the cell matches =HYPERLINK("link","name") --- src/Spout/Writer/XLSX/Internal/Worksheet.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 5896c05..4628bb3 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -200,11 +200,22 @@ EOD; $cellXML .= ' s="' . $styleId . '"'; if (CellHelper::isNonEmptyString($cellValue)) { - if ($this->shouldUseInlineStrings) { - $cellXML .= ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . ''; + $matches = array(); + if (preg_match('/=HYPERLINK\("(.*)","(.*)"\)/', $cellValue, $matches)) { + // 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 { - $sharedStringId = $this->sharedStringsHelper->writeString($cellValue); - $cellXML .= ' t="s">' . $sharedStringId . ''; + if ($this->shouldUseInlineStrings) { + $cellXML .= ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . ''; + } else { + $sharedStringId = $this->sharedStringsHelper->writeString($cellValue); + $cellXML .= ' t="s">' . $sharedStringId . ''; + } } } else if (CellHelper::isBoolean($cellValue)) { $cellXML .= ' t="b">' . intval($cellValue) . '';