在块克隆内使用块行克隆进行 PHPWord 模板处理
PHPWord template processing with block row cloning inside a block clone
我正在使用 PHPWord to create reports that make use of a .docx
template。
这个库允许我clone a block and clone table rows。但是,这些示例彼此分开。我需要结合这两种方法,因为我的模板 table 看起来像这样:
${block_group}
+--------+----------------------------------------------------------------+
| Group: | ${group} |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
${/block_group}
要求:
- 此报告将有一个单独的 table 每个
group
- (clone block)
- 并且每个
group
/block
可以有多个 table 行 - (clone table rows)
我有什么
到目前为止,我的代码是这样的:
# 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
步骤中我唯一担心的事情。我现在有两个 table,${group}
占位符已正确设置,${name}
和 ${address}
占位符也已正确设置。
现在我需要为每个组克隆 table 行,我被卡住了,我什至不知道如何开始编码。
这是我 运行 当前代码时得到的文件的样子:
+--------+----------------------------------------------------------------+
| Group: | Group 1 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
+--------+----------------------------------------------------------------+
| Group: | Group 2 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
经过无数次尝试,我终于想出了一个方法来做到这一点(不确定这是否是最好的方法,但它有效)。
假设
假设我的数据如下所示:
$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行
最后一步是根据您的数据克隆 table 行。
# 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. |
+---------------------+---------------------------------------------------+
我正在使用 PHPWord to create reports that make use of a .docx
template。
这个库允许我clone a block and clone table rows。但是,这些示例彼此分开。我需要结合这两种方法,因为我的模板 table 看起来像这样:
${block_group}
+--------+----------------------------------------------------------------+
| Group: | ${group} |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
${/block_group}
要求:
- 此报告将有一个单独的 table 每个
group
- (clone block) - 并且每个
group
/block
可以有多个 table 行 - (clone table rows)
我有什么
到目前为止,我的代码是这样的:
# 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
步骤中我唯一担心的事情。我现在有两个 table,${group}
占位符已正确设置,${name}
和 ${address}
占位符也已正确设置。
现在我需要为每个组克隆 table 行,我被卡住了,我什至不知道如何开始编码。
这是我 运行 当前代码时得到的文件的样子:
+--------+----------------------------------------------------------------+
| Group: | Group 1 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
+--------+----------------------------------------------------------------+
| Group: | Group 2 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
经过无数次尝试,我终于想出了一个方法来做到这一点(不确定这是否是最好的方法,但它有效)。
假设
假设我的数据如下所示:
$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行
最后一步是根据您的数据克隆 table 行。
# 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. |
+---------------------+---------------------------------------------------+