diff --git a/src/Spout/Writer/Common/Cell.php b/src/Spout/Writer/Common/Cell.php
new file mode 100644
index 0000000..d443dbb
--- /dev/null
+++ b/src/Spout/Writer/Common/Cell.php
@@ -0,0 +1,122 @@
+setValue($value);
+ $this->setComment($comment);
+ }
+
+ /**
+ * @param $value mixed
+ */
+ public function setValue($value)
+ {
+ $this->value = $value;
+ }
+
+ /**
+ * @return mixed|null
+ */
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ /**
+ * @param $comment string
+ */
+ public function setComment($comment)
+ {
+ $this->comment = $comment;
+ }
+
+ /**
+ * @return null|string
+ */
+ public function getComment()
+ {
+ return $this->comment;
+ }
+
+ /**
+ * Get the current value type
+ * @return int
+ */
+ public function getType()
+ {
+ $value = $this->getValue();
+
+ if(CellHelper::isBoolean($value)) {
+ return self::CELL_TYPE_BOOLEAN;
+ } elseif (CellHelper::isEmpty($value)) {
+ return self::CELL_TYPE_BLANK;
+ } elseif(CellHelper::isNumeric($this->getValue())) {
+ return self::CELL_TYPE_NUMERIC;
+ } elseif (CellHelper::isNonEmptyString($value)) {
+ return self::CELL_TYPE_STRING;
+ } else {
+ return self::CELL_TYPE_ERROR;
+ }
+ }
+
+ /**
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string)$this->value;
+ }
+}
\ No newline at end of file
diff --git a/src/Spout/Writer/ODS/Internal/Worksheet.php b/src/Spout/Writer/ODS/Internal/Worksheet.php
index 0920b6d..fe20d1f 100644
--- a/src/Spout/Writer/ODS/Internal/Worksheet.php
+++ b/src/Spout/Writer/ODS/Internal/Worksheet.php
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS\Internal;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\StringHelper;
+use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
@@ -191,27 +192,33 @@ class Worksheet implements WorksheetInterface
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
}
- if (CellHelper::isNonEmptyString($cellValue)) {
+ if($cellValue instanceof Cell) {
+ $cellContent = $cellValue->getValue();
+ } else {
+ $cellContent = $cellValue;
+ }
+
+ if (CellHelper::isNonEmptyString($cellContent)) {
$data .= ' office:value-type="string" calcext:value-type="string">';
- $cellValueLines = explode("\n", $cellValue);
+ $cellValueLines = explode("\n", $cellContent);
foreach ($cellValueLines as $cellValueLine) {
$data .= '' . $this->stringsEscaper->escape($cellValueLine) . '';
}
$data .= '';
- } else if (CellHelper::isBoolean($cellValue)) {
- $data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellValue . '">';
- $data .= '' . $cellValue . '';
+ } else if (CellHelper::isBoolean($cellContent)) {
+ $data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellContent . '">';
+ $data .= '' . $cellContent . '';
$data .= '';
- } else if (CellHelper::isNumeric($cellValue)) {
- $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellValue . '">';
- $data .= '' . $cellValue . '';
+ } else if (CellHelper::isNumeric($cellContent)) {
+ $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellContent . '">';
+ $data .= '' . $cellContent . '';
$data .= '';
- } else if (empty($cellValue)) {
+ } else if (empty($cellContent)) {
$data .= '/>';
} else {
- throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
+ throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent));
}
return $data;
diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php
index b5a3dc7..18406e3 100644
--- a/src/Spout/Writer/XLSX/Internal/Worksheet.php
+++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX\Internal;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\StringHelper;
+use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
@@ -213,13 +214,19 @@ EOD;
$cellXML = 'getCellXMLFragmentForNonEmptyString($cellValue);
- } else if (CellHelper::isBoolean($cellValue)) {
- $cellXML .= ' t="b">' . intval($cellValue) . '';
- } else if (CellHelper::isNumeric($cellValue)) {
- $cellXML .= '>' . $cellValue . '';
- } else if (empty($cellValue)) {
+ if($cellValue instanceof Cell) {
+ $cellContent = $cellValue->getValue();
+ } else {
+ $cellContent = $cellValue;
+ }
+
+ if (CellHelper::isNonEmptyString($cellContent)) {
+ $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cellContent);
+ } else if (CellHelper::isBoolean($cellContent)) {
+ $cellXML .= ' t="b">' . intval($cellContent) . '';
+ } else if (CellHelper::isNumeric($cellContent)) {
+ $cellXML .= '>' . $cellContent . '';
+ } else if (empty($cellContent)) {
if ($this->styleHelper->shouldApplyStyleOnEmptyCell($styleId)) {
$cellXML .= '/>';
} else {
@@ -228,7 +235,7 @@ EOD;
$cellXML = '';
}
} else {
- throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
+ throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent));
}
return $cellXML;
diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php
index 9558129..c2f1d2c 100644
--- a/tests/Spout/Writer/CSV/WriterTest.php
+++ b/tests/Spout/Writer/CSV/WriterTest.php
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
use Box\Spout\TestUsingResource;
use Box\Spout\Common\Type;
use Box\Spout\Common\Helper\EncodingHelper;
+use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\WriterFactory;
/**
@@ -177,6 +178,19 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('#This is, a comma#,csv--12,csv--13', $writtenContent, 'The fields should be enclosed with #');
}
+ /**
+ * @return void
+ */
+ public function testWriteShouldAcceptCellObjects()
+ {
+ $allRows = [
+ [new Cell('String Value'), new Cell(1)]
+ ];
+ $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv');
+ $writtenContent = $this->trimWrittenContent($writtenContent);
+ $this->assertEquals('"String Value",1', $writtenContent);
+ }
+
/**
* @param array $allRows
* @param string $fileName
diff --git a/tests/Spout/Writer/ODS/WriterTest.php b/tests/Spout/Writer/ODS/WriterTest.php
index 836e679..fbce7b5 100644
--- a/tests/Spout/Writer/ODS/WriterTest.php
+++ b/tests/Spout/Writer/ODS/WriterTest.php
@@ -6,6 +6,7 @@ use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource;
+use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\Common\Helper\ZipHelper;
use Box\Spout\Writer\WriterFactory;
@@ -463,6 +464,27 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath));
}
+ /**
+ * @return void
+ */
+ public function testWriteShouldAcceptCellObjects()
+ {
+ $fileName = 'test_writer_should_accept_cell_objects.ods';
+ $dataRows = [
+ [new Cell('ods--11'), new Cell('ods--12')],
+ [new Cell('ods--21'), new Cell('ods--22'), new Cell('ods--23')],
+ ];
+
+ $this->writeToODSFile($dataRows, $fileName);
+
+ foreach ($dataRows as $dataRow) {
+ /** @var Cell $cell */
+ foreach ($dataRow as $cell) {
+ $this->assertValueWasWritten($fileName, $cell->getValue());
+ }
+ }
+ }
+
/**
* @param array $allRows
* @param string $fileName
diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php
index c7f4f96..f626fcc 100644
--- a/tests/Spout/Writer/XLSX/WriterTest.php
+++ b/tests/Spout/Writer/XLSX/WriterTest.php
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
+use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Internal\Worksheet;
@@ -507,6 +508,27 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $finfo->file($resourcePath));
}
+ /**
+ * @return void
+ */
+ public function testWriterShouldAcceptCellObjects()
+ {
+ $fileName = 'test_writer_should_accept_cell_objects.xlsx';
+ $dataRows = [
+ [new Cell('xlsx--11'), new Cell('xlsx--12')],
+ [new Cell('xlsx--21'), new Cell('xlsx--22'), new Cell('xlsx--23')],
+ ];
+
+ $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
+
+ foreach ($dataRows as $dataRow) {
+ /** @var Cell $cell */
+ foreach ($dataRow as $cell) {
+ $this->assertSharedStringWasWritten($fileName, $cell->getValue());
+ }
+ }
+ }
+
/**
* @param array $allRows
* @param string $fileName