Set Table Cell Alignment - PHPPowerPoint

1.1k views Asked by At

I am struggling to set the alignment of a cell in PHPPowerpoint, I have searched and searched and can't seem to find anything

Here is a snippet of my PHP powerpoint building script -

$shape = $currentSlide->createTableShape(14);
$shape->setHeight(100);
$shape->setWidth(800);
$shape->setOffsetX(50);
$shape->setOffsetY(200);

echo date('H:i:s') . ' Add Header Row'.EOL;
$row = $shape->createRow()->setHeight(15);
$row->nextCell()->setWidth(75)->createTextRun('')->getFont()->setBold(true)->setSize(11);
$row->nextCell()->setColSpan(6)->createTextRun('Tests')->getFont()->setBold(true)->setSize(11);

$row->getCell()->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_BOTTOM); // trying to set alignment here

$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->setColSpan(6)->createTextRun('Long Tests')->getFont()->setBold(true)->setSize(11);
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->createTextRun('');
$row->nextCell()->setWidth(75)->createTextRun('XTests')->getFont()->setBold(true)->setSize(11);

I have tried setting the alignment inline with the text creation of the cell, like this -

 $row->nextCell()->setColSpan(6)->createTextRun('Tests')->getFont()->setBold(true)->setSize(11)->getCell()->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_BOTTOM);

This throws an error saying alignment doesn't exist in the font class, which to be fair it doesn't but I cannot seem to get into the alignment in any better way.

Secondly I have tried including the alignment code directly after the creation of the text in the cell -

$row->getCell()->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_BOTTOM); // trying to set alignment here

This seems to run, but doesn't have any effect on the slide that has been generated.

Any advice greatly appreciated!

1

There are 1 answers

1
kingcobra1986 On

So this worked for me.

Each cell I edited I created a variable for it:

$cellA1 = $row-> nextCell(); //a1 is the address it would have in an excel sheet

Then I created a variable for the textRun itself:

$textRunA1 = $cellA1 -> createTextRun('PAGE');//Page is what will be printed in the cell A1

From there I added my styles:

$textRunA1 -> getFont() -> setBold(true);
$textRunA1 -> getFont() -> setSize(12);
$textRunA1 -> getFont() -> setColor(new Color('FFFFFFFF'));

After that I put this in to get the centering right:

$cellA1 -> getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_BOTTOM);

I used the getActiveParagraph() off of the cell itself.