Skip to content

Commit

Permalink
PHPOffice#315 : PowerPoint2007 Writer : Support of margins in cell in…
Browse files Browse the repository at this point in the history
… table
  • Loading branch information
Progi1984 committed Feb 25, 2017
1 parent 3e0ce0a commit 331fbd7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- PowerPoint2007 Writer : Fix for PowerPoint2007 Writer (Need repair) @Progi1984 GH-266 GH-274 GH-276 GH-282 GH-302
- PowerPoint2007 Writer : Fixed the axis title in bar chart - @pgee70 GH-267
- PowerPoint2007 Writer : Fixed the label position in bar chart - @pgee70 GH-268
- PowerPoint2007 Writer : Support of margins in cell in table - @carlosafonso @Progi1984 GH-273 GH-315

### Features
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301
Expand Down
22 changes: 20 additions & 2 deletions docs/shapes_table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You can access cell objects with `nextCell` method of a Row object.
// Get the second cell
$cellA2 = $row->nextCell();
You can access cell object directly
You can access cell object directly.

.. code-block:: php
Expand All @@ -46,7 +46,25 @@ You can access cell object directly
$cellA1 = $row->getCell(0);
// Get the second cell
$cellA2 = $row->getCell(1);
Define margins of a cell
~~~~~~~~~~~~~~~~~~~~~~~~
Margins of cells are defined by margins of the first paragraph of cell.

For defining margins of cell, you can use the `setMargin*` method of a Alignment object of the active paragraph of a Cell object.

.. code-block:: php
$tableShape = $slide->createTableShape($columns);
$row = $tableShape->createRow();
$cellA1 = $row->nextCell();
$cellA1->getActiveParagraph()->getAlignment()
->setMarginBottom(20)
->setMarginLeft(40)
->setMarginRight(60)
->setMarginTop(80);
Define the width of a cell
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 6 additions & 0 deletions samples/Sample_04_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
$cell->getBorders()->getBottom()->setLineWidth(4)
->setLineStyle(Border::LINE_SINGLE)
->setDashStyle(Border::DASH_DASH);
$cell->getActiveParagraph()->getAlignment()
->setMarginLeft(10);

// Add row
echo date('H:i:s') . ' Add row'.EOL;
Expand All @@ -60,6 +62,7 @@
->setStartColor(new Color('FFE06B20'))
->setEndColor(new Color('FFFFFFFF'));
$row->nextCell()->createTextRun('R1C1')->getFont()->setBold(true);
$row->getCell()->getActiveParagraph()->getAlignment()->setMarginLeft(20);
$row->nextCell()->createTextRun('R1C2')->getFont()->setBold(true);
$row->nextCell()->createTextRun('R1C3')->getFont()->setBold(true);

Expand All @@ -76,6 +79,7 @@
->setStartColor(new Color('FFE06B20'))
->setEndColor(new Color('FFE06B20'));
$row->nextCell()->createTextRun('R2C1');
$row->getCell()->getActiveParagraph()->getAlignment()->setMarginLeft(30);
$row->nextCell()->createTextRun('R2C2');
$row->nextCell()->createTextRun('R2C3');

Expand All @@ -86,6 +90,7 @@
->setStartColor(new Color('FFE06B20'))
->setEndColor(new Color('FFE06B20'));
$row->nextCell()->createTextRun('R3C1');
$row->getCell()->getActiveParagraph()->getAlignment()->setMarginLeft(40);
$row->nextCell()->createTextRun('R3C2');
$row->nextCell()->createTextRun('R3C3');

Expand All @@ -98,6 +103,7 @@
$cellC1 = $row->nextCell();
$textRunC1 = $cellC1->createTextRun('Link');
$textRunC1->getHyperlink()->setUrl('https://github.com/PHPOffice/PHPPresentation/')->setTooltip('PHPPresentation');
$cellC1->getActiveParagraph()->getAlignment()->setMarginLeft(50);
$cellC2 = $row->nextCell();
$textRunC2 = $cellC2->createTextRun('RichText with');
$textRunC2->getFont()->setBold(true);
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpPresentation/Tests/Style/AlignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function testConstruct()
$this->assertEquals(0, $object->getIndent());
$this->assertEquals(0, $object->getMarginLeft());
$this->assertEquals(0, $object->getMarginRight());
$this->assertEquals(0, $object->getMarginTop());
$this->assertEquals(0, $object->getMarginBottom());
}

/**
Expand Down Expand Up @@ -109,6 +111,19 @@ public function testSetGetIndent()
$this->assertEquals($value, $object->getIndent());
}

/**
* Test get/set margin bottom
*/
public function testSetGetMarginBottom()
{
$object = new Alignment();
$value = rand(0, 100);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->setMarginBottom($value));
$this->assertEquals($value, $object->getMarginBottom());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->setMarginBottom());
$this->assertEquals(0, $object->getMarginBottom());
}

/**
* Test get/set margin left
*/
Expand Down Expand Up @@ -157,6 +172,19 @@ public function testSetGetMarginRight()
$this->assertEquals($value, $object->getMarginRight());
}

/**
* Test get/set margin top
*/
public function testSetGetMarginTop()
{
$object = new Alignment();
$value = rand(1, 100);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->setMarginTop($value));
$this->assertEquals($value, $object->getMarginTop());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->setMarginTop());
$this->assertEquals(0, $object->getMarginTop());
}

/**
* Test get/set hash index
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,31 @@ public function testTableWithBorder()
$this->assertTrue($pres->elementExists($element . '/a:lnB[@cmpd="' . Border::LINE_SINGLE . '"]/a:prstDash[@val="' . Border::DASH_SOLID . '"]', 'ppt/slides/slide1.xml'));
}

public function testTableWithCellMargin()
{
$oPhpPresentation = new PhpPresentation();
$oSlide = $oPhpPresentation->getActiveSlide();
$oShape = $oSlide->createTableShape(4);
$oShape->setHeight(200)->setWidth(600)->setOffsetX(150)->setOffsetY(300);
$oRow = $oShape->createRow();
$oCell = $oRow->getCell();
$oCell->createTextRun('AAA');
$oCell->getActiveParagraph()->getAlignment()
->setMarginBottom(10)
->setMarginLeft(20)
->setMarginRight(30)
->setMarginTop(40);

$pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007');

$element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tr/a:tc/a:tcPr';
$this->assertTrue($pres->elementExists($element, 'ppt/slides/slide1.xml'));
$this->assertEquals(10, $pres->getElementAttribute($element, 'marB', 'ppt/slides/slide1.xml'));
$this->assertEquals(20, $pres->getElementAttribute($element, 'marL', 'ppt/slides/slide1.xml'));
$this->assertEquals(30, $pres->getElementAttribute($element, 'marR', 'ppt/slides/slide1.xml'));
$this->assertEquals(40, $pres->getElementAttribute($element, 'marT', 'ppt/slides/slide1.xml'));
}

public function testTableWithColspan()
{
$oPhpPresentation = new PhpPresentation();
Expand Down

0 comments on commit 331fbd7

Please sign in to comment.