Merge pull request #1352 from troosan/column_width_for_odt
write column width in ODT writer
This commit is contained in:
commit
4fa945561f
|
|
@ -36,6 +36,7 @@ v0.15.0 (?? ??? 2018)
|
|||
- Fix colspan and rowspan for tables in HTML Writer @mattbolt #1292
|
||||
- Fix parsing of Heading and Title formating @troosan @gthomas2 #465
|
||||
- Fix Dateformat typo, fix hours casing, add Month-Day-Year formats @ComputerTinker #591
|
||||
- Fix missing column width in ODText writer @potofcoffee #413
|
||||
|
||||
### Changed
|
||||
- Remove zend-stdlib dependency @Trainmaster #1284
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
<?php
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
|
||||
include_once 'Sample_Header.php';
|
||||
|
||||
|
|
|
|||
|
|
@ -135,18 +135,40 @@ class Table extends AbstractElement
|
|||
public function countColumns()
|
||||
{
|
||||
$columnCount = 0;
|
||||
if (is_array($this->rows)) {
|
||||
$rowCount = count($this->rows);
|
||||
for ($i = 0; $i < $rowCount; $i++) {
|
||||
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
|
||||
$row = $this->rows[$i];
|
||||
$cellCount = count($row->getCells());
|
||||
if ($columnCount < $cellCount) {
|
||||
$columnCount = $cellCount;
|
||||
}
|
||||
|
||||
$rowCount = count($this->rows);
|
||||
for ($i = 0; $i < $rowCount; $i++) {
|
||||
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
|
||||
$row = $this->rows[$i];
|
||||
$cellCount = count($row->getCells());
|
||||
if ($columnCount < $cellCount) {
|
||||
$columnCount = $cellCount;
|
||||
}
|
||||
}
|
||||
|
||||
return $columnCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* The first declared cell width for each column
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public function findFirstDefinedCellWidths()
|
||||
{
|
||||
$cellWidths = array();
|
||||
|
||||
foreach ($this->rows as $row) {
|
||||
$cells = $row->getCells();
|
||||
if (count($cells) <= count($cellWidths)) {
|
||||
continue;
|
||||
}
|
||||
$cellWidths = array();
|
||||
foreach ($cells as $cell) {
|
||||
$cellWidths[] = $cell->getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
return $cellWidths;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,6 +163,13 @@ class Table extends Border
|
|||
/** @var TblWidthComplexType|null */
|
||||
private $indent;
|
||||
|
||||
/**
|
||||
* The width of each column, computed based on the max cell width of each column
|
||||
*
|
||||
* @var int[]
|
||||
*/
|
||||
private $columnWidths;
|
||||
|
||||
/**
|
||||
* Create new table style
|
||||
*
|
||||
|
|
@ -748,4 +755,24 @@ class Table extends Border
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columnWidths
|
||||
*
|
||||
* @return number[]
|
||||
*/
|
||||
public function getColumnWidths()
|
||||
{
|
||||
return $this->columnWidths;
|
||||
}
|
||||
|
||||
/**
|
||||
* The column widths
|
||||
*
|
||||
* @param int[] $value
|
||||
*/
|
||||
public function setColumnWidths(array $value = null)
|
||||
{
|
||||
$this->columnWidths = $value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\ODText\Element;
|
||||
|
||||
use PhpOffice\Common\XMLWriter;
|
||||
use PhpOffice\PhpWord\Element\Row as RowElement;
|
||||
use PhpOffice\PhpWord\Element\Table as TableElement;
|
||||
|
||||
/**
|
||||
* Table element writer
|
||||
*
|
||||
|
|
@ -36,32 +40,59 @@ class Table extends AbstractElement
|
|||
}
|
||||
$rows = $element->getRows();
|
||||
$rowCount = count($rows);
|
||||
$colCount = $element->countColumns();
|
||||
|
||||
if ($rowCount > 0) {
|
||||
$xmlWriter->startElement('table:table');
|
||||
$xmlWriter->writeAttribute('table:name', $element->getElementId());
|
||||
$xmlWriter->writeAttribute('table:style', $element->getElementId());
|
||||
|
||||
$xmlWriter->startElement('table:table-column');
|
||||
$xmlWriter->writeAttribute('table:number-columns-repeated', $colCount);
|
||||
$xmlWriter->endElement(); // table:table-column
|
||||
// Write columns
|
||||
$this->writeColumns($xmlWriter, $element);
|
||||
|
||||
// Write rows
|
||||
foreach ($rows as $row) {
|
||||
$xmlWriter->startElement('table:table-row');
|
||||
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
|
||||
foreach ($row->getCells() as $cell) {
|
||||
$xmlWriter->startElement('table:table-cell');
|
||||
$xmlWriter->writeAttribute('office:value-type', 'string');
|
||||
|
||||
$containerWriter = new Container($xmlWriter, $cell);
|
||||
$containerWriter->write();
|
||||
|
||||
$xmlWriter->endElement(); // table:table-cell
|
||||
}
|
||||
$xmlWriter->endElement(); // table:table-row
|
||||
$this->writeRow($xmlWriter, $row);
|
||||
}
|
||||
$xmlWriter->endElement(); // table:table
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write column.
|
||||
*
|
||||
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
||||
* @param \PhpOffice\PhpWord\Element\Table $element
|
||||
*/
|
||||
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
|
||||
{
|
||||
$colCount = $element->countColumns();
|
||||
|
||||
for ($i = 0; $i < $colCount; $i++) {
|
||||
$xmlWriter->startElement('table:table-column');
|
||||
$xmlWriter->writeAttribute('table:style-name', $element->getElementId() . '.' . $i);
|
||||
$xmlWriter->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write row.
|
||||
*
|
||||
* @param \PhpOffice\Common\XMLWriter $xmlWriter
|
||||
* @param \PhpOffice\PhpWord\Element\Row $row
|
||||
*/
|
||||
private function writeRow(XMLWriter $xmlWriter, RowElement $row)
|
||||
{
|
||||
$xmlWriter->startElement('table:table-row');
|
||||
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
|
||||
foreach ($row->getCells() as $cell) {
|
||||
$xmlWriter->startElement('table:table-cell');
|
||||
$xmlWriter->writeAttribute('office:value-type', 'string');
|
||||
|
||||
$containerWriter = new Container($xmlWriter, $cell);
|
||||
$containerWriter->write();
|
||||
|
||||
$xmlWriter->endElement(); // table:table-cell
|
||||
}
|
||||
$xmlWriter->endElement(); // table:table-row
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,6 +239,7 @@ class Content extends AbstractPart
|
|||
$style->setStyleName('fr' . $element->getMediaIndex());
|
||||
$this->autoStyles['Image'][] = $style;
|
||||
} elseif ($element instanceof Table) {
|
||||
/** @var \PhpOffice\PhpWord\Style\Table $style */
|
||||
$style = $element->getStyle();
|
||||
if ($style === null) {
|
||||
$style = new TableStyle();
|
||||
|
|
@ -246,6 +247,7 @@ class Content extends AbstractPart
|
|||
$style = Style::getStyle($style);
|
||||
}
|
||||
$style->setStyleName($element->getElementId());
|
||||
$style->setColumnWidths($element->findFirstDefinedCellWidths());
|
||||
$this->autoStyles['Table'][] = $style;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,5 +45,19 @@ class Table extends AbstractStyle
|
|||
$xmlWriter->writeAttribute('table:align', 'center');
|
||||
$xmlWriter->endElement(); // style:table-properties
|
||||
$xmlWriter->endElement(); // style:style
|
||||
|
||||
$cellWidths = $style->getColumnWidths();
|
||||
$countCellWidths = count($cellWidths);
|
||||
|
||||
for ($i = 0; $i < $countCellWidths; $i++) {
|
||||
$width = $cellWidths[$i];
|
||||
$xmlWriter->startElement('style:style');
|
||||
$xmlWriter->writeAttribute('style:name', $style->getStyleName() . '.' . $i);
|
||||
$xmlWriter->writeAttribute('style:family', 'table-column');
|
||||
$xmlWriter->startElement('style:table-column-properties');
|
||||
$xmlWriter->writeAttribute('style:column-width', number_format($width * 0.0017638889, 2, '.', '') . 'cm');
|
||||
$xmlWriter->endElement(); // style:table-column-properties
|
||||
$xmlWriter->endElement(); // style:style
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,21 +76,7 @@ class Table extends AbstractElement
|
|||
*/
|
||||
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
|
||||
{
|
||||
$rows = $element->getRows();
|
||||
$rowCount = count($rows);
|
||||
|
||||
$cellWidths = array();
|
||||
for ($i = 0; $i < $rowCount; $i++) {
|
||||
$row = $rows[$i];
|
||||
$cells = $row->getCells();
|
||||
if (count($cells) <= count($cellWidths)) {
|
||||
continue;
|
||||
}
|
||||
$cellWidths = array();
|
||||
foreach ($cells as $cell) {
|
||||
$cellWidths[] = $cell->getWidth();
|
||||
}
|
||||
}
|
||||
$cellWidths = $element->findFirstDefinedCellWidths();
|
||||
|
||||
$xmlWriter->startElement('w:tblGrid');
|
||||
foreach ($cellWidths as $width) {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Style;
|
||||
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\TestHelperDOCX;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
|||
use PhpOffice\PhpWord\ComplexType\ProofState;
|
||||
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\Settings;
|
||||
use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
|
||||
use PhpOffice\PhpWord\SimpleType\Zoom;
|
||||
use PhpOffice\PhpWord\Style\Language;
|
||||
|
|
|
|||
Loading…
Reference in New Issue