Auto-insert a table of contents and page numbers
Standardise structure across Northwind's long Docs — TOC at the top, page numbers in the footer.
Published Jul 27, 2025
Northwind produces a steady stream of long Docs — proposals, reports, handbooks — and every author lays them out slightly differently. Some have a table of contents, some do not; some have page numbers, some forget. The result is a set of documents that look like they came from different companies.
This script enforces one house style. Given a document ID, it strips any existing table of contents, inserts a fresh one at the very top followed by a page break, and adds a right-aligned page number to the footer. Run it over a finished draft and the structure is consistent every time, without anyone remembering to do it by hand.
What you’ll need
- A Google Doc that uses heading styles (Heading 1, Heading 2, and so on) — the table of contents is built from those headings.
- The document ID, which you pass to the function when you run it.
- Edit access to the document.
The script
/**
* Standardises a Doc's structure: removes any existing table of
* contents, inserts a fresh one at the top followed by a page break,
* and adds a right-aligned page number to the footer.
*
* @param {string} docId The ID of the document to standardise.
*/
function standardiseStructure(docId) {
const doc = DocumentApp.openById(docId);
const body = doc.getBody();
// 1. Remove any existing table of contents. Loop backwards so removing
// a child does not shift the indexes of the ones still to check.
for (let i = body.getNumChildren() - 1; i >= 0; i--) {
const child = body.getChild(i);
if (child.getType() === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
body.removeChild(child);
}
}
// 2. Insert a fresh table of contents at the very top of the document.
body.insertTableOfContents(0);
// 3. Add a page break after it so the TOC sits on its own page.
body.insertPageBreak(1);
// 4. Get the footer, creating one if the document does not have it yet.
const footer = doc.getFooter() || doc.addFooter();
// 5. Clear the footer and add a right-aligned page number.
footer.clear();
footer.appendParagraph('')
.appendPageNumber()
.setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
// 6. Save and close so the changes are written to the document.
doc.saveAndClose();
}
How it works
standardiseStructureopens the document and gets itsbody. It loops over the body’s children backwards — removing an element shifts the indexes of everything after it, so iterating in reverse keeps the loop correct — and removes any element that is aTABLE_OF_CONTENTS. This clears out a stale TOC before a new one goes in.- It inserts a fresh table of contents at index
0, the very top of the document. Google Docs builds the TOC from the document’s heading styles. - It inserts a page break at index
1, immediately after the new TOC, so the contents page is separated from the body text. - It fetches the footer with
getFooter, and if the document has none it creates one withaddFooter. - It clears the footer and appends a paragraph containing a page number, aligned to the right — the standard placement.
- It calls
saveAndCloseto commit every change to the document.
Example run
Before — a finished proposal with headings but no front matter:
Project Overview (Heading 1)
Background (Heading 2)
Goals (Heading 2)
Proposed Approach (Heading 1)
...body text, no page numbers...
After running standardiseStructure on the doc:
[ Table of contents ]
Project Overview ............... 2
Background ................... 2
Goals ........................ 2
Proposed Approach .............. 3
--- page break ---
Project Overview (Heading 1)
...body text...
[ footer: page 2, right-aligned ]
Every Northwind document run through the script ends up with the same opening page and the same footer.
Run it
This is an on-demand job — run it on a draft once the content is final:
- In the editor, add a small wrapper that passes your document ID, then click Run and approve the authorisation prompt the first time.
- Open the document to check the table of contents and footer.
function runStandardise() {
standardiseStructure('1abcDocId');
}
Watch out for
- The table of contents is built only from heading styles. Titles typed as bold body text will not appear — make sure the document uses Heading 1, Heading 2, and so on.
- A table of contents in Google Docs does not refresh on its own. If headings change after you run the script, run it again to rebuild the TOC.
- The script clears the entire footer before adding the page number. Anything else in the footer — a confidentiality notice, a document title — will be wiped, so add it back inside this function if you need it.
insertPageBreak(1)assumes the TOC is the only thing above it. If you change the order of the first few inserts, update the indexes to match.- Page numbers added through Apps Script start counting from the first page, including the contents page. If you want the body to start at page 1, adjust the numbering in the document by hand.
- Run this on a finished draft. Inserting a TOC and page break mid-edit can surprise an author who still has the document open.
Related
Generate personalized study guides from notes
Reformat raw notes into structured study guides — for Northwind's internal training programme.
Updated Feb 8, 2026
Build a contract-clause assembly system
Construct Northwind agreements from a library of approved clauses — drag-drop in code.
Updated Feb 1, 2026
Translate and resolve Doc comments
Localise reviewer feedback on a shared Doc so multilingual teams can collaborate.
Updated Jan 25, 2026
Auto-archive finalized Docs to dated folders
File completed Northwind Docs by month so the active folder stays focused on in-flight work.
Updated Jan 18, 2026
Build a fillable intake form inside a Doc
Create structured intake forms with placeholder fields readers can fill — for client briefs.
Updated Jan 11, 2026