docs(layout): leave updateFields off in the page-number demos, so Word opens them without asking (#3676)

* docs(layout): leave updateFields off in the page-number demos, so Word opens them without asking

The table-of-contents-page-numbers and template-page-numbers demos still set
features: { updateFields: true }, from before docx/layout wrote page references
and tables of contents clean, so Word asked to update the fields when it opened
them. They now leave it off, and show the page numbers docx/layout wrote.

patchDocument keeps a template's settings, w:updateFields among them, so the
Templates section of docs/usage/layout.md now says to leave it off in the
template.

* docs(demo): set updateFields: false explicitly in every demo that set it

The maintainer asked for every demo to say updateFields: false rather than
leave the option out. demo/fields/table-of-contents.ts now says so too, with
a comment that its table of contents stays empty until the reader updates
it in Word.

* docs(demo): write the fields demo's table of contents with docx/layout, so Word opens it without asking

The maintainer chose to switch demo/fields/table-of-contents.ts to
docx/layout: with pageNumbers: estimatePageNumbers its table of contents is
written with its page numbers and clean, and with updateFields off Word
shows it as it is written and doesn't ask to update the fields.

* docs(layout): say Word opens a filled-in template without asking only as it does a document whose fields are written clean

CodeRabbit read the Templates sentence as unconditional. It now points to
what still makes Word ask.
This commit is contained in:
Dolan
2026-10-04 13:15:00 +01:00
committed by GitHub
parent 2fd675a0c4
commit a871df8a25
4 changed files with 12 additions and 8 deletions
+6 -3
View File
@@ -2,17 +2,20 @@
import * as fs from "fs";
import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "docx";
import { estimatePageNumbers } from "docx/layout";
// WordprocessingML docs for TableOfContents can be found here:
// http://officeopenxml.com/WPtableOfContents.php
// Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle,
// making the entries be hyperlinks for the paragraph. The entries are written from the headings,
// and Word fills in their page numbers when it updates the fields
// making the entries be hyperlinks for the paragraph. docx/layout lays out the pages as Word would and
// writes the table of contents with its page numbers (see docs/usage/layout.md), and with updateFields
// off, Word shows it as it is written and doesn't ask to update the fields
const doc = new File({
features: {
updateFields: true,
updateFields: false,
},
pageNumbers: estimatePageNumbers,
styles: {
paragraphStyles: [
{
@@ -26,10 +26,10 @@ const chapter = (number: number): readonly Paragraph[] => [
];
const doc = new Document({
// Word updates the fields when it opens the document, which checks the page numbers
features: { updateFields: true },
// Lays out the pages as Word would, and writes the page numbers of the table of contents and the page references
pageNumbers: estimatePageNumbers,
// With updateFields off, Word shows the page numbers as they are written, and doesn't ask to update the fields
features: { updateFields: false },
sections: [
{
children: [
+3 -2
View File
@@ -16,9 +16,10 @@ const text = (words: number, seed: number): string =>
const CHAPTERS = ["The coast", "The harbour wall", "The footings", "The costs", "What is left to do"];
// A report template, as it might be made in Word: a table of contents, and a placeholder for each chapter's text. Its
// table of contents has no page numbers yet
// table of contents has no page numbers yet. Its updateFields is off, which patchDocument keeps as it is, so Word
// shows the page numbers docx/layout writes, and doesn't ask to update the fields
const template = new Document({
features: { updateFields: true },
features: { updateFields: false },
sections: [
{
footers: {
+1 -1
View File
@@ -109,7 +109,7 @@ const doc = await patchDocument({
});
```
The template is read as Word saved it, with its styles, theme, lists, settings, headers, footers and notes, and the numbers are written into its tables of contents, page references and numbers of pages, in its text, headers and footers. The numbers a template was saved with are those of the template before it was filled in, so one that `docx/layout` can't work out, after something it can't lay out, is left blank rather than kept. Its page references and tables of contents are written clean, as a document's are, so Word shows them as they are (see [Opening the document in Word](#opening-the-document-in-word)). What `estimatePageNumbersWith` returns works the same way.
The template is read as Word saved it, with its styles, theme, lists, settings, headers, footers and notes, and the numbers are written into its tables of contents, page references and numbers of pages, in its text, headers and footers. The numbers a template was saved with are those of the template before it was filled in, so one that `docx/layout` can't work out, after something it can't lay out, is left blank rather than kept. Its page references and tables of contents are written clean, as a document's are, so Word shows them as they are (see [Opening the document in Word](#opening-the-document-in-word)). The template's settings are kept as they are, `updateFields` (`w:updateFields`) among them, so a template that has it on still asks Word to update all of its fields when the filled-in document is opened. Leave it off in the template, and Word opens the filled-in document without asking, as it opens a document whose fields are written clean (see [Opening the document in Word](#opening-the-document-in-word) for what still makes it ask). What `estimatePageNumbersWith` returns works the same way.
A template can import other documents (`w:altChunk`), which Word turns into paragraphs and tables of the template's own when it opens it. A `.docx` it imports is laid out as Word lays it out: where it is imported, in the text, a table cell, a header, a footnote or another imported document, in the template's styles of the same names, its defaults, its theme and its settings, with the styles only the imported document has as they look there, and paragraphs of no style in its default style, its lists numbered on their own and its footnotes numbered with the template's, and without its own page size, margins, headers and footers. A bookmark of a name the template has is the template's. What the layout doesn't follow stops it (see [What it leaves blank](#what-it-leaves-blank)).