问题描述
我正在用 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",$v);
}
$currentHeight = $tableHeaderTop + $HeaderRowHeight + $rowHeight;
if ($currentHeight > $SlideHeight && $rowCount > 0) {
$currentSlide = createTemplatedSlide($objphppresentation);
createCommonheading($objphppresentation,$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,$AccountName,$Module = "") {
$slideIndex = $objphppresentation->getSlideCount();
$headingTextColor = '#000';
$headingAlignment = Alignment::HORIZONTAL_CENTER;
$headingShape = createCommonTextShape($currentSlide,100,700,125,20,$headingAlignment);
createCommonTextRun($headingShape,18,false,$headingTextColor);
}
function createCommonTableHeaderRows($tableShape,$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,$tableHeaderData[$i],$cellTextColor);
$oCell->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER)->setHorizontal( Alignment::HORIZONTAL_CENTER );
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)