Ever found yourself wishing you could create a table of contents (TOC) in Google Docs to navigate your lengthy documents with ease? You're not alone. Google Docs, while powerful, doesn't natively support TOC creation. However, there's a workaround that allows you to achieve this. Let's dive into the process.

Before we begin, it's important to note that this method uses bookmarks and a script to generate the TOC. It requires a bit of setup, but it's well worth the effort for long documents. Now, let's get started.

Setting Up Your Google Doc for a Table of Contents
To create a TOC in Google Docs, you first need to set up your document with headers and bookmarks. Headers will serve as your TOC entries, and bookmarks will help the script link them together.

Here's how to do it:
Adding Headers

Use Google Docs' built-in heading styles (Heading 1, Heading 2, etc.) to create your TOC entries. These will appear in your TOC once you've set it up.
To apply a heading style, select the text, then click on the 'Heading' dropdown in the toolbar and choose the desired style.
Creating Bookmarks

Bookmarks will help the script link your TOC entries to their respective sections in the document. Here's how to create them:
1. Select the text you want to bookmark (usually the same text you've formatted as a header).
2. Right-click (or control-click on a Mac) and select 'Add bookmark' from the context menu.

3. Give your bookmark a name. This name should be unique and descriptive, as it will help you identify the bookmark later.
Installing the TOC Script in Google Docs




















Now that your document is set up, it's time to install the TOC script. This script will generate your TOC based on the headers and bookmarks you've created.
Here's how to install it:
Accessing the Script Editor
1. Click on 'Extensions' in the menu, then 'Apps Script.'
2. This will open the Script Editor in a new tab.
Pasting the TOC Script
1. Delete any code in the 'Code.gs' file.
2. Copy and paste the following script into the 'Code.gs' file:
function onEdit(e) {
var body = DocumentApp.getActiveDocument().getBody();
var headers = body.getHeadings();
var toc = body.appendParagraph('Table of Contents', DocumentApp-heading1);
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
var bookmark = DocumentApp.getActiveDocument().getBookmarks().getByName(header.getText());
toc.appendLink(header.getText(), bookmark.getElement());
}
}
Saving and Running the Script
1. Click on the disk icon to save the project. Name it 'TOC Script'.
2. Close the Script Editor tab and go back to your Google Doc.
3. To run the script, press 'Ctrl + Alt + S' (Windows/Linux) or 'Cmd + Opt + S' (Mac).
4. A TOC should now appear at the beginning of your document. If it doesn't, try running the script again.
Updating Your Table of Contents
Whenever you add, remove, or rename headers in your document, the TOC will automatically update to reflect those changes. Just run the script again using the keyboard shortcut.
Managing Bookmarks
If you need to manage your bookmarks (e.g., rename or delete them), you can do so in the 'Bookmarks' panel. To open it, click on 'Insert' in the menu, then 'Bookmark'.
And there you have it! You've successfully created a table of contents in Google Docs. This feature can greatly improve the navigability of your long documents, making it easier for you and your collaborators to jump to specific sections.
Now that you've mastered the art of creating TOCs in Google Docs, why not explore other productivity-boosting features? The world of Google Docs is vast and full of hidden gems waiting to be discovered.