Get rows
public getRows ( ) : PhpOffice\PhpPresentation\Shape\Table\Row[] | ||
리턴 | PhpOffice\PhpPresentation\Shape\Table\Row[] |
public function testRows() { $object = new Table(); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Row', $object->createRow()); $this->assertCount(1, $object->getRows()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Row', $object->getRow(0)); $this->assertNull($object->getRow(1, true)); }
/** * Write the default style information for a Table shape * * @param XMLWriter $objWriter * @param Table $shape */ protected function writeTableStyle(XMLWriter $objWriter, Table $shape) { foreach ($shape->getRows() as $row) { foreach ($row->getCells() as $cell) { if ($cell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) { if (!in_array($cell->getFill()->getHashCode(), $this->arrayGradient)) { $this->writeGradientFill($objWriter, $cell->getFill()); } } } } }
/** * Write the default style information for a Table shape * * @param XMLWriter $objWriter * @param Table $shape */ public function writeTableStyle(XMLWriter $objWriter, Table $shape) { foreach ($shape->getRows() as $keyRow => $shapeRow) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'gr' . $this->shapeId . 'r' . $keyRow); $objWriter->writeAttribute('style:family', 'table-row'); // style:table-row-properties $objWriter->startElement('style:table-row-properties'); $objWriter->writeAttribute('style:row-height', Text::numberFormat(CommonDrawing::pixelsToCentimeters(CommonDrawing::pointsToPixels($shapeRow->getHeight())), 3) . 'cm'); $objWriter->endElement(); $objWriter->endElement(); foreach ($shapeRow->getCells() as $keyCell => $shapeCell) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'gr' . $this->shapeId . 'r' . $keyRow . 'c' . $keyCell); $objWriter->writeAttribute('style:family', 'table-cell'); // style:graphic-properties $objWriter->startElement('style:graphic-properties'); if ($shapeCell->getFill()->getFillType() == Fill::FILL_SOLID) { $objWriter->writeAttribute('draw:fill', 'solid'); $objWriter->writeAttribute('draw:fill-color', '#' . $shapeCell->getFill()->getStartColor()->getRGB()); } if ($shapeCell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) { $objWriter->writeAttribute('draw:fill', 'gradient'); $objWriter->writeAttribute('draw:fill-gradient-name', 'gradient_' . $shapeCell->getFill()->getHashCode()); } $objWriter->endElement(); // <style:graphic-properties // style:paragraph-properties $objWriter->startElement('style:paragraph-properties'); if ($shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getTop()->getHashCode() && $shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getLeft()->getHashCode() && $shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getRight()->getHashCode()) { $lineStyle = 'none'; $lineWidth = Text::numberFormat($shapeCell->getBorders()->getBottom()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getBottom()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getBottom()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); } else { $lineStyle = 'none'; $lineWidth = Text::numberFormat($shapeCell->getBorders()->getBottom()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getBottom()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getBottom()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-bottom', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // TOP $lineStyle = 'none'; $lineWidth = Text::numberFormat($shapeCell->getBorders()->getTop()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getTop()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getTop()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-top', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // RIGHT $lineStyle = 'none'; $lineWidth = Text::numberFormat($shapeCell->getBorders()->getRight()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getRight()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getRight()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-right', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // LEFT $lineStyle = 'none'; $lineWidth = Text::numberFormat($shapeCell->getBorders()->getLeft()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getLeft()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getLeft()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-left', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); } $objWriter->endElement(); $objWriter->endElement(); foreach ($shapeCell->getParagraphs() as $shapeParagraph) { foreach ($shapeParagraph->getRichTextElements() as $shapeRichText) { if ($shapeRichText instanceof Run) { // Style des font text if (!isset($this->arrStyleTextFont[$shapeRichText->getFont()->getHashCode()])) { $this->arrStyleTextFont[$shapeRichText->getFont()->getHashCode()] = $shapeRichText->getFont(); } } } } } } }
/** * Write table * * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer * @param \PhpOffice\PhpPresentation\Shape\Table $shape * @param int $shapeId * @throws \Exception */ private function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $shapeId) { // p:graphicFrame $objWriter->startElement('p:graphicFrame'); // p:nvGraphicFramePr $objWriter->startElement('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 $objWriter->startElement('p:cNvGraphicFramePr'); // a:graphicFrameLocks $objWriter->startElement('a:graphicFrameLocks'); $objWriter->writeAttribute('noGrp', '1'); $objWriter->endElement(); $objWriter->endElement(); // p:nvPr $objWriter->writeElement('p:nvPr', null); $objWriter->endElement(); // p:xfrm $objWriter->startElement('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 $objWriter->startElement('a:ext'); $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); $objWriter->endElement(); $objWriter->endElement(); // a:graphic $objWriter->startElement('a:graphic'); // a:graphicData $objWriter->startElement('a:graphicData'); $objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/table'); // a:tbl $objWriter->startElement('a:tbl'); // a:tblPr $objWriter->startElement('a:tblPr'); $objWriter->writeAttribute('firstRow', '1'); $objWriter->writeAttribute('bandRow', '1'); $objWriter->endElement(); // a:tblGrid $objWriter->startElement('a:tblGrid'); // Write cell widths $countCells = count($shape->getRow(0)->getCells()); for ($cell = 0; $cell < $countCells; $cell++) { // a:gridCol $objWriter->startElement('a:gridCol'); // Calculate column width $width = $shape->getRow(0)->getCell($cell)->getWidth(); if ($width == 0) { $colCount = count($shape->getRow(0)->getCells()); $totalWidth = $shape->getWidth(); $width = $totalWidth / $colCount; } $objWriter->writeAttribute('w', CommonDrawing::pixelsToEmu($width)); $objWriter->endElement(); } $objWriter->endElement(); // Colspan / rowspan containers $colSpan = array(); $rowSpan = array(); // Default border style $defaultBorder = new Border(); // Write rows $countRows = count($shape->getRows()); for ($row = 0; $row < $countRows; $row++) { // a:tr $objWriter->startElement('a:tr'); $objWriter->writeAttribute('h', CommonDrawing::pixelsToEmu($shape->getRow($row)->getHeight())); // Write cells $countCells = count($shape->getRow($row)->getCells()); for ($cell = 0; $cell < $countCells; $cell++) { // Current cell $currentCell = $shape->getRow($row)->getCell($cell); // Next cell right $nextCellRight = $shape->getRow($row)->getCell($cell + 1, true); // Next cell below $nextRowBelow = $shape->getRow($row + 1, true); $nextCellBelow = null; if ($nextRowBelow != null) { $nextCellBelow = $nextRowBelow->getCell($cell, true); } // a:tc $objWriter->startElement('a:tc'); // Colspan if ($currentCell->getColSpan() > 1) { $objWriter->writeAttribute('gridSpan', $currentCell->getColSpan()); $colSpan[$row] = $currentCell->getColSpan() - 1; } elseif (isset($colSpan[$row]) && $colSpan[$row] > 0) { $colSpan[$row]--; $objWriter->writeAttribute('hMerge', '1'); } // Rowspan if ($currentCell->getRowSpan() > 1) { $objWriter->writeAttribute('rowSpan', $currentCell->getRowSpan()); $rowSpan[$cell] = $currentCell->getRowSpan() - 1; } elseif (isset($rowSpan[$cell]) && $rowSpan[$cell] > 0) { $rowSpan[$cell]--; $objWriter->writeAttribute('vMerge', '1'); } // a:txBody $objWriter->startElement('a:txBody'); // a:bodyPr $objWriter->startElement('a:bodyPr'); $objWriter->writeAttribute('wrap', 'square'); $objWriter->writeAttribute('rtlCol', '0'); // a:spAutoFit $objWriter->writeElement('a:spAutoFit', null); $objWriter->endElement(); // a:lstStyle $objWriter->writeElement('a:lstStyle', null); // Write paragraphs $this->writeParagraphs($objWriter, $currentCell->getParagraphs()); $objWriter->endElement(); // a:tcPr $objWriter->startElement('a:tcPr'); // Alignment (horizontal) $firstParagraph = $currentCell->getParagraph(0); $verticalAlign = $firstParagraph->getAlignment()->getVertical(); if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) { $objWriter->writeAttribute('anchor', $verticalAlign); } // Determine borders $borderLeft = $currentCell->getBorders()->getLeft(); $borderRight = $currentCell->getBorders()->getRight(); $borderTop = $currentCell->getBorders()->getTop(); $borderBottom = $currentCell->getBorders()->getBottom(); $borderDiagonalDown = $currentCell->getBorders()->getDiagonalDown(); $borderDiagonalUp = $currentCell->getBorders()->getDiagonalUp(); // Fix PowerPoint implementation if (!is_null($nextCellRight) && $nextCellRight->getBorders()->getRight()->getHashCode() != $defaultBorder->getHashCode()) { $borderRight = $nextCellRight->getBorders()->getLeft(); } if (!is_null($nextCellBelow) && $nextCellBelow->getBorders()->getBottom()->getHashCode() != $defaultBorder->getHashCode()) { $borderBottom = $nextCellBelow->getBorders()->getTop(); } // Write borders $this->writeBorder($objWriter, $borderLeft, 'L'); $this->writeBorder($objWriter, $borderRight, 'R'); $this->writeBorder($objWriter, $borderTop, 'T'); $this->writeBorder($objWriter, $borderBottom, 'B'); $this->writeBorder($objWriter, $borderDiagonalDown, 'TlToBr'); $this->writeBorder($objWriter, $borderDiagonalUp, 'BlToTr'); // Fill $this->writeFill($objWriter, $currentCell->getFill()); $objWriter->endElement(); $objWriter->endElement(); } $objWriter->endElement(); } $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); }