Add support for setting column width by range
This commit is contained in:
parent
82170a058b
commit
80487f1ac1
@ -303,13 +303,22 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float|null $width
|
||||
* @param float $width
|
||||
* @param array $columns One or more columns with this width
|
||||
*/
|
||||
public function setColumnWidth($width, ...$columns) {
|
||||
public function setColumnWidth(float $width, ...$columns) {
|
||||
$this->worksheetManager->setColumnWidth($width, ...$columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $width The width to set
|
||||
* @param int $start First column index of the range
|
||||
* @param int $end Last column index of the range
|
||||
*/
|
||||
public function setColumnWidthForRange(float $width, int $start, int $end) {
|
||||
$this->worksheetManager->setColumnWidthForRange($width, $start, $end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the workbook and all its associated sheets.
|
||||
* All the necessary files are written to disk and zipped together to create the final file.
|
||||
|
@ -167,6 +167,17 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
|
||||
$this->workbookManager->setColumnWidth($width, ...$columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $width The width to set
|
||||
* @param int $start First column index of the range
|
||||
* @param int $end Last column index of the range
|
||||
* @throws WriterNotOpenedException
|
||||
*/
|
||||
public function setColumnWidthForRange(float $width, int $start, int $end) {
|
||||
$this->throwIfWorkbookIsNotAvailable();
|
||||
$this->workbookManager->setColumnWidthForRange($width, $start, $end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the workbook has been created. Throws an exception if not created yet.
|
||||
*
|
||||
|
@ -132,22 +132,31 @@ EOD;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float|null $width
|
||||
* @param float $width
|
||||
* @param array $columns One or more columns with this width
|
||||
*/
|
||||
public function setColumnWidth($width, ...$columns) {
|
||||
public function setColumnWidth(float $width, ...$columns) {
|
||||
// Gather sequences
|
||||
$sequence = [];
|
||||
foreach ($columns as $i) {
|
||||
$sequenceLength = count($sequence);
|
||||
$previousValue = $sequence[$sequenceLength - 1];
|
||||
if ($sequenceLength > 0 && $i !== $previousValue + 1) {
|
||||
$this->columnWidths[] = [$sequence[0], $previousValue, $width];
|
||||
$this->setColumnWidthForRange($width, $sequence[0], $previousValue);
|
||||
$sequence = [];
|
||||
}
|
||||
$sequence[] = $i;
|
||||
}
|
||||
$this->columnWidths[] = [$sequence[0], $sequence[count($sequence) - 1], $width];
|
||||
$this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $width The width to set
|
||||
* @param int $start First column index of the range
|
||||
* @param int $end Last column index of the range
|
||||
*/
|
||||
public function setColumnWidthForRange(float $width, int $start, int $end) {
|
||||
$this->columnWidths[] = [$start, $end, $width];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,6 +211,25 @@ class SheetTest extends TestCase
|
||||
$this->assertContains('<col min="5" max="5" width="100" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
public function testCanTakeColumnWidthsAsRange()
|
||||
{
|
||||
$fileName = 'test_column_widths_as_ranges.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setColumnWidthForRange(50.0, 1, 3);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<cols', $xmlContents, 'No cols tag found in sheet');
|
||||
$this->assertContains('<col min="1" max="3" width="50" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param string $sheetName
|
||||
|
Loading…
x
Reference in New Issue
Block a user