Colspan and Rowspan

Nov 29, 2010 at 12:04 PM

Hello:

I had a problem because I want to use colspan and rowspan  in my tables but when I read the documentation, and the discussions, I found that is not supported and for  that I did some changes for use these...

 

First I modified the file phpword/Style/Cell.php and  I added the attributes these attributes

private $_gridSpan;// for the colspan

private $_vMerge;// for the rowspan

 

and these methods

 

public function setGridSpan($pValue = null) 
{ 
   $this->_gridSpan = $pValue; 
} 
public function getGridSpan() 
{ 
   return $this->_gridSpan; 
}
public function setVMerge($pValue = null) 
{ 
   $this->_vMerge = $pValue; 
} 
public function getVMerge() 
{ 
   return $this->_vMerge; 
}

 

and in the contructor I added at the end

 

$this->_gridSpan=null;

$this->_vMerge=null;

And in the file phpWord/Writer/Base.php in the method _writeCellStyle I added this

 

$gridSpan = $style->getGridSpan();
if(!is_null($gridSpan)) 
{ 
    $objWriter->startElement('w:gridSpan'); 
    $objWriter->writeAttribute('w:val', $gridSpan); 
    $objWriter->endElement(); 
}

$vMerge = $style->getVMerge(); 
if(!is_null($vMerge)) 
{ 
    $objWriter->startElement('w:vMerge'); 
    $objWriter->writeAttribute('w:val', $vMerge); 
    $objWriter->endElement(); 
}

(I don't add these lines of code in the way that was implement because this way is more clear to show the changes)
Ok... how's this work.
The code the colspan works is symple:

 

$styleCell=array('gridSpan' => 2);

$table->addCell(100,$styleCell)->addText('Colspan');

 

And with that you have a cell that combine two, 
The rowspan is more complicated, and until now i don't found yet a better solution

You have to do it this way:

 

$table->addCell(100,array('vMerge' => 'restart'))->addText('1');

$table->addCell(100)->addText('2');

$table->addRow();

$table->addCell(100,array('vMerge' => 'fusion'));

$table->addCell(100)->addText('3');

 

This will get something like this:

Rowspan 2
3

the first cell must have the option restart and all the other must have the option fusion

 

I hope your comments...

 

Greetings... (Sorry for my english)

 

 

 

 

 

 

 

 

Nov 30, 2010 at 1:49 PM

Oh, thank you! This is very useful and appreciated. I will test it in the next few weeks and report back.

Since you have modified the code in a complex way, I want to ask you a question. Do you know how to modify the addImage() function so that it does not make a trailing line break? A new function like addImageNoLineBreak() ?

Dec 7, 2010 at 3:04 PM

I did test the colSpan function and it worked very well. Thank you for this addition!

Sep 10, 2011 at 5:29 PM

This is what i did, and it does not work. Please assist.

1. /Style/Cell.php added in red at the location r77-79

"

     * cspan rspan addon
     *
     *
     */
    private $_gridSpan;// for the colspan

    private $_vMerge;// for the rowspan

    /**
     * Create a new Table Cell

 

2. In _construct added at r93-94 in red

 

"    public function __construct($insideOf, $pCount, $width = null, $style = null) {
        $this->_insideOf = $insideOf;
        $this->_pCount = $pCount;
        $this->_width = $width;
        $this->_gridSpan=null;
        $this->_vMerge=null;"

3. and at the bottom of the page added r328-343

"    public function setGridSpan($pValue = null)
{
   $this->_gridSpan = $pValue;
}
public function getGridSpan()
{
   return $this->_gridSpan;
}
public function setVMerge($pValue = null)
{
   $this->_vMerge = $pValue;
}
public function getVMerge()
{
   return $this->_vMerge;
}

}

4. In \Writer\Word2007\Base.php added inside the "if($styles) {" section

 

"        if($styles) {

$gridSpan = $style->getGridSpan();
if(!is_null($gridSpan))
{
    $objWriter->startElement('w:gridSpan');
    $objWriter->writeAttribute('w:val', $gridSpan);
    $objWriter->endElement();
}

$vMerge = $style->getVMerge();
if(!is_null($vMerge))
{
    $objWriter->startElement('w:vMerge');
    $objWriter->writeAttribute('w:val', $vMerge);
    $objWriter->endElement();"

 

Very please assist - I need this to work.

Sep 27, 2011 at 10:23 AM

these changes does not work in version 0.6.2-1 PHPWord Beta ... Any solution?

Thanks. Regards.


Oct 8, 2011 at 2:14 PM

try adding : 

|| !is_null($gridSpan)
to line 518 of  \Writer\Word2007\Base.php, where it is checking if styles have been defined.

$styles = ( !is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders || !is_null($gridSpan) ) ? true : false;

Dec 26, 2011 at 8:22 PM

Funciona perfecto, GRACIAS :)

Jun 11, 2012 at 10:16 AM

Works very well. Should be integrated into PHPWord's next relase, because it's very useful. Thanks to the original author.

Jun 25, 2012 at 8:12 AM
Edited Jun 25, 2012 at 8:13 AM

To let the rowspanning work you have to add vMerge also to the $styles on line 518 of \Writer\Word2007\Base.php, where it is checking if styles have been defined.


$styles = ( !is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders || !is_null($gridSpan) || !is_null($vMerge) ) ? true : false;

Where is the new update form this great contribution???

Aug 10, 2012 at 6:14 AM

Rowspan 2

                 3

is not what we want, if the method just gives us this ,that means it does not succeed! Even without the method ,we can also get this.

Jun 24, 2013 at 8:30 AM
The colspan worked perfectly.. But the rowspan is not working to me..
What is the problem ? The file generated can be found here
Jul 10, 2013 at 1:30 PM
Edited Jul 11, 2013 at 7:55 AM
does it still work ? i edited this two files like u said in the post but i am not able to get it to work... may anyone can send me his edited cell.php and base.php ?

in my do_word.php i tryed this.
$styleCell = array('gridSpan' => 2);


$table = $section->addTable();
$table->addRow();
$table->addCell(1500, $styleCell)->addText('Test Colspan');
$table->addRow();
$table->addCell(750)->addText('Test');
$table->addCell(750)->addText('Test');
but nothing happends...
in the second row there are 2 cells
Jul 10, 2013 at 3:05 PM
Colspan works great but i couldnT figure out how to make rowspan work
Jul 12, 2013 at 10:22 AM
Finally i figured out to use rowspan and code i used is below for who can't use it.
   // Define table style arrays
$styleTable = array('cellMarginLeft'=>110, 'cellMarginRight'=>110, 'borderSize'=>1);
$styleCellMerged = array('valign'=>'center', 'gridSpan' => 2);
$styleCellBTLR1 = array('vMerge' => 'restart', 'valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
$styleCellBTLR2 = array('vMerge' => 'fusion', 'valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Add table style
$PHPWord->addTableStyle('tbl13', $styleTable);
// Add table
$table13 = $section->addTable('tbl13');
// Add rows
$PHPWord->addFontStyle('rStyle14', array('size'=>10));  
$PHPWord->addFontStyle('rStyle14Bold', array('bold'=>true, 'size'=>10));
$PHPWord->addFontStyle('rStyle14Italic', array('italic'=>true, 'size'=>8));
$PHPWord->addParagraphStyle('pStyle24', array('align'=>'left', 'spaceAfter'=>0));
$PHPWord->addParagraphStyle('pStyle24Header', array('align'=>'center', 'spaceAfter'=>0));
$PHPWord->addParagraphStyle('pStyle24Merged', array('align'=>'center', 'spaceAfter'=>0));

$table13->addRow(396);  
$table13->addCell(9810, $styleCellMerged)->addText('DENETİM SONUCUNA İLİŞKİN DENETİM EKİBİNİN TAVSİYE KARARI', 'rStyle14', 'pStyle24Merged');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR1)->addText('İLK{brbr}BEGELENDİRME', 'rStyle14', 'pStyle24Header');
$table13->addCell(7010, $styleCell)->addText('${box} Sertifika verilmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların doküman bazında takibi yapılarak, eksiklerin tamamlanması halinde sertifika verilmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların takibinin yerinde denetimle yapılması ve eksiklerin tamamlanması halinde sertifika verilmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Sertifika verilmemesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR1)->addText('GÖZETİM', 'rStyle14', 'pStyle24Header');
$table13->addCell(7010, $styleCell)->addText('Belgelendirmenin devamı', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların doküman bazında takibi yapılarak, eksiklerin tamamlanması halinde belgelendirmenin devamı', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların takibinin yerinde denetimle yapılması ve eksiklerin tamamlanması halinde belgelendirmenin devamı', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Belgelendirmenin askıya alınması', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Belgelendirmenin iptali', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR1)->addText('YENİDEN{brbr}BELGELENDİRME', 'rStyle14', 'pStyle24Header');
$table13->addCell(7010, $styleCell)->addText('Belgelendirmenin yenilenmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların doküman bazında takibi yapılarak, eksiklerin tamamlanması halinde belgelendirmenin yenilenmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Uygunsuzlukların takibinin yerinde denetimle yapılması ve eksiklerin tamamlanması halinde belgelendirmenin yenilenmesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCellBTLR2);
$table13->addCell(7010, $styleCell)->addText('Belgelendirmenin yenilenmemesi', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCell)->addText(' ', 'rStyle14', 'pStyle24');
$table13->addCell(7010, $styleCell)->addText('Kapsam değişikliği yapılması', 'rStyle14', 'pStyle24');

$table13->addRow(396);  
$table13->addCell(2800, $styleCell)->addText(' ', 'rStyle14', 'pStyle24');
$table13->addCell(7010, $styleCell)->addText('Adres değişikliği yapılması', 'rStyle14', 'pStyle24');
Jul 16, 2013 at 3:11 PM
Need to set below code in Style/Cell.php constructor .
    } elseif($key == '_gridSpan') {
        $this->setGridSpan($value);
    } elseif($key == '_vMerge') {
        $this->setVMerge($value);
    } else {
and below functions
public function setGridSpan($pValue = null) 
{ 
   $this->_gridSpan = $pValue; 
} 
public function getGridSpan() 
{ 
   return $this->_gridSpan; 
}
public function setVMerge($pValue = null) 
{ 
   $this->_vMerge = $pValue; 
} 
public function getVMerge() 
{ 
   return $this->_vMerge; 
}
Sep 18, 2013 at 8:59 AM
Thanks! Checked colSpan and it is working.
Oct 2, 2013 at 8:48 AM
How to make that work colSpan:
Make it as it is written in a previous post

PHPWord\Writer\Word2007\Base.php

protected function _writeCellStyle

$brdCol = $style->getBorderColor();
after :$GridSpan = $style->getGridSpan();

....

$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders) ? true : false;
after

if(!is_null($GridSpan)) {
            $objWriter->startElement('w:gridSpan');
                $objWriter->writeAttribute('w:val', $GridSpan);
            $objWriter->endElement();
    }
Sorry for bad English
Oct 23, 2013 at 1:43 PM
Edited Oct 23, 2013 at 6:00 PM
Hello! A have cheked all chages several times, but It's not works. I can't undersdant what I am doing wrong.

Image
$table = $section->addTable();
$table->addRow();
$table->addCell(1500, $styleCell)->addText('1');
$table->addCell(750)->addText('2');
$table->addRow();
$table->addCell(750)->addText('1.1');
$table->addCell(750)->addText('1.2');
$table->addCell(750)->addText('2');
Help please!
Oct 24, 2013 at 12:22 PM
Hello! Someone! Please post files with correct changes. Thanx!
Dec 2, 2013 at 7:13 AM
Edited Dec 2, 2013 at 7:25 AM
I can't delete this content
write back.
Dec 2, 2013 at 7:25 AM
I write solution!
Try this.....


1. modify file - /Style/Cell.php
(1) add attributes.
    /**
     * Horizontal align
     *
     * @var string
     */
    private $_align;
    /**
     * for the colspan
     */
    private $_gridSpan;
    /**
     * for the rowspan
     */
    private $_vMerge;
(2) modify function __construct()
    public function __construct() {
        $this->_align = null;       //add this line
        $this->_valign = null;
        $this->_textDirection = null;
        $this->_bgColor = null;
        $this->_borderTopSize = null;
        $this->_borderTopColor = null;
        $this->_borderLeftSize = null;
        $this->_borderLeftColor = null;
        $this->_borderRightSize = null;
        $this->_borderRightColor = null;
        $this->_borderBottomSize = null;
        $this->_borderBottomColor = null;
        $this->_defaultBorderColor = '000000';
        $this->_gridSpan = null;        //add this line
        $this->_vMerge = null;      //add this line
    }
(3) modify function setStyleValue($key, $value)
    public function setStyleValue($key, $value) {
        if($key == '_borderSize') {
            $this->setBorderSize($value);
        } elseif($key == '_borderColor') {
            $this->setBorderColor($value);
        } elseif($key == '_gridSpan') {     //add this line
            $this->setGridSpan($value);
        } elseif($key == '_vMerge') {       //add this line
            $this->setVMerge($value);
        } else {
            $this->$key = $value;
        }
    }
(4) add function
    public function getAlign() {
        return $this->_align;
    }
    public function setAlign($pValue = null) {
        $this->_align = $pValue;
    }
    public function setGridSpan($pValue = null) {
        $this->_gridSpan = $pValue;
    }
    public function getGridSpan() {
        return $this->_gridSpan;
    }
    public function setVMerge($pValue = null) {
        $this->_vMerge = $pValue;
    }
    public function getVMerge() {
        return $this->_vMerge;
    }
2. modify file - /Writer/Word2007/Base.php
(1) modify function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null)
    protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) {
        $bgColor = $style->getBgColor();
        $align = $style->getAlign();        //add this line
        $valign = $style->getVAlign();
        $textDir = $style->getTextDirection();
        $brdSz = $style->getBorderSize();
        $brdCol = $style->getBorderColor();
        $gridSpan = $style->getGridSpan();      //add this line
        $vMerge = $style->getVMerge();      //add this line

        $bTop = (!is_null($brdSz[0])) ? true : false;
        $bLeft = (!is_null($brdSz[1])) ? true : false;
        $bRight = (!is_null($brdSz[2])) ? true : false;
        $bBottom = (!is_null($brdSz[3])) ? true : false;
        $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false;

        $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || !is_null($gridSpan) || !is_null($vMerge) || $borders) ? true : false;       //modify this line

        if($styles) {
            if(!is_null($textDir)) {
                $objWriter->startElement('w:textDirection');
                    $objWriter->writeAttribute('w:val', $textDir);
                $objWriter->endElement();
            }

            if(!is_null($bgColor)) {
                $objWriter->startElement('w:shd');
                    $objWriter->writeAttribute('w:val', 'clear');
                    $objWriter->writeAttribute('w:color', 'auto');
                    $objWriter->writeAttribute('w:fill', $bgColor);
                $objWriter->endElement();
            }

            if(!is_null($valign)) {
                $objWriter->startElement('w:vAlign');
                    $objWriter->writeAttribute('w:val', $valign);
                $objWriter->endElement();
            }

            if(!is_null($align)) {      //add this line
                $objWriter->startElement('w:pPr');
                    $objWriter->startElement('w:jc');
                        $objWriter->writeAttribute('w:val', $valign);
                    $objWriter->endElement();
                $objWriter->endElement();
            }

            if(!is_null($gridSpan)) {       //add this line
                $objWriter->startElement('w:gridSpan');
                $objWriter->writeAttribute('w:val', $gridSpan);
                $objWriter->endElement();
            }

            if(!is_null($vMerge)) {     //add this line
                $objWriter->startElement('w:vMerge');
                $objWriter->writeAttribute('w:val', $vMerge);
                $objWriter->endElement();
            }

            if($borders) {
                $_defaultColor = $style->getDefaultBorderColor();

                $objWriter->startElement('w:tcBorders');
                    if($bTop) {
                        if(is_null($brdCol[0])) { $brdCol[0] = $_defaultColor; }
                        $objWriter->startElement('w:top');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[0]);
                            $objWriter->writeAttribute('w:color', $brdCol[0]);
                        $objWriter->endElement();
                    }

                    if($bLeft) {
                        if(is_null($brdCol[1])) { $brdCol[1] = $_defaultColor; }
                        $objWriter->startElement('w:left');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[1]);
                            $objWriter->writeAttribute('w:color', $brdCol[1]);
                        $objWriter->endElement();
                    }

                    if($bRight) {
                        if(is_null($brdCol[2])) { $brdCol[2] = $_defaultColor; }
                        $objWriter->startElement('w:right');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[2]);
                            $objWriter->writeAttribute('w:color', $brdCol[2]);
                        $objWriter->endElement();
                    }

                    if($bBottom) {
                        if(is_null($brdCol[3])) { $brdCol[3] = $_defaultColor; }
                        $objWriter->startElement('w:bottom');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[3]);
                            $objWriter->writeAttribute('w:color', $brdCol[3]);
                        $objWriter->endElement();
                    }

                $objWriter->endElement();
            }
        }
    }
3. Here is my source.
$table = $section->addTable(array('borderSize'=>6, 'borderColor'=>'e3e3e3', 'cellMarginRight'=>80, 'cellMarginLeft'=>80));

$table->addRow();
$table->addCell(2100, array('vMerge' => 'restart'))->addText('ROWSPAN 5', array('width'=>130, 'height'=>150, 'align'=>'center'));       //rowspan count 1
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Name', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('My Name is Landy Kim', array('size'=>9));       //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 2
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Nationality', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('South Korea', array('size'=>9));        //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 3
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Address', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('Top Secret^^', array('size'=>9));       //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 4
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Tel', array('bold'=>true, 'color'=>'666666'));
$table->addCell(2000)->addText('000-000-0000', array('size'=>9));
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Mobile', array('bold'=>true, 'color'=>'666666'));
$table->addCell(2000)->addText('000-0000-0000', array('size'=>9));

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 5
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('email', array('bold'=>true, 'color'=>'666666'));
$table->addCell(500, array('gridSpan' => 3))->addText('jun981126@hotmail.com', array('size'=>9));       //colspan