Skip to content

Commit

Permalink
Merge pull request #335 from Progi1984/issue240
Browse files Browse the repository at this point in the history
#240 : PowerPoint2007 Reader : Support of Table
  • Loading branch information
Progi1984 authored Mar 5, 2017
2 parents 0889665 + 0b98604 commit 3ae7102
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 370 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
- 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
- Fixed the corruption of file when an addExternalSlide is called - @Progi1984 GH-240

### Features
- ODPresentation Writer : Show/Hide Value / Name / Series Name in Chart - @Progi1984 GH-272
- ODPresentation Writer : Axis Bounds in Chart - @Progi1984 GH-269
- PowerPoint2007 Reader : Support of Table - @Progi1984 GH-240
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301
- PowerPoint2007 Writer : Axis Bounds in Chart - @Progi1984 GH-269
- PowerPoint2007 Writer : Implement Legend Key in Series for Chart - @Progi1984 GH-319
Expand Down
23 changes: 23 additions & 0 deletions samples/Sample_20_ExternalSlide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
include_once 'Sample_Header.php';

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\RichText;

// Create new PHPPresentation object
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
$objPHPPresentation = new PhpPresentation();
$objPHPPresentation->removeSlideByIndex(0);

$oReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
$oPresentation04 = $oReader->load(__DIR__ . '/results/Sample_04_Table.pptx');

foreach ($oPresentation04->getAllSlides() as $oSlide) {
$objPHPPresentation->addExternalSlide($oSlide);
}

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
4 changes: 3 additions & 1 deletion src/PhpPresentation/PhpPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public function addExternalSlide(Slide $slide)
{
$slide->rebindParent($this);

$this->addMasterSlide($slide->getSlideLayout()->getSlideMaster());

return $this->addSlide($slide);
}

Expand Down Expand Up @@ -353,8 +355,8 @@ public function createMasterSlide()
* Add masterslide
*
* @param \PhpOffice\PhpPresentation\Slide\SlideMaster $slide
* @return \PhpOffice\PhpPresentation\Slide\SlideMaster
* @throws \Exception
* @retun \PhpOffice\PhpPresentation\Slide\SlideMaster
*/
public function addMasterSlide(SlideMaster $slide = null)
{
Expand Down
461 changes: 355 additions & 106 deletions src/PhpPresentation/Reader/PowerPoint2007.php

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/PhpPresentation/Shape/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ public function createRow()
return $row;
}

/**
* @return int
*/
public function getNumColumns()
{
return $this->columnCount;
}

/**
* @param int $numColumn
* @return Table
*/
public function setNumColumns($numColumn)
{
$this->columnCount = $numColumn;
return $this;
}

/**
* Get hash code
*
Expand Down
21 changes: 13 additions & 8 deletions src/PhpPresentation/Shape/Table/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,21 @@ public function getParagraph($index = 0)
*/
public function createParagraph()
{
$alignment = clone $this->getActiveParagraph()->getAlignment();
$font = clone $this->getActiveParagraph()->getFont();
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();

$this->richTextParagraphs[] = new Paragraph();
$this->activeParagraph = count($this->richTextParagraphs) - 1;

$this->getActiveParagraph()->setAlignment($alignment);
$this->getActiveParagraph()->setFont($font);
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
if (count($this->richTextParagraphs) > 1) {
$alignment = clone $this->getActiveParagraph()->getAlignment();
$font = clone $this->getActiveParagraph()->getFont();
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();

$this->activeParagraph = count($this->richTextParagraphs) - 1;

$this->getActiveParagraph()->setAlignment($alignment);
$this->getActiveParagraph()->setFont($font);
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
} else {
$this->activeParagraph = count($this->richTextParagraphs) - 1;
}

return $this->getActiveParagraph();
}
Expand Down
8 changes: 8 additions & 0 deletions src/PhpPresentation/Slide/SlideLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ public function setLayoutName($layoutName)
$this->layoutName = $layoutName;
return $this;
}

/**
* @return SlideMaster
*/
public function getSlideMaster()
{
return $this->slideMaster;
}
}
10 changes: 7 additions & 3 deletions src/PhpPresentation/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,19 @@ public function getRGB()
/**
* Set RGB
*
* @param string $pValue
* @param string $pValue
* @param string $pAlpha
* @return \PhpOffice\PhpPresentation\Style\Color
*/
public function setRGB($pValue = '000000')
public function setRGB($pValue = '000000', $pAlpha = 'FF')
{
if ($pValue == '') {
$pValue = '000000';
}
$this->argb = 'FF' . $pValue;
if ($pAlpha == '') {
$pAlpha = 'FF';
}
$this->argb = $pAlpha . $pValue;

return $this;
}
Expand Down
55 changes: 37 additions & 18 deletions src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,55 +303,64 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
{
// p:graphicFrame
$objWriter->startElement('p:graphicFrame');
// p:nvGraphicFramePr
// p:graphicFrame/p:nvGraphicFramePr
$objWriter->startElement('p:nvGraphicFramePr');
// p:cNvPr
// p:graphicFrame/p:nvGraphicFramePr/p:cNvPr
$objWriter->startElement('p:cNvPr');
$objWriter->writeAttribute('id', $shapeId);
$objWriter->writeAttribute('name', $shape->getName());
$objWriter->writeAttribute('descr', $shape->getDescription());
$objWriter->endElement();
// p:cNvGraphicFramePr
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr
$objWriter->startElement('p:cNvGraphicFramePr');
// a:graphicFrameLocks
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr/a:graphicFrameLocks
$objWriter->startElement('a:graphicFrameLocks');
$objWriter->writeAttribute('noGrp', '1');
$objWriter->endElement();
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr/
$objWriter->endElement();
// p:nvPr
$objWriter->writeElement('p:nvPr', null);
// p:graphicFrame/p:nvGraphicFramePr/p:nvPr
$objWriter->startElement('p:nvPr');
if ($shape->isPlaceholder()) {
$objWriter->startElement('p:ph');
$objWriter->writeAttribute('type', $shape->getPlaceholder()->getType());
$objWriter->endElement();
}
$objWriter->endElement();
// p:xfrm
// p:graphicFrame/p:nvGraphicFramePr/
$objWriter->endElement();
// p:graphicFrame/p:xfrm
$objWriter->startElement('p:xfrm');
// a:off
// p:graphicFrame/p:xfrm/a:off
$objWriter->startElement('a:off');
$objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX()));
$objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY()));
$objWriter->endElement();
// a:ext
// p:graphicFrame/p:xfrm/a:ext
$objWriter->startElement('a:ext');
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth()));
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight()));
$objWriter->endElement();
// p:graphicFrame/p:xfrm/
$objWriter->endElement();
// a:graphic
// p:graphicFrame/a:graphic
$objWriter->startElement('a:graphic');
// a:graphicData
// p:graphicFrame/a:graphic/a:graphicData
$objWriter->startElement('a:graphicData');
$objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/table');
// a:tbl
// p:graphicFrame/a:graphic/a:graphicData/a:tbl
$objWriter->startElement('a:tbl');
// a:tblPr
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblPr
$objWriter->startElement('a:tblPr');
$objWriter->writeAttribute('firstRow', '1');
$objWriter->writeAttribute('bandRow', '1');
$objWriter->endElement();
// a:tblGrid
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid
$objWriter->startElement('a:tblGrid');
// Write cell widths
$countCells = count($shape->getRow(0)->getCells());
for ($cell = 0; $cell < $countCells; $cell++) {
// a:gridCol
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid/a:gridCol
$objWriter->startElement('a:gridCol');
// Calculate column width
$width = $shape->getRow(0)->getCell($cell)->getWidth();
Expand All @@ -363,6 +372,7 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
$objWriter->writeAttribute('w', CommonDrawing::pixelsToEmu($width));
$objWriter->endElement();
}
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid/
$objWriter->endElement();
// Colspan / rowspan containers
$colSpan = array();
Expand All @@ -372,7 +382,7 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
// Write rows
$countRows = count($shape->getRows());
for ($row = 0; $row < $countRows; $row++) {
// a:tr
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tr
$objWriter->startElement('a:tr');
$objWriter->writeAttribute('h', CommonDrawing::pixelsToEmu($shape->getRow($row)->getHeight()));
// Write cells
Expand Down Expand Up @@ -408,12 +418,13 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
}
// a:txBody
$objWriter->startElement('a:txBody');
// a:bodyPr
// a:txBody/a:bodyPr
$objWriter->startElement('a:bodyPr');
$objWriter->writeAttribute('wrap', 'square');
$objWriter->writeAttribute('rtlCol', '0');
// a:spAutoFit
// a:txBody/a:bodyPr/a:spAutoFit
$objWriter->writeElement('a:spAutoFit', null);
// a:txBody/a:bodyPr/
$objWriter->endElement();
// a:lstStyle
$objWriter->writeElement('a:lstStyle', null);
Expand All @@ -428,6 +439,14 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) {
$objWriter->writeAttribute('anchor', $verticalAlign);
}

// Margins
$alignment = $firstParagraph->getAlignment();
$objWriter->writeAttribute('marL', $alignment->getMarginLeft());
$objWriter->writeAttribute('marR', $alignment->getMarginRight());
$objWriter->writeAttribute('marT', $alignment->getMarginTop());
$objWriter->writeAttribute('marB', $alignment->getMarginBottom());

// Determine borders
$borderLeft = $currentCell->getBorders()->getLeft();
$borderRight = $currentCell->getBorders()->getRight();
Expand Down
Loading

0 comments on commit 3ae7102

Please sign in to comment.