Supercharge Your Google Workspace:Β  Generate Table of Contents Apps Script Projects!

🌐 Supercharge Your Google Workspace:Β  Generate Table of Contents Apps Script Projects! πŸ’»πŸš€

πŸš€ How to Automatically Generate a Table of Contents in Google Docs Using Google Apps Script πŸ“‘ | Easy Tutorial

Function: generateTOC

Objective:

Create a script to automatically generate a table of contents in a Google Document based on heading styles.

Description:

This function generates a table of contents (TOC) in a Google Document. It appends the TOC at the end of the document and links each TOC entry to its corresponding section within the document. The TOC includes entries for all text styled with HEADING2.

Code Sample:

function generateTOC() {

  const doc = DocumentApp.getActiveDocument();

  const body = doc.getBody();

  const paragraphs = body.getParagraphs();

  const toc = body.appendParagraph(‘Table of Contents’);

  toc.setHeading(DocumentApp.ParagraphHeading.HEADING1);

  paragraphs.forEach(function(paragraph){

    if(paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING1){

      const text = paragraph.getText();

      const tocEntry =body.appendParagraph(text);

      tocEntry.setLinkUrl(‘#’+text);

    }

  });

}

Usage:

Run the function: In your Google Document, open the script editor, paste this function, and run it.

View the TOC: The table of contents is appended at the end of the document.

Navigate the Document: Click on a TOC entry to navigate to the corresponding section.

Notes:

The TOC is generated based on paragraphs styled with HEADING2.

Each TOC entry is a link that jumps to the corresponding section in the document.

Step by Step explanation

Google Apps Script function named generateTOC(), designed to generate a Table of Contents (TOC) in a Google Document. Here’s a step-by-step explanation of what each part of the code does:

Function Definition:

function generateTOC() {

This line defines a new function named generateTOC.

Get Active Document:

const doc = DocumentApp.getActiveDocument();

This line retrieves the currently active Google Document using the DocumentApp.getActiveDocument() method and stores it in the variable doc.

Access Document Body:

const body = doc.getBody();

Here, the script accesses the body of the document (where all the content is) and stores it in the body variable.

Create TOC Title:

const toc = body.appendParagraph(“Table of Contents”);

This line appends a new paragraph with the text “Table of Contents” to the end of the document body. The new paragraph is stored in the variable toc.

Set TOC Heading Style:

toc.setHeading(DocumentApp.ParagraphHeading.HEADING1);

This sets the heading style of the “Table of Contents” title to HEADING1, making it stand out as a main heading.

Get All Paragraphs in the Document:

const paragraphs = body.getParagraphs();

This retrieves all the paragraphs in the document body and stores them in the paragraphs array.

Loop Through Each Paragraph:

paragraphs.forEach(function(paragraph) {

The script iterates over each paragraph in the document using the forEach method.

Check Heading Style:

if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING2) {

Within the loop, the script checks if the paragraph’s heading style is HEADING2. This is done using the getHeading() method on each paragraph.

Get Paragraph Text:

const text = paragraph.getText();

If a paragraph with HEADING2 is found, the script retrieves its text.

Create TOC Entry:

const tocEntry = body.appendParagraph(text);

For each HEADING2 paragraph, a corresponding entry is added to the TOC by appending a new paragraph with the same text to the document body.

Set Hyperlink to TOC Entry:

tocEntry.setLinkUrl(‘#’ + text);

Each TOC entry is turned into a hyperlink. The link is a local anchor (‘#’ + text) that should ideally navigate to the respective section in the document when clicked. However, note that Google Docs does not support these types of links for navigation within the document as of my last update, so this line may not work as intended.

In summary, this script scans a Google Document for all HEADING2 styled paragraphs, creates a “Table of Contents” at the end of the document, and lists these headings as clickable links (although the click functionality may not work as expected due to limitations in Google Docs).