为什么“deleteBlocks”在 phpWord 中不起作用?

问题描述 投票:0回答:3

我正在尝试在 Word 文档中使用块,但遇到了一些问题。首先,当我在文档中声明一个块时,如果我不使用函数“cloneBlock”,结果将如下所示:

${sec}
example
${/sec}

也许我必须使用该功能才能正确显示。但我的主要问题是“deleteBlock”不起作用。如果我不克隆该块,生成的 docx 就会损坏。但是,如果我克隆该块,“deleteBlock”函数不会删除该块,并且它会在我的最终 docx 文件中显示该块内的信息。

这是我的代码:

//Word
// Creating the new document...
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('../example.docx');
//set value
//$templateProcessor->setValue('title', 'Example');

//Triplicate block
$templateProcessor->cloneBlock('firstblock', 3, true, true);
$templateProcessor->setValue('firstname#1', 'John');
$templateProcessor->setValue('lastname#1', 'Doe');
$templateProcessor->setValue('firstname#2', 'John');
$templateProcessor->setValue('lastname#2', 'Doe');
$templateProcessor->setValue('firstname#3', 'John');
$templateProcessor->setValue('lastname#3', 'Doe');

//Delete Block
$templateProcessor->cloneBlock('sec', 1, true, true);
$templateProcessor->deleteBlock('sec');
$templateProcessor->saveAs('example.docx');

Docx 模板:

${firstblock}
Hello ${firstname} ${lastname}!
${/firstblock}
${sec}
example
${/sec}

更新: 我没有使用函数“deleteBlock”,而是使用函数“cloneBlock”,它会删除该块:

//Delete Block
$templateProcessor->cloneBlock('sec', 0, true, true);

所以,我已经写入克隆该块0次,所以它消失了 但我还有另一个问题。我不知道为什么,但这只是有时有效

block phpword
3个回答
6
投票

我不确定为什么用户@d1845412删除了他们之前的答案,但这实际上解决了我的问题。我用以下代码覆盖了 deleteBlock 方法,它似乎有效。与对 ReplaceBlock 方法进行较大更改相比,我更喜欢这个小更改。

/**
 * Override this method since deleteBlock doesn't seem to work.
 * @param string $blockname
 */
public function deleteBlock($blockname)
{
    $this->cloneBlock($blockname, 0, true, true);
}

1
投票

试试这个。我稍微改变了replaceBlock中的正则表达式,我还添加了一个删除未使用模式的函数,它可能会派上用场)我会立即警告你 - 没有真正测试,所以请小心使用它

class PatchedTemplateProcessor extends TemplateProcessor
{
    /**
     *  Remove ${*} and ${/*} from temporary document
     */
    public function removeSearchPatterns()
    {
        preg_match_all(
            '/(\$\{[^\}]*\})/',
            $this->tempDocumentMainPart,
            $matches,
            PREG_SET_ORDER
        );


        foreach ($matches as $match){
            $this->tempDocumentMainPart = str_replace(
                $match,
                '',
                $this->tempDocumentMainPart
            );
        }
    }

    /**
     * @param string $blockname
     * @param string $replacement
     */
    public function replaceBlock($blockname, $replacement)
    {
        $matches = array();
        preg_match(
            '/(<w:t.*>\${' . $blockname . '}<\/w:.*?t>)(.*)(<w:t.*\${\/' . $blockname . '}<\/w:.*?t>)/is',
            $this->tempDocumentMainPart,
            $matches
        );

        if (isset($matches[3])) {
            $this->tempDocumentMainPart = str_replace(
                $matches[2] . $matches[3],
                $replacement,
                $this->tempDocumentMainPart
            );
        }
    }

    /**
     * Delete a block of text.
     *
     * @param string $blockname
     */
    public function deleteBlock($blockname)
    {
        $this->replaceBlock($blockname, '');
    }
}

0
投票

我在 01/06/2024 也面临这个问题。 如果您已经解决了这个问题,请告诉我解决方案。 谢谢你。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.