diff options
author | Matthieu Aubry <matt@piwik.org> | 2016-04-08 03:57:40 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2016-04-08 03:57:40 +0300 |
commit | 802b0f10642236269c9992c3b90d748a7ee052c3 (patch) | |
tree | d5d14c1fede1458005afe5ae98997d7f949a5283 /core | |
parent | 7970c82f530c71a0fb7e2cd96e456404eea8dbaa (diff) | |
parent | 1809a57381b37b68a3bcc8f50845b61e38e51d6d (diff) |
Merge pull request #10015 from piwik/excel_labels
Display labels correctly in Excel / LibreOffice
Diffstat (limited to 'core')
-rw-r--r-- | core/DataTable/Renderer/Csv.php | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/core/DataTable/Renderer/Csv.php b/core/DataTable/Renderer/Csv.php index c3fb08b15a..15d6b8ce74 100644 --- a/core/DataTable/Renderer/Csv.php +++ b/core/DataTable/Renderer/Csv.php @@ -121,7 +121,6 @@ class Csv extends Renderer { if (is_array($table)) { // convert array to DataTable - $table = DataTable::makeFromSimpleArray($table); } @@ -247,6 +246,9 @@ class Csv extends Renderer } elseif ($value === false) { $value = 0; } + + $value = $this->formatFormulas($value); + if (is_string($value) && (strpos($value, '"') !== false || strpos($value, $this->separator) !== false) @@ -264,6 +266,30 @@ class Csv extends Renderer return $value; } + protected function formatFormulas($value) + { + // Excel / Libreoffice formulas may start with one of these characters + $formulaStartsWith = array('=', '+', '-', '@'); + + // remove first % sign + $count = 1; + $valueWithoutPercentSign = str_replace('%', '', $value, $count); + + if (empty($valueWithoutPercentSign) + || !is_string($value) + || is_numeric($valueWithoutPercentSign)) { + return $value; + } + + $firstCharCellValue = $valueWithoutPercentSign[0]; + $isFormula = in_array($firstCharCellValue, $formulaStartsWith); + if($isFormula) { + return "'" . $value; + } + + return $value; + } + /** * Sends the http headers for csv file */ |