Skip to content

Commit b5f7131

Browse files
committed
Create table rows on the fly with cloneRow() when using templates.
1 parent 545cbc6 commit b5f7131

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Classes/PHPWord/Template.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ public function getVariables()
121121
preg_match_all('/\$\{(.*?)}/i', $this->_documentXML, $matches);
122122
return $matches[1];
123123
}
124+
125+
/**
126+
* Clone a table row in a template document
127+
*
128+
* @param mixed $search
129+
* @param mixed $numberOfClones
130+
*/
131+
public function cloneRow($search, $numberOfClones) {
132+
if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
133+
$search = '${'.$search.'}';
134+
}
135+
136+
$tagPos = strpos($this->_documentXML, $search);
137+
if (!$tagPos) {
138+
trigger_error("Can not clone row, template variable not found or variable contains markup.");
139+
return false;
140+
}
141+
$rowStartPos = strrpos($this->_documentXML, "<w:tr ", ((strlen($this->_documentXML) - $tagPos) * -1));
142+
$rowEndPos = strpos($this->_documentXML, "</w:tr>", $tagPos) + 7;
143+
144+
$result = substr($this->_documentXML, 0, $rowStartPos);
145+
$xmlRow = substr($this->_documentXML, $rowStartPos, ($rowEndPos - $rowStartPos));
146+
for ($i = 1; $i <= $numberOfClones; $i++) {
147+
$result .= preg_replace('/\$\{(.*?)\}/','\${\\1#'.$i.'}', $xmlRow);
148+
}
149+
$result .= substr($this->_documentXML, $rowEndPos);
150+
151+
$this->_documentXML = $result;
152+
}
124153

125154
/**
126155
* Save Template
17.1 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
require_once '../Classes/PHPWord.php';
3+
4+
$PHPWord = new PHPWord();
5+
6+
$document = $PHPWord->loadTemplate('Sample_03_TemplateCloneRow.docx');
7+
8+
$document->cloneRow('rowValue', 10);
9+
10+
$document->setValue('rowValue#1', 'Sun');
11+
$document->setValue('rowValue#2', 'Mercury');
12+
$document->setValue('rowValue#3', 'Venus');
13+
$document->setValue('rowValue#4', 'Earth');
14+
$document->setValue('rowValue#5', 'Mars');
15+
$document->setValue('rowValue#6', 'Jupiter');
16+
$document->setValue('rowValue#7', 'Saturn');
17+
$document->setValue('rowValue#8', 'Uranus');
18+
$document->setValue('rowValue#9', 'Neptun');
19+
$document->setValue('rowValue#10', 'Pluto');
20+
21+
$document->setValue('rowNumber#1', '1');
22+
$document->setValue('rowNumber#2', '2');
23+
$document->setValue('rowNumber#3', '3');
24+
$document->setValue('rowNumber#4', '4');
25+
$document->setValue('rowNumber#5', '5');
26+
$document->setValue('rowNumber#6', '6');
27+
$document->setValue('rowNumber#7', '7');
28+
$document->setValue('rowNumber#8', '8');
29+
$document->setValue('rowNumber#9', '9');
30+
$document->setValue('rowNumber#10', '10');
31+
32+
$document->setValue('weekday', date('l'));
33+
$document->setValue('time', date('H:i'));
34+
35+
$document->save('SolarsystemRepeatingRows.docx');

0 commit comments

Comments
 (0)