Skip to content

Commit ab5d446

Browse files
committed
add the updateFields option on document settings
When set to true, word will ask you to update the fields in the document when you open the document.
1 parent ffa9c15 commit ab5d446

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ This is the last version to support PHP 5.3
1818
- Support for Comments - @troosan #1067
1919
- Support for paragraph textAlignment - @troosan #1165
2020
- Add support for HTML underline tag <u> in addHtml - @zNightFalLz #1186
21-
- Allow to change cell width unit - guillaume-ro-fr #986
21+
- Allow to change cell width unit - @guillaume-ro-fr #986
2222
- Allow to change the line height rule @troosan
23+
- Allow to force an update of all fields on opening a document - @troosan #951
2324

2425
### Fixed
2526
- Loosen dependency to Zend

docs/general.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,12 @@ points to twips.
271271
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
272272
// 2 cm right margin
273273
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
274+
275+
Automatically Recalculate Fields on Open
276+
----------------------------------------
277+
278+
To force an update of the fields present in the document, set updateFields to true
279+
280+
.. code-block:: php
281+
282+
$phpWord->getSettings()->setUpdateFields(true);

samples/Sample_17_TitleTOC.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// New Word document
55
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
$phpWord->getSettings()->setUpdateFields(true);
78

89
// New section
910
$section = $phpWord->addSection();

src/PhpWord/Metadata/Settings.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ class Settings
116116
*/
117117
private $themeFontLang;
118118

119+
/**
120+
* Automatically Recalculate Fields on Open
121+
*
122+
* @var bool
123+
*/
124+
private $updateFields = false;
125+
119126
/**
120127
* Radix Point for Field Code Evaluation
121128
*
@@ -345,6 +352,22 @@ public function setThemeFontLang($themeFontLang)
345352
$this->themeFontLang = $themeFontLang;
346353
}
347354

355+
/**
356+
* @return bool
357+
*/
358+
public function hasUpdateFields()
359+
{
360+
return $this->updateFields;
361+
}
362+
363+
/**
364+
* @param bool $updateFields
365+
*/
366+
public function setUpdateFields($updateFields)
367+
{
368+
$this->updateFields = $updateFields === null ? false : $updateFields;
369+
}
370+
348371
/**
349372
* Returns the Radix Point for Field Code Evaluation
350373
*

src/PhpWord/Writer/Word2007/Part/Settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private function getSettings()
147147
$this->setOnOffValue('w:doNotTrackMoves', $documentSettings->hasDoNotTrackMoves());
148148
$this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting());
149149
$this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders());
150+
$this->setOnOffValue('w:updateFields', $documentSettings->hasUpdateFields());
150151

151152
$this->setThemeFontLang($documentSettings->getThemeFontLang());
152153
$this->setRevisionView($documentSettings->getRevisionView());

tests/PhpWord/Metadata/SettingsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,14 @@ public function testZoomEnum()
153153
$oSettings->setZoom(Zoom::FULL_PAGE);
154154
$this->assertEquals('fullPage', $oSettings->getZoom());
155155
}
156+
157+
/**
158+
* Test Update Fields on update
159+
*/
160+
public function testUpdateFields()
161+
{
162+
$oSettings = new Settings();
163+
$oSettings->setUpdateFields(true);
164+
$this->assertTrue($oSettings->hasUpdateFields());
165+
}
156166
}

0 commit comments

Comments
 (0)