Inserting a Table of Contents in a Google Doc Using Apps Script

Google Apps Script provides a powerful way to automate and enhance your Google Docs experience. In this blog post, we will walk through an exercise where you will write a script to insert a table of contents (TOC) at the beginning of a Google Doc. This will demonstrate how you can manipulate and customize the content of Google Docs using Apps Script.


Before we start, ensure you have:

  • A Google account
  • Basic understanding of JavaScript
  • A Google Doc ready to use for this exercise

Step 1: Setting Up Your Google Doc

  1. Create or Open a Google Doc:
    • Open Google Docs and create a new document or open an existing one where you want to add a table of contents.

Step 2: Writing the Apps Script

  1. Open the Apps Script Editor:
    • In your Google Doc, go to Extensions > Apps Script to open the script editor.
  2. Create a New Script:
    • Replace any code in the script editor with the following code:

function createTableOfContents() {

  var doc = DocumentApp.getActiveDocument();

  var body = doc.getBody();

  // Check if a table of contents already exists and remove it

  var firstElement = body.getChild(0);

  if (firstElement.getType() == DocumentApp.ElementType.TABLE_OF_CONTENTS) {



  // Insert a new table of contents at the beginning of the document

  body.insertParagraph(0, ‘Table of Contents’).setHeading(DocumentApp.ParagraphHeading.HEADING1);


  // Refresh the TOC to reflect the current document structure

  DocumentApp.getUi().alert(‘Table of Contents added successfully!’);


Step 3: Running the Script

  1. Save and Run the Script:
    • Save your script by clicking the floppy disk icon or pressing Ctrl+S (Windows) or Cmd+S (Mac).
    • Click on the Run button (the play icon) to execute the script.
  2. Authorize the Script:
    • The first time you run the script, you will need to authorize it. Click on Review Permissions and follow the steps to grant the necessary permissions.
  3. Check Your Google Doc:
    • After running the script, go back to your Google Doc. You should see a table of contents inserted at the beginning of the document.

Step 4: Customizing the Script

  1. Customize TOC Settings:
    • You can customize the heading of the TOC or adjust the position where the TOC is inserted. For example, changing body.insertParagraph(0, ‘Table of Contents’) to body.insertParagraph(0, ‘Contents’) will alter the heading.
  2. Refresh TOC:
    • If you make changes to the document structure (like adding or removing headings), you can re-run the script to update the TOC.

Step 5: Adding More Customizations

  1. Formatting the TOC:
    • You can add formatting to the TOC heading or content. For instance, you can change the text style or alignment using Apps Script methods.
  2. Additional Functionalities:
    • Explore adding more functionalities like inserting page numbers, linking to specific sections, or even generating a dynamic TOC based on custom criteria.


With this exercise, you’ve learned how to use Google Apps Script to insert a table of contents into a Google Doc. This powerful feature can save you time and enhance the organization of your documents. Apps Script offers a vast array of methods to manipulate and customize Google Docs, from formatting text to integrating with other Google services.

Start exploring these possibilities to automate your document workflows and create more dynamic and interactive Google Docs.