From f47c00090175147d11f59a7349c859b4c20ec096 Mon Sep 17 00:00:00 2001 From: Grzegorz Daszuta Date: Thu, 2 Apr 2015 11:33:51 +0200 Subject: [PATCH 1/4] Guess Excel cell type basing on array cell value type --- .../Writer/Exception/InvalidDataException.php | 12 ++++++++++ src/Spout/Writer/Internal/XLSX/Worksheet.php | 22 ++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/Spout/Writer/Exception/InvalidDataException.php diff --git a/src/Spout/Writer/Exception/InvalidDataException.php b/src/Spout/Writer/Exception/InvalidDataException.php new file mode 100644 index 0000000..adaae1f --- /dev/null +++ b/src/Spout/Writer/Exception/InvalidDataException.php @@ -0,0 +1,12 @@ +' . PHP_EOL; - } else { - if (is_numeric($cellValue)) { + switch(true) { + case empty($cellValue): + $data .= '/>' . PHP_EOL; + break; + case gettype($cellValue) === 'integer': + case gettype($cellValue) === 'boolean': + case gettype($cellValue) === 'float': $data .= '>' . $cellValue . '' . PHP_EOL; - } else { + break; + case gettype($cellValue) === 'object' && method_exists($cellValue, '__toString'): + $cellValue = (string)$cellValue; + case gettype($cellValue) === 'string': if ($this->shouldUseInlineStrings) { $data .= ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . '' . PHP_EOL; } else { $sharedStringId = $this->sharedStringsHelper->writeString($cellValue); $data .= ' t="s">' . $sharedStringId . '' . PHP_EOL; } - } + break; + default: + throw new InvalidDataException("Invalid data type " . gettype($cellValue)); } $cellNumber++; From 208c3515a544fb787d7c361b95fe662cdfa802d6 Mon Sep 17 00:00:00 2001 From: Grzegorz Daszuta Date: Thu, 2 Apr 2015 12:51:35 +0200 Subject: [PATCH 2/4] fix exception --- src/Spout/Writer/Exception/InvalidDataException.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Spout/Writer/Exception/InvalidDataException.php b/src/Spout/Writer/Exception/InvalidDataException.php index adaae1f..a51c8e7 100644 --- a/src/Spout/Writer/Exception/InvalidDataException.php +++ b/src/Spout/Writer/Exception/InvalidDataException.php @@ -2,6 +2,8 @@ namespace Box\Spout\Writer\Exception; +use Box\Spout\Common\Exception\SpoutException; + /** * Class InvalidDataException * From 8f51d4ac31fb9e704f43a87bb5abb1ccc47652e4 Mon Sep 17 00:00:00 2001 From: Grzegorz Daszuta Date: Thu, 2 Apr 2015 12:53:44 +0200 Subject: [PATCH 3/4] allow double --- src/Spout/Writer/Internal/XLSX/Worksheet.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Spout/Writer/Internal/XLSX/Worksheet.php b/src/Spout/Writer/Internal/XLSX/Worksheet.php index 101cb00..124feb2 100644 --- a/src/Spout/Writer/Internal/XLSX/Worksheet.php +++ b/src/Spout/Writer/Internal/XLSX/Worksheet.php @@ -141,6 +141,7 @@ EOD; case gettype($cellValue) === 'integer': case gettype($cellValue) === 'boolean': case gettype($cellValue) === 'float': + case gettype($cellValue) === 'double': $data .= '>' . $cellValue . '' . PHP_EOL; break; case gettype($cellValue) === 'object' && method_exists($cellValue, '__toString'): From b7831cc93313a0e56186df41d11c9bdcfb881bae Mon Sep 17 00:00:00 2001 From: Grzegorz Daszuta Date: Thu, 2 Apr 2015 12:54:56 +0200 Subject: [PATCH 4/4] allow numeric 0 --- src/Spout/Writer/Internal/XLSX/Worksheet.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Spout/Writer/Internal/XLSX/Worksheet.php b/src/Spout/Writer/Internal/XLSX/Worksheet.php index 124feb2..6d6dab5 100644 --- a/src/Spout/Writer/Internal/XLSX/Worksheet.php +++ b/src/Spout/Writer/Internal/XLSX/Worksheet.php @@ -135,9 +135,6 @@ EOD; $data .= ' ' . PHP_EOL; } break; + case empty($cellValue): + $data .= '/>' . PHP_EOL; + break; default: throw new InvalidDataException("Invalid data type " . gettype($cellValue)); }