我\xc2\xb4m 尝试在我的Word 文档中使用块,但我\xc2\xb4m 遇到一些问题。首先,当我在文档中声明一个块时,如果我不\xc2\xb4t使用函数“cloneBlock”,结果将如下所示:
\n\n${sec}\nexample\n${/sec}\nRun Code Online (Sandbox Code Playgroud)\n\n也许我必须使用该功能才能正确显示。但我的主要问题是“deleteBlock”不起作用。如果我不\xc2\xb4t克隆该块,则生成的docx会损坏。但是,如果我克隆该块,函数“deleteBlock”不会删除该块,并且它会在我的最终 docx 文件中显示该块内的信息。
\n\n这是我的代码:
\n\n//Word\n// Creating the new document...\n$templateProcessor = new \\PhpOffice\\PhpWord\\TemplateProcessor(\'../example.docx\');\n//set value\n//$templateProcessor->setValue(\'title\', \'Example\');\n\n//Triplicate block\n$templateProcessor->cloneBlock(\'firstblock\', 3, true, true);\n$templateProcessor->setValue(\'firstname#1\', \'John\');\n$templateProcessor->setValue(\'lastname#1\', \'Doe\');\n$templateProcessor->setValue(\'firstname#2\', \'John\');\n$templateProcessor->setValue(\'lastname#2\', \'Doe\');\n$templateProcessor->setValue(\'firstname#3\', \'John\');\n$templateProcessor->setValue(\'lastname#3\', \'Doe\');\n\n//Delete Block\n$templateProcessor->cloneBlock(\'sec\', 1, true, true);\n$templateProcessor->deleteBlock(\'sec\');\n$templateProcessor->saveAs(\'example.docx\');\nRun Code Online (Sandbox Code Playgroud)\n\n文档模板:
\n\n${firstblock}\nHello ${firstname} ${lastname}!\n${/firstblock}\n${sec}\nexample\n${/sec}\nRun Code Online (Sandbox Code Playgroud)\n\n更新:\n我没有使用函数“deleteBlock”,而是使用函数“cloneBlock”,如下所示,它删除了该块:
\n\n//Delete Block\n$templateProcessor->cloneBlock(\'sec\', 0, true, true);\nRun Code Online (Sandbox Code Playgroud)\n\n因此,我已写入克隆该块 0 次,因此它消失了\n但我还有另一个问题。我不\xc2\xb4t 知道为什么,但这只是有时有效
\n