PHPPowerpoint在表格单元格中设置超链接

时间:2014-11-10 15:12:16

标签: hyperlink cell phppowerpoint

我想在表格单元格中设置超链接:

/* ADD TABLE ROW */
    foreach ($entries as $entry) {
        $row = $tableShape->createRow();
        $row->getFill()->setFillType(Fill::FILL_SOLID)
                ->setStartColor(new Color('FFFFFFFF'))
                ->setEndColor(new Color('FFFFFFFF'));
        $row->nextCell()->createTextRun(date_format($entry->getDate(), "d.m.Y"));
        $row->nextCell()->createTextRun($entry->getTonality()->getName());
        $row->nextCell()->createTextRun($entry->getAccountname());
        $row->nextCell()->createTextRun($entry->getContent());
        $row->nextCell()->createTextRun($entry->getFollower());
        $row->nextCell()->createTextRun($entry->getLink());
    }

此代码不起作用:

$row->nextCell()->createTextRun('Link')->setUrl($entry->getLink())
                    ->setTooltip('Link');;

2 个答案:

答案 0 :(得分:0)

我现在通过在正确的位置添加形状来做到这一点。

/* SET HYPERLINK WITH SHAPE */
$shape = $slide->createRichTextShape();
$shape->setWidth($this->cell_link_width)
      ->setHeight($this->cell_height)
      ->setOffsetX($this->cell_link_offsetX)
       >setOffsetY($this->tableOffsetY + $height);
        $textLink = $shape->createTextRun('Link');
        $textLink->getHyperlink()->setUrl('http://' . $entry->getLink())
       >setTooltip('http://' . $entry->getLink());

我编写了一个算法并定义了一个变量line_height来设置正确位置的链接。

这是我的完整功能:

public function createTableSlide($objPHPPowerPoint, $pathLogo, $user, $entries) {

    $slide = $this->createTemplatedSlide($objPHPPowerPoint, $pathLogo, $user);
    /* CREATE TABLE WITH COLUMNS */
    $tableShape = $this->getTable($slide, 6);
    $this->setTableSlideHeader($slide, 'Social Media', 'Twitter', $tableShape);

    /* ADD TABLE ROW */
    $i = 1;
    $height = 22;
    $height_tmp = 0;
    $max_height = 554;
    foreach ($entries as $entry) {
        $height += $height_tmp; 
        $modulId = $entry->getModul()->getId();

        /* NEW SLIDE IF TABLE HEIGHT AT END OF SLIDE */
        if($height >= $max_height){
            $slide = $this->createTemplatedSlide($objPHPPowerPoint, $pathLogo, $user);
            /* CREATE TABLE WITH COLUMNS */
            $tableShape = $this->getTable($slide, 6);
            $this->setTableSlideHeader($slide, 'Social Media', 'Twitter', $tableShape);

            $i = 1;
            $height = 22;
            $height_tmp = 0;
        }

        $row_in_cell = ceil(strlen($entry->getContent()) / $this->char_in_row);

        if ($row_in_cell > 2) {
            $height_tmp = $row_in_cell * $this->line_height + $this->line_height;
        } else {
            $height_tmp = $this->line_height * 3 + 0.8;
        }



        $row = $tableShape->createRow();
        $row->setHeight($this->cell_height);
        $row->getFill()->setFillType(Fill::FILL_SOLID)
                ->setStartColor(new Color('FFFFFFFF'))
                ->setEndColor(new Color('FFFFFFFF'));
        $row->nextCell()->createTextRun(date_format($entry->getDate(), "d.m.Y"));
        $row->nextCell()->createTextRun($entry->getTonality()->getName());
        $row->nextCell()->createTextRun($entry->getAccountname());
        $row->nextCell()->createTextRun($entry->getContent());
        $row->nextCell()->createTextRun($entry->getFollower());
        $row->nextCell()->createTextRun($modulId);
        /* SET HYPERLINK WITH SHAPE */
        $shape = $slide->createRichTextShape();
        $shape->setWidth($this->cell_link_width)
                ->setHeight($this->cell_height)
                ->setOffsetX($this->cell_link_offsetX)
                ->setOffsetY($this->tableOffsetY + $height);
        $textLink = $shape->createTextRun('Link');
        $textLink->getHyperlink()->setUrl('http://' . $entry->getLink())
                ->setTooltip('http://' . $entry->getLink());
        $i++;
    }
}

希望如果您搜索相同的解决方案,它可以帮助您。如果有人有更好的解决方案,他可以回答我:)

答案 1 :(得分:0)