How to Use Gemini AI to Summarize Google Docs and Email the Results

πŸ“˜ Introduction

Have you ever wished you could summarize the contents of multiple Google Docs with the help of AI and email the results to your inbox? This tutorial will show you how to do just that using Google Apps Script and Gemini AI.

With this system, you can:

  • Automatically summarize Google Docs using Gemini AI.
  • Email the summary directly to your inbox.
  • Speed up reporting and document review for large sets of documents.

This is perfect for creating executive summaries, generating reports, or providing quick overviews of multiple documents.


πŸ“˜ Key Features

  • Read Google Docs from a Folder β€” Extracts text from every Google Doc in a specified folder.
  • Summarize with Gemini AI β€” Calls the Gemini AI API to summarize the document’s contents.
  • Email the Summary β€” Emails the summarized results to the user’s inbox.

πŸ“˜ Prerequisites

  1. Google Drive Folder β€” Create a folder where the Docs are stored.
  2. Gemini AI API Key β€” Get an API key for Gemini AI.
  3. Google Apps Script β€” Access Google Apps Script and set up the automation.

πŸ“˜ Full Apps Script

Here is the complete Apps Script that will extract, summarize, and email the summaries of Google Docs from a specific folder.

1️⃣ Main Function: Summarize and Email Google Docs

function summarizeAndEmailGoogleDocs() {
const folderId = 'YOUR_FOLDER_ID_HERE'; // Replace with your folder ID
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
const userEmail = Session.getActiveUser().getEmail();
let emailBody = 'Here is a summary of the contents from the Google Docs in your folder (powered by Gemini AI):\n\n';

while (files.hasNext()) {
const file = files.next();
const fileName = file.getName();
Logger.log(`Processing file: ${fileName}`);

try {
const docId = file.getId();
const doc = DocumentApp.openById(docId);
const text = doc.getBody().getText();

// Send the text to Gemini AI to get a summary
const summary = callGeminiAI(text);
emailBody += `---\n**${fileName}**\n${summary}\n\n`;

} catch (e) {
Logger.log(`Failed to process file: ${fileName}. Error: ${e}`);
emailBody += `Failed to process file: ${fileName}\n\n`;
}
}

// Send the email
MailApp.sendEmail({
to: userEmail,
subject: 'Summary of Google Docs (Generated by Gemini AI)',
body: emailBody
});

Logger.log('Email sent to ' + userEmail);
}

2️⃣ Call Gemini AI to Summarize the Text

This function calls Gemini AI to generate a summary for the content of a document.

function callGeminiAI(text) {
const payload = {
contents: [{ parts: [{ text: text }] }]
};

const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload)
};

try {
const response = UrlFetchApp.fetch(geminiModel, options);
const data = JSON.parse(response.getContentText());
const summary = data["candidates"][0]["content"]["parts"][0]["text"];
return summary;
} catch (error) {
Logger.log('Error calling Gemini AI: ' + error);
return 'Failed to summarize content using Gemini AI.';
}
}

πŸ“˜ How It Works

  1. Extracts Docs from a Folder
    • Loops through all Google Docs in a specific folder.
    • Extracts the entire text of each document.
  2. Summarize with Gemini AI
    • Sends the document text to Gemini AI.
    • Gemini AI returns a summary of the content.
  3. Email the Summary
    • Combines the summaries from all documents into one email.
    • Sends the email to the user’s email address.

πŸ“˜ Instructions for Use

1️⃣ Create a Google Drive Folder

  1. Create a folder in Google Drive.
  2. Add some Google Docs to this folder (manually or by script).
  3. Copy the folder ID from the URL (the part after /folders/).

2️⃣ Replace the Folder ID

Update this line with your folder ID:

const folderId = 'YOUR_FOLDER_ID_HERE';

3️⃣ Get Your Gemini API Key

  1. Go to Google Cloud Console.
  2. Create a project and enable Gemini AI API.
  3. Generate an API key and copy it.
  4. Replace the geminiApiKey in the script:
const geminiApiKey = 'YOUR_GEMINI_API_KEY_HERE';

4️⃣ Run the Script

  1. Open Google Apps Script.
  2. Run summarizeAndEmailGoogleDocs().
  3. Check your email for the summary.

πŸ“˜ Example Email

Subject: Summary of Google Docs (Generated by Gemini AI)

Body:

Here is a summary of the contents from the Google Docs in your folder (powered by Gemini AI):

---
**Sample Document 1**
Gemini AI Summary: Google Apps Script is a tool that allows you to automate workflows and process data using cloud-based scripting. This document introduces the concept of automation with Apps Script.

---
**Sample Document 2**
Gemini AI Summary: Cloud automation can save time and effort. This document explains the importance of using automation tools like Google Apps Script to increase productivity and streamline workflows.

---
**Sample Document 3**
Gemini AI Summary: This document demonstrates how to extract and summarize multiple Google Docs using automation. It highlights the importance of summarizing large sets of documents for reporting or executive summaries.

πŸ“˜ Customization Ideas

  1. Attach the Documents
    • Attach the Google Docs as PDF files in the email.
  2. Change the Email Subject
    • Update this line to customize the subject line:subject: 'Summary of Google Docs (Generated by Gemini AI)'
  3. Custom Summarization
    • Instead of summarizing the entire document, extract specific bullet points, headings, or specific paragraphs.

πŸ“˜ Troubleshooting

IssuePossible CauseSolution
No API KeyMissing Gemini API keyAdd your API key to the script.
No Email SentIncorrect Folder IDVerify that you set the correct folder ID.
Document Not FoundFile permissions issueEnsure the file is accessible.
Summary is too shortText not sufficient for Gemini AIIncrease the amount of content sent to Gemini AI.
Error calling Gemini AIAPI not enabled or wrong API keyEnsure the API is enabled in Google Cloud Console.

πŸ“˜ Final Thoughts

With this Apps Script, you can:

  • Summarize Google Docs with AI (powered by Gemini AI).
  • Email the summary directly to your inbox.

This process is perfect for managing large sets of Google Docs, creating summaries, and sending reports. You can use it to automate daily, weekly, or monthly reporting workflows.

If you’d like any changes, help with the Gemini API, or want to customize the script, just let me know! 😊