diff --git a/src/Spout/Writer/Common/Helper/CellHelper.php b/src/Spout/Writer/Common/Helper/CellHelper.php index b32e9ac..50ead93 100644 --- a/src/Spout/Writer/Common/Helper/CellHelper.php +++ b/src/Spout/Writer/Common/Helper/CellHelper.php @@ -46,6 +46,15 @@ class CellHelper return self::$columnIndexToCellIndexCache[$originalColumnIndex]; } + /** + * @param $value + * @return bool Whether the given value is considered "empty" + */ + public static function isEmpty($value) + { + return ($value === null || $value === ''); + } + /** * @param $value * @return bool Whether the given value is a non empty string diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 72aa419..5896c05 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -131,6 +131,38 @@ EOD; * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported */ public function addRow($dataRow, $style) + { + if (!$this->isEmptyRow($dataRow)) { + $this->addNonEmptyRow($dataRow, $style); + } + + $this->lastWrittenRowIndex++; + } + + /** + * Returns whether the given row is empty + * + * @param array $dataRow Array containing data to be written. Cannot be empty. + * Example $dataRow = ['data1', 1234, null, '', 'data5']; + * @return bool Whether the given row is empty + */ + private function isEmptyRow($dataRow) + { + $numCells = count($dataRow); + return ($numCells === 1 && CellHelper::isEmpty($dataRow[0])); + } + + /** + * Adds non empty row to the worksheet. + * + * @param array $dataRow Array containing data to be written. Cannot be empty. + * Example $dataRow = ['data1', 1234, null, '', 'data5']; + * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. + * @return void + * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written + * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported + */ + private function addNonEmptyRow($dataRow, $style) { $cellNumber = 0; $rowIndex = $this->lastWrittenRowIndex + 1; @@ -149,9 +181,6 @@ EOD; if ($wasWriteSuccessful === false) { throw new IOException("Unable to write data in {$this->worksheetFilePath}"); } - - // only update the count if the write worked - $this->lastWrittenRowIndex++; } /** diff --git a/tests/Spout/Writer/Common/Helper/CellHelperTest.php b/tests/Spout/Writer/Common/Helper/CellHelperTest.php index 2abb4cc..a07046f 100644 --- a/tests/Spout/Writer/Common/Helper/CellHelperTest.php +++ b/tests/Spout/Writer/Common/Helper/CellHelperTest.php @@ -35,6 +35,23 @@ class CellHelperTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex)); } + /** + * @return array + */ + public function testIsEmpty() + { + $this->assertTrue(CellHelper::isEmpty(null)); + $this->assertTrue(CellHelper::isEmpty("")); + + $this->assertFalse(CellHelper::isEmpty("string")); + $this->assertFalse(CellHelper::isEmpty(0)); + $this->assertFalse(CellHelper::isEmpty(1)); + $this->assertFalse(CellHelper::isEmpty(true)); + $this->assertFalse(CellHelper::isEmpty(false)); + $this->assertFalse(CellHelper::isEmpty(["string"])); + $this->assertFalse(CellHelper::isEmpty(new \stdClass())); + } + /** * @return array */ diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index cc8daca..a49a7f6 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -108,7 +108,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase /** * @return void */ - public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown2() + public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown() { $fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx'; $dataRows = [ @@ -276,7 +276,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase ['foo' => 'xlsx--11', 'bar' => 'xlsx--12'], ]; - $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true); + $this->writeToXLSXFile($dataRows, $fileName); foreach ($dataRows as $dataRow) { foreach ($dataRow as $cellValue) { @@ -285,6 +285,29 @@ class WriterTest extends \PHPUnit_Framework_TestCase } } + /** + * @return void + */ + public function testAddRowShouldNotWriteEmptyRows() + { + $fileName = 'test_add_row_should_not_write_empty_rows.xlsx'; + $dataRows = [ + [''], + ['xlsx--21', 'xlsx--22'], + [''], + [''], + ['xlsx--51', 'xlsx--52'], + ]; + + $this->writeToXLSXFile($dataRows, $fileName); + + $this->assertInlineDataWasWrittenToSheet($fileName, 1, 'row r="2"'); + $this->assertInlineDataWasWrittenToSheet($fileName, 1, 'row r="5"'); + $this->assertInlineDataWasNotWrittenToSheet($fileName, 1, 'row r="1"'); + $this->assertInlineDataWasNotWrittenToSheet($fileName, 1, 'row r="3"'); + $this->assertInlineDataWasNotWrittenToSheet($fileName, 1, 'row r="4"'); + } + /** * @return void */