Skip to content

Commit

Permalink
XOL-2519 Autosize columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rushi committed Aug 19, 2015
1 parent 382bbf7 commit 34c6ca4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Service/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(LoggerInterface $logger, PHPExcelFactory $phpExcel)
parent::__construct($logger);
$this->phpexcel = $phpExcel;
$this->handle = $this->phpexcel->createPHPExcelObject();
\PHPExcel_Shared_Font::setAutoSizeMethod(\PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
}

/**
Expand Down Expand Up @@ -77,6 +78,7 @@ public function writeHeaders($sortedHeaders)
$worksheet->setCellValue($cell, $header);
// Assumption that all headers are multi-row, so we merge the rows of non-multirow headers
$worksheet->mergeCells($column . $initRow . ':' . $column . ($initRow+1));
$worksheet->getColumnDimension($column)->setAutoSize(true);
$column++;
} else {
// This is a multi-row header, the first row consists of one value merged across several cells and the
Expand All @@ -95,11 +97,14 @@ public function writeHeaders($sortedHeaders)
// Now write the children's values onto the second row
foreach ($header[$headerName] as $subHeaderName) {
$worksheet->setCellValue($column . ($initRow + 1), $subHeaderName);
$worksheet->getColumnDimension($column)->setAutoSize(true);
$column++;
}
}
}

$worksheet->calculateColumnWidths(true);

$this->currentRow += 2;
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/Service/ExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ public function testShouldSetCurrentWorksheet()

public function testShouldWriteSingleRowHeaders()
{
$columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock();
$columnDimensionMock->expects($this->exactly(4))->method('setAutoSize')->with(true);

$worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock();
$worksheetMock->expects($this->exactly(4))->method('setCellValue')->withConsecutive(
['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta']
);
$worksheetMock->expects($this->exactly(4))->method('mergeCells')->withConsecutive(
['A1:A2'], ['B1:B2'], ['C1:C2'], ['D1:D2']
);
$worksheetMock->expects($this->exactly(4))->method('getColumnDimension')->withConsecutive(
['A'], ['B'], ['C'], ['D']
)->willReturn($columnDimensionMock);
$this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock);

$headers = ['Alpha', 'Bravo', 'Gamma', 'Delta'];
Expand All @@ -79,6 +85,9 @@ public function testShouldWriteSingleRowHeaders()

public function testShouldWriteNestedHeaders()
{
$columnDimensionMock = $this->getMockBuilder('\PHPExcel_Worksheet_ColumnDimension')->disableOriginalConstructor()->getMock();
$columnDimensionMock->expects($this->exactly(6))->method('setAutoSize')->with(true);

$worksheetMock = $this->getMockBuilder('\PHPExcel_Worksheet')->disableOriginalConstructor()->getMock();
$worksheetMock->expects($this->exactly(7))->method('setCellValue')->withConsecutive(
['A1', 'Alpha'], ['B1', 'Bravo'], ['C1', 'Gamma'], ['D1', 'Delta'], ['E1', 'Echo'],
Expand All @@ -87,6 +96,9 @@ public function testShouldWriteNestedHeaders()
$worksheetMock->expects($this->exactly(5))->method('mergeCells')->withConsecutive(
['A1:A2'], ['B1:B2'], ['C1:C2'], ['D1:D2'], ['E1:F1']
);
$worksheetMock->expects($this->exactly(6))->method('getColumnDimension')->withConsecutive(
['A'], ['B'], ['C'], ['D'], ['E'], ['F']
)->willReturn($columnDimensionMock);
$this->phpExcelHandleMock->expects($this->once())->method('getActiveSheet')->willReturn($worksheetMock);

$headers = [0 => 'Alpha', 1 => 'Bravo', 2 => 'Gamma', 3 => 'Delta', 4 => ['Echo' => ['Foxtrot', 'Hotel']]];
Expand Down

0 comments on commit 34c6ca4

Please sign in to comment.