Add column width settings
This commit is contained in:
parent
cc42c1d29f
commit
d9582d1366
@ -26,6 +26,10 @@ class Sheet
|
||||
|
||||
/** @var SheetManager Sheet manager */
|
||||
private $sheetManager;
|
||||
/**
|
||||
* @var array<int, int>
|
||||
*/
|
||||
private $columnWidths = [];
|
||||
|
||||
/**
|
||||
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
|
||||
@ -108,4 +112,23 @@ class Sheet
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function getColumnWidths()
|
||||
{
|
||||
return $this->columnWidths;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $columnWidths
|
||||
* @return Sheet
|
||||
*/
|
||||
public function setColumnWidths(array $columnWidths)
|
||||
{
|
||||
$this->columnWidths = $columnWidths;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -59,6 +59,9 @@ EOD;
|
||||
/** @var StringHelper String helper */
|
||||
private $stringHelper;
|
||||
|
||||
/** @var bool */
|
||||
private $isSheetDataStarted = false;
|
||||
|
||||
/**
|
||||
* WorksheetManager constructor.
|
||||
*
|
||||
@ -107,7 +110,35 @@ EOD;
|
||||
$worksheet->setFilePointer($sheetFilePointer);
|
||||
|
||||
\fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
|
||||
\fwrite($sheetFilePointer, '<sheetData>');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Worksheet $worksheet The worksheet to start data to
|
||||
* @return void
|
||||
*/
|
||||
private function startSheetData(Worksheet $worksheet)
|
||||
{
|
||||
if ($this->isSheetDataStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
$columnWidths = $worksheet->getExternalSheet()->getColumnWidths();
|
||||
if ($columnWidths !== []) {
|
||||
$columnWidthsSpec = '<cols>';
|
||||
foreach ($columnWidths as $columnIndex => $columnWidth) {
|
||||
$columnWidthsSpec .= sprintf(
|
||||
'<col min="%1$s" max="%1$s" width="%2$s" customWidth="true"/>',
|
||||
$columnIndex,
|
||||
$columnWidth
|
||||
);
|
||||
}
|
||||
$columnWidthsSpec .= '</cols>';
|
||||
\fwrite($worksheet->getFilePointer(), $columnWidthsSpec);
|
||||
}
|
||||
|
||||
\fwrite($worksheet->getFilePointer(), '<sheetData>');
|
||||
|
||||
$this->isSheetDataStarted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,6 +178,8 @@ EOD;
|
||||
*/
|
||||
private function addNonEmptyRow(Worksheet $worksheet, Row $row)
|
||||
{
|
||||
$this->startSheetData($worksheet);
|
||||
|
||||
$rowStyle = $row->getStyle();
|
||||
$rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1;
|
||||
$numCells = $row->getNumCells();
|
||||
@ -286,6 +319,8 @@ EOD;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->startSheetData($worksheet);
|
||||
|
||||
\fwrite($worksheetFilePointer, '</sheetData>');
|
||||
\fwrite($worksheetFilePointer, '</worksheet>');
|
||||
\fclose($worksheetFilePointer);
|
||||
|
@ -92,6 +92,34 @@ class SheetTest extends TestCase
|
||||
$this->assertStringContainsString(' state="hidden"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetSheetColumnWidthsShouldCreateSheetWidthCustomWidths()
|
||||
{
|
||||
$fileName = 'test_set_column_widths.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setColumnWidths([
|
||||
1 => 10,
|
||||
3 => 40,
|
||||
]);
|
||||
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertStringContainsString('<cols>', $xmlContents);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param string $sheetName
|
||||
|
Loading…
x
Reference in New Issue
Block a user