From 7246fb5e34275cf1741fe418796e7306ef8d177d Mon Sep 17 00:00:00 2001 From: Michael Simpson Date: Wed, 2 Nov 2016 10:33:28 -0400 Subject: [PATCH 1/2] 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 5896c051..4628bb3e 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -200,11 +200,22 @@ private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) $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) . ''; From bc8fcf1c02ab23c80e4ff623e60548da988620c2 Mon Sep 17 00:00:00 2001 From: Michael Simpson Date: Thu, 3 Nov 2016 19:51:00 -0400 Subject: [PATCH 2/2] 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 4f4a49c7..9dff716f 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -202,7 +202,7 @@ private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) if (CellHelper::isNonEmptyString($cellValue)) { $matches = array(); - if (preg_match('/=HYPERLINK\("(.*)","(.*)"\)/', $cellValue, $matches)) { + if (preg_match('/=HYPERLINK\([\'"](.*)[\'"],\s*[\'"](.*)[\'"]\)/', $cellValue, $matches)) { // Special case to add HYPERLINK Formula $url = $this->stringsEscaper->escape($matches[1]); $text = $this->stringsEscaper->escape($matches[2]);