How to edit table of contents in google docs

Here's what I have come up with to make a TOC that uses only one specific heading style. It works pretty well and I included the code (onOpen) that makes this a custom function that appears at the top with the document menu bar. So once you have this code tweaked for your document, you can actually run the function from the document and not go back to the script.

Note you have to use Style sheets (and any headers you want) and you must first create/refresh a Google TOC first.

WARNING: These items in ANY HEADING STYLE will cause Script Failures: Parentheses, blank line, space(s) at end of line, Horz Rule in Heading style, etc. I could probably catch these with a try{} function but haven't messed with that enough to go forth with it.

Here is the typical message I put at top just as a reminder of how to build the specific TOC.

“Specific HEADING TOC” will fill-in above this line – Cut & Paste to Desired Location

Heres the code

function onOpen() { // will need to set up a trigger (alarm clock) to make this run on Opening the doc or refreshing it var docId = DocumentApp.getActiveDocument().getId().toString() var doc = DocumentApp.openById(docId); // create a custom menu item at the end of the menu row of the doc (avoids having to "run" through this script) var ui = DocumentApp.getUi(); ui.createMenu('CustomFunct') // note, no semi-colons at the end of each ".add____( )" below .addItem('Heading2 Only TOC -- "As Is" Order', 'menuItem1') .addItem('Heading2 Only TOC -- "Reverse" Order', 'menuItem2') .addSeparator() .addItem('Heading3 Only TOC -- "As Is" Order', 'menuItem3') .addItem('Heading3 Only TOC -- "Reverse" Order', 'menuItem4') /* // Saving this in case you would expand the menu item with other functions and/or submenus .addSeparator() .addSubMenu(ui.createMenu('Sub-menu') .addItem('Temp Sum Menu Item', 'menuItem5')) */ .addToUi(); } function menuItem1() {DocumentApp.getUi(); parseText("As Is","HEADING2");} function menuItem2() {DocumentApp.getUi(); parseText("Reverse","HEADING2");} function menuItem3() {DocumentApp.getUi(); parseText("As Is","HEADING3");} function menuItem4() {DocumentApp.getUi(); parseText("Reverse","HEADING3");} function menuItem5() { DocumentApp.getUi() .alert('You need to create a script function for this submenu item!'); // just saving this as a reminder } function parseText(indexOrder,headStyle) { var docId = DocumentApp.getActiveDocument().getId().toString(); // Use this line when access the CustomFunct menu item var doc = DocumentApp.openById(docId); // Define the search parameters var searchElement = doc.getBody(); var searchParas = doc.getBody().getParagraphs(); // Look thru ALL Paragraphs to LOCATE AND SAVE all of the links and anchor points based on Heading Style var urls = []; var urlsText = []; for (var i = 0; i < searchParas.length; i++) { // Get values for the NEW TOC using only "headStyle" Styles (see UI menu items above) // Note: items in any HEADING#'s that cause this to fail: parentheses, is a blank line, space at end of line, Horz Rule in any Heading style, etc. if (searchParas[i].getHeading() == headStyle) { var searchedText = searchElement.findText(searchParas[i].getText()); var dropParens = searchParas[i].getText(); var getAnchor = searchedText.getElement().asText().getLinkUrl(searchedText.getStartOffset()) if (dropParens.indexOf("/##/") == -1){ urlsText.push(dropParens); // builds the array of link text urls.push(getAnchor); // builds the array of link anchors } } } // Inserts the NEW TOC at the top of the Document -- then you can copy/paste to a location of your choice for (var i = 0;i<urlsText.length;i++) { if(indexOrder == "Reverse"){var indexToUse = i;} // Produces a TOC in the reverse order as they are in the document if(indexOrder == "As Is")indexToUse = urls.length-1-i; // Produces a TOC in the order they are in the document if (i==0){doc.getBody().editAsText().insertText(0,"\r");doc.getBody().editAsText().insertText(0,"\r")} doc.getBody().editAsText().insertText(0,urlsText[indexToUse]).setLinkUrl(0,urlsText[indexToUse].length,urls[indexToUse]) doc.getBody().editAsText().insertText(0,"\r") /* Adapted from --> //stackoverflow.com/questions/32602495/how-to-add-a-hyperlink-in-a-google-docs-using-a-google-script var text = "link text\n"; var url = "www.google.com"; DocumentApp.getActiveDocument().getBody().editAsText().insertText(0, text).setLinkUrl(0, text.length, url); */ } doc.getBody().editAsText().insertText(0,"TOC using only " + headStyle + " (in '" + indexOrder + "' Order)\r") }

When you’re writing a document in Google Docs, you may want to add a table of contents. Thankfully, a lot of word processors understand the value of automatically-generated tables of contents, and Google Docs is no different. This saves you the effort of manually making your own table by typing in page numbers and chapter names. Fortunately, it’s very simple to create and update a table of contents in Google Docs.

Using Headings

In order to make a table, we need to “tell” Google Docs how to construct it. Google Docs isn’t quite advanced enough to automatically know how to construct a decent table of contents via your writing alone. We need to let it know where our chapters and sub-chapters are so it can properly format its table of contents. This is done by using the “Headings” formatting in a document.

If you click “Normal text” at the top left of a Google Doc, you’ll see a range of options. The ones we’re interested in are the ones that start with “Headings.”

This is what Google Docs will look when generating a table of contents. When making the table Google Docs will put all text that share a heading format on the same level of importance. “Heading 1” is the highest level of heading, “Heading 2” is the second highest, and so on. Any heading used under a heading of higher level will be treated as a subheader. For example, if I put the text “Alpha” in Heading 1 format, “Beta” in Heading 2, and “Charlie” in Heading 3, Google Docs will see “Alpha” as the main header, “Beta” as Alpha’s subheader, and “Charlie” as Beta’s subheader.

Adding and Changing Headings

You can create headings in two ways. You can place your cursor where you want the heading, select it, then type what you want the heading to be. If you’ve already typed the text, you can highlight it and select the heading you want to format the text to.

If you don’t like the formatting Google Docs gives you for a heading, you can change it. Simply format some text in the style you’d like the header to be and highlight it. Click on the “Normal text” box, then the right arrow beside the heading you want to change, then “Update ‘Heading’ to match.”

You can reset heading styles by clicking “Options” and then “Reset styles” instead.

Making a Table of Contents

Once all your headers have been defined, you can tell Google Docs to generate a table of contents. You do this by clicking “Insert,” then “Table of contents”, and then selecting the table you like the looks of.

As you can see here, Google Docs has not only generated a table of contents, but also formatted them based on the headings we used:

This means you should think about how you want to structure your document before using headings. Let’s say we were writing an ebook about computer hardware. Each chapter will cover a different piece of hardware. We can set the chapter titles with Heading 1 (Chapter 1: Processor), then the titles defining each aspect with Heading 2 (Brands), then the details of each aspect with Heading 3 (Intel, AMD). When we generate the table of contents, it will look like this:

Updating the Table of Contents

As you add to and edit the document, you’ll notice that the table of contents isn’t updating itself. Don’t worry – it’s not broken! It just needs to be manually told to update its headings. Once you’re done changing or adding text, there’s two ways to update that table of contents in Google Docs. Either right-click the table of contents and click “Update table of contents” or click the Refresh button beside the table when you click on it.

The table of contents will then “catch up” with what you’ve typed.

If you ever find that a title is too long to fit on a single line in your table of contents, note that you can change the margins of your Google Doc to add more space.

Content with Tables

When you want to make or update a table of contents in Google Docs, you don’t have to do it manually! Just mark out the headings as you see fit, then let Google Docs do all the heavy lifting for you.

Do you use tables of contents in your documents regularly? Let us know below!

Simon Batt

Simon Batt is a Computer Science graduate with a passion for cybersecurity.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

How do you update a table of contents in Google Docs?

The table of contents reflects the titles and headings in your document..
On your computer, open a document in Google Docs..
Add a heading in your document..
Click Refresh. . The change will update in your table of contents..

Can you edit a table in Google Docs?

You can add and delete tables, and adjust the size and style of table rows and columns. If you're using Google Docs on a computer, you can also: Sort rows. Drag and move rows and columns.

How do I format the contents page in Google Docs?

More videos on YouTube.
Highlight the title of a section..
Click the Styles menu and select a heading..
Once you create a heading style for each section, click where you want the table of contents to go..
Click Insert. ... .
Select a layout style. ... .
Use the toolbar to edit and format the text as you please..

How do you add page numbers to a table of contents in Google Docs?

To do so, follow these steps: Click in your Document where you'd like the table of contents to appear. Go to Insert > Table of contents… and simply select the numbered option. You will see a gadget appear which has the table of contents for you to click on.

Toplist

Latest post

TAGs