【问题标题】:PHPWord template processing with block row cloning inside a block clone在块克隆中使用块行克隆的 PHPWord 模板处理
【发布时间】:2021-10-31 08:40:11
【问题描述】:

我正在使用 PHPWord 创建报告 make use of a .docx template

这个库允许我clone a blockclone table rows。但是,这些示例彼此分开。我需要结合这两种方法,因为我的模板表如下所示:

${block_group}
+--------+----------------------------------------------------------------+
| Group: | ${group}                                                       |
+--------+------------+---------------------------------------------------+
| Name                | Address                                           |
+---------------------+---------------------------------------------------+
| ${name}             | ${address}                                        |
+---------------------+---------------------------------------------------+
${/block_group}

要求:

我有什么

到目前为止,这是我的代码的样子:

# Create the template processor
$templateProcessor = new TemplateProcessor('/path/to/template/Template.docx');

# Block cloning
$replacements = array(
    array('group' => 'Group 1'),
    array('group' => 'Group 2')
);
$templateProcessor->cloneBlock('block_group', 0, true, false, $replacements);

如您所见,$replacements 只处理了${group} 占位符,因为这是我对clone block 步骤唯一担心的事情。我现在有两张表,${group} 占位符设置正确,${name}${address} 占位符设置正确。

现在我需要为每个组克隆表行,我被卡住了,我什至不知道如何开始编码。


这是我运行当前代码时得到的文件的样子:

+--------+----------------------------------------------------------------+
| Group: | Group 1                                                        |
+--------+------------+---------------------------------------------------+
| Name                | Address                                           |
+---------------------+---------------------------------------------------+
| ${name}             | ${address}                                        |
+---------------------+---------------------------------------------------+

+--------+----------------------------------------------------------------+
| Group: | Group 2                                                        |
+--------+------------+---------------------------------------------------+
| Name                | Address                                           |
+---------------------+---------------------------------------------------+
| ${name}             | ${address}                                        |
+---------------------+---------------------------------------------------+

【问题讨论】:

    标签: php templates ms-word phpword


    【解决方案1】:

    经过无数次尝试,我终于想出了一个方法来做到这一点(不确定这是否是最好的方法,但它确实有效)

    假设

    假设我的数据如下所示:

    $data = array(
        'Group 1' => array(
            array(
                'name' => 'John Smith',
                'address' => '123 Main Rd.'
            ),
            array(
                'name' => 'Jane Doe',
                'address' => '456 Second St.'
            )
        ),
        'Group 2' => array(
            array(
                'name' => 'Noah Ford',
                'address' => '987 Rich Blvd.'
            ),
            array(
                'name' => 'Oliver Brown',
                'address' => '654 Third St.'
            )
        )
    );
    

    克隆块

    第一步是克隆block

    # Block cloning
    $replacements = array();
    $i = 0;
    foreach($data as $group_name => $group) {
        $replacements[] = array(
            'group' => $group_name,
            'name' => '${name_'.$i.'}',
            'address' => '${address_'.$i.'}'
        );
    
        $i++;
    }
    $templateProcessor->cloneBlock('block_group', count($replacements), true, false, $replacements);
    

    这里的主要思想是不放置任何行数据,而是更改占位符标识符以包含组索引。

    在这段代码之后,文档应该是这样的:

    +--------+----------------------------------------------------------------+
    | Group: | Group 1                                                        |
    +--------+------------+---------------------------------------------------+
    | Name                | Address                                           |
    +---------------------+---------------------------------------------------+
    | ${name_0}           | ${address_0}                                      |
    +---------------------+---------------------------------------------------+
    
    +--------+----------------------------------------------------------------+
    | Group: | Group 2                                                        |
    +--------+------------+---------------------------------------------------+
    | Name                | Address                                           |
    +---------------------+---------------------------------------------------+
    | ${name_1}           | ${address_1}                                      |
    +---------------------+---------------------------------------------------+
    

    克隆表行

    最后一步是根据您的数据克隆表行。

    # Table row cloning
    $i = 0;
    foreach($data as $group) {
        $values = array();
        foreach($group as $row) {
            $values[] = array(
                "name_{$i}" => $row['name'],
                "address_{$i}" => $row['address']
            );
        }
        $templateProcessor->cloneRowAndSetValues("name_{$i}", $values);
    
        $i++;
    }
    

    在这段代码之后,文档应该是这样的:

    +--------+----------------------------------------------------------------+
    | Group: | Group 1                                                        |
    +--------+------------+---------------------------------------------------+
    | Name                | Address                                           |
    +---------------------+---------------------------------------------------+
    | John Smith          | 123 Main Rd.                                      |
    +---------------------+---------------------------------------------------+
    | Jane Doe            | 456 Second St.                                    |
    +---------------------+---------------------------------------------------+
    
    +--------+----------------------------------------------------------------+
    | Group: | Group 2                                                        |
    +--------+------------+---------------------------------------------------+
    | Name                | Address                                           |
    +---------------------+---------------------------------------------------+
    | Noah Ford           | 987 Rich Blvd.                                    |
    +---------------------+---------------------------------------------------+
    | Oliver Brown        | 654 Third St.                                     |
    +---------------------+---------------------------------------------------+
    

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多