我正在用 PHP 编写代码来创建 PPT 文档。在此,我以表格格式显示数据,除了一个问题外,一切都很好。对于行的单元格高度中的大字符串,当我在创建行时设置 40px 高度时,行的单元格高度会自动调整大小,因为一些表格行绘制在幻灯片之外。有没有办法计算桌子高度来比较幻灯片高度?
我的代码是:
<?php
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Fill;
$rowHeight = 0;
$rowCount = count($valuesArray);
$SlideHeight = 514;
$tableHeaderTop = 115;
$HeaderRowHeight = 50;
$AccountName = 'XYZ';
$currentSlide = createTemplatedSlide($objPHPPresentation);
createCommonHeading($objPHPPresentation, $currentSlide, "TEAM", $AccountName);
foreach ($valuesArray as $key => $val) {
$row = $tableShape->createRow();
$row->setHeight(40);
$rowHeight += $row->getHeight();
$rowCount --;
$rowNumber++;
if ($key % 2 == 0)
$row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FFcbcdd4'))->setEndColor(new Color('FFcbcdd4'));
else
$row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FFe7e8eb'))->setEndColor(new Color('FFe7e8eb'));
foreach ($val as $k => $v) {
$textAlignment = Alignment::HORIZONTAL_CENTER;
$oCell = $row->nextCell();
$oCell->setWidth(200);
$oCell->getActiveParagraph()->getAlignment()
->setMarginLeft(3)
->setMarginRight(3)
->setMarginTop(3)
->setMarginBottom(3);
createCommonTextRun($oCell, 7, $bold, "Montserrat", 0, $v);
}
$currentHeight = $tableHeaderTop + $HeaderRowHeight + $rowHeight;
if ($currentHeight > $SlideHeight && $rowCount > 0) {
$currentSlide = createTemplatedSlide($objPHPPresentation);
createCommonHeading($objPHPPresentation, $currentSlide, $headingText, $AccountName);
$tableShape = createCommonTable($currentSlide, count($tableHeaderData));
createCommonTableHeaderRows($tableShape, $tableHeaderData, $Module);
$rowHeight = 0;
}
}
function createCommonTextRun($shape, $size, $setBold, $textStyle, $characterSpacing, $content, $color = null) {
$textRun = $shape->createTextRun($content);
$textRun->getFont()->setBold($setBold)
->setName($textStyle);
$textRun->getFont()->setCharacterSpacing($characterSpacing);
$textRun->getFont()->setSize($size);
if ($color != null)
$textRun->getFont()->setColor($color);
}
function createTemplatedSlide(PhpOffice\PhpPresentation\PhpPresentation $objPHPPresentation) {
// Create slide
$slide = $objPHPPresentation->createSlide();
// Return slide
return $slide;
}
function createCommonTable($currentSlide, $columnsCount, $continue = FALSE, $Module = "") {
$tableShape = $currentSlide->createTableShape($columnsCount);
return $tableShape;
}
function createCommonTextShape($slide, $height, $width, $offsetX, $offsetY, $horizontalAlignment) {
$shape = $slide->createRichTextShape();
$shape->setHeight($height);
$shape->setWidth($width);
$shape->setOffsetX($offsetX);
$shape->setOffsetY($offsetY);
$shape->getActiveParagraph()->getAlignment()->setHorizontal($horizontalAlignment);
return $shape;
}
function createCommonHeading($objPHPPresentation, $currentSlide, $headingText, $AccountName, $Module = "") {
$slideIndex = $objPHPPresentation->getSlideCount();
$headingTextColor = '#000';
$headingAlignment = Alignment::HORIZONTAL_CENTER;
$headingShape = createCommonTextShape($currentSlide, 100, 700, 125, 20, $headingAlignment);
createCommonTextRun($headingShape, 18, false, "Montserrat", 0, $headingText, $headingTextColor);
}
function createCommonTableHeaderRows($tableShape, $tableHeaderData, $Module) {
$totalColumns = count($tableHeaderData);
$topWidth = 2;
$row = $tableShape->createRow();
$row->setHeight(50);
$row->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FF002f6c'))->setEndColor(new Color('FF002f6c'));
for ($i = 0; $i < $totalColumns; $i++) {
$oCell = $row->nextCell();
createCommonTextRun($oCell, 9, false, "Montserrat", 0, $tableHeaderData[$i], $cellTextColor);
$oCell->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal( Alignment::HORIZONTAL_CENTER );
}
}