Create table rows on the fly with cloneRow() when using templates.
This commit is contained in:
parent
545cbc6e23
commit
b5f71317b7
|
|
@ -122,6 +122,35 @@ class PHPWord_Template
|
|||
return $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a table row in a template document
|
||||
*
|
||||
* @param mixed $search
|
||||
* @param mixed $numberOfClones
|
||||
*/
|
||||
public function cloneRow($search, $numberOfClones) {
|
||||
if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
|
||||
$search = '${'.$search.'}';
|
||||
}
|
||||
|
||||
$tagPos = strpos($this->_documentXML, $search);
|
||||
if (!$tagPos) {
|
||||
trigger_error("Can not clone row, template variable not found or variable contains markup.");
|
||||
return false;
|
||||
}
|
||||
$rowStartPos = strrpos($this->_documentXML, "<w:tr ", ((strlen($this->_documentXML) - $tagPos) * -1));
|
||||
$rowEndPos = strpos($this->_documentXML, "</w:tr>", $tagPos) + 7;
|
||||
|
||||
$result = substr($this->_documentXML, 0, $rowStartPos);
|
||||
$xmlRow = substr($this->_documentXML, $rowStartPos, ($rowEndPos - $rowStartPos));
|
||||
for ($i = 1; $i <= $numberOfClones; $i++) {
|
||||
$result .= preg_replace('/\$\{(.*?)\}/','\${\\1#'.$i.'}', $xmlRow);
|
||||
}
|
||||
$result .= substr($this->_documentXML, $rowEndPos);
|
||||
|
||||
$this->_documentXML = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Template
|
||||
*
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
require_once '../Classes/PHPWord.php';
|
||||
|
||||
$PHPWord = new PHPWord();
|
||||
|
||||
$document = $PHPWord->loadTemplate('Sample_03_TemplateCloneRow.docx');
|
||||
|
||||
$document->cloneRow('rowValue', 10);
|
||||
|
||||
$document->setValue('rowValue#1', 'Sun');
|
||||
$document->setValue('rowValue#2', 'Mercury');
|
||||
$document->setValue('rowValue#3', 'Venus');
|
||||
$document->setValue('rowValue#4', 'Earth');
|
||||
$document->setValue('rowValue#5', 'Mars');
|
||||
$document->setValue('rowValue#6', 'Jupiter');
|
||||
$document->setValue('rowValue#7', 'Saturn');
|
||||
$document->setValue('rowValue#8', 'Uranus');
|
||||
$document->setValue('rowValue#9', 'Neptun');
|
||||
$document->setValue('rowValue#10', 'Pluto');
|
||||
|
||||
$document->setValue('rowNumber#1', '1');
|
||||
$document->setValue('rowNumber#2', '2');
|
||||
$document->setValue('rowNumber#3', '3');
|
||||
$document->setValue('rowNumber#4', '4');
|
||||
$document->setValue('rowNumber#5', '5');
|
||||
$document->setValue('rowNumber#6', '6');
|
||||
$document->setValue('rowNumber#7', '7');
|
||||
$document->setValue('rowNumber#8', '8');
|
||||
$document->setValue('rowNumber#9', '9');
|
||||
$document->setValue('rowNumber#10', '10');
|
||||
|
||||
$document->setValue('weekday', date('l'));
|
||||
$document->setValue('time', date('H:i'));
|
||||
|
||||
$document->save('SolarsystemRepeatingRows.docx');
|
||||
Loading…
Reference in New Issue