From c52c96d6573363f764d7bfba42be3ce998d1dc0e Mon Sep 17 00:00:00 2001 From: troosan Date: Tue, 17 Apr 2018 07:34:57 +0200 Subject: [PATCH] add support for STYLEREF field --- samples/Sample_27_Field.php | 5 +++++ src/PhpWord/Element/Field.php | 4 ++++ src/PhpWord/Writer/Word2007/Element/Field.php | 3 +++ tests/PhpWord/Writer/Word2007/ElementTest.php | 1 + 4 files changed, 13 insertions(+) diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 9c37dffe..4e7a5b22 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -6,15 +6,20 @@ include_once 'Sample_Header.php'; // New Word document echo date('H:i:s'), ' Create new PhpWord object', EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); +PhpOffice\PhpWord\Style::addTitleStyle(1, array('size' => 14)); // New section $section = $phpWord->addSection(); +$section->addTitle('This page demos fields'); // Add Field elements // See Element/Field.php for all options $section->addText('Date field:'); $section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat')); +$section->addText('Style Ref field:'); +$section->addField('STYLEREF', array('StyleIdentifier' => 'Heading 1')); + $section->addText('Page field:'); $section->addField('PAGE', array('format' => 'Arabic')); diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 0e5e28ed..2efc6b0b 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -78,6 +78,10 @@ class Field extends AbstractElement 'properties' => array(), 'options' => array('PreserveFormat'), ), + 'STYLEREF' => array( + 'properties' => array('StyleIdentifier' => ''), + 'options' => array('PreserveFormat'), + ), ); /** diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index cf3fbd66..e79dd24a 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -177,6 +177,9 @@ class Field extends Text case 'macroname': $propertiesAndOptions .= $propval . ' '; break; + default: + $propertiesAndOptions .= '"' . $propval . '" '; + break; } } diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index d365861a..25c62ecc 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -288,6 +288,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase $section->addField('DATE', array(), array('LunarCalendar')); $section->addField('DATE', array(), array('SakaEraCalendar')); $section->addField('NUMPAGES', array('format' => 'roman', 'numformat' => '0,00'), array('SakaEraCalendar')); + $section->addField('STYLEREF', array('StyleIdentifier' => 'Heading 1')); $doc = TestHelperDOCX::getDocument($phpWord); $element = '/w:document/w:body/w:p/w:r/w:instrText';