Skip to content

Commit 4fa9455

Browse files
authored
Merge pull request #1352 from troosan/column_width_for_odt
write column width in ODT writer
2 parents 844a7c9 + 5b97190 commit 4fa9455

File tree

10 files changed

+123
-43
lines changed

10 files changed

+123
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ v0.15.0 (?? ??? 2018)
3636
- Fix colspan and rowspan for tables in HTML Writer @mattbolt #1292
3737
- Fix parsing of Heading and Title formating @troosan @gthomas2 #465
3838
- Fix Dateformat typo, fix hours casing, add Month-Day-Year formats @ComputerTinker #591
39+
- Fix missing column width in ODText writer @potofcoffee #413
3940

4041
### Changed
4142
- Remove zend-stdlib dependency @Trainmaster #1284

samples/Sample_01_SimpleText.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
use PhpOffice\PhpWord\Style\Font;
3-
use PhpOffice\PhpWord\Style\Paragraph;
43

54
include_once 'Sample_Header.php';
65

src/PhpWord/Element/Table.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,40 @@ public function setWidth($width)
135135
public function countColumns()
136136
{
137137
$columnCount = 0;
138-
if (is_array($this->rows)) {
139-
$rowCount = count($this->rows);
140-
for ($i = 0; $i < $rowCount; $i++) {
141-
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
142-
$row = $this->rows[$i];
143-
$cellCount = count($row->getCells());
144-
if ($columnCount < $cellCount) {
145-
$columnCount = $cellCount;
146-
}
138+
139+
$rowCount = count($this->rows);
140+
for ($i = 0; $i < $rowCount; $i++) {
141+
/** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
142+
$row = $this->rows[$i];
143+
$cellCount = count($row->getCells());
144+
if ($columnCount < $cellCount) {
145+
$columnCount = $cellCount;
147146
}
148147
}
149148

150149
return $columnCount;
151150
}
151+
152+
/**
153+
* The first declared cell width for each column
154+
*
155+
* @return int[]
156+
*/
157+
public function findFirstDefinedCellWidths()
158+
{
159+
$cellWidths = array();
160+
161+
foreach ($this->rows as $row) {
162+
$cells = $row->getCells();
163+
if (count($cells) <= count($cellWidths)) {
164+
continue;
165+
}
166+
$cellWidths = array();
167+
foreach ($cells as $cell) {
168+
$cellWidths[] = $cell->getWidth();
169+
}
170+
}
171+
172+
return $cellWidths;
173+
}
152174
}

src/PhpWord/Style/Table.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ class Table extends Border
163163
/** @var TblWidthComplexType|null */
164164
private $indent;
165165

166+
/**
167+
* The width of each column, computed based on the max cell width of each column
168+
*
169+
* @var int[]
170+
*/
171+
private $columnWidths;
172+
166173
/**
167174
* Create new table style
168175
*
@@ -748,4 +755,24 @@ public function setIndent(TblWidthComplexType $indent)
748755

749756
return $this;
750757
}
758+
759+
/**
760+
* Get the columnWidths
761+
*
762+
* @return number[]
763+
*/
764+
public function getColumnWidths()
765+
{
766+
return $this->columnWidths;
767+
}
768+
769+
/**
770+
* The column widths
771+
*
772+
* @param int[] $value
773+
*/
774+
public function setColumnWidths(array $value = null)
775+
{
776+
$this->columnWidths = $value;
777+
}
751778
}

src/PhpWord/Writer/ODText/Element/Table.php

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
1919

20+
use PhpOffice\Common\XMLWriter;
21+
use PhpOffice\PhpWord\Element\Row as RowElement;
22+
use PhpOffice\PhpWord\Element\Table as TableElement;
23+
2024
/**
2125
* Table element writer
2226
*
@@ -36,32 +40,59 @@ public function write()
3640
}
3741
$rows = $element->getRows();
3842
$rowCount = count($rows);
39-
$colCount = $element->countColumns();
4043

4144
if ($rowCount > 0) {
4245
$xmlWriter->startElement('table:table');
4346
$xmlWriter->writeAttribute('table:name', $element->getElementId());
4447
$xmlWriter->writeAttribute('table:style', $element->getElementId());
4548

46-
$xmlWriter->startElement('table:table-column');
47-
$xmlWriter->writeAttribute('table:number-columns-repeated', $colCount);
48-
$xmlWriter->endElement(); // table:table-column
49+
// Write columns
50+
$this->writeColumns($xmlWriter, $element);
4951

52+
// Write rows
5053
foreach ($rows as $row) {
51-
$xmlWriter->startElement('table:table-row');
52-
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
53-
foreach ($row->getCells() as $cell) {
54-
$xmlWriter->startElement('table:table-cell');
55-
$xmlWriter->writeAttribute('office:value-type', 'string');
56-
57-
$containerWriter = new Container($xmlWriter, $cell);
58-
$containerWriter->write();
59-
60-
$xmlWriter->endElement(); // table:table-cell
61-
}
62-
$xmlWriter->endElement(); // table:table-row
54+
$this->writeRow($xmlWriter, $row);
6355
}
6456
$xmlWriter->endElement(); // table:table
6557
}
6658
}
59+
60+
/**
61+
* Write column.
62+
*
63+
* @param \PhpOffice\Common\XMLWriter $xmlWriter
64+
* @param \PhpOffice\PhpWord\Element\Table $element
65+
*/
66+
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
67+
{
68+
$colCount = $element->countColumns();
69+
70+
for ($i = 0; $i < $colCount; $i++) {
71+
$xmlWriter->startElement('table:table-column');
72+
$xmlWriter->writeAttribute('table:style-name', $element->getElementId() . '.' . $i);
73+
$xmlWriter->endElement();
74+
}
75+
}
76+
77+
/**
78+
* Write row.
79+
*
80+
* @param \PhpOffice\Common\XMLWriter $xmlWriter
81+
* @param \PhpOffice\PhpWord\Element\Row $row
82+
*/
83+
private function writeRow(XMLWriter $xmlWriter, RowElement $row)
84+
{
85+
$xmlWriter->startElement('table:table-row');
86+
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
87+
foreach ($row->getCells() as $cell) {
88+
$xmlWriter->startElement('table:table-cell');
89+
$xmlWriter->writeAttribute('office:value-type', 'string');
90+
91+
$containerWriter = new Container($xmlWriter, $cell);
92+
$containerWriter->write();
93+
94+
$xmlWriter->endElement(); // table:table-cell
95+
}
96+
$xmlWriter->endElement(); // table:table-row
97+
}
6798
}

src/PhpWord/Writer/ODText/Part/Content.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,15 @@ private function getContainerStyle($container, &$paragraphStyleCount, &$fontStyl
239239
$style->setStyleName('fr' . $element->getMediaIndex());
240240
$this->autoStyles['Image'][] = $style;
241241
} elseif ($element instanceof Table) {
242+
/** @var \PhpOffice\PhpWord\Style\Table $style */
242243
$style = $element->getStyle();
243244
if ($style === null) {
244245
$style = new TableStyle();
245246
} elseif (is_string($style)) {
246247
$style = Style::getStyle($style);
247248
}
248249
$style->setStyleName($element->getElementId());
250+
$style->setColumnWidths($element->findFirstDefinedCellWidths());
249251
$this->autoStyles['Table'][] = $style;
250252
}
251253
}

src/PhpWord/Writer/ODText/Style/Table.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,19 @@ public function write()
4545
$xmlWriter->writeAttribute('table:align', 'center');
4646
$xmlWriter->endElement(); // style:table-properties
4747
$xmlWriter->endElement(); // style:style
48+
49+
$cellWidths = $style->getColumnWidths();
50+
$countCellWidths = count($cellWidths);
51+
52+
for ($i = 0; $i < $countCellWidths; $i++) {
53+
$width = $cellWidths[$i];
54+
$xmlWriter->startElement('style:style');
55+
$xmlWriter->writeAttribute('style:name', $style->getStyleName() . '.' . $i);
56+
$xmlWriter->writeAttribute('style:family', 'table-column');
57+
$xmlWriter->startElement('style:table-column-properties');
58+
$xmlWriter->writeAttribute('style:column-width', number_format($width * 0.0017638889, 2, '.', '') . 'cm');
59+
$xmlWriter->endElement(); // style:table-column-properties
60+
$xmlWriter->endElement(); // style:style
61+
}
4862
}
4963
}

src/PhpWord/Writer/Word2007/Element/Table.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,7 @@ public function write()
7676
*/
7777
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
7878
{
79-
$rows = $element->getRows();
80-
$rowCount = count($rows);
81-
82-
$cellWidths = array();
83-
for ($i = 0; $i < $rowCount; $i++) {
84-
$row = $rows[$i];
85-
$cells = $row->getCells();
86-
if (count($cells) <= count($cellWidths)) {
87-
continue;
88-
}
89-
$cellWidths = array();
90-
foreach ($cells as $cell) {
91-
$cellWidths[] = $cell->getWidth();
92-
}
93-
}
79+
$cellWidths = $element->findFirstDefinedCellWidths();
9480

9581
$xmlWriter->startElement('w:tblGrid');
9682
foreach ($cellWidths as $width) {

tests/PhpWord/Style/PaperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20-
use PhpOffice\PhpWord\PhpWord;
2120
use PhpOffice\PhpWord\TestHelperDOCX;
2221

2322
/**

tests/PhpWord/Writer/Word2007/Part/SettingsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use PhpOffice\PhpWord\ComplexType\ProofState;
2121
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
2222
use PhpOffice\PhpWord\PhpWord;
23-
use PhpOffice\PhpWord\Settings;
2423
use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
2524
use PhpOffice\PhpWord\SimpleType\Zoom;
2625
use PhpOffice\PhpWord\Style\Language;

0 commit comments

Comments
 (0)