Empty rows do not need to be written for XLSX files
This commit is contained in:
parent
2fafb63115
commit
19fb328c84
@ -46,6 +46,15 @@ class CellHelper
|
|||||||
return self::$columnIndexToCellIndexCache[$originalColumnIndex];
|
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
|
* @param $value
|
||||||
* @return bool Whether the given value is a non empty string
|
* @return bool Whether the given value is a non empty string
|
||||||
|
@ -131,6 +131,38 @@ EOD;
|
|||||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||||
*/
|
*/
|
||||||
public function addRow($dataRow, $style)
|
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;
|
$cellNumber = 0;
|
||||||
$rowIndex = $this->lastWrittenRowIndex + 1;
|
$rowIndex = $this->lastWrittenRowIndex + 1;
|
||||||
@ -149,9 +181,6 @@ EOD;
|
|||||||
if ($wasWriteSuccessful === false) {
|
if ($wasWriteSuccessful === false) {
|
||||||
throw new IOException("Unable to write data in {$this->worksheetFilePath}");
|
throw new IOException("Unable to write data in {$this->worksheetFilePath}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// only update the count if the write worked
|
|
||||||
$this->lastWrittenRowIndex++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,6 +35,23 @@ class CellHelperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex));
|
$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
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -108,7 +108,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown2()
|
public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown()
|
||||||
{
|
{
|
||||||
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx';
|
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx';
|
||||||
$dataRows = [
|
$dataRows = [
|
||||||
@ -276,7 +276,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
['foo' => 'xlsx--11', 'bar' => 'xlsx--12'],
|
['foo' => 'xlsx--11', 'bar' => 'xlsx--12'],
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true);
|
$this->writeToXLSXFile($dataRows, $fileName);
|
||||||
|
|
||||||
foreach ($dataRows as $dataRow) {
|
foreach ($dataRows as $dataRow) {
|
||||||
foreach ($dataRow as $cellValue) {
|
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
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user